Attachment.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. MWF.xApplication.process.Xform.Attachment = MWF.APPAttachment = new Class({
  3. Extends: MWF.APP$Module,
  4. options: {
  5. "moduleEvents": ["upload", "delete"]
  6. },
  7. initialize: function(node, json, form, options){
  8. this.node = $(node);
  9. this.node.store("module", this);
  10. this.json = json;
  11. this.form = form;
  12. this.field = true;
  13. },
  14. _loadUserInterface: function(){
  15. this.node.empty();
  16. this.loadAttachmentController();
  17. },
  18. loadAttachmentController: function(){
  19. debugger;
  20. MWF.require("MWF.widget.AttachmentController", function() {
  21. var options = {
  22. "title": "附件区域",
  23. "listStyle": this.json.listStyle || "icon",
  24. "size": this.json.size || "max",
  25. "resize": (this.json.size=="true") ? true : false,
  26. "attachmentCount": this.json.attachmentCount || 0,
  27. "isUpload": (this.json.isUpload=="y") ? true : false,
  28. "isDelete": (this.json.isDelete=="y") ? true : false,
  29. "isReplace": (this.json.isReplace=="y") ? true : false,
  30. "isDownload": (this.json.isDownload=="y") ? true : false,
  31. "isSizeChange": (this.json.isSizeChange=="y") ? true : false,
  32. "readonly": (this.json.readonly=="y") ? true : false
  33. }
  34. if (this.readonly) options.readonly = true;
  35. this.attachmentController = new MWF.widget.ATTER(this.node, this, options);
  36. this.attachmentController.load();
  37. this.form.businessData.attachmentList.each(function (att) {
  38. if (att.site==this.json.id) this.attachmentController.addAttachment(att);
  39. }.bind(this));
  40. }.bind(this));
  41. },
  42. _loadEvents: function(editorConfig){
  43. Object.each(this.json.events, function(e, key){
  44. if (e.code){
  45. if (this.options.moduleEvents.indexOf(key)!=-1){
  46. this.addEvent(key, function(event){
  47. return this.form.Macro.fire(e.code, this, event);
  48. }.bind(this));
  49. }else{
  50. this.node.addEvent(key, function(event){
  51. return this.form.Macro.fire(e.code, this, event);
  52. }.bind(this));
  53. }
  54. }
  55. }.bind(this));
  56. },
  57. getData: function(){
  58. return this.attachmentController.getAttachmentNames();
  59. },
  60. createUploadFileNode: function(){
  61. this.uploadFileAreaNode = new Element("div");
  62. var html = "<input name=\"file\" type=\"file\" multiple/>";
  63. this.uploadFileAreaNode.set("html", html);
  64. this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  65. this.fileUploadNode.addEvent("change", function(){
  66. var files = this.fileUploadNode.files;
  67. if (files.length){
  68. if ((files.length+this.attachmentController.attachments.length > this.attachmentController.options.attachmentCount) && this.attachmentController.options.attachmentCount>0){
  69. var content = MWF.xApplication.process.Xform.LP.uploadMore;
  70. content = content.replace("{n}", this.attachmentController.options.attachmentCount);
  71. this.form.notice(content, "error");
  72. }else{
  73. for (var i = 0; i < files.length; i++) {
  74. var file = files.item(i);
  75. var formData = new FormData();
  76. formData.append('file', file);
  77. formData.append('site', this.json.id);
  78. //formData.append('folder', folderId);
  79. this.form.workAction.uploadAttachment(this.form.businessData.work.id ,function(o, text){
  80. if (o.id){
  81. this.form.workAction.getAttachment(o.id, this.form.businessData.work.id, function(json){
  82. if (json.data) this.attachmentController.addAttachment(json.data);
  83. this.attachmentController.checkActions();
  84. this.fireEvent("upload", [json.data]);
  85. }.bind(this))
  86. }
  87. this.attachmentController.checkActions();
  88. }.bind(this), null, formData, file);
  89. }
  90. }
  91. }
  92. }.bind(this));
  93. },
  94. uploadAttachment: function(e, node){
  95. if (!this.uploadFileAreaNode){
  96. this.createUploadFileNode();
  97. }
  98. //var fileNode = this.uploadFileAreaNode.getFirst();
  99. this.fileUploadNode.click();
  100. },
  101. deleteAttachments: function(e, node, attachments){
  102. var names = [];
  103. attachments.each(function(attachment){
  104. names.push(attachment.data.name);
  105. }.bind(this));
  106. var _self = this;
  107. this.form.confirm("warn", e, MWF.xApplication.process.Xform.LP.deleteAttachmentTitle, MWF.xApplication.process.Xform.LP.deleteAttachment+"( "+names.join(", ")+" )", 300, 120, function(){
  108. while (attachments.length){
  109. attachment = attachments.shift();
  110. _self.deleteAttachment(attachment);
  111. }
  112. this.close();
  113. }, function(){
  114. this.close();
  115. }, null);
  116. },
  117. deleteAttachment: function(attachment){
  118. this.fireEvent("delete", [attachment.data]);
  119. this.form.workAction.deleteAttachment(attachment.data.id, this.form.businessData.work.id, function(josn){
  120. this.attachmentController.removeAttachment(attachment);
  121. this.attachmentController.checkActions();
  122. }.bind(this));
  123. },
  124. replaceAttachment: function(e, node, attachment){
  125. var _self = this;
  126. this.form.confirm("warn", e, MWF.xApplication.process.Xform.LP.replaceAttachmentTitle, MWF.xApplication.process.Xform.LP.replaceAttachment+"( "+attachment.data.name+" )", 300, 120, function(){
  127. _self.replaceAttachmentFile(attachment);
  128. this.close();
  129. }, function(){
  130. this.close();
  131. }, null);
  132. },
  133. createReplaceFileNode: function(attachment){
  134. this.replaceFileAreaNode = new Element("div");
  135. var html = "<input name=\"file\" type=\"file\" multiple/>";
  136. this.replaceFileAreaNode.set("html", html);
  137. this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
  138. this.fileReplaceNode.addEvent("change", function(){
  139. var files = this.fileReplaceNode.files;
  140. if (files.length){
  141. for (var i = 0; i < files.length; i++) {
  142. var file = files.item(i);
  143. var formData = new FormData();
  144. formData.append('file', file);
  145. // formData.append('site', this.json.id);
  146. this.form.workAction.replaceAttachment(attachment.data.id, this.form.businessData.work.id ,function(o, text){
  147. this.form.workAction.getAttachment(attachment.data.id, this.form.businessData.work.id, function(json){
  148. attachment.data = json.data;
  149. attachment.reload();
  150. this.attachmentController.checkActions();
  151. }.bind(this))
  152. }.bind(this), null, formData, file);
  153. }
  154. }
  155. }.bind(this));
  156. },
  157. replaceAttachmentFile: function(attachment){
  158. if (!this.replaceFileAreaNode){
  159. this.createReplaceFileNode(attachment);
  160. }
  161. this.fileReplaceNode.click();
  162. },
  163. downloadAttachment: function(e, node, attachments){
  164. if (this.form.businessData.work){
  165. attachments.each(function(att){
  166. debugger;
  167. this.form.workAction.getAttachmentStream(att.data.id, this.form.businessData.work.id);
  168. }.bind(this));
  169. }else{
  170. attachments.each(function(att){
  171. this.form.workAction.getWorkcompletedAttachmentStream(att.data.id, this.form.businessData.workCompleted.id);
  172. }.bind(this));
  173. }
  174. },
  175. openAttachment: function(e, node, attachments){
  176. if (this.form.businessData.work){
  177. attachments.each(function(att){
  178. this.form.workAction.getAttachmentData(att.data.id, this.form.businessData.work.id);
  179. }.bind(this));
  180. }else{
  181. attachments.each(function(att){
  182. this.form.workAction.getWorkcompletedAttachmentData(att.data.id, this.form.businessData.workCompleted.id);
  183. }.bind(this));
  184. }
  185. //this.downloadAttachment(e, node, attachment);
  186. },
  187. getAttachmentUrl: function(attachment, callback){
  188. if (this.form.businessData.work){
  189. this.form.workAction.getAttachmentUrl(attachment.data.id, this.form.businessData.work.id, callback);
  190. }else{
  191. this.form.workAction.getAttachmentWorkcompletedUrl(attachment.data.id, this.form.businessData.workCompleted.id, callback);
  192. }
  193. },
  194. createErrorNode: function(text){
  195. var node = new Element("div");
  196. var iconNode = new Element("div", {
  197. "styles": {
  198. "width": "20px",
  199. "height": "20px",
  200. "float": "left",
  201. "background": "url("+"/x_component_process_Xform/$Form/default/icon/error.png) center center no-repeat"
  202. }
  203. }).inject(node);
  204. var textNode = new Element("div", {
  205. "styles": {
  206. "line-height": "20px",
  207. "margin-left": "20px",
  208. "color": "red"
  209. },
  210. "text": text
  211. }).inject(node);
  212. return node;
  213. },
  214. notValidationMode: function(text){
  215. if (!this.isNotValidationMode){
  216. this.isNotValidationMode = true;
  217. this.node.store("borderStyle", this.node.getStyles("border-left", "border-right", "border-top", "border-bottom"));
  218. this.node.setStyle("border", "1px solid red");
  219. this.errNode = this.createErrorNode(text).inject(this.node, "after");
  220. this.showNotValidationMode(this.node);
  221. }
  222. },
  223. showNotValidationMode: function(node){
  224. var p = node.getParent("div");
  225. if (p){
  226. if (p.get("MWFtype") == "tab$Content"){
  227. if (p.getParent("div").getStyle("display")=="none"){
  228. var contentAreaNode = p.getParent("div").getParent("div");
  229. var tabAreaNode = contentAreaNode.getPrevious("div");
  230. var idx = contentAreaNode.getChildren().indexOf(p.getParent("div"));
  231. var tabNode = tabAreaNode.getChildren()[idx];
  232. tabNode.click();
  233. p = tabAreaNode.getParent("div");
  234. }
  235. }
  236. this.showNotValidationMode(p);
  237. }
  238. },
  239. validationMode: function(){
  240. if (this.isNotValidationMode){
  241. this.isNotValidationMode = false;
  242. this.node.setStyles(this.node.retrieve("borderStyle"));
  243. if (this.errNode){
  244. this.errNode.destroy();
  245. this.errNode = null;
  246. }
  247. }
  248. },
  249. validationConfigItem: function(routeName, data){
  250. var flag = (data.status=="all") ? true: (routeName == data.decision);
  251. if (flag){
  252. var n = this.getData();
  253. var v = (data.valueType=="value") ? n : n.length;
  254. switch (data.operateor){
  255. case "isnull":
  256. if (!v){
  257. this.notValidationMode(data.prompt);
  258. return false;
  259. }
  260. break;
  261. case "notnull":
  262. if (v){
  263. this.notValidationMode(data.prompt);
  264. return false;
  265. }
  266. break;
  267. case "gt":
  268. if (v>data.value){
  269. this.notValidationMode(data.prompt);
  270. return false;
  271. }
  272. break;
  273. case "lt":
  274. if (v<data.value){
  275. this.notValidationMode(data.prompt);
  276. return false;
  277. }
  278. break;
  279. case "equal":
  280. if (v==data.value){
  281. this.notValidationMode(data.prompt);
  282. return false;
  283. }
  284. break;
  285. case "neq":
  286. if (v!=data.value){
  287. this.notValidationMode(data.prompt);
  288. return false;
  289. }
  290. break;
  291. case "contain":
  292. if (v.indexOf(data.value)!=-1){
  293. this.notValidationMode(data.prompt);
  294. return false;
  295. }
  296. break;
  297. case "notcontain":
  298. if (v.indexOf(data.value)==-1){
  299. this.notValidationMode(data.prompt);
  300. return false;
  301. }
  302. break;
  303. }
  304. }
  305. return true;
  306. },
  307. validationConfig: function(routeName, opinion){
  308. debugger;
  309. if (this.json.validationConfig){
  310. if (this.json.validationConfig.length){
  311. for (var i=0; i<this.json.validationConfig.length; i++) {
  312. var data = this.json.validationConfig[i];
  313. if (!this.validationConfigItem(routeName, data)) return false;
  314. }
  315. }
  316. return true;
  317. }
  318. return true;
  319. },
  320. validation: function(routeName, opinion){
  321. if (!this.validationConfig(routeName, opinion)) return false;
  322. if (!this.json.validation) return true;
  323. if (!this.json.validation.code) return true;
  324. var flag = this.form.Macro.exec(this.json.validation.code, this);
  325. if (!flag) flag = MWF.xApplication.process.Xform.LP.notValidation;
  326. if (flag.toString()!="true"){
  327. this.notValidationMode(flag);
  328. return false;
  329. }
  330. return true;
  331. }
  332. });