YUI.add('strategy_viz', function(Y) { var Lang = Y.Lang, Node = Y.Node, Widget = Y.Widget; var svgNS = "http://www.w3.org/2000/svg"; function StratViz(config) { StratViz.superclass.constructor.apply(this, arguments); } StratViz.NAME = "strategy_viz"; StratViz.ATTRS = { strategy: { value: null }, paths: { value: null }, focus: { value: null } }; Y.extend(StratViz, Y.Widget, { initializer: function(config) { this.infoDS = new Y.DataSource.IO({ source: this.get("paths").nodeinfo }); this.publish("nodeSelect", {}); }, destructor : function() {}, renderUI : function() {}, bindUI : function() { this.after("focusChange", this.syncUI, this); }, _bindSVG : function () { var contentBox = this.get("contentBox"); // Bind event handlers to the links in the graph // and prevent the default behavior from xlink:href if(contentBox.one("svg")) { Y.Event.purgeElement(contentBox, true); Y.delegate("click", this._onNodeSelect, "svg", "a", this); } }, syncUI : function() { this.updateGraph(this.get("focus").uri); }, updateGraph : function(uri) { var oSelf = this, paths = this.get("paths"), data = {"strategy":this.get("strategy")}; if(uri) { data.selected = uri } Y.io(paths.strategyGraph, { data:data, on:{ success: function(e,o) { // As the server returns an XML document, including doctype // we first take out the actual svg element oSelf.get("contentBox").setContent(o.responseXML.lastChild); oSelf._setSelection(); oSelf._bindSVG(); } } }); }, _setSelection : function() { var focus = this.get("focus").uri; this.get("contentBox").all("a").each(function(svgnode) { if(svgnode.getAttribute("xlink:href") === focus) { svgnode.setAttribute("class", "selected"); } }); }, _onNodeSelect : function(e) { e.preventDefault(); var target = e.currentTarget, uri = target.getAttribute("xlink:href"); Y.log("node selected in strategy svg: "+uri); Y.all("svg a").removeAttribute("class", "selected"); target.setAttribute("class", "selected"); this.fire("nodeSelect", {target:target, uri:uri}); } }); Y.StratViz = StratViz; }, '0.0.1', { requires: ['node','event','widget']});