ActionsEditor.js 21 KB

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