tag_yellowDisplays tags for a document with add/remove functionality if user logged in
TypeSnippet
Category
Developed by

xwiki:XWiki.ajdinbcoventry

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

##########################################################################################################
##tags : created by Ajdin Brandic. Coventry University. E: aa6345@coventry.ac.uk
##########################################################################################################
##this stylesheet/javascript should be with other stylesheet/javascript code.
##Tutorial http://platform.xwiki.org/xwiki/bin/view/DevGuide/SkinExtensionsTutorial#HMinimalStyleSheeteXtension
<style type="text/css">
div.tagsClass {display: inline;}
a.tagsClass:link {text-decoration: none}
a.tagsClass:visited {text-decoration: none}
a.tagsClass:active {text-decoration: none}
a.tagsClass:hover {text-decoration: none; color: white; background: #0063DC;}
</style>

##javascript function "confirmTagDel" is used for tag removal

<script language="javascript" type="text/javascript">
function confirmTagDel(mytag) {
 var confirmSub = confirm("WARNING! Tags are shared by all users" +
 ". You are about to delete this tag from the document. Are you sure? Tag: " + mytag);
 if (confirmSub){
   //alert("This function is not yet available. Accepted! " + mytag);
   window.location = "$xwiki.getURL($doc.fullName)?deleteTag="+mytag;
  }else{
   alert("Tag not deleted ! ");
  }
}
</script>
<p/>
#if ($context.action == "view")<p/>
##delete tag
#if($request.deleteTag && $request.deleteTag!="")
  #set($tagsToSave = $xwiki.arrayList)
  #set($tags = "")
  ##get index of this tag in the list of all tags then remove it
  #set($tmpIndex = $!doc.getTagList().indexOf($request.deleteTag))
  #set($discardBoo3 = $!doc.getTagList().remove($tmpIndex))
  ##need this redirect to make sure users cannot use refresh with the same params in the url.
  ##$response.sendRedirect($xwiki.getURL($doc.fullName,"save"))
  ##could not redirect with anchor using $response.sendRedirect so used JS
 <script lang="JavaScript">
   window.location = "$xwiki.getURL($doc.fullName,"save")"+"#tagsAn"
 </script>
#end ##end if $request.deleteTag<p/>
##add tag(s)
#if($request.newTags && $request.newTags!="")
  #set($tagsToSave = $xwiki.arrayList)
  #set($tags = "")
  #foreach($tag in $!request.newTags.split("<a class="wikicreatelink" href="/xwiki/bin/edit/Snippets/%2C?parent=Snippets.DocumentTagsSnippet"><span class="wikicreatelinktext">,</span><span class="wikicreatelinkqm">?</span></a>"))
    #set($discardBoo5 = $tagsToSave.add($tag.trim()))
  #end
  #if($tagsToSave.size() > 0)
    #set($discardBoo4 = $!doc.getTagList().addAll($tagsToSave))
  #end ##end if $tagsToSave
  ##need this redirect to make sure users cannot use refresh with the same params in the url.
  ##$response.sendRedirect($xwiki.getURL($doc.fullName,"save"))
  ##could not redirect with anchor using $response.sendRedirect  so used JS
 <script lang="JavaScript">
   window.location = "$xwiki.getURL($doc.fullName,"save")"+"#tagsAn"
 </script>
#end ## end if $request.newTags<p/>
##display tags
<div style="margin: 0cm 0cm 0cm 0cm; padding: 0cm; border: #AFAFB5 solid thin;">
 <div style="background-color: #DFDFDF;font-weight: bold;margin: 0cm 0cm 0cm 0cm;padding: 0cm;"><a name="tagsAn">Tags</div>
 <div style="background-color: #F9F9F9;margin: 0cm 0cm 0cm 0cm;padding: 0cm;">
   
    #foreach($tagEl in $doc.getTagList())    
     <div class="tagsClass"><a class="tagsClass" href='$xwiki.getURL("Main.Tags")?tag=$tagEl'>$tagEl</a></div>
      #if($doc.checkAccess("Edit")=="true")<a style="color: red; background-color: #f9f9f9;" href='#' onclick="javascript:confirmTagDel('$tagEl'); return false;" alt="Delete tag">[x]</a>
      #end ## end $doc.checkAccess("Edit")
    #end ##end foreach in $doc.getTagList
   <p/>
    #if($doc.checkAccess("Edit")=="true")
     <form action="$xwiki.getURL($doc.fullName,"view")" method="get">      
       <input name="newTags" type="text" style="width:200px"/>
       <img src="/xwiki/bin/download/XWiki/Images/helpicon.png" title="Valid seperator : comma ','" />
       <input type="submit" value="Add tag(s)"/>
     </form>
    #end ##end of Edit true
 </div>
</div>
#end ##end of if action view

Result

TODO: Display result here with a screenshot.

Tags: tag
     

Get Connected