(function(){"use strict";u8.Base.Widgets.ConfigurationGraph=function(t,e){this.Widget(t,$.extend({},u8.Base.Widgets.ConfigurationGraph.defaults,e));this._isCreated=false;this._create()};u8.Base.Widgets.ConfigurationGraph.defaults={showBreadcrumbs:false,graph:{layoutOptions:{algorithm:"EfficientSugiyama",edgeRouting:"Orthogonal",positionMode:-1,vertexSpacing:10,layerSpacing:35}}};u8.Base.Widgets.ConfigurationGraph.makeClass({name:"ConfigurationGraph",deriveFrom:u8.Base.Widgets.Widget,events:["Create"]});$.extend(u8.Base.Widgets.ConfigurationGraph.prototype,{update:function(t){Function.chain(this._updateGraphModel,this._updateView).call(this,t)},_create:function(){Function.chain(this._createModel,this._createView).call(this,u8.tools.created.curry(this,{}))},_createModel:function(t){if(!this._model){this._model={breadCrumbs:{style:"h1",parent:this,items:[],onItemClick:this._onBreadCrumbsItemClick.bind(this)}}}this._updateGraphModel(t)},_createView:function(e){var i=this.$(this.element).empty().addClass("u8-configurationgraph").css("visibility","hidden").html("<div class='breadcrumbs top'></div><div class='graph center'></div>").layout("dock");var a=$.extend({},this.graph,{selection:"configId"in this?t(this.configId):undefined,parent:this,model:this._model.graphModel,vertexRenderer:this._vertexRenderer.bind(this),onVertexClick:this._onGraphVertexClick.bind(this),onVertexDblClick:this._onGraphVertexDblClick.bind(this),onCreate:function(){i.css("visibility","inherit").layout("dock")}});this._graph=this.$(".graph",this.element).widget("Graph",a)[0];this._breadCrumbs=this.$(".breadcrumbs",this.element).widget("BreadCrumbs",this._model.breadCrumbs)[0];if(e){e()}},_vertexRenderer:function(t,e){var i=this.$;var a=i(t).empty();a.toggleClass("role",e.isRole);i("<span/>").addClass("text").text("{name} ({configId})".format(e)).attr("title",e.name+(e.description?"\r\n"+e.description:"")).appendTo(a);var n=this.actions;if(n){var r=i("<div/>").addClass("actions");n.forEach(function(t){if(t.condition&&!t.condition(e)){return}var a=i("<span/>").attr("data-command",t.command).addClass("action");var n=i("<span/>").sprite(t.image).attr("title",t.title).appendTo(a);if(t.title){n.attr("title",t.title)}a.appendTo(r)});r.appendTo(a)}},_updateGraphModel:function(e){var i={},a="ConfigurationChannel.GetConfigurationGraph";if("configId"in this){a="ConfigurationChannel.GetConfigurationGraphById";i.configId=this.configId}var n=this;u8.request(a,i,function i(a,r){n._model.graphModel=r.response;var s=r.response;var o=s.data;var d;if("configId"in n){d=o[t(n.configId)]}else{d=o[s.vertices[0].id];n.configId=d.configId}n._addBreadCrumb({key:d.configId,text:d.name});e()})},_updateView:function(t){if(this._graph){this._graph.setModel(this._model.graphModel)}if(t){t()}},_addBreadCrumb:function(t){var e=this._model.breadCrumbs.items;e.push(t);if(this._breadCrumbs){this._breadCrumbs.update()}},_gotoBreadCrumb:function(t){if(t.key===this.configId){return}var e=this._model.breadCrumbs.items;var i=e.indexOf(t);if(i>=0){e.splice(i,e.length-i)}this.configId=t.key;this.update()},_onGraphVertexDblClick:function(t,e){var i=this._graph.model.getData(e.id);this.configId=i.configId;this.update()},_onGraphVertexClick:function(t,e){var i=this.$(e.elEvent.target);var a=i.closest("[data-command]").attr("data-command");if(!a){return undefined}var n=this._graph.model.getData(e.id);return $command(this,a,{configId:n.configId})},_onBreadCrumbsItemClick:function(t,e){e.cancel=true;this._gotoBreadCrumb(e.item)}});function t(t){return"v"+t}})();