//>>built
define("dojox/data/ItemExplorer",["dijit","dojo","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(l,c,x){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 v=function(b,a,e){var f=b.getValues(a,e);2>f.length&&(f=b.getValue(a,e));return f};c.declare("dojox.data.ItemExplorer",l.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 m=1;this.model={getRoot:function(d){d(f)},mayHaveChildren:function(d){return d.value&&"object"==typeof d.value&&!(d.value instanceof Date)},getChildren:function(d,k,g){function r(){if(t)p=
a.store.getAttributes(h),u=h;else if(h&&"object"==typeof h){u=d.value;p=[];for(var n in h)h.hasOwnProperty(n)&&"__id"!=n&&"__clientId"!=n&&p.push(n)}if(p){for(var w=0;n=p[w++];)q.push({property:n,value:t?v(a.store,h,n):h[n],parent:u});q.push({addNew:!0,parent:u,parentNode:d})}k(q)}var p,u,h=d.value,q=[];if(h==e)k([]);else{var t=a.store&&a.store.isItem(h,!0);t&&!a.store.isItemLoaded(h)?a.store.loadItem({item:h,onItem:function(n){h=n;r()}}):r()}},getIdentity:function(d){if(!d.id&&(d.addNew&&(d.property=
"--addNew"),d.id=m++,a.store)){if(a.store.isItem(d.value)){var k=a.store.getIdentity(d.value);(a._modelNodeIdMap[k]=a._modelNodeIdMap[k]||[]).push(d)}d.parent&&(k=a.store.getIdentity(d.parent)+"."+d.property,(a._modelNodePropMap[k]=a._modelNodePropMap[k]||[]).push(d))}return d.id},getLabel:function(d){return d===f?"Object Properties":d.addNew?d.parent instanceof Array?"Add new value":"Add new property":d.property+": "+(d.value instanceof Array?"("+d.value.length+" elements)":d.value)},onChildrenChange:function(d){},
onChange:function(d){}}},postCreate:function(){this.inherited(arguments);c.connect(this,"onClick",function(a,e){this.lastFocused=e;a.addNew?this._addProperty():this._editProperty()});var b=new l.Menu({targetNodeIds:[this.rootNode.domNode],id:"contextMenu"});c.connect(b,"_openMyself",this,function(a){if(a=l.getEnclosingWidget(a.target)){var e=a.item;this.store.isItem(e.value,!0)&&!e.parent?(c.forEach(b.getChildren(),function(f){f.attr("disabled","Add"!=f.label)}),this.lastFocused=a):!e.value||"object"!=
typeof e.value||e.value instanceof Date?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(f){f.attr("disabled","Edit"!=f.label&&"Delete"!=f.label)}),this.lastFocused=a):(c.forEach(b.getChildren(),function(f){f.attr("disabled","Add"!=f.label&&"Delete"!=f.label)}),this.lastFocused=a)}});b.addChild(new l.MenuItem({label:"Add",onClick:c.hitch(this,"_addProperty")}));
b.addChild(new l.MenuItem({label:"Edit",onClick:c.hitch(this,"_editProperty")}));b.addChild(new l.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(e,f,m,d){var k=a.store.getIdentity(e);if((e=a._modelNodeIdMap[k])&&(void 0===m||void 0===d||m instanceof Array||d instanceof Array||"object"==typeof m||"object"==
typeof d))for(m=0;m<e.length;m++)(function(g){a.model.getChildren(g,function(r){a.model.onChildrenChange(g,r)})})(e[m]);if(e=a._modelNodePropMap[k+"."+f])for(m=0;m<e.length;m++)e[m].value=d,a.model.onChange(e[m])});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(e){a.rootNode.setChildItems(e)})},refreshItem:function(){this.setItem(this.rootModelNode.value)},
_createEditDialog:function(){this._editDialog=new l.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 l.form.ValidationTextBox({name:"property",value:"",required:!0,disabled:!0})).placeAt(b);b.appendChild(c.doc.createElement("br"));
b.appendChild(c.doc.createElement("br"));(new l.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 l.form.Textarea({name:"jsonVal"})).placeAt(a);b.appendChild(a);(new l.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 l.form.FilteringSelect({name:"_reference",store:this.store,searchAttr:this.refSelectSearchAttr||this.store.getIdentityAttributes()[0],required:!1,value:null,pageSize:10})).placeAt(a):(new l.form.ValidationTextBox({name:"_reference",value:"",promptMessage:"Enter the ID of the item to reference",
isValid:c.hitch(this,function(e){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 l.form.Button({type:"reset",label:"Cancel"})).placeAt(a).onClick=c.hitch(this._editDialog,"onCancel");(new l.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){l.getEnclosingWidget(a).attr("disabled",!0)});c.query(".reference [widgetId]",this._editDialog.containerNode).forEach(function(a){l.getEnclosingWidget(a).attr("disabled",!1)});break;case "value":c.query(".value [widgetId]",this._editDialog.containerNode).forEach(function(a){l.getEnclosingWidget(a).attr("disabled",!1)}),c.query(".reference [widgetId]",this._editDialog.containerNode).forEach(function(a){l.getEnclosingWidget(a).attr("disabled",!0)})}},
_updateItem:function(b){function a(){try{var h,q=[],t=b.property;if(f){for(;!d.isItem(g.parent,!0);)k=k.getParent(),q.push(g.property),g=k.item;if(0==q.length)d.setValue(g.parent,g.property,p);else{e=v(d,g.parent,g.property);e instanceof Array&&(e=e.concat());for(h=e;1<q.length;)h=h[q.pop()];h[q]=p;d.setValue(g.parent,g.property,e)}}else if(d.isItem(r,!0))d.isItemLoaded(r)?(r instanceof Array&&(t=r.length),d.setValue(r,t,p)):d.loadItem({item:r,onItem:function(n){n instanceof Array&&(t=n.length);d.setValue(n,
t,p)}});else{for(g.value instanceof Array?q.push(g.value.length):q.push(b.property);!d.isItem(g.parent,!0);)k=k.getParent(),q.push(g.property),g=k.item;for(h=e=v(d,g.parent,g.property);1<q.length;)h=h[q.pop()];h[q]=p;d.setValue(g.parent,g.property,e)}}catch(n){alert(n)}}var e,f="Edit Property"==this._editDialog.attr("title"),m=this._editDialog,d=this.store;if(m.validate()){var k=this.lastFocused;var g=k.item;var r=g.value;g.addNew&&(r=k.item.parent,k=k.getParent(),g=k.item);var p=null;switch(b.itemType){case "reference":this.store.fetchItemByIdentity({identity:b._reference,
onItem:function(h){p=h;a()},onError:function(){alert("The id could not be found")}});break;case "value":var u=b.jsonVal;p=c.fromJson(u);"function"==typeof p&&(p.toString=function(){return u});a()}}else m.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"),l.getEnclosingWidget(c.query("input",
this._editDialog.containerNode)[0]).attr("disabled",!0),this.store.isItem(b.value,!0))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,m=v(this.store,a.parent,a.property);for(f=m;1<e.length;)f=f[e.pop()];c.isArray(f)?f.splice(e,1):delete f[e];this.store.setValue(a.parent,a.property,m)}else this.store.unsetAttribute(a.parent,
a.property)}catch(d){alert(d)}},_addProperty:function(){var b=this.lastFocused.item,a=b.value,e=c.hitch(this,function(){var f=null;this._editDialog?this._editDialog.reset():this._createEditDialog();a instanceof Array?(f=a.length,l.getEnclosingWidget(c.query("input",this._editDialog.containerNode)[0]).attr("disabled",!0)):l.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:f});this._editDialog.show()});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(f){a=f;e()}}):e()}})})()});