1 /** 2 * Copyright (C) 2005-2010 Alfresco Software Limited. 3 * 4 * This file is part of Alfresco 5 * 6 * Alfresco is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * Alfresco is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 /** 21 * Alfresco.TagComponent 22 * 23 * @namespace Alfresco 24 * @class Alfresco.TagComponent 25 */ 26 (function() 27 { 28 /** 29 * YUI Library aliases 30 */ 31 var Dom = YAHOO.util.Dom; 32 33 /** 34 * Alfresco Slingshot aliases 35 */ 36 var $html = Alfresco.util.encodeHTML; 37 38 /** 39 * TagComponent constructor. 40 * 41 * @param {String} htmlId The HTML id of the parent element 42 * @return {Alfresco.TagComponent} The new TagComponent instance 43 * @constructor 44 */ 45 Alfresco.TagComponent = function(htmlId) 46 { 47 Alfresco.TagComponent.superclass.constructor.call(this, "Alfresco.TagComponent", htmlId); 48 49 // Decoupled event listeners 50 YAHOO.Bubbling.on("tagSelected", this.onTagSelected, this); 51 52 return this; 53 }; 54 55 YAHOO.extend(Alfresco.TagComponent, Alfresco.component.Base, 56 { 57 /** 58 * Object container for initialization options 59 * 60 * @property options 61 * @type object 62 */ 63 options: 64 { 65 /** 66 * Current siteId. 67 * 68 * @property siteId 69 * @type string 70 */ 71 siteId: "", 72 73 /** 74 * ContainerId representing root container 75 * 76 * @property containerId 77 * @type string 78 */ 79 containerId: "" 80 }, 81 82 /** 83 * Fired by YUI when parent element is available for scripting. 84 * Registers event handler on "tagRefresh" event. If a component wants to refresh 85 * the tags component, they need to fire this event. 86 * 87 * @method onReady 88 */ 89 onReady: function TagComponent_onReady() 90 { 91 this._registerDefaultActionHandler(); 92 93 // Create twister from our H2 tag 94 Alfresco.util.createTwister(this.id + "-h2", "TagComponent"); 95 96 YAHOO.Bubbling.on("tagRefresh", this.onTagRefresh, this); 97 }, 98 99 /** 100 * Registers a default action listener on <em>all</em> of the tag links in the 101 * component. Fires "tagSelected" event with the name of the tag that was selected. 102 * 103 * To register for the event, interested components should do something like this: 104 * YAHOO.Bubbling.on("tagSelected", this.onTagSelected, this); 105 * 106 * @method _registerDefaultActionHandler 107 */ 108 _registerDefaultActionHandler: function TagComponent_registerDefaultActionHandler() 109 { 110 YAHOO.Bubbling.addDefaultAction('tag-link', function(layer, args) 111 { 112 var link = args[1].target; 113 if (link) 114 { 115 var tagName = link.firstChild.nodeValue; 116 YAHOO.Bubbling.fire("tagSelected", 117 { 118 "tagname": tagName 119 }); 120 } 121 return true; 122 }); 123 }, 124 125 /** 126 * Handler for the "tagRefresh" event 127 * Issues a request to the repo to retrieve the latest tag data. 128 * 129 * @method onTagRefresh 130 * @param e {object} DomEvent 131 */ 132 onTagRefresh: function TagComponent_onRefresh(e) 133 { 134 var uri = YAHOO.lang.substitute(Alfresco.constants.PROXY_URI + "api/tagscopes/site/{site}/{container}/tags?d={d}", 135 { 136 site: this.options.siteId, 137 container: this.options.containerId, 138 d: new Date().getTime() 139 }); 140 141 Alfresco.util.Ajax.request( 142 { 143 method: Alfresco.util.Ajax.GET, 144 url: uri, 145 successCallback: 146 { 147 fn: this.onTagsLoaded, 148 scope: this 149 }, 150 failureMessage: "Couldn't refresh tag data" 151 }); 152 }, 153 154 /** 155 * Event handler for tagSelected event 156 * 157 * @method onTagSelected 158 */ 159 onTagSelected: function TagComponent_onTagSelected(layer, args) 160 { 161 var tagname = args[1].tagname, 162 candidates = YAHOO.util.Selector.query("a[rel='" + tagname.replace("'", "\\'") + "']", this.id), 163 liTags = YAHOO.util.Selector.query("li", this.id); 164 165 Dom.removeClass(liTags, "selected"); 166 if (candidates.length == 1) 167 { 168 Dom.addClass(candidates[0].parentNode.parentNode, "selected"); 169 } 170 }, 171 172 /** 173 * Event handler that gets called when the tag data 174 * loads successfully. 175 * 176 * @method onTagsLoaded 177 * @param e {object} DomEvent 178 */ 179 onTagsLoaded: function TagComponent_onTagsLoaded(e) 180 { 181 var resp = YAHOO.lang.JSON.parse(e.serverResponse.responseText); 182 if (resp && !YAHOO.lang.isUndefined(resp.tags)) 183 { 184 var html = '<li><span class="tag"><a href="#" class="tag-link" rel="-all-">' + this.msg("label.all-tags") + '</a></span></li>', 185 tags = resp.tags, tag, i, ii; 186 187 for (i = 0, ii = tags.length; i < ii; i++) 188 { 189 tag = tags[i]; 190 html += this._generateTagMarkup(tag); 191 } 192 193 Dom.get(this.id + '-ul').innerHTML = html; 194 } 195 }, 196 197 /** 198 * Generates the HTML for a tag. 199 * 200 * @method _generateTagMarkup 201 * @param tag {Object} the tag to render 202 */ 203 _generateTagMarkup: function TagComponent__generateTagMarkup(tag) 204 { 205 var html = '<li><span class="tag">'; 206 html += '<a href="#" class="tag-link" rel="' + $html(tag.name) + '">' + $html(tag.name) + '</a> (' + tag.count + ')'; 207 html += '</span></li>'; 208 return html; 209 } 210 }); 211 })();