YUI.add('tag-linker', function(Y) { var Lang = Y.Lang, Widget = Y.Widget, Node = Y.Node; var NS = Y.namespace('mazzle'); NS.TagLinker = TagLinker; /* TagLinker class constructor */ function TagLinker(config) { TagLinker.superclass.constructor.apply(this, arguments); } /* * Required NAME static field, to identify the Widget class and * used as an event prefix, to generate class names etc. (set to the * class name in camel case). */ TagLinker.NAME = "tag-linker"; /* * The attribute configuration for the TagLinker widget. Attributes can be * defined with default values, get/set functions and validator functions * as with any other class extending Base. */ TagLinker.ATTRS = { tag: { value: null }, sources: { value: {} }, limit: { value: 3 } }; /* TagLinker extends the base Widget class */ Y.extend(TagLinker, Widget, { initializer: function() { }, destructor : function() { }, renderUI : function() { var content = this.get("contentBox"); var tagNode = content.appendChild(Node.create('
')); var linkBox = content.appendChild(Node.create('
')); var controls = content.appendChild('
'); this.submitButton = controls.appendChild(''); this.cancelButton = controls.appendChild(''); this._renderSources(linkBox); this.tagNode = tagNode; }, bindUI : function() { var sources = this.get("sources"); this.after("tagChange", this.syncUI); this.cancelButton.on("click", this._cancel, this); this.submitButton.on("click", this._submit, this); /*for(var key in sources) { Y.delegate("click", this._itemSelect, sources[key]._list, "li input", this, key); }*/ }, syncUI : function() { var tag = this.get("tag"); if(tag) { this.tagNode.setContent(tag); this._fetchConcepts(); } else { this.tagNode.setContent(""); // hide source results } }, _cancel: function() { var tag = this.get("tag"); this.fire("cancel", {"tag":tag}); }, _submit: function() { var tag = this.get("tag"), sources = this.get("sources"), selected = []; for(var key in sources) { var source = sources[key], items = source.items, nodes = source._list.all("li"); nodes.each(function(node, i) { var concept = items[i]; if(node.one(".check:checked")) { selected.push({value:concept.id, label:concept.name, source:key}); } }) } this.fire("submit", {"tag":tag, concepts:selected}); }, _renderSources : function(node) { var sources = this.get("sources"); for(var key in sources) { var source = sources[key], label = source.label; //img = source.img; var box = node.appendChild(Node.create('
')); //box.appendChild('
'+label+'
'); box.appendChild('
'+label+'
'); var bd = box.appendChild(Node.create('
')); source._list = bd.appendChild(this._renderSourceList()); } }, _renderSourceList : function() { var limit = this.get("limit"); var listNode = Node.create(''); for (var i=0; i < limit; i++) { listNode.appendChild(""); }; return listNode; }, _fetchConcepts : function() { var tag = this.get("tag"), sources = this.get("sources"), limit = this.get("limit"); for(var key in sources) { var source = sources[key], type = source.type, query = { query: tag, limit: limit, type_strict: "should" }; if(type) { query.type = type; } var url = source.url + "?callback={callback}" + source.parameters + "&query="+Y.JSON.stringify(query); source._list.addClass("hidden"); Y.log('reconcile against '+source.label); this.fire("reconcileStart", {"source":source}); this.reconcile(url, source); } }, reconcile : function(request, source) { var oSelf = this, items = source._list.all("li"); Y.jsonp(request, function(response) { items.each(function(node, i) { var results = response.result; source.items = results; if(results[i]) { node.setContent(oSelf.formatItem(results[i])); node.removeClass("hidden"); } else { node.addClass("hidden"); } }) source._list.removeClass("hidden"); }) }, formatItem : function(item) { var id = item.id, name = item.name, types = item.type||[]; var url = (id.substr(0,1)=='/') ? 'http://www.freebase.com'+id : id; var html = ""; html += ""+name+""; if(types&&!item.desc) { html += "
"; html += ""+types[0].name+""; html += "
"; } if(item.desc) { html += "
"+item.desc+"
"; } return html; } }) }, 'gallery-2010.03.02-18' ,{requires:['node','event','widget','json']});