ActionsEditor.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. debugger;
  83. this.restoreButtonAction();
  84. }.bind(this));
  85. }
  86. },
  87. listRemovedSystemTool : function(){
  88. var list = [];
  89. if( !this.defaultTools ){
  90. MWF.getJSON( this.options.systemToolsAddress, function(tools){
  91. this.defaultTools = tools;
  92. if( this.options.target && this.options.target === "mobileForm" ){
  93. this.defaultTools.push({
  94. "type": "MWFToolBarButton",
  95. "img": "read.png",
  96. "title": "标记为已阅",
  97. "action": "readedWork",
  98. "text": "已阅",
  99. "id": "action_readed",
  100. "control": "allowReadProcessing",
  101. "condition": "",
  102. "read": true
  103. });
  104. }
  105. }.bind(this), false);
  106. }
  107. this.defaultTools.each( function( tool ){
  108. var flag = true;
  109. for( var i=0; i<(this.data || []).length; i++ ){
  110. if( this.data[i].id === tool.id ){
  111. flag = false;
  112. break;
  113. }
  114. }
  115. if(flag)list.push(tool);
  116. }.bind(this));
  117. return list;
  118. },
  119. addButtonAction: function(){
  120. var o = {
  121. "type": "MWFToolBarButton",
  122. "img": "4.png",
  123. "title": "",
  124. "action": "",
  125. "text": "Unnamed",
  126. "actionScript" : "",
  127. "condition": "",
  128. "editShow": true,
  129. "readShow": true
  130. };
  131. var action = new MWF.xApplication.process.FormDesigner.widget.ActionsEditor.ButtonAction(this);
  132. action.load(o);
  133. this.data.push(o);
  134. this.actions.push(action);
  135. this.fireEvent("change");
  136. },
  137. restoreButtonAction : function(){
  138. var list = this.listRemovedSystemTool();
  139. if( !list.length )return;
  140. var selectableItems = [];
  141. list.each( function(d){
  142. selectableItems.push( {
  143. name : d.text,
  144. id : d.id
  145. })
  146. }.bind(this));
  147. MWF.xDesktop.requireApp("Template", "Selector.Custom", null, false);
  148. var opt = {
  149. "count": 0,
  150. "title": this.designer.lp.actionbar.selectDefaultTool,
  151. "selectableItems" : selectableItems,
  152. "values": [],
  153. "onComplete": function( array ){
  154. if( !array || array.length == 0 )return;
  155. array.each( function(tool){
  156. for( var i=0; i<list.length; i++ ){
  157. if( list[i].id === tool.data.id ){
  158. this.data.push( list[i] );
  159. var action = new MWF.xApplication.process.FormDesigner.widget.ActionsEditor.ButtonAction(this);
  160. action.load(list[i]);
  161. this.actions.push(action);
  162. break;
  163. }
  164. }
  165. }.bind(this));
  166. this.fireEvent("change");
  167. }.bind(this)
  168. };
  169. var selector = new MWF.xApplication.Template.Selector.Custom(this.options.maxObj, opt );
  170. selector.load();
  171. }
  172. });
  173. MWF.xApplication.process.FormDesigner.widget.ActionsEditor.ButtonAction = new Class({
  174. initialize: function (editor) {
  175. this.editor = editor;
  176. this.css = this.editor.css;
  177. this.container = this.editor.actionArea
  178. },
  179. load: function (data) {
  180. this.data = data;
  181. this.loadNode();
  182. var form = this.editor.designer.form || this.editor.designer.page || this.editor.designer.view;
  183. if (form.scriptDesigner){
  184. this.scriptItem = form.scriptDesigner.addScriptItem(this.data, "actionScript", this.editor.module, "action.tools", this.data.text);
  185. }
  186. var text = this.data.text;
  187. Object.defineProperty(this.data, "text", {
  188. configurable : true,
  189. enumerable : true,
  190. "get": function(){return text;},
  191. "set": function(v){
  192. if (this.scriptItem){
  193. this.scriptItem.par = v;
  194. this.scriptItem.resetText();
  195. }
  196. text = v;
  197. }.bind(this)
  198. });
  199. },
  200. loadNode: function () {
  201. this.node = new Element("div", {"styles": this.css.actionNode}).inject(this.container);
  202. this.titleNode = new Element("div", {"styles": this.css.actionTitleNode}).inject(this.node);
  203. this.iconNode = new Element("div", {"styles": this.css.actionIconNode}).inject(this.titleNode);
  204. this.textNode = new Element("div", {"styles": this.css.actionTextNode, "text": this.data.text}).inject(this.titleNode);
  205. this.upButton = new Element("div", {"styles": this.css.actionUpButtonNode, "title": this.editor.designer.lp.actionbar.up}).inject(this.titleNode);
  206. this.propertiesButton = new Element("div", {"styles": this.css.actionPropertiesButtonNode, "title": this.editor.designer.lp.actionbar.property}).inject(this.titleNode);
  207. if (!this.data.properties || Object.keys(this.data.properties).length === 0 ){
  208. this.propertiesButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/property_empty.png)");
  209. }else{
  210. this.propertiesButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/property.png)");
  211. }
  212. this.propertiesNode = new Element("div", {"styles": this.css.actionScriptNode}).inject(this.node);
  213. this.propertiesArea = new MWF.widget.Maplist(this.propertiesNode, {
  214. "title": this.editor.designer.lp.actionbar.setProperties,
  215. "collapse": false,
  216. "onChange": function(){
  217. this.data.properties = this.propertiesArea.toJson();
  218. if (!this.data.properties || Object.keys(this.data.properties).length === 0 ){
  219. this.propertiesButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/property_empty.png)");
  220. }else{
  221. this.propertiesButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/property.png)");
  222. }
  223. this.editor.fireEvent("change");
  224. }.bind(this)
  225. });
  226. this.propertiesArea.load( this.data.properties || {});
  227. if (!this.editor.options.noDelete) this.delButton = new Element("div", {
  228. "styles": this.css.actionDelButtonNode,
  229. "text": "-",
  230. "title" : this.editor.designer.lp.actionbar.delete
  231. }).inject(this.titleNode);
  232. this.conditionButton = new Element("div", {"styles": this.css.actionConditionButtonNode, "title": this.editor.designer.lp.actionbar.hideCondition}).inject(this.titleNode);
  233. if (this.data.condition){
  234. this.conditionButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/code.png)");
  235. }else{
  236. this.conditionButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/code_empty.png)");
  237. }
  238. if (!this.editor.options.noEditShow){
  239. this.editButton = new Element("div", {"styles": this.css.actionEditButtonNode, "title": this.editor.designer.lp.actionbar.edithide}).inject(this.titleNode);
  240. if (this.data.editShow){
  241. this.editButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/edit.png)");
  242. }else{
  243. this.editButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/edit_hide.png)");
  244. }
  245. }
  246. if (!this.editor.options.noReadShow){
  247. this.readButton = new Element("div", {"styles": this.css.actionReadButtonNode, "title": this.editor.designer.lp.actionbar.readhide}).inject(this.titleNode);
  248. if (this.data.readShow){
  249. this.readButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/read.png)");
  250. }else{
  251. this.readButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/read_hide.png)");
  252. }
  253. }
  254. var icon = this.editor.path+this.editor.options.style+"/tools/"+this.data.img;
  255. this.iconNode.setStyle("background-image", "url("+icon+")");
  256. if (!this.editor.options.noCode){
  257. this.scriptNode = new Element("div", {"styles": this.css.actionScriptNode}).inject(this.node);
  258. this.scriptArea = new MWF.widget.ScriptArea(this.scriptNode, {
  259. "title": this.editor.designer.lp.actionbar.editScript,
  260. "maxObj": this.editor.designer.formContentNode,
  261. "key": "actionScript",
  262. "onChange": function(){
  263. this.data.actionScript = this.scriptArea.editor.getValue();
  264. this.editor.fireEvent("change");
  265. }.bind(this),
  266. "onSave": function(){
  267. this.data.actionScript = this.scriptArea.editor.getValue();
  268. this.editor.fireEvent("change");
  269. this.editor.designer.saveForm();
  270. }.bind(this),
  271. "onPostLoad": function(){
  272. // this.scriptNode.setStyle("display", "none");
  273. }.bind(this)
  274. });
  275. this.scriptArea.load({"code": this.data.actionScript});
  276. }
  277. this.conditionNode = new Element("div", {"styles": this.css.actionScriptNode}).inject(this.node);
  278. this.conditionArea = new MWF.widget.ScriptArea(this.conditionNode, {
  279. "title": this.editor.designer.lp.actionbar.editCondition,
  280. "maxObj": this.editor.designer.formContentNode,
  281. "onChange": function(){
  282. this.data.condition = this.conditionArea.editor.getValue();
  283. if (this.data.condition){
  284. this.conditionButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/code.png)");
  285. }else{
  286. this.conditionButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/code_empty.png)");
  287. }
  288. this.editor.fireEvent("change");
  289. }.bind(this),
  290. "onSave": function(){
  291. this.data.condition = this.conditionArea.editor.getValue();
  292. this.editor.fireEvent("change");
  293. this.editor.designer.saveForm();
  294. }.bind(this),
  295. "onPostLoad": function(){
  296. // this.conditionNode.setStyle("display", "none");
  297. }.bind(this)
  298. });
  299. this.conditionArea.load({"code": this.data.condition});
  300. this.setEvent();
  301. //this.loadEditNode();
  302. //this.loadChildNode();
  303. //this.setTitleNode();
  304. //this.setEditNode();
  305. },
  306. setEvent: function(){
  307. this.iconMenu = new MWF.widget.Menu(this.iconNode, {
  308. "event": "click",
  309. "style": "actionbarIcon",
  310. "onPostShow" : function (ev) {
  311. ev.stopPropagation();
  312. }
  313. });
  314. this.iconMenu.load();
  315. var _self = this;
  316. for (var i=1; i<=136; i++){
  317. var icon = this.editor.path+this.editor.options.style+"/tools/"+i+".png";
  318. var item = this.iconMenu.addMenuItem("", "click", function(ev){
  319. var src = this.item.getElement("img").get("src");
  320. _self.data.img = src.substr(src.lastIndexOf("/")+1, src.length);
  321. _self.iconNode.setStyle("background-image", "url("+src+")");
  322. _self.editor.fireEvent("change");
  323. ev.stopPropagation();
  324. }, icon);
  325. item.iconName = i+".png";
  326. }
  327. this.upButton.addEvent("click", function(e){
  328. var actions = this.editor.actions;
  329. var dataList = this.editor.data;
  330. var dataIndex = dataList.indexOf( this.data );
  331. var index = actions.indexOf( this );
  332. if( index === 0 || dataIndex === 0 ){
  333. e.stopPropagation();
  334. return;
  335. }
  336. var index_before = index-1;
  337. var action = actions[index_before];
  338. this.node.inject(action.node, "before");
  339. actions[index_before] = actions.splice(index, 1, actions[index_before])[0];
  340. this.editor.actions = actions;
  341. var dataIndex_before = dataIndex - 1;
  342. dataList[dataIndex_before] = dataList.splice(dataIndex, 1, dataList[dataIndex_before])[0];
  343. this.editor.data = dataList;
  344. this.editor.fireEvent("change");
  345. e.stopPropagation();
  346. }.bind(this));
  347. this.propertiesButton.addEvent("click", function(e){
  348. var dis = this.propertiesNode.getStyle("display");
  349. if (dis=="none"){
  350. this.propertiesNode.setStyle("display", "block");
  351. }else{
  352. this.propertiesNode.setStyle("display", "none");
  353. }
  354. e.stopPropagation();
  355. }.bind(this));
  356. this.textNode.addEvent("click", function(e){
  357. this.textNode.empty();
  358. var editTitleNode = new Element("input", {"styles": this.css.actionEditTextNode, "type": "text", "value": this.data.text}).inject(this.textNode);
  359. editTitleNode.focus();
  360. editTitleNode.select();
  361. var _self = this;
  362. editTitleNode.addEvent("blur", function(){_self.editTitleComplete(this);});
  363. editTitleNode.addEvent("keydown:keys(enter)", function(){_self.editTitleComplete(this);});
  364. editTitleNode.addEvent("click", function(e){e.stopPropagation()});
  365. e.stopPropagation();
  366. }.bind(this));
  367. this.conditionButton.addEvent("click", function(e){
  368. e.stopPropagation();
  369. this.editCondition();
  370. }.bind(this));
  371. if (this.editButton) this.editButton.addEvent("click", function(e){
  372. if (this.data.editShow){
  373. this.editButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/edit_hide.png)");
  374. this.data.editShow = false;
  375. }else{
  376. this.editButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/edit.png)");
  377. this.data.editShow = true;
  378. }
  379. this.editor.fireEvent("change");
  380. e.stopPropagation();
  381. }.bind(this));
  382. if (this.readButton) this.readButton.addEvent("click", function(e){
  383. if (this.data.readShow){
  384. this.readButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/read_hide.png)");
  385. this.data.readShow = false;
  386. }else{
  387. this.readButton.setStyle("background-image", "url("+this.editor.path+this.editor.options.style+"/icon/read.png)");
  388. this.data.readShow = true;
  389. }
  390. this.editor.fireEvent("change");
  391. e.stopPropagation();
  392. }.bind(this));
  393. this.titleNode.addEvent("click", function(){
  394. if (this.scriptNode){
  395. var dis = this.scriptNode.getStyle("display");
  396. if (dis=="none"){
  397. this.scriptNode.setStyle("display", "block");
  398. if (this.scriptArea){
  399. this.scriptArea.resizeContentNodeSize();
  400. if (this.scriptArea.editor) this.scriptArea.editor.resize();
  401. if (!this.scriptArea.jsEditor){
  402. this.scriptArea.contentNode.empty();
  403. this.scriptArea.loadEditor({"code": this.data.actionScript});
  404. }
  405. }
  406. }else{
  407. this.scriptNode.setStyle("display", "none");
  408. }
  409. }
  410. }.bind(this));
  411. if (this.delButton) this.delButton.addEvent("click", function(e){
  412. var _self = this;
  413. this.editor.designer.confirm("warn", this.delButton, MWF.APPFD.LP.notice.deleteButtonTitle, MWF.APPFD.LP.notice.deleteButton, 300, 120, function(){
  414. _self.destroy();
  415. this.close();
  416. }, function(){
  417. this.close();
  418. }, null);
  419. e.stopPropagation();
  420. }.bind(this));
  421. },
  422. editCondition: function(){
  423. var dis = this.conditionNode.getStyle("display");
  424. if (dis=="none"){
  425. this.conditionNode.setStyle("display", "block");
  426. if (this.conditionArea){
  427. this.conditionArea.resizeContentNodeSize();
  428. if (this.conditionArea.editor) this.conditionArea.editor.resize();
  429. if (!this.conditionArea.jsEditor){
  430. this.conditionArea.contentNode.empty();
  431. this.conditionArea.loadEditor({"code": this.data.condition});
  432. }
  433. }
  434. }else{
  435. this.conditionNode.setStyle("display", "none");
  436. }
  437. },
  438. destroy: function(){
  439. var form = this.editor.designer.form || this.editor.designer.page || this.editor.designer.view;
  440. if ( form && form.scriptDesigner){
  441. form.scriptDesigner.deleteScriptItem(this.editor.module, "action.tools", this.data.text);
  442. }
  443. this.editor.data.erase(this.data);
  444. this.editor.actions.erase(this);
  445. this.editor.fireEvent("change");
  446. this.node.destroy();
  447. MWF.release(this.scriptArea);
  448. MWF.release(this);
  449. },
  450. editTitleComplete: function(el){
  451. this.data.text = el.get("value");
  452. this.data.title = el.get("value");
  453. el.destroy();
  454. this.textNode.empty();
  455. this.textNode.set("text", this.data.text);
  456. this.editor.fireEvent("change");
  457. }
  458. });