//>>built
define("dojox/data/ItemExplorer",["dojo","dijit","dojox","dojo/require!dijit/Tree,dijit/Dialog,dijit/Menu,dijit/form/ValidationTextBox,dijit/form/Textarea,dijit/form/Button,dijit/form/RadioButton,dijit/form/FilteringSelect"],function(c,d){c.provide("dojox.data.ItemExplorer");c.require("dijit.Tree");c.require("dijit.Dialog");c.require("dijit.Menu");c.require("dijit.form.ValidationTextBox");c.require("dijit.form.Textarea");c.require("dijit.form.Button");c.require("dijit.form.RadioButton");c.require("dijit.form.FilteringSelect");
(function(){var j=function(b,a,c){var f=b.getValues(a,c);2>f.length&&(f=b.getValue(a,c));return f};c.declare("dojox.data.ItemExplorer",d.Tree,{useSelect:!1,refSelectSearchAttr:null,constructor:function(b){c.mixin(this,b);var a=this,e={},f=this.rootModelNode={value:e,id:"root"};this._modelNodeIdMap={};this._modelNodePropMap={};var g=1;this.model={getRoot:function(a){a(f)},mayHaveChildren:function(a){return a.value&&"object"==typeof a.value&&!(a.value instanceof Date)},getChildren:function(b,c){function f(){if(m)g=
a.store.getAttributes(h),d=h;else if(h&&"object"==typeof h){d=b.value;g=[];for(var e in h)h.hasOwnProperty(e)&&"__id"!=e&&"__clientId"!=e&&g.push(e)}if(g){for(var l=0;e=g[l++];)n.push({property:e,value:m?j(a.store,h,e):h[e],parent:d});n.push({addNew:!0,parent:d,parentNode:b})}c(n)}var g,d,h=b.value,n=[];if(h==e)c([]);else{var m=a.store&&a.store.isItem(h,!0);m&&!a.store.isItemLoaded(h)?a.store.loadItem({item:h,onItem:function(a){h=a;f()}}):f()}},getIdentity:function(b){if(!b.id){if(b.addNew)b.property=
"--addNew";b.id=g++;if(a.store){if(a.store.isItem(b.value)){var c=a.store.getIdentity(b.value);(a._modelNodeIdMap[c]=a._modelNodeIdMap[c]||[]).push(b)}b.parent&&(c=a.store.getIdentity(b.parent)+"."+b.property,(a._modelNodePropMap[c]=a._modelNodePropMap[c]||[]).push(b))}}return b.id},getLabel:function(a){return a===f?"Object Properties":a.addNew?a.parent instanceof Array?"Add new value":"Add new property":a.property+": "+(a.value instanceof Array?"("+a.value.length+" elements)":a.value)},onChildrenChange:function(){},
onChange:function(){}}},postCreate:function(){this.inherited(arguments);c.connect(this,"onClick",function(a,b){this.lastFocused=b;a.addNew?this._addProperty():this._editProperty()});var b=new d.Menu({targetNodeIds:[this.rootNode.domNode],id:"contextMenu"});c.connect(b,"_openMyself",this,function(a){if(a=d.getEnclosingWidget(a.target)){var e=a.item;this.store.isItem(e.value,!0)&&!e.parent?(c.forEach(b.getChildren(),function(a){a.attr("disabled","Add"!=a.label)}),this.lastFocused=a):e.value&&"object"==
typeof e.value&&!(e.value instanceof Date)?(c.forEach(b.getChildren(),function(a){a.attr("disabled","Add"!=a.label&&"Delete"!=a.label)}),this.lastFocused=a):e.property&&0<=c.indexOf(this.store.getIdentityAttributes(),e.property)?(this.focusNode(a),alert("Cannot modify an Identifier node.")):e.addNew?this.focusNode(a):(c.forEach(b.getChildren(),function(a){a.attr("disabled","Edit"!=a.label&&"Delete"!=a.label)}),this.lastFocused=a)}});b.addChild(new d.MenuItem({label:"Add",onClick:c.hitch(this,"_addProperty")}));
b.addChild(new d.MenuItem({label:"Edit",onClick:c.hitch(this,"_editProperty")}));b.addChild(new d.MenuItem({label:"Delete",onClick:c.hitch(this,"_destroyProperty")}));b.startup()},store:null,setStore:function(b){this.store=b;var a=this;this._editDialog&&(this._editDialog.destroyRecursive(),delete this._editDialog);c.connect(b,"onSet",function(b,c,g,d){var j=a.store.getIdentity(b);if((b=a._modelNodeIdMap[j])&&(void 0===g||void 0===d||g instanceof Array||d instanceof Array||"object"==typeof g||"object"==
typeof d))for(g=0;g<b.length;g++)(function(b){a.model.getChildren(b,function(c){a.model.onChildrenChange(b,c)})})(b[g]);if(b=a._modelNodePropMap[j+"."+c])for(g=0;g<b.length;g++)b[g].value=d,a.model.onChange(b[g])});this.rootNode.setChildItems([])},setItem:function(b){(this._modelNodeIdMap={})[this.store.getIdentity(b)]=[this.rootModelNode];this._modelNodePropMap={};this.rootModelNode.value=b;var a=this;this.model.getChildren(this.rootModelNode,function(b){a.rootNode.setChildItems(b)})},refreshItem:function(){this.setItem(this.rootModelNode.value)},
_createEditDialog:function(){this._editDialog=new d.Dialog({title:"Edit Property",execute:c.hitch(this,"_updateItem"),preload:!0});this._editDialog.placeAt(c.body());this._editDialog.startup();var b=c.doc.createElement("div"),a=c.doc.createElement("label");c.attr(a,"for","property");c.style(a,"fontWeight","bold");c.attr(a,"innerHTML","Property:");b.appendChild(a);(new d.form.ValidationTextBox({name:"property",value:"",required:!0,disabled:!0})).placeAt(b);b.appendChild(c.doc.createElement("br"));
b.appendChild(c.doc.createElement("br"));(new d.form.RadioButton({name:"itemType",value:"value",onClick:c.hitch(this,function(){this._enableFields("value")})})).placeAt(b);a=c.doc.createElement("label");c.attr(a,"for","value");c.attr(a,"innerHTML","Value (JSON):");b.appendChild(a);a=c.doc.createElement("div");c.addClass(a,"value");(new d.form.Textarea({name:"jsonVal"})).placeAt(a);b.appendChild(a);(new d.form.RadioButton({name:"itemType",value:"reference",onClick:c.hitch(this,function(){this._enableFields("reference")})})).placeAt(b);
a=c.doc.createElement("label");c.attr(a,"for","_reference");c.attr(a,"innerHTML","Reference (ID):");b.appendChild(a);b.appendChild(c.doc.createElement("br"));a=c.doc.createElement("div");c.addClass(a,"reference");this.useSelect?(new d.form.FilteringSelect({name:"_reference",store:this.store,searchAttr:this.refSelectSearchAttr||this.store.getIdentityAttributes()[0],required:!1,value:null,pageSize:10})).placeAt(a):(new d.form.ValidationTextBox({name:"_reference",value:"",promptMessage:"Enter the ID of the item to reference",
isValid:c.hitch(this,function(){return!0})})).placeAt(a);b.appendChild(a);b.appendChild(c.doc.createElement("br"));b.appendChild(c.doc.createElement("br"));a=document.createElement("div");a.setAttribute("dir","rtl");(new d.form.Button({type:"reset",label:"Cancel"})).placeAt(a).onClick=c.hitch(this._editDialog,"onCancel");(new d.form.Button({type:"submit",label:"OK"})).placeAt(a);b.appendChild(a);this._editDialog.attr("content",b)},_enableFields:function(b){switch(b){case "reference":c.query(".value [widgetId]",
this._editDialog.containerNode).forEach(function(a){d.getEnclosingWidget(a).attr("disabled",!0)});c.query(".reference [widgetId]",this._editDialog.containerNode).forEach(function(a){d.getEnclosingWidget(a).attr("disabled",!1)});break;case "value":c.query(".value [widgetId]",this._editDialog.containerNode).forEach(function(a){d.getEnclosingWidget(a).attr("disabled",!1)}),c.query(".reference [widgetId]",this._editDialog.containerNode).forEach(function(a){d.getEnclosingWidget(a).attr("disabled",!0)})}},
_updateItem:function(b){function a(){try{var a,c=[],h=b.property;if(p){for(;!i.isItem(f.parent,!0);)e=e.getParent(),c.push(f.property),f=e.item;if(0==c.length)i.setValue(f.parent,f.property,g);else{d=j(i,f.parent,f.property);d instanceof Array&&(d=d.concat());for(a=d;1<c.length;)a=a[c.pop()];a[c]=g;i.setValue(f.parent,f.property,d)}}else if(i.isItem(k,!0))if(i.isItemLoaded(k)){if(k instanceof Array)h=k.length;i.setValue(k,h,g)}else i.loadItem({item:k,onItem:function(a){if(a instanceof Array)h=a.length;
i.setValue(a,h,g)}});else{for(f.value instanceof Array?c.push(f.value.length):c.push(b.property);!i.isItem(f.parent,!0);)e=e.getParent(),c.push(f.property),f=e.item;for(a=d=j(i,f.parent,f.property);1<c.length;)a=a[c.pop()];a[c]=g;i.setValue(f.parent,f.property,d)}}catch(l){alert(l)}}var e,f,g,d,p="Edit Property"==this._editDialog.attr("title"),l=this._editDialog,i=this.store;if(l.validate()){e=this.lastFocused;f=e.item;var k=f.value;if(f.addNew)k=e.item.parent,e=e.getParent(),f=e.item;g=null;switch(b.itemType){case "reference":this.store.fetchItemByIdentity({identity:b._reference,
onItem:function(b){g=b;a()},onError:function(){alert("The id could not be found")}});break;case "value":var h=b.jsonVal;g=c.fromJson(h);if("function"==typeof g)g.toString=function(){return h};a()}}else l.show()},_editProperty:function(){var b=c.mixin({},this.lastFocused.item);this._editDialog?this._editDialog.reset():this._createEditDialog();if(0<=c.indexOf(this.store.getIdentityAttributes(),b.property))alert("Cannot Edit an Identifier!");else if(this._editDialog.attr("title","Edit Property"),d.getEnclosingWidget(c.query("input",
this._editDialog.containerNode)[0]).attr("disabled",!0),this.store.isItem(b.value,!0)){if(b.parent)b.itemType="reference",this._enableFields(b.itemType),b._reference=this.store.getIdentity(b.value),this._editDialog.attr("value",b),this._editDialog.show()}else if(!b.value||"object"!=typeof b.value||b.value instanceof Date)b.itemType="value",this._enableFields(b.itemType),b.jsonVal="function"==typeof b.value?b.value.toString():b.value instanceof Date?'new Date("'+b.value+'")':c.toJson(b.value),this._editDialog.attr("value",
b),this._editDialog.show()},_destroyProperty:function(){for(var b=this.lastFocused,a=b.item,e=[];!this.store.isItem(a.parent,!0)||a.parent instanceof Array;)b=b.getParent(),e.push(a.property),a=b.item;if(0<=c.indexOf(this.store.getIdentityAttributes(),a.property))alert("Cannot Delete an Identifier!");else try{if(0<e.length){var f,d=j(this.store,a.parent,a.property);for(f=d;1<e.length;)f=f[e.pop()];c.isArray(f)?f.splice(e,1):delete f[e];this.store.setValue(a.parent,a.property,d)}else this.store.unsetAttribute(a.parent,
a.property)}catch(o){alert(o)}},_addProperty:function(){var b=this.lastFocused.item,a=b.value,e=c.hitch(this,function(){var b=null;this._editDialog?this._editDialog.reset():this._createEditDialog();a instanceof Array?(b=a.length,d.getEnclosingWidget(c.query("input",this._editDialog.containerNode)[0]).attr("disabled",!0)):d.getEnclosingWidget(c.query("input",this._editDialog.containerNode)[0]).attr("disabled",!1);this._editDialog.attr("title","Add Property");this._enableFields("value");this._editDialog.attr("value",
{itemType:"value",property:b});this._editDialog.show()});if(b.addNew)b=this.lastFocused.getParent().item,a=this.lastFocused.item.parent;b.property&&0<=c.indexOf(this.store.getIdentityAttributes(),b.property)?alert("Cannot add properties to an ID node!"):this.store.isItem(a,!0)&&!this.store.isItemLoaded(a)?this.store.loadItem({item:a,onItem:function(b){a=b;e()}}):e()}})})()});