Flash.js 9.4 KB

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