FiledConfigurator.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. MWF.xApplication.process.FormDesigner.widget.FiledConfigurator = new Class({
  2. Implements: [Options, Events],
  3. Extends: MWF.widget.Common,
  4. options: {
  5. "style": "default",
  6. "title": "",
  7. "hasRefresh": false
  8. },
  9. initialize: function(node, designer, options, data){
  10. this.setOptions(options);
  11. this.node = $(node);
  12. this.app = designer;
  13. this.data = data || [];
  14. this.path = "../x_component_process_FormDesigner/widget/$FiledConfigurator/";
  15. this.cssPath = "../x_component_process_FormDesigner/widget/$FiledConfigurator/"+this.options.style+"/css.wcss";
  16. this._loadCss();
  17. },
  18. load: function(){
  19. this.loadTitle();
  20. this.loadContent();
  21. this.loadAddAction();
  22. },
  23. loadTitle: function(){
  24. this.titleNode = new Element("div",{
  25. "styles": this.css.titleNode,
  26. "text": this.options.title,
  27. }).inject(this.node);
  28. this.refreshNode = new Element("div",{
  29. "styles": this.css.refreshNode,
  30. "title": this.app.lp.filedConfigurator.importFromForm
  31. }).inject(this.titleNode);
  32. this.refreshNode.addEvent("click", function () {
  33. this.fireEvent("refresh")
  34. }.bind(this))
  35. },
  36. loadContent: function(){
  37. if(!this.contentNode)this.contentNode = new Element("div").inject(this.node);
  38. this.items = [];
  39. this.table = new Element("table").inject(this.contentNode);
  40. var tr = new Element("tr").inject(this.table);
  41. new Element("th", {"text": this.app.lp.filedConfigurator.sequence, "width":"10%"}).inject(tr);
  42. new Element("th", {"text": this.app.lp.filedConfigurator.fieldTitle, "width":"35%"}).inject(tr);
  43. new Element("th", {"text": this.app.lp.filedConfigurator.fieldId, "width":"35%"}).inject(tr);
  44. var td = new Element("th", {"text": this.app.lp.filedConfigurator.action, "width":"20%"}).inject(tr);
  45. // if( this.data.length === 0 ){
  46. // this.addItem()
  47. // }else{
  48. Array.each(this.data, function(obj, i){
  49. var item = new MWF.xApplication.process.FormDesigner.widget.FiledConfigurator.Item(this);
  50. item.load(obj, i);
  51. this.items.push(item);
  52. }.bind(this));
  53. // }
  54. },
  55. loadAddAction: function(){
  56. this.addNewItemAction = new Element("div", {"styles": this.css.addNewItemAction, "text": "+"}).inject(this.node);
  57. this.addNewItemAction.addEvent("click", function(){
  58. this.addItem();
  59. this.addNewItemAction.hide();
  60. }.bind(this));
  61. if(this.data.length > 0)this.addNewItemAction.hide();
  62. },
  63. reloadContent: function(){
  64. this.contentNode.empty();
  65. this.loadContent();
  66. if(this.data.length > 0){
  67. this.addNewItemAction.hide();
  68. }else{
  69. this.addNewItemAction.show();
  70. }
  71. },
  72. getData: function(){
  73. return this.data;
  74. },
  75. addItem: function(){
  76. var item = new MWF.xApplication.process.FormDesigner.widget.FiledConfigurator.Item(this);
  77. item.load("", this.items.length);
  78. this.items.push(item);
  79. this.fireEvent("change");
  80. },
  81. insertItem: function(beforeItem){
  82. var index = beforeItem.index+1;
  83. this.data.splice(index, 0, {"field": "", "title":""});
  84. this.reloadContent();
  85. this.fireEvent("change");
  86. // var item = new MWF.xApplication.process.FormDesigner.widget.FiledConfigurator.Item(this);
  87. // item.load("", beforeItem.index+1);
  88. // this.items.push(item);
  89. },
  90. deleteItem: function(item){
  91. this.items.erase(item);
  92. if (this.data[item.index]){
  93. this.data.splice(item.index, 1);
  94. }
  95. if (item.tr){
  96. item.tr.destroy();
  97. }
  98. this.setItemsSequence();
  99. if (this.data.length === 0){
  100. if (this.addNewItemAction) this.addNewItemAction.setStyle("display", "block");
  101. }
  102. this.fireEvent("change");
  103. },
  104. upItem: function(item){
  105. if( item.index === 0)return;
  106. var beforeData = this.data[item.index-1];
  107. this.data[item.index-1] = this.data[item.index];
  108. this.data[item.index] = beforeData;
  109. this.reloadContent();
  110. this.fireEvent("change");
  111. },
  112. setItemsSequence: function () {
  113. this.items.each(function (item, i) {
  114. item.index = i;
  115. item.sequenceNode.set("text", i+1)
  116. })
  117. }
  118. });
  119. MWF.xApplication.process.FormDesigner.widget.FiledConfigurator.Item = new Class({
  120. initialize: function(configurator){
  121. this.configurator = configurator;
  122. },
  123. load: function(data, i){
  124. this.index = i;
  125. if (!data){
  126. this.data = {"field": "", "title":""};
  127. this.configurator.data.push(this.data);
  128. }else{
  129. this.data = data;
  130. }
  131. this.create();
  132. },
  133. create: function () {
  134. var lp = this.configurator.app.lp.filedConfigurator;
  135. this.tr = new Element("tr").inject(this.configurator.table);
  136. var td;
  137. this.sequenceNode = new Element("td", {
  138. text : this.index+1,
  139. styles: this.configurator.css.sequenceNode,
  140. }).inject(this.tr);
  141. td = new Element("td").inject(this.tr);
  142. this.titleInput = new Element("input", {
  143. value: this.data.title,
  144. styles: this.configurator.css.inputNode,
  145. events: {
  146. blur: function (){
  147. this.data.title = this.titleInput.get("value");
  148. this.configurator.fireEvent("change");
  149. }.bind(this)
  150. }
  151. }).inject(td);
  152. td = new Element("td").inject(this.tr);
  153. this.fieldInput = new Element("input", {
  154. value: this.data.field,
  155. styles: this.configurator.css.inputNode,
  156. events: {
  157. blur: function (){
  158. this.data.field = this.fieldInput.get("value");
  159. this.configurator.fireEvent("change");
  160. }.bind(this)
  161. }
  162. }).inject(td);
  163. td = new Element("td").inject(this.tr);
  164. this.upAction = new Element("div", {
  165. text : "↑",
  166. "title": lp.moveup,
  167. styles: this.configurator.css.addAction,
  168. events: {
  169. click: function (ev) {
  170. this.up(ev)
  171. }.bind(this)
  172. }
  173. }).inject(td);
  174. this.delectAction = new Element("div", {
  175. text : "-",
  176. "title": lp.deleteRow,
  177. styles: this.configurator.css.delectAction,
  178. events: {
  179. click: function (ev) {
  180. this.delect(ev)
  181. }.bind(this)
  182. }
  183. }).inject(td);
  184. this.addAction = new Element("div", {
  185. text : "+",
  186. "title": lp.insertRow,
  187. styles: this.configurator.css.addAction,
  188. events: {
  189. click: function (ev) {
  190. this.add(ev)
  191. }.bind(this)
  192. }
  193. }).inject(td);
  194. },
  195. add: function(){
  196. this.configurator.insertItem(this)
  197. },
  198. delect: function () {
  199. this.configurator.deleteItem(this);
  200. },
  201. up: function () {
  202. this.configurator.upItem(this);
  203. }
  204. });