Dictionary.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. MWF.xApplication = MWF.xApplication || {};
  2. MWF.xApplication.cms = MWF.xApplication.cms || {};
  3. MWF.xApplication.cms.DictionaryDesigner = MWF.xApplication.cms.DictionaryDesigner || {};
  4. MWF.CMSDD = MWF.xApplication.cms.DictionaryDesigner;
  5. MWF.require("MWF.widget.Common", null, false);
  6. MWF.xDesktop.requireApp("cms.DictionaryDesigner", "lp."+MWF.language, null, false);
  7. MWF.xApplication.cms.DictionaryDesigner.Dictionary = new Class({
  8. Extends: MWF.widget.Common,
  9. Implements: [Options, Events],
  10. options: {
  11. "style": "default",
  12. "isView": false,
  13. "showTab": true,
  14. "types": ["object", "array", "string", "number", "boolean"]
  15. },
  16. initialize: function(designer, data, options){
  17. this.setOptions(options);
  18. this.path = "/x_component_cms_DictionaryDesigner/$Dictionary/";
  19. this.cssPath = "/x_component_cms_DictionaryDesigner/$Dictionary/"+this.options.style+"/css.wcss";
  20. this._loadCss();
  21. this.designer = designer;
  22. this.data = data;
  23. if (!this.data.data) this.data.data = {};
  24. this.node = this.designer.designNode;
  25. this.tab = this.designer.tab;
  26. this.areaNode = new Element("div", {"styles": {"overflow": "hidden"}});
  27. MWF.require("MWF.widget.ScrollBar", function(){
  28. new MWF.widget.ScrollBar(this.areaNode, {"distance": 100});
  29. }.bind(this));
  30. this.propertyListNode = this.designer.propertyDomArea;
  31. //this.propertyNode = this.designer.propertyContentArea;
  32. if(this.designer.application) this.data.applicationName = this.designer.application.name;
  33. if(this.designer.application) this.data.application = this.designer.application.id;
  34. this.isNewDictionary = (this.data.id) ? false : true;
  35. this.items = [];
  36. this.autoSave();
  37. this.designer.addEvent("queryClose", function(){
  38. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  39. }.bind(this));
  40. },
  41. autoSave: function(){
  42. this.autoSaveTimerID = window.setInterval(function(){
  43. if (!this.autoSaveCheckNode) this.autoSaveCheckNode = this.designer.contentToolbarNode.getElement("#MWFDictionaryAutoSaveCheck");
  44. if (this.autoSaveCheckNode){
  45. if (this.autoSaveCheckNode.get("checked")){
  46. this.save();
  47. }
  48. }
  49. }.bind(this), 60000);
  50. },
  51. createTitle: function(){
  52. this.itemsNode = new Element("div", {"styles": this.css.itemsNode}).inject(this.areaNode);
  53. this.typesNode = new Element("div", {"styles": this.css.typesNode}).inject(this.areaNode);
  54. this.valuesNode = new Element("div", {"styles": this.css.valuesNode}).inject(this.areaNode);
  55. this.itemTitleNode = new Element("div", {"styles": this.css.itemTitleNode}).inject(this.itemsNode);
  56. this.typeTitleNode = new Element("div", {"styles": this.css.typeTitleNode}).inject(this.typesNode);
  57. this.valueTitleNode = new Element("div", {"styles": this.css.valueTitleNode}).inject(this.valuesNode);
  58. this.itemResizeNode = new Element("div", {"styles": this.css.itemResizeNode}).inject(this.itemTitleNode);
  59. this.typeResizeNode = new Element("div", {"styles": this.css.typeResizeNode}).inject(this.typeTitleNode);
  60. // this.addTopItemNode = new Element("div", {"styles": this.css.addTopItemNode}).inject(this.itemTitleNode);
  61. this.itemTitleTextNode = new Element("div", {"styles": this.css.itemTitleTextNode, "text": this.designer.lp.item}).inject(this.itemTitleNode);
  62. this.typeTitleTextNode = new Element("div", {"styles": this.css.typeTitleTextNode, "text": this.designer.lp.type}).inject(this.typeTitleNode);
  63. this.valueTitleTextNode = new Element("div", {"styles": this.css.valueTitleTextNode, "text": this.designer.lp.value}).inject(this.valueTitleNode);
  64. // this.addTopItemNode.addEvent("click", this.addTopItem.bind(this));
  65. },
  66. load : function(){
  67. this.setAreaNodeSize();
  68. this.designer.addEvent("resize", this.setAreaNodeSize.bind(this));
  69. this.page = this.tab.addTab(this.areaNode, this.data.name || this.designer.lp.newDictionary, (!this.data.isNewDictionary && this.data.id!=this.designer.options.id));
  70. this.page.dictionary = this;
  71. this.page.addEvent("show", function(){
  72. this.designer.dictionaryListAreaNode.getChildren().each(function(node){
  73. var dictionary = node.retrieve("dictionary");
  74. if (dictionary.id==this.data.id){
  75. if (this.designer.currentListDictionaryItem){
  76. this.designer.currentListDictionaryItem.setStyles(this.designer.css.listDictionaryItem);
  77. }
  78. node.setStyles(this.designer.css.listDictionaryItem_current);
  79. this.designer.currentListDictionaryItem = node;
  80. this.lisNode = node;
  81. }
  82. }.bind(this));
  83. this.setPropertyContent();
  84. }.bind(this));
  85. this.page.addEvent("queryClose", function(){
  86. if (this.autoSaveTimerID) window.clearInterval(this.autoSaveTimerID);
  87. this.saveSilence();
  88. if (this.lisNode) this.lisNode.setStyles(this.designer.css.listScriptItem);
  89. }.bind(this));
  90. this.page.tabNode.addEvent("dblclick", this.designer.maxOrReturnEditor.bind(this.designer));
  91. this.createTitle();
  92. this.createRootItem();
  93. if (this.options.showTab) this.page.showTabIm();
  94. },
  95. setPropertyContent: function(){
  96. this.designer.propertyIdNode.set("text", this.data.id);
  97. this.designer.propertyNameNode.set("value", this.data.name);
  98. this.designer.propertyAliasNode.set("value", this.data.alias);
  99. this.designer.propertyDescriptionNode.set("value", this.data.description);
  100. this.designer.jsonDomNode.empty();
  101. MWF.require("MWF.widget.JsonParse", function(){
  102. this.jsonParse = new MWF.widget.JsonParse(this.data.data, this.designer.jsonDomNode, this.designer.jsonTextAreaNode);
  103. window.setTimeout(function(){
  104. this.jsonParse.load();
  105. }.bind(this), 1);
  106. }.bind(this));
  107. },
  108. setAreaNodeSize: function(){
  109. var size = this.node.getSize();
  110. var tabSize = this.tab.tabNodeContainer.getSize();
  111. var y = size.y - tabSize.y;
  112. this.areaNode.setStyle("height", ""+y+"px");
  113. if (this.editor) if (this.editor.editor) this.editor.editor.resize();
  114. },
  115. createRootItem: function() {
  116. this.items.push(new MWF.xApplication.cms.DictionaryDesigner.Dictionary.item("ROOT", this.data.data, null, 0, this, true));
  117. },
  118. saveSilence: function(){
  119. var name = this.designer.propertyNameNode.get("value");
  120. var alias = this.designer.propertyAliasNode.get("value");
  121. var description = this.designer.propertyDescriptionNode.get("value");
  122. if (!name){
  123. this.designer.notice(this.designer.lp.notice.inputName, "error");
  124. return false;
  125. }
  126. this.data.name = name;
  127. this.data.alias = alias;
  128. this.data.description = description;
  129. this.designer.actions.saveDictionary(this.data, function(json){
  130. this.data.id = json.data.id;
  131. if (callback) callback();
  132. }.bind(this));
  133. },
  134. save: function(callback){
  135. if (this.designer.tab.showPage==this.page){
  136. var name = this.designer.propertyNameNode.get("value");
  137. var alias = this.designer.propertyAliasNode.get("value");
  138. var description = this.designer.propertyDescriptionNode.get("value");
  139. if (!name){
  140. this.designer.notice(this.designer.lp.notice.inputName, "error");
  141. return false;
  142. }
  143. this.data.name = name;
  144. this.data.alias = alias;
  145. this.data.description = description;
  146. this.data.appId = this.data.application;
  147. }
  148. this.designer.actions.saveDictionary(this.data, function(json){
  149. this.designer.notice(this.designer.lp.notice.save_success, "success", this.node, {"x": "left", "y": "bottom"});
  150. this.data.isNewDictionary = false;
  151. this.isNewDictionary = false;
  152. this.data.id = json.data.id;
  153. this.page.textNode.set("text", this.data.name);
  154. if (this.lisNode) {
  155. this.lisNode.getLast().set("text", this.data.name+"("+this.data.alias+")");
  156. }
  157. if (callback) callback();
  158. }.bind(this));
  159. },
  160. saveAs: function(){},
  161. explode: function(){},
  162. implode: function(){}
  163. });
  164. MWF.xApplication.cms.DictionaryDesigner.Dictionary.item = new Class({
  165. initialize: function(key, value, parent, level, dictionary, exp, nextSibling){
  166. this.key = key;
  167. this.value = value;
  168. this.parent = parent;
  169. this.level = level;
  170. this.dictionary = dictionary;
  171. this.exp = exp || false;
  172. this.nextSibling = nextSibling;
  173. this.children = [];
  174. this.childrenItemCreated = false;
  175. this.css = this.dictionary.css;
  176. this.type = typeOf(this.value);
  177. // if (this.parent) this.parent.children.push(this);
  178. this.load();
  179. },
  180. load: function(){
  181. this.createNodes();
  182. this.setNodeText();
  183. this.setEvent();
  184. if (this.exp) this.createChildrenItems();
  185. },
  186. createNodes: function() {
  187. this.itemNode = new Element("div", {"styles": this.css.itemNode});
  188. this.typeNode = new Element("div", {"styles": this.css.typeNode});
  189. this.valueNode = new Element("div", {"styles": this.css.valueNode});
  190. //var left = this.itemNode.getStyle("padding-left").toFloat();
  191. var left = 10;
  192. left = left + (this.level*20);
  193. this.itemNode.setStyle("padding-left", ""+left+"px");
  194. this.itemActionsAreaNode = new Element("div", {"styles": this.css.itemActionsAreaNode}).inject(this.itemNode);
  195. if (this.type=="array" || this.type=="object" || (this.parent && this.parent.type=="array")){
  196. this.itemAddActionNode = new Element("div", {"styles": this.css.itemAddActionNode}).inject(this.itemActionsAreaNode);
  197. }
  198. if (this.parent) this.itemDelActionNode = new Element("div", {"styles": this.css.itemDelActionNode}).inject(this.itemActionsAreaNode);
  199. if (this.type=="array" || this.type=="object"){
  200. this.itemExpColActionNode = new Element("div", {"styles": this.css.itemExpColActionNode}).inject(this.itemNode);
  201. if (this.exp){
  202. this.itemExpColActionNode.setStyle("background-image", "url("+"/x_component_cms_DictionaryDesigner/$Dictionary/default/icon/col.png)");
  203. }else{
  204. this.itemExpColActionNode.setStyle("background-image", "url("+"/x_component_cms_DictionaryDesigner/$Dictionary/default/icon/exp.png)");
  205. }
  206. }
  207. this.typeActionsAreaNode = new Element("div", {"styles": this.css.typeActionsAreaNode}).inject(this.typeNode);
  208. this.typeSelActionNode = new Element("div", {"styles": this.css.typeSelActionNode}).inject(this.typeActionsAreaNode);
  209. this.valueActionsAreaNode = new Element("div", {"styles": this.css.valueActionsAreaNode}).inject(this.valueNode);
  210. if (this.type=="boolean") this.valueSelActionNode = new Element("div", {"styles": this.css.valueSelActionNode}).inject(this.valueActionsAreaNode);
  211. this.itemTextNode = new Element("div", {"styles": this.css.itemTextNode}).inject(this.itemNode);
  212. this.typeTextNode = new Element("div", {"styles": this.css.typeTextNode}).inject(this.typeNode);
  213. this.valueTextNode = new Element("div", {"styles": this.css.valueTextNode}).inject(this.valueNode);
  214. if (this.nextSibling){
  215. this.itemNode.inject(this.nextSibling.itemNode, "before");
  216. this.typeNode.inject(this.nextSibling.typeNode, "before");
  217. this.valueNode.inject(this.nextSibling.valueNode, "before");
  218. }else{
  219. if (this.parent){
  220. if (this.parent.children.length){
  221. var injectItem = this.parent.children.getLast();
  222. this.itemNode.inject(injectItem.itemNode, "after");
  223. this.typeNode.inject(injectItem.typeNode, "after");
  224. this.valueNode.inject(injectItem.valueNode, "after");
  225. }else{
  226. this.itemNode.inject(this.parent.itemNode, "after");
  227. this.typeNode.inject(this.parent.typeNode, "after");
  228. this.valueNode.inject(this.parent.valueNode, "after");
  229. }
  230. }else{
  231. this.itemNode.inject(this.dictionary.itemsNode);
  232. this.typeNode.inject(this.dictionary.typesNode);
  233. this.valueNode.inject(this.dictionary.valuesNode);
  234. }
  235. }
  236. },
  237. resetNodes: function(){
  238. this.itemTextNode.removeEvents("mousedown");
  239. this.valueTextNode.removeEvents("mousedown");
  240. if (this.type=="array" || this.type=="object"){
  241. if (!this.itemExpColActionNode){
  242. this.itemExpColActionNode = new Element("div", {"styles": this.css.itemExpColActionNode}).inject(this.itemTextNode, "before");
  243. if (this.exp){
  244. this.itemExpColActionNode.setStyle("background-image", "url("+"/x_component_cms_DictionaryDesigner/$Dictionary/default/icon/col.png)");
  245. }else{
  246. this.itemExpColActionNode.setStyle("background-image", "url("+"/x_component_cms_DictionaryDesigner/$Dictionary/default/icon/exp.png)");
  247. }
  248. this.itemExpColActionNode.addEvents({
  249. "click": function(){this.expOrColChildren();}.bind(this)
  250. });
  251. }
  252. }else{
  253. if (this.itemExpColActionNode){
  254. this.itemExpColActionNode.destroy();
  255. this.itemExpColActionNode = null;
  256. }
  257. if (this.type!="boolean") this.valueTextNode.addEvent("mousedown", function(e){this.editValue();}.bind(this));
  258. }
  259. if (this.type=="array" || this.type=="object" || (this.parent && this.parent.type=="array")){
  260. if (!this.itemAddActionNode){
  261. this.itemAddActionNode = new Element("div", {"styles": this.css.itemAddActionNode}).inject(this.itemActionsAreaNode);
  262. this.itemAddActionNode.addEvent("click", function(e){this.addItem(e);}.bind(this));
  263. }
  264. }else{
  265. if (this.itemAddActionNode) this.itemAddActionNode.destroy();
  266. this.itemAddActionNode = null;
  267. }
  268. if (this.type=="boolean"){
  269. if (!this.valueSelActionNode) this.valueSelActionNode = new Element("div", {"styles": this.css.valueSelActionNode}).inject(this.valueActionsAreaNode);
  270. this.valueSelActionNode.addEvent("click", function(){this.selectBooleanValue();}.bind(this));
  271. this.valueTextNode.addEvent("click", function(){this.selectBooleanValue();}.bind(this));
  272. }
  273. if (this.parent){
  274. if (this.parent.type!="array"){
  275. this.itemTextNode.addEvent("mousedown", function(e){this.editKey();}.bind(this));
  276. }
  277. }
  278. },
  279. setNodeText: function(){
  280. var text = this.key;
  281. if (this.parent) if (this.parent.type=="array") text = "["+text+"]";
  282. this.itemTextNode.set("text", text);
  283. this.typeTextNode.set("text", this.type);
  284. switch(this.type){
  285. case "array":
  286. this.valueTextNode.setStyles(this.css.valueTextNode);
  287. this.valueTextNode.set("text", ""+this.value.length+" Items");
  288. break;
  289. case "object":
  290. var i=0;
  291. Object.each(this.value, function(){i++;});
  292. this.valueTextNode.setStyles(this.css.valueTextNode);
  293. this.valueTextNode.set("text", ""+i+" Items");
  294. break;
  295. default:
  296. this.valueTextNode.setStyles(this.css.valueTextNode_edit);
  297. this.valueTextNode.set("text", this.value);
  298. break;
  299. }
  300. },
  301. setEvent: function(){
  302. this.itemNode.addEvent("click", function(e){this.selected();}.bind(this));
  303. this.typeNode.addEvent("click", function(e){this.selected();}.bind(this));
  304. this.valueNode.addEvent("click", function(e){this.selected();}.bind(this));
  305. this.typeSelActionNode.addEvent("click", function(e){this.selectType();}.bind(this));
  306. this.typeTextNode.addEvent("click", function(e){this.selectType();}.bind(this));
  307. this.itemNode.addEvents({
  308. "mouseover": function(){this.itemActionsAreaNode.fade("in");}.bind(this),
  309. "mouseout": function(){this.itemActionsAreaNode.fade("out");}.bind(this)
  310. });
  311. if (this.itemAddActionNode) this.itemAddActionNode.addEvent("click", function(e){this.addItem(e);}.bind(this));
  312. if (this.itemDelActionNode) this.itemDelActionNode.addEvent("click", function(e){this.delItem(e);}.bind(this));
  313. if (this.type=="array" || this.type=="object"){
  314. this.itemExpColActionNode.addEvents({
  315. "click": function(){this.expOrColChildren();}.bind(this)
  316. });
  317. }else{
  318. if (this.type!="boolean") this.valueTextNode.addEvent("mousedown", function(e){this.editValue();}.bind(this));
  319. }
  320. if (this.parent){
  321. if (this.parent.type!="array"){
  322. this.itemTextNode.addEvent("mousedown", function(e){this.editKey();}.bind(this));
  323. }
  324. }
  325. if (this.type=="boolean"){
  326. this.valueSelActionNode.addEvent("click", function(){this.selectBooleanValue();}.bind(this));
  327. this.valueTextNode.addEvent("click", function(){this.selectBooleanValue();}.bind(this));
  328. }
  329. },
  330. expOrColChildren: function(){
  331. if (this.exp){
  332. this.colChildren();
  333. this.itemExpColActionNode.setStyle("background-image", "url("+"/x_component_cms_DictionaryDesigner/$Dictionary/default/icon/exp.png)");
  334. this.exp = false;
  335. }else{
  336. this.expChildren();
  337. this.itemExpColActionNode.setStyle("background-image", "url("+"/x_component_cms_DictionaryDesigner/$Dictionary/default/icon/col.png)");
  338. this.exp = true;
  339. }
  340. },
  341. colChildren: function(){
  342. this.children.each(function(item){
  343. item.colChildren();
  344. item.colChildrenNode();
  345. });
  346. },
  347. expChildren: function(){
  348. this.createChildrenItems();
  349. this.children.each(function(item){
  350. if (item.exp) item.expChildren();
  351. item.expChildrenNode();
  352. });
  353. },
  354. colChildrenNode: function(){
  355. this.itemNode.setStyle("display", "none");
  356. this.typeNode.setStyle("display", "none");
  357. this.valueNode.setStyle("display", "none");
  358. },
  359. expChildrenNode: function(){
  360. this.itemNode.setStyle("display", "block");
  361. this.typeNode.setStyle("display", "block");
  362. this.valueNode.setStyle("display", "block");
  363. },
  364. unSelected: function(){
  365. this.itemNode.setStyles(this.css.itemNode);
  366. this.typeNode.setStyles(this.css.typeNode);
  367. this.valueNode.setStyles(this.css.valueNode);
  368. this.dictionary.currentSelectedItem = null;
  369. },
  370. selected: function(){
  371. if (this.dictionary.currentSelectedItem!=this){
  372. if (this.dictionary.currentSelectedItem) this.dictionary.currentSelectedItem.unSelected();
  373. this.itemNode.setStyles(this.css.itemNode_selected);
  374. this.typeNode.setStyles(this.css.typeNode_selected);
  375. this.valueNode.setStyles(this.css.valueNode_selected);
  376. this.listDataItems();
  377. this.dictionary.currentSelectedItem = this;
  378. }
  379. },
  380. listDataItems: function(){
  381. this.dictionary.propertyListNode.empty();
  382. switch(this.type){
  383. case "array":
  384. this.value.each(function(v, idx){
  385. this.createDataListItem("["+idx+"]", v.toString());
  386. }.bind(this));
  387. break;
  388. case "object":
  389. Object.each(this.value, function(v, key){
  390. this.createDataListItem(key, v.toString());
  391. }.bind(this));
  392. break;
  393. }
  394. },
  395. createDataListItem: function(key, v){
  396. var node = new Element("div", {"styles": this.css.dataListItemNode}).inject(this.dictionary.propertyListNode);
  397. var keyNode = new Element("div", {"styles": this.css.dataListItemKeyNode, "text": key, "title": key}).inject(node);
  398. var vNode = new Element("div", {"styles": this.css.dataListItemValueNode, "text": v, "title": v}).inject(node);
  399. },
  400. createNewItem: function(key, value, parent, level, dictionary, exp, nextSibling){
  401. return new MWF.xApplication.cms.DictionaryDesigner.Dictionary.item(key, value, parent, level, dictionary, exp, nextSibling);
  402. },
  403. createChildrenItems: function(){
  404. if (!this.childrenItemCreated){
  405. switch(this.type){
  406. case "array":
  407. this.value.each(function(v, idx){
  408. this.children.push(this.createNewItem(idx, v, this, this.level+1, this.dictionary, false));
  409. }.bind(this));
  410. break;
  411. case "object":
  412. Object.each(this.value, function(v, key){
  413. this.children.push(this.createNewItem(key, v, this, this.level+1, this.dictionary, false));
  414. }.bind(this));
  415. break;
  416. default:
  417. //nothing
  418. break;
  419. }
  420. this.childrenItemCreated = true;
  421. }
  422. },
  423. addItem: function(e){
  424. if (!this.parent){
  425. this.createChildrenItems();
  426. this.addChild();
  427. }else{
  428. if (this.exp){
  429. this.addChild();
  430. }else{
  431. this.addSibling();
  432. }
  433. }
  434. this.dictionary.jsonParse.loadObjectTree();
  435. },
  436. addChild: function(){
  437. var item;
  438. if (this.type=="array"){
  439. var idx = this.value.length;
  440. var arrayValue = "New Element Value";
  441. this.value.push(arrayValue);
  442. item = this.createNewItem(idx, arrayValue, this, this.level+1, this.dictionary, false);
  443. }
  444. if (this.type=="object") {
  445. var key = "NewItem";
  446. var i = 0;
  447. while (this.value[key] !== undefined) {
  448. i++;
  449. key = "NewItem" + i;
  450. }
  451. var objValue1 = "New Item Value";
  452. this.value[key] = objValue1;
  453. item = this.createNewItem(key, objValue1, this, this.level + 1, this.dictionary, false)
  454. }
  455. if (this.children.length) this.children[this.children.length-1].nextSibling = item;
  456. this.children.push(item);
  457. },
  458. addSibling: function(){
  459. var item = null;
  460. var idx;
  461. if (this.parent.type=="array"){
  462. idx = this.key;
  463. var value = "New Element Value";
  464. this.parent.value.splice(this.key, 0, value);
  465. for (var i=this.key; i<this.parent.children.length; i++){
  466. var item = this.parent.children[i];
  467. item.key = item.key+1;
  468. item.setNodeText();
  469. }
  470. item = this.createNewItem(idx, value, this.parent, this.level, this.dictionary, false, this);
  471. }else{
  472. var key = "NewItem";
  473. var i = 0;
  474. while (this.parent.value[key] != undefined) {
  475. i++;
  476. key = "NewItem" + i;
  477. }
  478. var value = "New Item Value";
  479. this.parent.value[key] = value;
  480. var item = this.createNewItem(key, value, this.parent, this.level, this.dictionary, false, this);
  481. idx = this.parent.children.indexOf(this);
  482. }
  483. if (idx) this.parent.children[idx-1].nextSibling = item;
  484. this.parent.children.splice(idx, 0, item);
  485. },
  486. delItem: function(e){
  487. var _self = this;
  488. this.dictionary.designer.shortcut = false;
  489. this.dictionary.designer.confirm("warn", e, this.dictionary.designer.lp.notice.deleteDataTitle, this.dictionary.designer.lp.notice.deleteData, 300, 120, function(){
  490. _self.destroy();
  491. _self.dictionary.jsonParse.loadObjectTree();
  492. _self.dictionary.designer.shortcut = true;
  493. this.close();
  494. }, function(){
  495. _self.dictionary.designer.shortcut = true;
  496. this.close();
  497. });
  498. },
  499. destroy: function(){
  500. var idx = this.parent.children.indexOf(this);
  501. if (idx) this.parent.children[idx-1].nextSibling = this.nextSibling;
  502. this.destroyAllNodes();
  503. this.parent.children.erase(this);
  504. if (this.parent.type=="object"){
  505. delete this.parent.value[this.key];
  506. delete this;
  507. }
  508. if (this.parent.type=="array"){
  509. this.parent.value.splice(this.key, 1);
  510. for (var i=this.key; i<this.parent.children.length; i++){
  511. this.parent.children[i].key = this.parent.children[i].key-1;
  512. this.parent.children[i].setNodeText();
  513. }
  514. }
  515. this.dictionary.jsonParse.loadObjectTree();
  516. },
  517. destroyAllNodes: function(){
  518. this.children.each(function(item){
  519. item.destroyAllNodes();
  520. });
  521. this.itemNode.destroy();
  522. this.typeNode.destroy();
  523. this.valueNode.destroy();
  524. if (this.typeSelectNode) this.typeSelectNode.destroy();
  525. },
  526. selectType: function(){
  527. if (!this.typeSelectNode) this.createTypeSelectNode();
  528. this.typeSelectNode.setStyle("display", "block");
  529. var size = this.dictionary.node.getSize();
  530. var selSize = this.typeSelectNode.getSize();
  531. var itemNodes = this.typeSelectNode.getChildren();
  532. for (var i=0; i<itemNodes.length; i++){
  533. if (itemNodes[i].get("text")==this.type){
  534. itemNodes[i].setStyles(this.css.typeSelectItemNode_over);
  535. }else{
  536. itemNodes[i].setStyles(this.css.typeSelectItemNode);
  537. }
  538. };
  539. this.typeSelectNode.position({
  540. relativeTo: this.typeNode,
  541. position: 'upperLeft',
  542. edge: 'upperLeft'
  543. });
  544. var p = this.typeSelectNode.getPosition(this.typeSelectNode.getOffsetParent());
  545. if ((p.y+selSize.y)>size.y){
  546. this.typeSelectNode.position({
  547. relativeTo: this.typeNode,
  548. position: 'bottomLeft',
  549. edge: 'bottomLeft'
  550. });
  551. };
  552. this.closeTypeSelectNodeFun = this.closeTypeSelectNode.bind(this);
  553. $(document.body).addEvent("mousedown", this.closeTypeSelectNodeFun);
  554. },
  555. closeTypeSelectNode: function(){
  556. this.typeSelectNode.setStyle("display", "none");
  557. $(document.body).removeEvent("mousedown", this.closeTypeSelectNodeFun);
  558. },
  559. createTypeSelectNode: function(){
  560. var _self = this;
  561. this.typeSelectNode = new Element("div", {"styles": this.css.typeSelectNode});
  562. var types = this.dictionary.options.types;
  563. if (!this.parent){
  564. types = ["object", "array"];
  565. this.typeSelectNode.setStyle("height", "50px");
  566. }
  567. types.each(function(type){
  568. var itemNode = new Element("div", {"styles": this.css.typeSelectItemNode}).inject(this.typeSelectNode);
  569. itemNode.set("text", type);
  570. if (this.type==type) itemNode.setStyles(this.css.typeSelectItemNode_over);
  571. itemNode.addEvents({
  572. "mouseover": function(){this.setStyles(_self.css.typeSelectItemNode_over);},
  573. "mouseout": function(){this.setStyles(_self.css.typeSelectItemNode);},
  574. "mousedown": function(e){_self.selectedType(this, e);}
  575. })
  576. }.bind(this));
  577. this.typeSelectNode.inject(this.dictionary.node);
  578. },
  579. selectedType: function(itemNode, e){
  580. e.target = null;
  581. var type = itemNode.get("text");
  582. if (this.type!=type){
  583. var _self = this;
  584. switch(type){
  585. case "array":
  586. if (this.value!="New Item Value" && this.value!="New Element Value"){
  587. this.dictionary.designer.confirm("warn", e, this.dictionary.designer.lp.notice.changeType, this.dictionary.designer.lp.notice.changeType, 300, 120, function(){
  588. if (_self.type=="object"){
  589. _self.changeTypeObjectToArray(type);
  590. }else{
  591. _self.changeTypePrimitiveToArray(type);
  592. }
  593. this.close();
  594. _self.dictionary.jsonParse.loadObjectTree();
  595. }, function(){
  596. this.close();
  597. });
  598. }else{
  599. if (this.type=="object"){
  600. this.changeTypeObjectToArray(type);
  601. }else{
  602. this.changeTypePrimitiveToArray(type);
  603. }
  604. this.dictionary.jsonParse.loadObjectTree();
  605. }
  606. break;
  607. case "object":
  608. if (this.value!="New Item Value" && this.value!="New Element Value") {
  609. this.dictionary.designer.confirm("warn", e, this.dictionary.designer.lp.notice.changeType, this.dictionary.designer.lp.notice.changeType, 300, 120, function () {
  610. if (_self.type == "array") {
  611. _self.changeTypeArrayToObject(type);
  612. } else {
  613. _self.changeTypePrimitiveToObject(type);
  614. }
  615. this.close();
  616. _self.dictionary.jsonParse.loadObjectTree();
  617. }, function () {
  618. this.close();
  619. });
  620. }else{
  621. if (this.type == "array") {
  622. this.changeTypeArrayToObject(type);
  623. } else {
  624. this.changeTypePrimitiveToObject(type);
  625. }
  626. this.dictionary.jsonParse.loadObjectTree();
  627. }
  628. break;
  629. default:
  630. if (this.value!="New Item Value" && this.value!="New Element Value") {
  631. this.dictionary.designer.confirm("warn", e, this.dictionary.designer.lp.notice.changeType, this.dictionary.designer.lp.notice.changeTypeDeleteChildren, 300, 120, function () {
  632. if (_self.type == "array") {
  633. _self.changeTypeArrayToPrimitive(type);
  634. } else if (_self.type == "object") {
  635. _self.changeTypeObjectToPrimitive(type);
  636. } else {
  637. _self.changeTypePrimitiveToPrimitive(type);
  638. }
  639. this.close();
  640. _self.dictionary.jsonParse.loadObjectTree();
  641. }, function () {
  642. this.close();
  643. });
  644. }else{
  645. if (this.type == "array") {
  646. this.changeTypeArrayToPrimitive(type);
  647. } else if (_self.type == "object") {
  648. this.changeTypeObjectToPrimitive(type);
  649. } else {
  650. this.changeTypePrimitiveToPrimitive(type);
  651. }
  652. this.dictionary.jsonParse.loadObjectTree();
  653. }
  654. break;
  655. }
  656. }
  657. },
  658. deleteAllChildren: function(){
  659. this.children.each(function(item){
  660. item.destroyAllNodes();
  661. });
  662. },
  663. changeTypeObjectToPrimitive: function(type){
  664. this.deleteAllChildren();
  665. this.children = [];
  666. this.childrenItemCreated = false;
  667. var value;
  668. switch(type){
  669. case "string":
  670. value = "";
  671. break;
  672. case "number":
  673. value = 0;
  674. break;
  675. case "boolean":
  676. value = true;
  677. break;
  678. }
  679. delete this.parent.value[this.key];
  680. this.parent.value[this.key] = value;
  681. this.value = value;
  682. this.type = type;
  683. this.exp = false;
  684. this.setNodeText();
  685. this.resetNodes();
  686. },
  687. changeTypeArrayToPrimitive: function(type){
  688. this.changeTypeObjectToPrimitive(type);
  689. },
  690. changeTypePrimitiveToPrimitive: function(type){
  691. switch(type){
  692. case "string":
  693. value = this.value.toString();
  694. break;
  695. case "number":
  696. value = this.value.toFloat();
  697. if (isNaN(value)) value = 0;
  698. break;
  699. case "boolean":
  700. value = true;
  701. if (this.value=="false") value = false;
  702. break;
  703. }
  704. delete this.parent.value[this.key];
  705. this.parent.value[this.key] = value;
  706. this.value = value;
  707. this.type = type;
  708. this.exp = false;
  709. this.setNodeText();
  710. this.resetNodes();
  711. },
  712. changeTypePrimitiveToObject: function(type){
  713. value = {};
  714. delete this.parent.value[this.key];
  715. this.parent.value[this.key] = value;
  716. this.value = value;
  717. this.type = type;
  718. this.exp = false;
  719. this.setNodeText();
  720. this.resetNodes();
  721. },
  722. changeTypeArrayToObject: function(type){
  723. this.deleteAllChildren();
  724. this.children = [];
  725. this.childrenItemCreated = false;
  726. var value = {};
  727. this.value.each(function(v, idx){
  728. value["ITEM"+idx] = v;
  729. });
  730. if (this.parent){
  731. delete this.parent.value[this.key];
  732. this.parent.value[this.key] = value;
  733. this.value = value;
  734. this.type = type;
  735. this.setNodeText();
  736. this.resetNodes();
  737. if (this.exp) this.createChildrenItems();
  738. }else{
  739. this.dictionary.data.data = value;
  740. this.dictionary.jsonParse.json = value;
  741. this.value = value;
  742. this.type = type;
  743. this.setNodeText();
  744. this.resetNodes();
  745. if (this.exp) this.createChildrenItems();
  746. }
  747. },
  748. changeTypePrimitiveToArray: function(type) {
  749. value = [];
  750. delete this.parent.value[this.key];
  751. this.parent.value[this.key] = value;
  752. this.value = value;
  753. this.type = type;
  754. this.exp = false;
  755. this.setNodeText();
  756. this.resetNodes();
  757. },
  758. changeTypeObjectToArray: function(type){
  759. this.deleteAllChildren();
  760. this.children = [];
  761. this.childrenItemCreated = false;
  762. var value = [];
  763. Object.each(this.value, function(v, idx){
  764. value.push(v);
  765. });
  766. if (this.parent){
  767. delete this.parent.value[this.key];
  768. this.parent.value[this.key] = value;
  769. this.value = value;
  770. this.type = type;
  771. this.childrenItemCreated = false;
  772. this.setNodeText();
  773. this.resetNodes();
  774. if (this.exp) this.createChildrenItems();
  775. }else{
  776. this.dictionary.data.data = value;
  777. this.dictionary.jsonParse.json = value;
  778. this.value = value;
  779. this.type = type;
  780. this.childrenItemCreated = false;
  781. this.setNodeText();
  782. this.resetNodes();
  783. if (this.exp) this.createChildrenItems();
  784. }
  785. },
  786. editValue: function(){
  787. //this.inEdit
  788. this.valueTextNode.empty();
  789. // this.valueTextNode.removeEvents("mousedown");
  790. this.editValueNode = new Element("input", {"styles": this.css.itemEditValueNode}).inject(this.valueTextNode);
  791. this.editValueNode.set("value", this.value);
  792. window.setTimeout(function(){
  793. this.editValueNode.focus();
  794. this.editValueNode.select();
  795. this.editValueNode.addEvents({
  796. "blur": function(e){
  797. this.editValueConfirm(e);
  798. }.bind(this),
  799. "keydown": function(e){
  800. if (e.code==13){
  801. this.editValueConfirm(e);
  802. if (this.nextSibling){
  803. this.nextSibling.editKey();
  804. }
  805. }
  806. e.stopPropagation();
  807. }.bind(this),
  808. "mousedown": function(e){e.stopPropagation();}
  809. });
  810. }.bind(this), 10)
  811. },
  812. editValueConfirm: function(e){
  813. var value = this.editValueNode.get("Value");
  814. if (this.type=="number"){
  815. if (isNaN(parseFloat(value))){
  816. this.dictionary.designer.notice(this.dictionary.designer.lp.notice.inputTypeError, "error", this.editValueNode, {"x": "left", "y": "bottom"}, {"x": 0, "y": 24});
  817. this.editValueNode.setStyles(this.css.itemEditValueNode_error);
  818. this.editValueNode.select();
  819. e.preventDefault();
  820. return false;
  821. }
  822. value = value.toFloat();
  823. }
  824. this.value = value;
  825. this.parent.value[this.key] = this.value;
  826. this.editValueNode.destroy();
  827. this.editValueNode = null;
  828. this.setNodeText();
  829. this.dictionary.jsonParse.loadObjectTree();
  830. // if (this.type!="boolean") this.valueTextNode.addEvent("mousedown", function(e){this.editValue();}.bind(this));
  831. },
  832. editKey: function(){
  833. this.itemTextNode.empty();
  834. // this.itemTextNode.removeEvents("mousedown");
  835. this.editKeyNode = new Element("input", {"styles": this.css.itemEditValueNode, "type": "text"}).inject(this.itemTextNode);
  836. this.editKeyNode.set("value", this.key);
  837. window.setTimeout(function(){
  838. this.editKeyNode.focus();
  839. this.editKeyNode.select();
  840. //this.editKeyNode.setSelectionRange(1,this.key.length-1);
  841. this.editKeyNode.addEvents({
  842. "blur": function(e){this.editKeyConfirm(e);}.bind(this),
  843. "keydown": function(e){
  844. if (e.code==13){
  845. this.editKeyConfirm(e);
  846. if (this.type!="array" && this.type!="object" && this.type!="boolean") {
  847. this.editValue();
  848. }else{
  849. if (this.nextSibling){
  850. this.nextSibling.editKey();
  851. }
  852. }
  853. }
  854. e.stopPropagation();
  855. }.bind(this),
  856. "mousedown": function(e){e.stopPropagation();}
  857. });
  858. }.bind(this), 10);
  859. },
  860. editKeyConfirm: function(e){
  861. var key = this.editKeyNode.get("Value");
  862. if (key!=this.key){
  863. if (this.parent.value[key]){
  864. this.dictionary.designer.notice(this.dictionary.designer.lp.notice.sameKey, "error", this.editKeyNode, {"x": "left", "y": "bottom"}, {"x": 0, "y": 24});
  865. this.editKeyNode.setStyles(this.css.itemEditValueNode_error);
  866. this.editKeyNode.select();
  867. e.preventDefault();
  868. return false;
  869. }
  870. if (!isNaN(parseFloat(key))){
  871. this.dictionary.designer.notice(this.dictionary.designer.lp.notice.numberKey, "error", this.editKeyNode, {"x": "left", "y": "bottom"}, {"x": 0, "y": 24});
  872. this.editKeyNode.setStyles(this.css.itemEditValueNode_error);
  873. this.editKeyNode.select();
  874. e.preventDefault();
  875. return false;
  876. }
  877. if (!key){
  878. this.dictionary.designer.notice(this.dictionary.designer.lp.notice.emptyKey, "error", this.editKeyNode, {"x": "left", "y": "bottom"}, {"x": 0, "y": 24});
  879. this.editKeyNode.setStyles(this.css.itemEditValueNode_error);
  880. this.editKeyNode.select();
  881. e.preventDefault();
  882. return false;
  883. }
  884. delete this.parent.value[this.key];
  885. this.parent.value[key] = this.value;
  886. this.key = key;
  887. }
  888. this.editKeyNode.destroy();
  889. this.editKeyNode = null;
  890. this.setNodeText();
  891. this.dictionary.jsonParse.loadObjectTree();
  892. //if (this.parent){
  893. // if (this.parent.type!="array"){
  894. // this.itemTextNode.addEvent("mousedown", function(e){this.editKey();}.bind(this));
  895. // }
  896. //}
  897. },
  898. selectBooleanValue: function(){
  899. if (!this.booleanSelectNode) this.createBooleanSelectNode();
  900. var size = this.dictionary.node.getSize();
  901. this.booleanSelectNode.setStyle("display", "block");
  902. var selSize = this.booleanSelectNode.getSize();
  903. var itemNodes = this.booleanSelectNode.getChildren();
  904. for (var i=0; i<itemNodes.length; i++){
  905. if (itemNodes[i].get("text")==this.value.toString()){
  906. itemNodes[i].setStyles(this.css.typeSelectItemNode_over);
  907. }else{
  908. itemNodes[i].setStyles(this.css.typeSelectItemNode);
  909. }
  910. };
  911. this.booleanSelectNode.position({
  912. relativeTo: this.valueNode,
  913. position: 'upperLeft',
  914. edge: 'upperLeft'
  915. });
  916. var p = this.booleanSelectNode.getPosition(this.booleanSelectNode.getOffsetParent());
  917. if ((p.y+selSize.y)>size.y){
  918. this.booleanSelectNode.position({
  919. relativeTo: this.valueNode,
  920. position: 'bottomLeft',
  921. edge: 'bottomLeft'
  922. });
  923. };
  924. this.closeBooleanSelectNodeFun = this.closeBooleanSelectNode.bind(this);
  925. $(document.body).addEvent("mousedown", this.closeBooleanSelectNodeFun);
  926. },
  927. closeBooleanSelectNode: function(){
  928. this.booleanSelectNode.setStyle("display", "none");
  929. $(document.body).removeEvent("mousedown", this.closeBooleanSelectNodeFun);
  930. },
  931. createBooleanSelectNode: function(){
  932. var _self = this;
  933. this.booleanSelectNode = new Element("div", {"styles": this.css.booleanSelectNode});
  934. ["true", "false"].each(function(type){
  935. var itemNode = new Element("div", {"styles": this.css.typeSelectItemNode}).inject(this.booleanSelectNode);
  936. itemNode.set("text", type);
  937. if (this.value.toString()==type) itemNode.setStyles(this.css.typeSelectItemNode_over);
  938. itemNode.addEvents({
  939. "mouseover": function(){this.setStyles(_self.css.typeSelectItemNode_over);},
  940. "mouseout": function(){this.setStyles(_self.css.typeSelectItemNode);},
  941. "mousedown": function(e){_self.selectedBoolean(this, e);}
  942. })
  943. }.bind(this));
  944. this.booleanSelectNode.inject(this.dictionary.node);
  945. },
  946. selectedBoolean: function(item, e){
  947. var text = item.get("text");
  948. var value = (text=="false") ? false : true;
  949. this.value = value;
  950. this.parent.value[this.key] = value;
  951. this.setNodeText();
  952. this.dictionary.jsonParse.loadObjectTree();
  953. }
  954. });
  955. MWF.xApplication.cms.DictionaryDesigner.DictionaryReader = new Class({
  956. Extends: MWF.xApplication.cms.DictionaryDesigner.Dictionary,
  957. autoSave: function(){},
  958. createRootItem: function() {
  959. this.items.push(new MWF.xApplication.cms.DictionaryDesigner.Dictionary.ItemReader("ROOT", this.data.data, null, 0, this, true));
  960. }
  961. });
  962. MWF.xApplication.cms.DictionaryDesigner.Dictionary.ItemReader= new Class({
  963. Extends: MWF.xApplication.cms.DictionaryDesigner.Dictionary.item,
  964. createNewItem: function(key, value, parent, level, dictionary, exp, nextSibling){
  965. return new MWF.xApplication.cms.DictionaryDesigner.Dictionary.ItemReader(key, value, parent, level, dictionary, exp, nextSibling);
  966. },
  967. setEvent: function(){
  968. this.itemNode.addEvent("click", function(e){this.selected();}.bind(this));
  969. this.typeNode.addEvent("click", function(e){this.selected();}.bind(this));
  970. this.valueNode.addEvent("click", function(e){this.selected();}.bind(this));
  971. //this.typeSelActionNode.addEvent("click", function(e){this.selectType();}.bind(this));
  972. //this.typeTextNode.addEvent("click", function(e){this.selectType();}.bind(this));
  973. //this.itemNode.addEvents({
  974. // "mouseover": function(){this.itemActionsAreaNode.fade("in");}.bind(this),
  975. // "mouseout": function(){this.itemActionsAreaNode.fade("out");}.bind(this)
  976. //});
  977. //if (this.itemAddActionNode) this.itemAddActionNode.addEvent("click", function(e){this.addItem(e);}.bind(this));
  978. //if (this.itemDelActionNode) this.itemDelActionNode.addEvent("click", function(e){this.delItem(e);}.bind(this));
  979. if (this.type=="array" || this.type=="object"){
  980. this.itemExpColActionNode.addEvents({
  981. "click": function(){this.expOrColChildren();}.bind(this)
  982. });
  983. }else{
  984. //if (this.type!="boolean") this.valueTextNode.addEvent("mousedown", function(e){this.editValue();}.bind(this));
  985. }
  986. //if (this.parent){
  987. // if (this.parent.type!="array"){
  988. // this.itemTextNode.addEvent("mousedown", function(e){this.editKey();}.bind(this));
  989. // }
  990. //}
  991. //if (this.type=="boolean"){
  992. // this.valueSelActionNode.addEvent("click", function(){this.selectBooleanValue();}.bind(this));
  993. // this.valueTextNode.addEvent("click", function(){this.selectBooleanValue();}.bind(this));
  994. //}
  995. }
  996. });
  997. //MWF.xApplication.cms.ProcessDesigner.Process.Property = new Class({
  998. // Implements: [Options, Events],
  999. // Extends: MWF.APPPD.Property,
  1000. // initialize: function(process, options){
  1001. // this.setOptions(options);
  1002. // this.process = process;
  1003. // this.paper = this.process.paper;
  1004. // this.data = process.process;
  1005. // this.htmlPath = "/x_component_process_ProcessDesigner//$Process/process.html";
  1006. // }
  1007. //});