Exporter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. MWF.xDesktop.requireApp("process.ApplicationExplorer", "Actions.RestActions", null, false);
  2. MWF.xApplication.process.ApplicationExplorer.Exporter = new Class({
  3. Extends: MWF.widget.Common,
  4. Implements: [Options, Events],
  5. options: {
  6. "style": "default"
  7. },
  8. initialize: function(app, data, options){
  9. this.setOptions(options);
  10. this.app = app;
  11. this.container = this.app.content;
  12. this.data = data;
  13. this.path = "/x_component_process_ApplicationExplorer/$Exporter/";
  14. this.cssPath = "/x_component_process_ApplicationExplorer/$Exporter/"+this.options.style+"/css.wcss";
  15. this._loadCss();
  16. },
  17. load: function(){
  18. this.container.mask({
  19. "destroyOnHide": true,
  20. "style": {
  21. "background-color": "#666",
  22. "opacity": 0.6
  23. }
  24. });
  25. this.node = new Element("div", {"styles": this.css.content});
  26. this.titleNode = new Element("div", {"styles": this.css.titleNode, "text": this.app.lp.application.export}).inject(this.node);
  27. this.contentNode = new Element("div", {"styles": this.css.contentNode}).inject(this.node);
  28. this.buttonAreaNode = new Element("div", {"styles": this.css.buttonAreaNode}).inject(this.node);
  29. this.cancelButton = new Element("div", {"styles": this.css.button, "text": this.app.lp.application.export_cancel}).inject(this.buttonAreaNode);
  30. this.okButton = new Element("div", {"styles": this.css.okButton, "text": this.app.lp.application.export_ok}).inject(this.buttonAreaNode);
  31. this.loadContent();
  32. this.setEvent();
  33. this.node.inject(this.container);
  34. this.node.position({
  35. relativeTo: this.container,
  36. position: 'center',
  37. edge: 'center'
  38. });
  39. },
  40. loadContent: function(){
  41. this.actions = this.app.restActions;
  42. var listTitleNodeArea = new Element("div", {"styles": this.css.listTitleNodeArea}).inject(this.contentNode);
  43. this.processSelectAction = new Element("div", {"styles": this.css.listTitleActionNode, "text": this.app.lp.application.select}).inject(listTitleNodeArea);
  44. new Element("div", {"styles": this.css.listTitleNode, "text": this.app.lp.application.process}).inject(listTitleNodeArea);
  45. this.processListNode = new Element("div", {"styles": this.css.listNode}).inject(this.contentNode);
  46. listTitleNodeArea = new Element("div", {"styles": this.css.listTitleNodeArea}).inject(this.contentNode);
  47. this.formSelectAction = new Element("div", {"styles": this.css.listTitleActionNode, "text": this.app.lp.application.select}).inject(listTitleNodeArea);
  48. new Element("div", {"styles": this.css.listTitleNode, "text": this.app.lp.application.form}).inject(listTitleNodeArea);
  49. this.formListNode = new Element("div", {"styles": this.css.listNode}).inject(this.contentNode);
  50. listTitleNodeArea = new Element("div", {"styles": this.css.listTitleNodeArea}).inject(this.contentNode);
  51. this.dictionarySelectAction = new Element("div", {"styles": this.css.listTitleActionNode, "text": this.app.lp.application.select}).inject(listTitleNodeArea);
  52. new Element("div", {"styles": this.css.listTitleNode, "text": this.app.lp.application.dictionary}).inject(listTitleNodeArea);
  53. this.dictionaryListNode = new Element("div", {"styles": this.css.listNode}).inject(this.contentNode);
  54. listTitleNodeArea = new Element("div", {"styles": this.css.listTitleNodeArea}).inject(this.contentNode);
  55. this.scriptSelectAction = new Element("div", {"styles": this.css.listTitleActionNode, "text": this.app.lp.application.select}).inject(listTitleNodeArea);
  56. new Element("div", {"styles": this.css.listTitleNode, "text": this.app.lp.application.script}).inject(listTitleNodeArea);
  57. this.scriptListNode = new Element("div", {"styles": this.css.listNode}).inject(this.contentNode);
  58. this.listProcess();
  59. this.listForm();
  60. this.listDictionary();
  61. this.listScript();
  62. this.processSelectAction.addEvent("click", function(){this.inverse(this.processListNode);}.bind(this));
  63. this.formSelectAction.addEvent("click", function(){this.inverse(this.formListNode);}.bind(this));
  64. this.dictionarySelectAction.addEvent("click", function(){this.inverse(this.dictionaryListNode);}.bind(this));
  65. this.scriptSelectAction.addEvent("click", function(){this.inverse(this.scriptListNode);}.bind(this));
  66. },
  67. listProcess: function(){
  68. this.actions.listProcess(this.data.id, function(json){
  69. if (json.data){
  70. json.data.each(function(process){
  71. this.createItem(process, this.processListNode);
  72. }.bind(this));
  73. }
  74. }.bind(this));
  75. },
  76. listForm: function(){
  77. this.actions.listForm(this.data.id, function(json){
  78. if (json.data){
  79. json.data.each(function(form){
  80. this.createItem(form, this.formListNode);
  81. }.bind(this));
  82. }
  83. }.bind(this));
  84. },
  85. listDictionary: function(){
  86. this.actions.listDictionary(this.data.id, function(json){
  87. if (json.data){
  88. json.data.each(function(dic){
  89. this.createItem(dic, this.dictionaryListNode);
  90. }.bind(this));
  91. }
  92. }.bind(this));
  93. },
  94. listScript: function(){
  95. this.actions.listScript(this.data.id, function(json){
  96. if (json.data){
  97. json.data.each(function(script){
  98. this.createItem(script, this.scriptListNode);
  99. }.bind(this));
  100. }
  101. }.bind(this));
  102. },
  103. inverse: function(node){
  104. var inputs = node.getElements("input");
  105. inputs.each(function(input){
  106. if (input.checked){
  107. input.set("checked", false);
  108. }else{
  109. input.set("checked", true);
  110. }
  111. });
  112. },
  113. createItem: function(data, node){
  114. var item = new Element("div", {"styles": this.css.processItem}).inject(node);
  115. var checkbox = new Element("input", {
  116. "styles": this.css.processItemCheckbox,
  117. "type": "checkbox",
  118. "checked": true
  119. }).inject(item);
  120. checkbox.store("itemData", data);
  121. var textNode = new Element("div", {"text": data.name, "styles": this.css.processItemText}).inject(item);
  122. },
  123. setEvent: function(){
  124. this.cancelButton.addEvent("click", function(e){
  125. this.close();
  126. }.bind(this));
  127. this.okButton.addEvent("click", function(e){
  128. this.exportApplication();
  129. // this.close();
  130. }.bind(this));
  131. },
  132. close: function(){
  133. this.container.unmask();
  134. this.node.destroy();
  135. this.cancelButton = null;
  136. this.okButton = null;
  137. this.buttonAreaNode = null;
  138. this.contentNode = null;
  139. this.titleNode = null;
  140. this.node = null;
  141. this.processListNode = null;
  142. this.formListNode = null;
  143. this.dictionaryListNode = null;
  144. this.scriptListNode = null;
  145. this.fireEvent("close");
  146. },
  147. exportApplication: function(){
  148. this.applicationJson = {
  149. "application": {},
  150. "processList": [],
  151. "formList": [],
  152. "dictionaryList": [],
  153. "scriptList": []
  154. };
  155. this.createProgressBar();
  156. var process = this.processListNode.getElements("input:checked");
  157. var forms = this.formListNode.getElements("input:checked");
  158. var dics = this.dictionaryListNode.getElements("input:checked");
  159. var scripts = this.scriptListNode.getElements("input:checked");
  160. this.status = {
  161. "count": process.length+forms.length+dics.length+scripts.length+1,
  162. "complete": 0
  163. }
  164. this.exportProperty();
  165. this.exportProcesses(process);
  166. this.exportForms(forms);
  167. this.exportDictionarys(dics);
  168. this.exportScripts(scripts);
  169. },
  170. exportProperty: function(){
  171. this.actions.getApplication(this.data.id, function(json){
  172. this.progressBarTextNode.set("text", "load Application Property ...");
  173. if (json.data){
  174. this.applicationJson.application = json.data;
  175. }
  176. this.checkExport();
  177. }.bind(this));
  178. },
  179. exportProcesses: function(processes){
  180. processes.each(function(processCheckbox){
  181. var process = processCheckbox.retrieve("itemData");
  182. this.actions.getProcess(process.id, function(json){
  183. this.progressBarTextNode.set("text", "load Process \""+process.name+"\" ...");
  184. if (json.data){
  185. this.applicationJson.processList.push(json.data);
  186. }
  187. this.checkExport();
  188. }.bind(this));
  189. }.bind(this));
  190. },
  191. exportForms: function(forms){
  192. forms.each(function(formCheckbox){
  193. var form = formCheckbox.retrieve("itemData");
  194. this.actions.getForm(form.id, function(json){
  195. this.progressBarTextNode.set("text", "load Form \""+form.name+"\" ...");
  196. if (json.data){
  197. this.applicationJson.formList.push(json.data);
  198. }
  199. this.checkExport();
  200. }.bind(this));
  201. }.bind(this));
  202. },
  203. exportDictionarys: function(dics){
  204. dics.each(function(dicCheckbox){
  205. var dic = dicCheckbox.retrieve("itemData");
  206. this.actions.getDictionary(dic.id, function(json){
  207. this.progressBarTextNode.set("text", "load Dictionary \""+dic.name+"\" ...");
  208. if (json.data){
  209. this.applicationJson.dictionaryList.push(json.data);
  210. }
  211. this.checkExport();
  212. }.bind(this));
  213. }.bind(this));
  214. },
  215. exportScripts: function(scripts){
  216. scripts.each(function(scriptCheckbox){
  217. var script = scriptCheckbox.retrieve("itemData");
  218. this.actions.getScript(script.id, function(json){
  219. this.progressBarTextNode.set("text", "load Script \""+script.name+"\" ...");
  220. if (json.data){
  221. this.applicationJson.scriptList.push(json.data);
  222. }
  223. this.checkExport();
  224. }.bind(this));
  225. }.bind(this));
  226. },
  227. checkExport: function(){
  228. this.status.complete = this.status.complete+1;
  229. var x = 358*(this.status.complete/this.status.count);
  230. this.progressBarPercent.setStyle("width", ""+x+"px");
  231. if (this.status.complete == this.status.count){
  232. this.saveApplicationToLocal();
  233. }
  234. },
  235. saveApplicationToLocal: function(){
  236. debugger;
  237. if (window.hasOwnProperty("ActiveXObject")){
  238. var win = window.open("", "_blank");
  239. win.document.write(JSON.encode(this.applicationJson));
  240. }else{
  241. this.downloadFile(this.data.name+".xapp", JSON.encode(this.applicationJson));
  242. }
  243. this.progressBarNode.destroy();
  244. this.progressBarNode = null;
  245. this.progressBarTextNode = null;
  246. this.progressBar = null;
  247. this.progressBarPercent = null;
  248. this.close();
  249. },
  250. downloadFile: function(fileName, content){
  251. var link = new Element("a", {"text": this.data.name}).inject(this.progressBarTextNode);
  252. var blob = new Blob([content]);
  253. link.download = fileName;
  254. link.href = URL.createObjectURL(blob);
  255. //link.href = "data:text/plain," + content;
  256. var evt = document.createEvent("HTMLEvents");
  257. evt.initEvent("click", false, false);
  258. link.dispatchEvent(evt);
  259. link.click();
  260. },
  261. createProgressBar: function(){
  262. this.node.hide();
  263. this.progressBarNode = new Element("div", {"styles": this.css.progressBarNode});
  264. this.progressBarNode.inject(this.container);
  265. this.progressBarNode.position({
  266. relativeTo: this.container,
  267. position: 'center',
  268. edge: 'center'
  269. });
  270. this.progressBarTextNode = new Element("div", {"styles": this.css.progressBarTextNode}).inject(this.progressBarNode);
  271. this.progressBar = new Element("div", {"styles": this.css.progressBar}).inject(this.progressBarNode);
  272. this.progressBarPercent = new Element("div", {"styles": this.css.progressBarPercent}).inject(this.progressBar);
  273. }
  274. });