ActionsEditor.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. MWF.xApplication.process.FormDesigner.widget = MWF.xApplication.process.FormDesigner.widget || {};
  2. MWF.require("MWF.widget.ScriptArea", null, false);
  3. MWF.require("MWF.widget.Maplist", null, false);
  4. MWF.xApplication.process.FormDesigner.widget.ActionsEditor = new Class({
  5. Implements: [Options, Events],
  6. Extends: MWF.widget.Common,
  7. options: {
  8. "style": "default",
  9. "maxObj": document.body,
  10. "isSystemTool" : false,
  11. "noCreate": false,
  12. "noDelete": false,
  13. "noCode": false,
  14. "noHide": false
  15. },
  16. initialize: function(node, designer, module, options){
  17. this.setOptions(options);
  18. this.node = $(node);
  19. this.module = module;
  20. this.designer = designer;
  21. this.path = "/x_component_process_FormDesigner/widget/$ActionsEditor/";
  22. this.cssPath = "/x_component_process_FormDesigner/widget/$ActionsEditor/"+this.options.style+"/css.wcss";
  23. this._loadCss();
  24. this.actions = [];
  25. this.currentEditItem = null;
  26. this.createActionsAreaNode();
  27. },
  28. createActionsAreaNode: function(){
  29. this.actionsContainer = new Element("div", {
  30. "styles": this.css.actionsContainer
  31. }).inject(this.node);
  32. // var size = this.node.getUsefulSize();
  33. // this.eventsContainer.setStyle("height", size.y);
  34. },
  35. load: function(data){
  36. this.loadActionsArea();
  37. if (!this.options.noCreate) this.loadCreateActionButton();
  38. this.data = data;
  39. if ( !this.data || typeOf(this.data)!="array") this.data = [];
  40. this.loadRestoreActionButton();
  41. this.data.each(function(actionData, idx){
  42. if (actionData.type!="MWFToolBarSeparator"){
  43. var action = new MWF.xApplication.process.FormDesigner.widget.ActionsEditor.ButtonAction(this);
  44. action.load(actionData);
  45. this.actions.push(action);
  46. }
  47. }.bind(this));
  48. },
  49. loadActionsArea: function(){
  50. if (!this.options.noCreate){
  51. this.actionTitleArea = new Element("div", {
  52. "styles": this.css.actionTitleArea
  53. }).inject(this.actionsContainer);
  54. }
  55. this.actionArea = new Element("div", {
  56. "styles": this.css.actionArea
  57. }).inject(this.actionsContainer);
  58. },
  59. loadCreateActionButton: function(){
  60. this.createActionButtonButton = new Element("div", {
  61. "styles": this.css.createActionButton,
  62. "title" : this.designer.lp.actionbar.addCustomTool,
  63. "text": "+"
  64. }).inject(this.actionTitleArea);
  65. this.createActionButtonButton.addEvent("click", function(){
  66. this.addButtonAction();
  67. }.bind(this));
  68. },
  69. loadRestoreActionButton : function(){
  70. if( this.options.isSystemTool ){
  71. if( !this.actionTitleArea ){
  72. this.actionTitleArea = new Element("div", { "styles": this.css.actionTitleArea }).inject(this.actionArea,"before");
  73. }
  74. this.restoreActionButtonButton = new Element("div", {
  75. "styles": this.css.restoreActionButton,
  76. "title" : this.designer.lp.actionbar.restoreDefaultTool
  77. }).inject(this.actionTitleArea);
  78. this.restoreActionButtonButton.addEvent("click", function(){
  79. this.restoreButtonAction();
  80. }.bind(this));
  81. }
  82. },
  83. listRemovedSystemTool : function(){
  84. var list = [];
  85. if( !this.defaultTools ){
  86. MWF.getJSON( "/x_component_process_FormDesigner/Module/Actionbar/toolbars.json", function(tools){
  87. this.defaultTools = tools;
  88. }.bind(this), false);
  89. }
  90. this.defaultTools.each( function( tool ){
  91. var flag = true;
  92. for( var i=0; i<(this.data || []).length; i++ ){
  93. if( this.data[i].id === tool.id ){
  94. flag = false;
  95. break;
  96. }
  97. }
  98. if(flag)list.push(tool);
  99. }.bind(this));
  100. return list;
  101. },
  102. addButtonAction: function(){
  103. var o = {
  104. "type": "MWFToolBarButton",
  105. "img": "4.png",
  106. "title": "",
  107. "action": "",
  108. "text": "Unnamed",
  109. "actionScript" : "",
  110. "condition": "",
  111. "editShow": true,
  112. "readShow": true
  113. };
  114. var action = new MWF.xApplication.process.FormDesigner.widget.ActionsEditor.ButtonAction(this);
  115. action.load(o);
  116. this.data.push(o);
  117. this.actions.push(action);
  118. this.fireEvent("change");
  119. },
  120. restoreButtonAction : function(){
  121. var list = this.listRemovedSystemTool();
  122. if( !list.length )return;
  123. var selectableItems = [];
  124. list.each( function(d){
  125. selectableItems.push( {
  126. name : d.text,
  127. id : d.id
  128. })
  129. }.bind(this));
  130. MWF.xDesktop.requireApp("Template", "Selector.Custom", null, false);
  131. var opt = {
  132. "count": 0,
  133. "title": this.designer.lp.actionbar.selectDefaultTool,
  134. "selectableItems" : selectableItems,
  135. "values": [],
  136. "onComplete": function( array ){
  137. if( !array || array.length == 0 )return;
  138. array.each( function(tool){
  139. for( var i=0; i<list.length; i++ ){
  140. if( list[i].id === tool.data.id ){
  141. this.data.push( list[i] );
  142. var action = new MWF.xApplication.process.FormDesigner.widget.ActionsEditor.ButtonAction(this);
  143. action.load(list[i]);
  144. this.actions.push(action);
  145. break;
  146. }
  147. }
  148. }.bind(this));
  149. this.fireEvent("change");
  150. }.bind(this)
  151. };
  152. var selector = new MWF.xApplication.Template.Selector.Custom(this.options.maxObj, opt );
  153. selector.load();
  154. }
  155. });
  156. MWF.xApplication.process.FormDesigner.widget.ActionsEditor.ButtonAction = new Class({
  157. initialize: function (editor) {
  158. this.editor = editor;
  159. this.css = this.editor.css;
  160. this.container = this.editor.actionArea
  161. },
  162. load: function (data) {
  163. this.data = data;
  164. this.loadNode();
  165. var form = this.editor.designer.form || this.editor.designer.page;
  166. if (form.scriptDesigner){
  167. this.scriptItem = form.scriptDesigner.addScriptItem(this.data, "actionScript", this.editor.module, "action.tools", this.data.text);
  168. }
  169. var text = this.data.text;
  170. Object.defineProperty(this.data, "text", {
  171. configurable : true,
  172. enumerable : true,
  173. "get": function(){return text;},
  174. "set": function(v){
  175. if (this.scriptItem){
  176. this.scriptItem.par = v;
  177. this.scriptItem.resetText();
  178. }
  179. text = v;
  180. }.bind(this)
  181. });
  182. },
  183. loadNode: function () {
  184. this.node = new Element("div", {"styles": this.css.actionNode}).inject(this.container);
  185. this.titleNode = new Element("div", {"styles": this.css.actionTitleNode}).inject(this.node);
  186. this.iconNode = new Element("div", {"styles": this.css.actionIconNode}).inject(this.titleNode);
  187. this.textNode = new Element("div", {"styles": this.css.actionTextNode, "text": this.data.text}).inject(this.titleNode);
  188. this.upButton = new Element("div", {"styles": this.css.actionUpButtonNode, "title": this.editor.designer.lp.actionbar.up}).inject(this.titleNode);
  189. this.propertiesButton = new Element("div", {"styles": this.css.actionPropertiesButtonNode, "title": this.editor.designer.lp.actionbar.property}).inject(this.titleNode);
  190. if (!this.data.properties || Object.keys(this.data.properties).length === 0 ){
  191. this.propertiesButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/property_empty.png)");
  192. }else{
  193. this.propertiesButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/property.png)");
  194. }
  195. this.propertiesNode = new Element("div", {"styles": this.css.actionScriptNode}).inject(this.node);
  196. this.propertiesArea = new MWF.widget.Maplist(this.propertiesNode, {
  197. "title": this.editor.designer.lp.actionbar.setProperties,
  198. "collapse": false,
  199. "onChange": function(){
  200. this.data.properties = this.propertiesArea.toJson();
  201. if (!this.data.properties || Object.keys(this.data.properties).length === 0 ){
  202. this.propertiesButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/property_empty.png)");
  203. }else{
  204. this.propertiesButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/property.png)");
  205. }
  206. this.editor.fireEvent("change");
  207. }.bind(this)
  208. });
  209. this.propertiesArea.load( this.data.properties || {});
  210. if (!this.editor.options.noDelete) this.delButton = new Element("div", {
  211. "styles": this.css.actionDelButtonNode,
  212. "text": "-",
  213. "title" : this.editor.designer.lp.actionbar.delete
  214. }).inject(this.titleNode);
  215. this.conditionButton = new Element("div", {"styles": this.css.actionConditionButtonNode, "title": this.editor.designer.lp.actionbar.hideCondition}).inject(this.titleNode);
  216. if (this.data.condition){
  217. this.conditionButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/code.png)");
  218. }else{
  219. this.conditionButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/code_empty.png)");
  220. }
  221. if (!this.editor.options.noEditShow){
  222. this.editButton = new Element("div", {"styles": this.css.actionEditButtonNode, "title": this.editor.designer.lp.actionbar.edithide}).inject(this.titleNode);
  223. if (this.data.editShow){
  224. this.editButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/edit.png)");
  225. }else{
  226. this.editButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/edit_hide.png)");
  227. }
  228. }
  229. if (!this.editor.options.noReadShow){
  230. this.readButton = new Element("div", {"styles": this.css.actionReadButtonNode, "title": this.editor.designer.lp.actionbar.readhide}).inject(this.titleNode);
  231. if (this.data.readShow){
  232. this.readButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/read.png)");
  233. }else{
  234. this.readButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/read_hide.png)");
  235. }
  236. }
  237. var icon = this.editor.path+this.editor.options.style+"/tools/"+this.data.img;
  238. this.iconNode.setStyle("background-image", "url("+icon+")");
  239. if (!this.editor.options.noCode){
  240. this.scriptNode = new Element("div", {"styles": this.css.actionScriptNode}).inject(this.node);
  241. this.scriptArea = new MWF.widget.ScriptArea(this.scriptNode, {
  242. "title": this.editor.designer.lp.actionbar.editScript,
  243. "maxObj": this.editor.designer.formContentNode,
  244. "key": "actionScript",
  245. "onChange": function(){
  246. this.data.actionScript = this.scriptArea.editor.getValue();
  247. this.editor.fireEvent("change");
  248. }.bind(this),
  249. "onSave": function(){
  250. this.data.actionScript = this.scriptArea.editor.getValue();
  251. this.editor.fireEvent("change");
  252. this.editor.designer.saveForm();
  253. }.bind(this),
  254. "onPostLoad": function(){
  255. // this.scriptNode.setStyle("display", "none");
  256. }.bind(this)
  257. });
  258. this.scriptArea.load({"code": this.data.actionScript});
  259. }
  260. this.conditionNode = new Element("div", {"styles": this.css.actionScriptNode}).inject(this.node);
  261. this.conditionArea = new MWF.widget.ScriptArea(this.conditionNode, {
  262. "title": this.editor.designer.lp.actionbar.editCondition,
  263. "maxObj": this.editor.designer.formContentNode,
  264. "onChange": function(){
  265. this.data.condition = this.conditionArea.editor.getValue();
  266. if (this.data.condition){
  267. this.conditionButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/code.png)");
  268. }else{
  269. this.conditionButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/code_empty.png)");
  270. }
  271. this.editor.fireEvent("change");
  272. }.bind(this),
  273. "onSave": function(){
  274. this.data.condition = this.conditionArea.editor.getValue();
  275. this.editor.fireEvent("change");
  276. this.editor.designer.saveForm();
  277. }.bind(this),
  278. "onPostLoad": function(){
  279. // this.conditionNode.setStyle("display", "none");
  280. }.bind(this)
  281. });
  282. this.conditionArea.load({"code": this.data.condition});
  283. this.setEvent();
  284. //this.loadEditNode();
  285. //this.loadChildNode();
  286. //this.setTitleNode();
  287. //this.setEditNode();
  288. },
  289. setEvent: function(){
  290. this.iconMenu = new MWF.widget.Menu(this.iconNode, {"event": "click", "style": "actionbarIcon"});
  291. this.iconMenu.load();
  292. var _self = this;
  293. for (var i=1; i<=136; i++){
  294. var icon = this.editor.path+this.editor.options.style+"/tools/"+i+".png";
  295. var item = this.iconMenu.addMenuItem("", "click", function(){
  296. var src = this.item.getElement("img").get("src");
  297. _self.data.img = src.substr(src.lastIndexOf("/")+1, src.length);
  298. _self.iconNode.setStyle("background-image", "url("+src+")");
  299. _self.editor.fireEvent("change");
  300. }, icon);
  301. item.iconName = i+".png";
  302. }
  303. this.upButton.addEvent("click", function(e){
  304. var actions = this.editor.actions;
  305. var dataList = this.editor.data;
  306. var dataIndex = dataList.indexOf( this.data );
  307. var index = actions.indexOf( this );
  308. if( index === 0 || dataIndex === 0 ){
  309. e.stopPropagation();
  310. return;
  311. }
  312. var index_before = index-1;
  313. var action = actions[index_before];
  314. this.node.inject(action.node, "before");
  315. actions[index_before] = actions.splice(index, 1, actions[index_before])[0]; //数组交换位置
  316. this.editor.actions = actions;
  317. var dataIndex_before = dataIndex - 1;
  318. dataList[dataIndex_before] = dataList.splice(dataIndex, 1, dataList[dataIndex_before])[0]; //数组交换位置
  319. this.editor.data = dataList;
  320. this.editor.fireEvent("change");
  321. e.stopPropagation();
  322. }.bind(this));
  323. this.propertiesButton.addEvent("click", function(e){
  324. var dis = this.propertiesNode.getStyle("display");
  325. if (dis=="none"){
  326. this.propertiesNode.setStyle("display", "block");
  327. }else{
  328. this.propertiesNode.setStyle("display", "none");
  329. }
  330. e.stopPropagation();
  331. }.bind(this));
  332. this.textNode.addEvent("click", function(e){
  333. this.textNode.empty();
  334. var editTitleNode = new Element("input", {"styles": this.css.actionEditTextNode, "type": "text", "value": this.data.text}).inject(this.textNode);
  335. editTitleNode.focus();
  336. editTitleNode.select();
  337. var _self = this;
  338. editTitleNode.addEvent("blur", function(){_self.editTitleComplete(this);});
  339. editTitleNode.addEvent("keydown:keys(enter)", function(){_self.editTitleComplete(this);});
  340. editTitleNode.addEvent("click", function(e){e.stopPropagation()});
  341. e.stopPropagation();
  342. }.bind(this));
  343. this.conditionButton.addEvent("click", function(e){
  344. e.stopPropagation();
  345. this.editCondition();
  346. }.bind(this));
  347. if (this.editButton) this.editButton.addEvent("click", function(e){
  348. if (this.data.editShow){
  349. this.editButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/edit_hide.png)");
  350. this.data.editShow = false;
  351. }else{
  352. this.editButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/edit.png)");
  353. this.data.editShow = true;
  354. }
  355. this.editor.fireEvent("change");
  356. e.stopPropagation();
  357. }.bind(this));
  358. if (this.readButton) this.readButton.addEvent("click", function(e){
  359. if (this.data.readShow){
  360. this.readButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/read_hide.png)");
  361. this.data.readShow = false;
  362. }else{
  363. this.readButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/read.png)");
  364. this.data.readShow = true;
  365. }
  366. this.editor.fireEvent("change");
  367. e.stopPropagation();
  368. }.bind(this));
  369. this.titleNode.addEvent("click", function(){
  370. if (this.scriptNode){
  371. var dis = this.scriptNode.getStyle("display");
  372. if (dis=="none"){
  373. this.scriptNode.setStyle("display", "block");
  374. if (this.scriptArea){
  375. this.scriptArea.resizeContentNodeSize();
  376. if (this.scriptArea.editor) this.scriptArea.editor.resize();
  377. if (!this.scriptArea.jsEditor){
  378. this.scriptArea.contentNode.empty();
  379. this.scriptArea.loadEditor({"code": this.data.actionScript});
  380. }
  381. }
  382. }else{
  383. this.scriptNode.setStyle("display", "none");
  384. }
  385. }
  386. }.bind(this));
  387. if (this.delButton) this.delButton.addEvent("click", function(e){
  388. var _self = this;
  389. this.editor.designer.confirm("warn", this.delButton, MWF.APPFD.LP.notice.deleteButtonTitle, MWF.APPFD.LP.notice.deleteButton, 300, 120, function(){
  390. _self.destroy();
  391. this.close();
  392. }, function(){
  393. this.close();
  394. }, null);
  395. e.stopPropagation();
  396. }.bind(this));
  397. },
  398. editCondition: function(){
  399. var dis = this.conditionNode.getStyle("display");
  400. if (dis=="none"){
  401. this.conditionNode.setStyle("display", "block");
  402. if (this.conditionArea){
  403. this.conditionArea.resizeContentNodeSize();
  404. if (this.conditionArea.editor) this.conditionArea.editor.resize();
  405. if (!this.conditionArea.jsEditor){
  406. this.conditionArea.contentNode.empty();
  407. this.conditionArea.loadEditor({"code": this.data.condition});
  408. }
  409. }
  410. }else{
  411. this.conditionNode.setStyle("display", "none");
  412. }
  413. },
  414. destroy: function(){
  415. this.editor.data.erase(this.data);
  416. this.editor.actions.erase(this);
  417. this.editor.fireEvent("change");
  418. this.node.destroy();
  419. var form = this.editor.designer.form || this.editor.designer.page;
  420. if (form.scriptDesigner){
  421. form.scriptDesigner.deleteScriptItem(this.editor.module, "action.tools", this.data.text);
  422. }
  423. MWF.release(this.scriptArea);
  424. MWF.release(this);
  425. },
  426. editTitleComplete: function(el){
  427. this.data.text = el.get("value");
  428. this.data.title = el.get("value");
  429. el.destroy();
  430. this.textNode.empty();
  431. this.textNode.set("text", this.data.text);
  432. this.editor.fireEvent("change");
  433. }
  434. });