EventsEditor.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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.xApplication.process.FormDesigner.widget.EventsEditor = new Class({
  6. Implements: [Options, Events],
  7. Extends: MWF.widget.Common,
  8. options: {
  9. "style": "default",
  10. "maxObj": document.body
  11. },
  12. initialize: function(node, designer, options){
  13. this.setOptions(options);
  14. this.node = $(node);
  15. this.app = designer;
  16. this.path = "../x_component_process_FormDesigner/widget/$EventsEditor/";
  17. this.cssPath = "../x_component_process_FormDesigner/widget/$EventsEditor/"+this.options.style+"/css.wcss";
  18. this._loadCss();
  19. this.items = [];
  20. this.currentEditItem = null;
  21. this.createEventsAreaNode();
  22. },
  23. createEventsAreaNode: function(){
  24. this.eventsContainer = new Element("div", {
  25. "styles": this.css.eventsContainer
  26. }).inject(this.node);
  27. var size = this.node.getUsefulSize();
  28. // this.eventsContainer.setStyle("height", size.y);
  29. },
  30. load: function(data, module, path){
  31. this.data = data;
  32. this.module = module;
  33. this.scriptPath = path;
  34. Object.each(data, function(obj, key){
  35. var item = new MWF.xApplication.process.FormDesigner.widget.EventsEditor.Item(this);
  36. item.load(key, obj);
  37. this.items.push(item);
  38. }.bind(this));
  39. },
  40. deleteItem: function(item){
  41. this.items.erase(item);
  42. var data;
  43. if (this.data[item.event]){
  44. data = Object.clone( this.data[item.event] );
  45. this.data[item.event].code = "";
  46. this.data[item.event].html = "";
  47. delete this.data[item.event];
  48. }
  49. item.deleteScriptDesignerItem();
  50. this.fireEvent("change", [item.event, null, data, item.event+" [delete]"]);
  51. if (item.container){
  52. item.container.destroy();
  53. }
  54. },
  55. addItem: function(item){
  56. this.data[item.event] = item.data;
  57. this.fireEvent("change", [item.event, Object.clone(item.data)]);
  58. this.items.push(item);
  59. },
  60. addEventItem: function(){
  61. var item = new MWF.xApplication.process.FormDesigner.widget.EventsEditor.Item(this);
  62. item.load("", "");
  63. }
  64. });
  65. MWF.xApplication.process.FormDesigner.widget.EventsEditor.Item = new Class({
  66. initialize: function(editor){
  67. this.editor = editor;
  68. },
  69. load: function(event, data){
  70. if (!event){
  71. this.create();
  72. }else{
  73. this.event = event;
  74. this.data = data;
  75. this.oldData = Object.clone(data);
  76. this.createContainer();
  77. this.createActions();
  78. }
  79. },
  80. create: function(){
  81. this.event = "";
  82. this.data = {"code": "", "html": ""};
  83. this.createContainerTitle();
  84. this.createInput = new Element("input", {
  85. "styles": this.editor.css.createInput
  86. }).inject(this.textNode);
  87. this.createInput.focus();
  88. this.createInput.addEvents({
  89. "keydown": function(e){
  90. if (e.code==13){
  91. this.checkCreate();
  92. }
  93. }.bind(this),
  94. "blur": function(){
  95. this.checkCreate();
  96. }.bind(this)
  97. });
  98. },
  99. checkCreate: function(){
  100. var event = this.createInput.get("value");
  101. if (!event){
  102. this.editor.deleteItem(this);
  103. return false;
  104. }
  105. if (this.editor.data[event]){
  106. this.iconNode.setStyle("background", "url("+this.editor.path+this.editor.options.style+"/icon/error.png) center center no-repeat");
  107. this.iconNode.title = MWF.LP.process.repetitionsEvent;
  108. this.createInput.focus();
  109. }else{
  110. this.event = event;
  111. this.container.destroy();
  112. this.load(this.event, this.data);
  113. this.editCode();
  114. this.editor.addItem(this);
  115. this.bindScriptDesigner();
  116. }
  117. },
  118. bindScriptDesigner: function(){
  119. var form = this.editor.app.form || this.editor.app.page;
  120. if (form.scriptDesigner) form.scriptDesigner.addScriptItem(this.data, "code", this.editor.module, this.editor.scriptPath+"."+this.event);
  121. },
  122. deleteScriptDesignerItem: function(){
  123. var form = this.editor.app.form || this.editor.app.page || this.editor.app.view;
  124. if (form.scriptDesigner){
  125. form.scriptDesigner.deleteScriptItem(this.editor.module, this.editor.scriptPath+"."+this.event);
  126. }
  127. },
  128. createContainerTitle: function(){
  129. this.container = new Element("div", {
  130. "styles": this.editor.css.itemContainer
  131. }).inject(this.editor.eventsContainer);
  132. this.titleContainer = new Element("div", {
  133. "styles": this.editor.css.itemTitleContainer
  134. }).inject(this.container);
  135. this.iconNode = new Element("div", {
  136. "styles": this.editor.css.iconNode
  137. }).inject(this.titleContainer);
  138. this.actionNode = new Element("div", {
  139. "styles": this.editor.css.actionNode
  140. }).inject(this.titleContainer);
  141. this.textNode = new Element("div", {
  142. "styles": this.editor.css.textNode,
  143. "text": this.event
  144. }).inject(this.titleContainer);
  145. },
  146. createContainer: function(){
  147. this.createContainerTitle();
  148. this.codeNode = new Element("div", {
  149. "styles": this.editor.css.codeNode
  150. }).inject(this.container);
  151. this.titleContainer.addEvents({
  152. "mouseover": function(){
  153. this.actionNode.fade("in");
  154. }.bind(this),
  155. "mouseout": function(){
  156. this.actionNode.fade("out");
  157. }.bind(this)
  158. });
  159. this.textNode.addEvent("click", function(){
  160. this.editCode();
  161. }.bind(this));
  162. this.checkIcon();
  163. },
  164. editCode: function(){
  165. if (this.editor.currentEditItem){
  166. if (this.editor.currentEditItem!=this) this.editor.currentEditItem.editCodeComplete();
  167. }
  168. if (this.editor.currentEditItem!=this){
  169. if (!this.codeEditor){
  170. var moduleType = this.editor.module.type;
  171. if( moduleType === "Datatable" ){
  172. if( this.editor.app && this.editor.app.currentDesignerMode ){
  173. moduleType = moduleType + this.editor.app.currentDesignerMode;
  174. }
  175. }
  176. this.codeEditor = new MWF.widget.ScriptArea(this.codeNode, {
  177. "style": "event",
  178. "isbind": false,
  179. "api": "../api/MWF.xApplication.process.Xform."+moduleType+".html#event:"+this.event,
  180. "title": "on"+this.event+" (S)",
  181. "maxObj": this.editor.options.maxObj,
  182. "onChange": function(){
  183. var json = this.codeEditor.toJson();
  184. this.data.code = json.code;
  185. this.data.html = json.html;
  186. this.editor.fireEvent("change", [this.event, Object.clone(this.data), this.oldData]);
  187. this.checkIcon();
  188. }.bind(this),
  189. "onSave": function(){
  190. debugger;
  191. var json = this.codeEditor.toJson();
  192. this.data.code = json.code;
  193. this.data.html = json.html;
  194. this.editor.fireEvent("change", [this.event, Object.clone(this.data), this.oldData]);
  195. this.editor.app.saveForm();
  196. }.bind(this)
  197. });
  198. this.codeEditor.load(this.data);
  199. }
  200. if (!this.morph){
  201. this.morph = new Fx.Morph(this.codeNode, {duration: 200});
  202. }
  203. this.codeNode.setStyle("display", "block");
  204. this.morph.start({"height": [0,300]}).chain(function(){
  205. this.codeEditor.resizeContentNodeSize();
  206. this.codeEditor.focus();
  207. // this.fireEvent("postShow");
  208. }.bind(this));
  209. this.editor.currentEditItem = this;
  210. }else{
  211. this.editCodeComplete();
  212. }
  213. },
  214. editCodeComplete: function(){
  215. if (this.codeEditor){
  216. var json = this.codeEditor.toJson();
  217. this.data.code = json.code;
  218. this.data.html = json.html;
  219. this.editor.fireEvent("change", [this.event, Object.clone(this.data), this.oldData]);
  220. this.checkIcon();
  221. }
  222. if (!this.morph){
  223. this.morph = new Fx.Morph(this.codeNode, {duration: 200});
  224. }
  225. this.morph.start({"height": [300,0]}).chain(function(){
  226. this.codeNode.setStyle("display", "none");
  227. // this.fireEvent("postHide");
  228. }.bind(this));
  229. this.editor.currentEditItem = null;
  230. },
  231. createActions: function(){
  232. var deleteAction = new Element("div", {
  233. "styles": this.editor.css.actionNodeDelete
  234. }).inject(this.actionNode);
  235. deleteAction.addEvent("click", function(e){
  236. var item = this;
  237. this.editor.app.confirm("warn", e, this.editor.app.lp.notice.deleteEventTitle, this.editor.app.lp.notice.deleteEvent, 300, 120, function(){
  238. item.editor.deleteItem(item);
  239. this.close();
  240. }, function(){
  241. this.close();
  242. }, null);
  243. }.bind(this));
  244. var addAction = new Element("div", {
  245. "styles": this.editor.css.actionNodeAdd
  246. }).inject(this.actionNode);
  247. addAction.addEvent("click", function(e){
  248. this.editor.addEventItem();
  249. }.bind(this));
  250. },
  251. checkIcon: function(){
  252. if (this.data.code){
  253. this.iconNode.setStyle("background", "url("+this.editor.path+this.editor.options.style+"/icon/code.png) center center no-repeat");
  254. }else{
  255. this.iconNode.setStyle("background", "url("+this.editor.path+this.editor.options.style+"/icon/code_empty.png) center center no-repeat");
  256. }
  257. }
  258. });