ActionsEditor.js 20 KB

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