$Block.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. MWF.xApplication.ScriptEditor.block = MWF.xApplication.ScriptEditor.block || {};
  2. MWF.xApplication.ScriptEditor.block.$Block = new Class({
  3. Extends: MWF.widget.Common,
  4. Implements: [Options, Events],
  5. options: {
  6. "style": "default"
  7. },
  8. _loadPath: function(){
  9. this.path = "/x_component_ScriptEditor/block/$Block/";
  10. this.cssPath = "/x_component_ScriptEditor/block/$Block/"+this.options.style+"/css.wcss";
  11. },
  12. initialize: function(data, type, options){
  13. this.setOptions(options);
  14. this._loadPath();
  15. this._loadCss();
  16. this.data = data;
  17. this.type = type;
  18. this.editor = this.type.editor;
  19. this.areaNode = this.type.blocksNode;
  20. this.scriptArea = this.editor.scriptArea;
  21. this.scriptAreaNode = this.editor.scriptArea.blockArea;
  22. this.load();
  23. },
  24. init: function(){
  25. this.before = true;
  26. this.after = true;
  27. this.around = false;
  28. this.blockName = ""
  29. },
  30. load: function(){
  31. this.init();
  32. this.createBlock();
  33. this.setEvent();
  34. },
  35. setEvent: function(){
  36. var drag = new Drag(this.node, {
  37. "snap": "1",
  38. "onStart": function(dragging, e){
  39. this.dragScript(e);
  40. drag.stop();
  41. }.bind(this)
  42. });
  43. // this.node.addEvent("drag", function(e){
  44. // this.dragScript(e);
  45. // }.bind(this));
  46. },
  47. createDragNode: function(){
  48. var dragNode = this.node.clone();
  49. dragNode.setStyle("position", "absolute").inject(this.scriptAreaNode);
  50. dragNode.position({
  51. relativeTo: this.node,
  52. position: 'upperLeft',
  53. edge: 'upperLeft'
  54. });
  55. //dragNode.setStyle("z-index", MWF.SES.zIndexPool.apply());
  56. var canvas = this.node.getElements("canvas");
  57. var dragCanvas = dragNode.getElements("canvas");
  58. if (canvas.length){
  59. canvas.each(function(c, i){
  60. var dc = dragCanvas[i];
  61. var draw = c.retrieve("draw");
  62. if (draw) this[draw](dc);
  63. }.bind(this));
  64. }
  65. return dragNode;
  66. },
  67. dragScript: function(e){
  68. var dragNode = this.createDragNode();
  69. var droppables = [this.scriptAreaNode].concat(this.scriptArea.statementNodes);
  70. var nodeDrag = new Drag.Move(dragNode, {
  71. "droppables": droppables,
  72. "onStart": function(dragging){
  73. this.dragStart(dragging);
  74. }.bind(this),
  75. "onEnter": function(dragging, inObj){
  76. }.bind(this),
  77. "onLeave": function(dragging, inObj){
  78. }.bind(this),
  79. "onDrag": function(){
  80. var statement = dragNode.retrieve("statement");
  81. this.scriptArea.checkBlockDrag(dragNode, statement, statement.links);
  82. }.bind(this),
  83. "onDrop": function(dragging, inObj){
  84. }.bind(this),
  85. "onComplete": function(e, event){
  86. this.dragComplete(event, dragNode, nodeDrag);
  87. }.bind(this),
  88. "onCancel": function(){
  89. var statement = dragNode.retrieve("statement");
  90. if (statement){ statement.destroy(); }else{ dragNode.destroy(); }
  91. }.bind(this)
  92. });
  93. nodeDrag.start(e);
  94. },
  95. dragStart: function(dragNode){
  96. dragNode.setStyle("z-index", MWF.SES.zIndexPool.apply());
  97. var statement = this.scriptArea.createStatement(dragNode, this);
  98. dragNode.store("statement", statement);
  99. },
  100. dragComplete: function(event, dragNode, nodeDrag){
  101. var statement = dragNode.retrieve("statement");
  102. if (this.scriptAreaNode.isOutside(event)){
  103. if (statement){ statement.destroy(); }else{ dragNode.destroy(); }
  104. }else{
  105. var p0 = dragNode.getPosition(dragNode.getOffsetParent());
  106. var p1 = this.scriptAreaNode.getPosition(dragNode.getOffsetParent());
  107. if (p0.x-p1.x<10){
  108. var x = 0+p1.x+10;
  109. dragNode.setStyle("left", ""+x+"px");
  110. }
  111. if (p0.y-p1.y<10){
  112. var y = 0+p1.y+10;
  113. dragNode.setStyle("top", ""+y+"px");
  114. }
  115. if (statement){
  116. this.scriptArea.buildStatement(statement);
  117. }else{
  118. statement = this.scriptArea.createStatement(dragNode, this);
  119. dragNode.store("statement", statement);
  120. this.scriptArea.buildStatement(statement);
  121. }
  122. nodeDrag.detach();
  123. }
  124. }
  125. });
  126. MWF.xApplication.ScriptEditor.block.$Block.$Operation = new Class({
  127. Extends: MWF.xApplication.ScriptEditor.block.$Block,
  128. init: function(){
  129. var css = Object.clone(this.css.block);
  130. this.blockCss = Object.merge(css, this.css.operation);
  131. this.blockName = this.data.name;
  132. },
  133. createContent: function(){
  134. this.node = new Element("div", {"styles": this.blockCss.node}).inject(this.areaNode);
  135. this.contentNode = new Element("div", {"styles": this.blockCss.contentNode}).inject(this.node);
  136. this.contentNode.setStyle("background", this.type.data.color);
  137. },
  138. loadContent: function(){
  139. this.table = new Element("table", {
  140. "styles": this.blockCss.contentTable,
  141. "border": "0",
  142. "cellpadding": "0",
  143. "cellspacing": "0"
  144. });
  145. var tr = new Element("tr").inject(this.table);
  146. this.data.contents.each(function(content){
  147. var td = new Element("td", {"styles": this.blockCss.contentTdNode}).inject(tr);
  148. if (typeOf(content)==="string"){
  149. td.set("text", content);
  150. }else if (typeOf(content)==="object"){
  151. td.set("class", "MWFBlockContent_"+content.type);
  152. this.createContentNode(content).inject(td);
  153. }
  154. }.bind(this));
  155. this.table.inject(this.contentNode);
  156. },
  157. createBlock: function(){
  158. this.createContent();
  159. this.loadContent();
  160. },
  161. createContentNode: function(content){
  162. if (content.type==="input"){
  163. return this.createInputNode(content);
  164. }
  165. if (content.type==="select"){
  166. return this.createSelectNode(content);
  167. }
  168. if (content.type==="mortise"){
  169. return this.createMortiseNode(content);
  170. }
  171. if (content.type==="inputMortise"){
  172. return this.createInputMortiseNode(content);
  173. }
  174. },
  175. createInputNode: function(content){
  176. var text = content.default;
  177. var width = MWF.getTextSize(text, {"font-size": "12px"}).x;
  178. var node = new Element("input", {"styles": this.blockCss.inputNode, "value": text, "readonly": true});
  179. node.setStyle("width", ""+width+"px");
  180. return node;
  181. },
  182. createSelectNode: function(content){
  183. var select = new Element("select", {"styles": this.blockCss.selectNode, "class": "script"});
  184. content.options.each(function(t){
  185. var v = t.split("|");
  186. new Element("option", {
  187. "value": v[1],
  188. "text": v[0],
  189. "selected": (content.default===v[1])
  190. }).inject(select);
  191. }.bind(this));
  192. select.setStyle("background", this.type.data.mortiseColor);
  193. return select;
  194. },
  195. createMortiseNode: function(){
  196. var node = new Element("div", {"styles": this.blockCss.mortiseNode});
  197. node.setStyle("background", this.type.data.mortiseColor);
  198. return node;
  199. },
  200. createInputMortiseNode: function(content){
  201. var text = content.default;
  202. var width = MWF.getTextSize(text, {"font-size": "12px"}).x;
  203. var node = new Element("input", {"styles": this.blockCss.inputMortiseNode, "value": text, "readonly": true});
  204. node.setStyle("width", ""+width+"px");
  205. return node;
  206. }
  207. });
  208. MWF.xApplication.ScriptEditor.block.$Block.$Expression = new Class({
  209. Extends: MWF.xApplication.ScriptEditor.block.$Block.$Operation,
  210. init: function(){
  211. var css = Object.clone(this.css.block);
  212. this.blockCss = Object.merge(css, this.css.expression);
  213. this.blockName = this.data.name;
  214. }
  215. });
  216. MWF.xApplication.ScriptEditor.block.$Block.$Enumerate = new Class({
  217. Extends: MWF.xApplication.ScriptEditor.block.$Block.$Operation,
  218. init: function(){
  219. var css = Object.clone(this.css.block);
  220. this.blockCss = Object.merge(css, this.css.enumerate);
  221. this.blockName = this.data.name;
  222. }
  223. });
  224. MWF.xApplication.ScriptEditor.block.$Block.$Top = new Class({
  225. Extends: MWF.xApplication.ScriptEditor.block.$Block.$Operation,
  226. init: function(){
  227. var css = Object.clone(this.css.block);
  228. this.blockCss = Object.merge(css, this.css.top);
  229. this.blockName = this.data.name;
  230. },
  231. createContent: function(){
  232. this.node = new Element("div", {"styles": this.blockCss.node}).inject(this.areaNode);
  233. this.topNode = new Element("div", {"styles": this.blockCss.topNode}).inject(this.node);
  234. this.contentNode = new Element("div", {"styles": this.blockCss.contentNode}).inject(this.node);
  235. this.contentNode.setStyle("background", this.type.data.color);
  236. this.topNode.setStyle("background", this.type.data.color);
  237. }
  238. });
  239. MWF.xApplication.ScriptEditor.block.$Block.$Around = new Class({
  240. Extends: MWF.xApplication.ScriptEditor.block.$Block.$Operation,
  241. init: function(){
  242. var css = Object.clone(this.css.block);
  243. this.blockCss = Object.merge(css, this.css.around);
  244. this.blockName = this.data.name;
  245. },
  246. createBlock: function(){
  247. this.node = new Element("div", {"styles": this.blockCss.node}).inject(this.areaNode);
  248. this.createBeginNode();
  249. this.createMiddleNode();
  250. this.createEndNode();
  251. },
  252. createBeginNode: function(){
  253. this.beginNode = new Element("div", {"styles": this.blockCss.beginNode}).inject(this.node);
  254. this.beginContentNode = new Element("div", {"styles": this.blockCss.beginContentNode}).inject(this.beginNode);
  255. this.beginContentNode.setStyle("background", this.type.data.color);
  256. this.contentNode = this.beginContentNode;
  257. this.loadContent();
  258. },
  259. createMiddleNode: function(){
  260. this.middleNode = new Element("div", {"styles": this.blockCss.middleNode}).inject(this.node);
  261. var html = "<table border='0' cellspacing='0' cellpadding='0'><tr><td valign='top'></td><td valign='top'></td></tr></table>";
  262. this.middleNode.set("html", html);
  263. this.tableNode = this.middleNode.getElement("table").setStyles(this.blockCss.middleTableNode);
  264. var tds = this.tableNode.getElements("td");
  265. this.middleLeftNode = tds[0].setStyles(this.blockCss.middleLeftNode);
  266. this.middleRightNode = tds[1].setStyles(this.blockCss.middleRightNode);
  267. this.subContentNode = new Element("div", {"styles": this.blockCss.subContentNode}).inject(this.middleRightNode);
  268. this.middleLeftNode.setStyle("background", this.type.data.color);
  269. },
  270. createEndNode: function(){
  271. this.endNode = new Element("div", {"styles": this.blockCss.endNode}).inject(this.node);
  272. this.endContentNode = new Element("div", {"styles": this.blockCss.endContentNode}).inject(this.endNode);
  273. this.endContentNode.setStyle("background", this.type.data.color);
  274. }
  275. // createContent: function(){
  276. // this.node = new Element("div", {"styles": this.css.node}).inject(this.areaNode);
  277. // this.topNode = new Element("div", {"styles": this.css.topNode}).inject(this.node);
  278. // this.contentNode = new Element("div", {"styles": this.css.contentNode}).inject(this.node);
  279. // this.contentNode.setStyle("background", this.type.data.color);
  280. // this.topNode.setStyle("background", this.type.data.color);
  281. // }
  282. });
  283. MWF.xApplication.ScriptEditor.block.$Block.$EnumerateAround = new Class({
  284. Extends: MWF.xApplication.ScriptEditor.block.$Block.$Around,
  285. init: function(){
  286. var css = Object.clone(this.css.block);
  287. css = Object.merge(css, this.css.around);
  288. this.blockCss = Object.merge(css, this.css.enumerateAround);
  289. this.blockName = this.data.name;
  290. }
  291. });