Flash.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. define("dojox/form/uploader/plugins/Flash", [
  2. "dojo/dom-form",
  3. "dojo/dom-style",
  4. "dojo/dom-construct",
  5. "dojo/dom-attr",
  6. "dojo/_base/declare",
  7. "dojo/_base/config",
  8. "dojo/_base/connect",
  9. "dojo/_base/lang",
  10. "dojo/_base/array",
  11. "dojox/form/uploader/plugins/HTML5",
  12. "dojox/embed/Flash"
  13. ],function(domForm, domStyle, domConstruct, domAttr, declare, config, connect, lang, array, formUploaderPluginsHTML5, embedFlash){
  14. var pluginsFlash = declare("dojox.form.uploader.plugins.Flash", [], {
  15. //
  16. // Version: 1.6
  17. //
  18. // summary:
  19. // A plugin for dojox.form.Uploader that utilizes a Flash SWF for handling to upload in IE.
  20. // All other browsers will use the HTML5 plugin, unless force="flash" is used, then Flash
  21. // will be used in all browsers. force="flash" is provided because Flash has some features
  22. // that HTML5 does not yet have. But it is still not recommended because of the many problems
  23. // that Firefox and Webkit have with the Flash plugin.
  24. //
  25. // description:
  26. // Inherits all properties from dojox.form.Uploader and formUploaderPluginsHTML5.
  27. // All properties and methods listed here are specific to the Flash plugin only.
  28. //
  29. // swfPath:String
  30. // Path to SWF. Can be overwritten or provided in djConfig.
  31. swfPath:config.uploaderPath || require.toUrl("dojox/form/resources/uploader.swf"),
  32. //
  33. // skipServerCheck: Boolean
  34. // If true, will not verify that the server was sent the correct format.
  35. // This can be safely set to true. The purpose of the server side check
  36. // is mainly to show the dev if they've implemented the different returns
  37. // correctly.
  38. skipServerCheck:true,
  39. //
  40. // serverTimeout:Number (milliseconds)
  41. // The amount of time given to the uploaded file
  42. // to wait for a server response. After this amount
  43. // of time, the onComplete is fired but with a 'server timeout'
  44. // error in the returned item.
  45. serverTimeout: 2000,
  46. //
  47. // isDebug: Boolean
  48. // If true, outputs traces from the SWF to console. What exactly gets passed
  49. // is very relative, and depends upon what traces have been left in the DEFT SWF.
  50. isDebug:false,
  51. //
  52. // devMode: Boolean.
  53. // Re-implemented. devMode increases the logging, adding style tracing from the SWF.
  54. devMode:false,
  55. //
  56. // deferredUploading: Number (1 - X)
  57. // (Flash only) throttles the upload to a certain amount of files at a time.
  58. // By default, Flash uploads file one at a time to the server, but in parallel.
  59. // Firefox will try to queue all files at once, leading to problems. Set this
  60. // to the amount to upload in parallel at a time.
  61. // Generally, 1 should work fine, but you can experiment with queuing more than
  62. // one at a time.
  63. // This is of course ignored if selectMultipleFiles equals false.
  64. deferredUploading:0,
  65. //
  66. // force: String
  67. // Use "flash" to always use Flash (and hopefully force the user to download the plugin
  68. // if they don't have it).
  69. force:"",
  70. postMixInProperties: function(){
  71. if(!this.supports("multiple")){
  72. // Flash will only be used in IE6-8 unless force="flash"
  73. this.uploadType = "flash";
  74. this._files = [];
  75. this._fileMap = {};
  76. this._createInput = this._createFlashUploader;
  77. this.getFileList = this.getFlashFileList;
  78. this.reset = this.flashReset;
  79. this.upload = this.uploadFlash;
  80. this.fieldname = "flashUploadFiles"; ///////////////////// this.name
  81. }
  82. this.inherited(arguments);
  83. },
  84. /*************************
  85. * Public Events *
  86. *************************/
  87. onReady: function(/* dojox.form.FileUploader */ uploader){
  88. // summary:
  89. // Stub - Fired when embedFlash has created the
  90. // Flash object, but it has not necessarilly finished
  91. // downloading, and is ready to be communicated with.
  92. },
  93. onLoad: function(/* dojox.form.FileUploader */ uploader){
  94. // summary:
  95. // Stub - SWF has been downloaded 100%.
  96. },
  97. onFileChange: function(fileArray){
  98. // summary:
  99. // Stub - Flash-specific event. Fires on each selection of files
  100. // and only provides the files selected on that event - not all files
  101. // selected, as with HTML5
  102. },
  103. onFileProgress: function(fileArray){
  104. // summary:
  105. // Stub - Flash-specific event. Fires on progress of upload
  106. // and only provides a file-specific event
  107. },
  108. /*************************
  109. * Public Methods *
  110. *************************/
  111. getFlashFileList: function(){
  112. // summary:
  113. // Returns list of currently selected files
  114. return this._files; // Array
  115. },
  116. flashReset: function(){
  117. this.flashMovie.reset();
  118. this._files = [];
  119. },
  120. /*************************
  121. * Private Methods *
  122. *************************/
  123. uploadFlash: function(/*Object ? */formData){
  124. // summary:
  125. // Uploads selected files. Alias "upload()" should be used instead.
  126. // tags:
  127. // private
  128. this.onBegin(this.getFileList());
  129. this.flashMovie.doUpload(formData);
  130. },
  131. _change: function(fileArray){
  132. this._files = this._files.concat(fileArray);
  133. array.forEach(fileArray, function(f){
  134. f.bytesLoaded = 0;
  135. f.bytesTotal = f.size;
  136. this._fileMap[f.name+"_"+f.size] = f;
  137. }, this);
  138. this.onChange(this._files);
  139. this.onFileChange(fileArray);
  140. },
  141. _complete: function(fileArray){
  142. var o = this._getCustomEvent();
  143. o.type = "load";
  144. this.onComplete(fileArray);
  145. },
  146. _progress: function(f){
  147. this._fileMap[f.name+"_"+f.bytesTotal].bytesLoaded = f.bytesLoaded;
  148. var o = this._getCustomEvent();
  149. this.onFileProgress(f);
  150. this.onProgress(o);
  151. },
  152. _error: function(err){
  153. this.onError(err);
  154. },
  155. _onFlashBlur: function(fileArray){
  156. //console.log("UploaderFlash._onFlashBlur");
  157. },
  158. _getCustomEvent: function(){
  159. var o = {
  160. bytesLoaded:0,
  161. bytesTotal:0,
  162. type:"progress",
  163. timeStamp:new Date().getTime()
  164. };
  165. for(var nm in this._fileMap){
  166. o.bytesTotal += this._fileMap[nm].bytesTotal;
  167. o.bytesLoaded += this._fileMap[nm].bytesLoaded;
  168. }
  169. o.decimal = o.bytesLoaded / o.bytesTotal;
  170. o.percent = Math.ceil((o.bytesLoaded / o.bytesTotal)*100)+"%";
  171. return o; // Object
  172. },
  173. _connectFlash: function(){
  174. // summary:
  175. // Subscribing to published topics coming from the
  176. // Flash uploader.
  177. // description:
  178. // Sacrificing some readbilty for compactness. this.id
  179. // will be on the beginning of the topic, so more than
  180. // one uploader can be on a page and can have unique calls.
  181. //
  182. this._subs = [];
  183. this._cons = [];
  184. var doSub = lang.hitch(this, function(s, funcStr){
  185. this._subs.push(connect.subscribe(this.id + s, this, funcStr));
  186. });
  187. doSub("/filesSelected", "_change");
  188. doSub("/filesUploaded", "_complete");
  189. doSub("/filesProgress", "_progress");
  190. doSub("/filesError", "_error");
  191. doSub("/filesCanceled", "onCancel");
  192. doSub("/stageBlur", "_onFlashBlur");
  193. this.connect(this.domNode, "focus", function(){
  194. // TODO: some kind of indicator that the Flash button
  195. // is in focus
  196. this.flashMovie.focus();
  197. this.flashMovie.doFocus();
  198. });
  199. if(this.tabIndex>=0){
  200. domAttr.set(this.domNode, "tabIndex", this.tabIndex);
  201. }
  202. },
  203. _createFlashUploader: function(){
  204. // summary:
  205. // Internal. Creates Flash Uploader
  206. //
  207. var url = this.getUrl();
  208. if(url){
  209. if(url.toLowerCase().indexOf("http")<0 && url.indexOf("/")!=0){
  210. // Appears to be a relative path. Attempt to
  211. // convert it to absolute, so it will better
  212. //target the SWF.
  213. //
  214. var loc = window.location.href.split("/");
  215. loc.pop();
  216. loc = loc.join("/")+"/";
  217. url = loc+url;
  218. }
  219. }else{
  220. console.warn("Warning: no uploadUrl provided.");
  221. }
  222. this.inputNode = domConstruct.create("div", {className:"dojoxFlashNode"}, this.domNode, "first");
  223. domStyle.set(this.inputNode, {
  224. position:"absolute",
  225. top:"-2px",
  226. width:this.btnSize.w+"px",
  227. height:this.btnSize.h+"px",
  228. opacity:0
  229. });
  230. var w = this.btnSize.w;
  231. var h = this.btnSize.h;
  232. var args = {
  233. expressInstall:true,
  234. path: (this.swfPath.uri || this.swfPath) + "?cb_" + (new Date().getTime()),
  235. width: w,
  236. height: h,
  237. allowScriptAccess:"always",
  238. allowNetworking:"all",
  239. vars: {
  240. uploadDataFieldName: this.flashFieldName || this.name+"Flash",
  241. uploadUrl: url,
  242. uploadOnSelect: this.uploadOnSelect,
  243. deferredUploading:this.deferredUploading || 0,
  244. selectMultipleFiles: this.multiple,
  245. id: this.id,
  246. isDebug: this.isDebug,
  247. noReturnCheck: this.skipServerCheck,
  248. serverTimeout:this.serverTimeout
  249. },
  250. params: {
  251. scale:"noscale",
  252. wmode:"transparent",
  253. wmode:"opaque",
  254. allowScriptAccess:"always",
  255. allowNetworking:"all"
  256. }
  257. };
  258. this.flashObject = new embedFlash(args, this.inputNode);
  259. this.flashObject.onError = lang.hitch(function(msg){
  260. console.error("Flash Error: " + msg);
  261. });
  262. this.flashObject.onReady = lang.hitch(this, function(){
  263. this.onReady(this);
  264. });
  265. this.flashObject.onLoad = lang.hitch(this, function(mov){
  266. this.flashMovie = mov;
  267. this.flashReady = true;
  268. this.onLoad(this);
  269. });
  270. this._connectFlash();
  271. }
  272. });
  273. dojox.form.addUploaderPlugin(pluginsFlash);
  274. return pluginsFlash;
  275. });