Attachment.js 16 KB

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