EventsEditor.js 7.6 KB

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