Upload.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. o2.widget = o2.widget || {};
  2. o2.widget.Upload = new Class({
  3. Extends: o2.widget.Common,
  4. Implements: [Options, Events],
  5. options: {
  6. "data": null,
  7. "parameter": null,
  8. "action": null,
  9. "method": "",
  10. "accept" : "*/*",
  11. "style": "default",
  12. "multiple": true
  13. },
  14. initialize: function(container, options){
  15. this.setOptions(options);
  16. this.path = o2.session.path+"/widget/$Upload/";
  17. this.cssPath = o2.session.path+"/widget/$Upload/"+this.options.style+"/css.wcss";
  18. this._loadCss();
  19. this.container = $(container);
  20. this.action = (typeOf(this.options.action)=="string") ? o2.Actions.get(this.options.action).action : this.options.action;
  21. },
  22. load: function(){
  23. if (FormData.expiredIE){
  24. this.doUpload_InputFile();
  25. }else{
  26. this.doUpload_FormData();
  27. }
  28. },
  29. upload:function(){
  30. if (FormData.expiredIE){
  31. this.doUpload_InputFile();
  32. }else{
  33. if (!this.fileUploadNode) this.formData_CreateUploadArea();
  34. this.fileUploadNode.click();
  35. }
  36. } ,
  37. doUpload_FormData: function(){
  38. this.formData_CreateUploadArea();
  39. this.fileUploadNode.click();
  40. },
  41. formData_CreateUploadArea: function(){
  42. if (!this.uploadFileAreaNode){
  43. this.uploadFileAreaNode = new Element("div");
  44. var html = "<input name=\"file\" "+((this.options.multiple) ? "multiple": "")+" type=\"file\" accept=\"" + this.options.accept + "\"/>";
  45. this.uploadFileAreaNode.set("html", html);
  46. this.fileUploadNode = this.uploadFileAreaNode.getFirst();
  47. this.fileUploadNode.addEvent("change", this.formData_Upload.bind(this));
  48. }
  49. },
  50. formData_Upload: function(){
  51. var files = this.fileUploadNode.files;
  52. if (files.length){
  53. debugger;
  54. var count = files.length;
  55. var current = 0;
  56. this.isContinue = true;
  57. this.fireEvent("beforeUpload", [files, this]);
  58. var uploadBack = function(json){
  59. if (current == count) this.fireEvent("completed", [json]);
  60. }.bind(this);
  61. if (this.isContinue){
  62. for (var i = 0; i < files.length; i++) {
  63. var file = files.item(i);
  64. this.fireEvent("beforeUploadEntry", [file, this]);
  65. var formData = new FormData();
  66. Object.each(this.options.data, function(v, k){
  67. formData.append(k, v)
  68. });
  69. formData.append('file', file);
  70. this.action.invoke({
  71. "name": this.options.method,
  72. "async": true,
  73. "data": formData,
  74. "file": file,
  75. "parameter": this.options.parameter,
  76. "success": function(json){
  77. current++;
  78. this.fireEvent("every", [json, current, count, file]);
  79. uploadBack(json);
  80. }.bind(this)
  81. });
  82. }
  83. }
  84. this.uploadFileAreaNode.destroy();
  85. this.uploadFileAreaNode = null;
  86. }
  87. },
  88. inputFile_CreateMaskNode: function(){
  89. this.container.mask({
  90. "style": {
  91. "opacity": 0.7,
  92. "background-color": "#999"
  93. }
  94. });
  95. },
  96. addUploadMessage: function(fileName){
  97. var contentHTML = "";
  98. contentHTML = "<div style=\"overflow: hidden\"><div style=\"height: 2px; border:0px solid #999; margin: 3px 0px\">" +
  99. "<div style=\"height: 2px; background-color: #acdab9; width: 0px;\"></div></div>" +
  100. "<div style=\"height: 20px; line-height: 20px\">"+o2.LP.desktop.action.uploadTitle+"</div></div>" +
  101. "<iframe name='o2_upload_iframe' style='display:none'></iframe>" ;
  102. var msg = {
  103. "subject": o2.LP.desktop.action.uploadTitle,
  104. "content": fileName+"<br/>"+contentHTML
  105. };
  106. if (layout.desktop.message){
  107. var messageItem = layout.desktop.message.addMessage(msg);
  108. messageItem.close = function(callback, e){
  109. if (!messageItem.completed){
  110. }else{
  111. messageItem.closeItem(callback, e);
  112. }
  113. };
  114. }
  115. window.setTimeout(function(){
  116. if (layout.desktop.message) if (!layout.desktop.message.isShow) layout.desktop.message.show();
  117. }.bind(this), 300);
  118. return messageItem;
  119. },
  120. setMessageTitle: function(messageItem, text){
  121. if (messageItem) messageItem.subjectNode.set("text", text);
  122. },
  123. setMessageText: function(messageItem, text){
  124. if (messageItem){
  125. var progressNode = messageItem.contentNode.getFirst("div").getFirst("div");
  126. var progressPercentNode = progressNode.getFirst("div");
  127. var progressInforNode = messageItem.contentNode.getFirst("div").getLast("div");
  128. progressInforNode.set("text", text);
  129. messageItem.dateNode.set("text", (new Date()).format("db"));
  130. }
  131. },
  132. inputFile_CreateInputNode: function(url){
  133. this.inputUploadAreaNode = new Element("div", {"styles": this.css.inputUploadAreaNode}).inject(this.container);
  134. this.inputUploadAreaNode.position({
  135. relativeTo: this.container,
  136. position: "center"
  137. });
  138. var formNode = new Element("form", {
  139. "method": "POST",
  140. //"action": url+"/callback/window.frameElement.ownerDocument.defaultView.o2.O2UploadCallbackFun",
  141. "action": url+"/callback/window.frameElement.ownerDocument.O2UploadCallbackFun",
  142. //"action": url,
  143. "enctype": "multipart/form-data",
  144. "target": "o2_upload_iframe"
  145. }).inject(this.inputUploadAreaNode);
  146. var titleNode = new Element("div", {"styles": this.css.inputUploadAreaTitleNode, "text": o2.LP.widget.uploadTitle}).inject(formNode);
  147. var inforNode = new Element("div", {"styles": this.css.inputUploadAreaInforNode, "text": o2.LP.widget.uploadInfor}).inject(formNode);
  148. var inputAreaNode = new Element("div", {"styles": this.css.inputUploadAreaInputAreaNode}).inject(formNode);
  149. var inputNode = new Element("input", {"name":"file", "type": "file", "styles": this.css.inputUploadAreaInputNode}).inject(inputAreaNode);
  150. var inputNameNode = new Element("input", {"type": "hidden", "name": "fileName"}).inject(inputAreaNode);
  151. Object.each(this.options.data, function(v, k){
  152. new Element("input", {"type": "hidden", "name": k, "value": v}).inject(inputAreaNode);
  153. });
  154. var actionNode = new Element("div", {"styles": this.css.inputUploadActionNode}).inject(formNode);
  155. var cancelButton = new Element("button", {"styles": this.css.inputUploadCancelButton, "text": o2.LP.widget.cancel}).inject(actionNode);
  156. var okButton = new Element("input", {"type": "button", "styles": this.css.inputUploadOkButton, "value": o2.LP.widget.ok}).inject(actionNode);
  157. inputNode.addEvent("change", function(){
  158. var fileName = inputNode.get("value");
  159. if (fileName){
  160. var tmpv = fileName.replace(/\\/g, "/");
  161. var i = tmpv.lastIndexOf("/");
  162. var fname = (i===-1) ? tmpv : tmpv.substr(i+1, tmpv.length-i);
  163. inputNameNode.set("value", fname);
  164. }
  165. }.bind(this));
  166. cancelButton.addEvent("click", function(){
  167. this.container.unmask();
  168. this.inputUploadAreaNode.destroy();
  169. }.bind(this));
  170. okButton.addEvent("click", function(){
  171. formNode.mask({
  172. "style": {
  173. "opacity": 0.7,
  174. "background-color": "#999"
  175. }
  176. });
  177. this.isContinue = true;
  178. this.fireEvent("beforeUpload", [inputNameNode.get("value")]);
  179. if (this.isContinue){
  180. this.messageItem = this.addUploadMessage(inputNameNode.get("value"));
  181. formNode.submit();
  182. this.container.unmask();
  183. if (this.inputUploadAreaNode) this.inputUploadAreaNode.destroy();
  184. }
  185. }.bind(this));
  186. },
  187. inputFile_UploadCallback: function(str){
  188. var json = JSON.decode(str);
  189. this.messageItem.completed = true;
  190. if (json.type==="success"){
  191. //if (every) every(json.data);
  192. this.setMessageTitle(this.messageItem, o2.LP.desktop.action.uploadComplete);
  193. this.setMessageText(this.messageItem, o2.LP.desktop.action.uploadComplete);
  194. this.fireEvent("every", [json]);
  195. this.fireEvent("completed", [json]);
  196. }else{
  197. this.setMessageTitle(this.messageItem, o2.LP.desktop.action.sendError);
  198. this.setMessageText(this.messageItem, o2.LP.desktop.action.sendError+": "+json.message);
  199. o2.xDesktop.notice("error", {x: "right", y:"top"}, json.message);
  200. }
  201. },
  202. doUpload_InputFile: function(){
  203. this.action.getActions(function(){
  204. var url = this.action.actions[this.options.method];
  205. url = this.action.address+url.uri;
  206. Object.each(this.options.parameter, function(v, k){
  207. url = url.replace("{"+k+"}", v);
  208. });
  209. this.messageItem = null;
  210. //o2.O2UploadCallbackFun = this.inputFile_UploadCallback.bind(this);
  211. document.O2UploadCallbackFun = this.inputFile_UploadCallback.bind(this);
  212. this.inputFile_CreateInputNode(url);
  213. }.bind(this));
  214. }
  215. });