plupload.dev.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  1. /**
  2. * Plupload - multi-runtime File Uploader
  3. * v2.1.9
  4. *
  5. * Copyright 2013, Moxiecode Systems AB
  6. * Released under GPL License.
  7. *
  8. * License: http://www.plupload.com/license
  9. * Contributing: http://www.plupload.com/contributing
  10. *
  11. * Date: 2016-05-15
  12. */
  13. /**
  14. * Plupload.js
  15. *
  16. * Copyright 2013, Moxiecode Systems AB
  17. * Released under GPL License.
  18. *
  19. * License: http://www.plupload.com/license
  20. * Contributing: http://www.plupload.com/contributing
  21. */
  22. /*global mOxie:true */
  23. ;(function(window, o, undef) {
  24. var delay = window.setTimeout
  25. , fileFilters = {}
  26. ;
  27. // convert plupload features to caps acceptable by mOxie
  28. function normalizeCaps(settings) {
  29. var features = settings.required_features, caps = {};
  30. function resolve(feature, value, strict) {
  31. // Feature notation is deprecated, use caps (this thing here is required for backward compatibility)
  32. var map = {
  33. chunks: 'slice_blob',
  34. jpgresize: 'send_binary_string',
  35. pngresize: 'send_binary_string',
  36. progress: 'report_upload_progress',
  37. multi_selection: 'select_multiple',
  38. dragdrop: 'drag_and_drop',
  39. drop_element: 'drag_and_drop',
  40. headers: 'send_custom_headers',
  41. urlstream_upload: 'send_binary_string',
  42. canSendBinary: 'send_binary',
  43. triggerDialog: 'summon_file_dialog'
  44. };
  45. if (map[feature]) {
  46. caps[map[feature]] = value;
  47. } else if (!strict) {
  48. caps[feature] = value;
  49. }
  50. }
  51. if (typeof(features) === 'string') {
  52. plupload.each(features.split(/\s*,\s*/), function(feature) {
  53. resolve(feature, true);
  54. });
  55. } else if (typeof(features) === 'object') {
  56. plupload.each(features, function(value, feature) {
  57. resolve(feature, value);
  58. });
  59. } else if (features === true) {
  60. // check settings for required features
  61. if (settings.chunk_size > 0) {
  62. caps.slice_blob = true;
  63. }
  64. if (settings.resize.enabled || !settings.multipart) {
  65. caps.send_binary_string = true;
  66. }
  67. plupload.each(settings, function(value, feature) {
  68. resolve(feature, !!value, true); // strict check
  69. });
  70. }
  71. return caps;
  72. }
  73. /**
  74. * @module plupload
  75. * @static
  76. */
  77. var plupload = {
  78. /**
  79. * Plupload version will be replaced on build.
  80. *
  81. * @property VERSION
  82. * @for Plupload
  83. * @static
  84. * @final
  85. */
  86. VERSION : '2.1.9',
  87. /**
  88. * The state of the queue before it has started and after it has finished
  89. *
  90. * @property STOPPED
  91. * @static
  92. * @final
  93. */
  94. STOPPED : 1,
  95. /**
  96. * Upload process is running
  97. *
  98. * @property STARTED
  99. * @static
  100. * @final
  101. */
  102. STARTED : 2,
  103. /**
  104. * File is queued for upload
  105. *
  106. * @property QUEUED
  107. * @static
  108. * @final
  109. */
  110. QUEUED : 1,
  111. /**
  112. * File is being uploaded
  113. *
  114. * @property UPLOADING
  115. * @static
  116. * @final
  117. */
  118. UPLOADING : 2,
  119. /**
  120. * File has failed to be uploaded
  121. *
  122. * @property FAILED
  123. * @static
  124. * @final
  125. */
  126. FAILED : 4,
  127. /**
  128. * File has been uploaded successfully
  129. *
  130. * @property DONE
  131. * @static
  132. * @final
  133. */
  134. DONE : 5,
  135. // Error constants used by the Error event
  136. /**
  137. * Generic error for example if an exception is thrown inside Silverlight.
  138. *
  139. * @property GENERIC_ERROR
  140. * @static
  141. * @final
  142. */
  143. GENERIC_ERROR : -100,
  144. /**
  145. * HTTP transport error. For example if the server produces a HTTP status other than 200.
  146. *
  147. * @property HTTP_ERROR
  148. * @static
  149. * @final
  150. */
  151. HTTP_ERROR : -200,
  152. /**
  153. * Generic I/O error. For example if it wasn't possible to open the file stream on local machine.
  154. *
  155. * @property IO_ERROR
  156. * @static
  157. * @final
  158. */
  159. IO_ERROR : -300,
  160. /**
  161. * @property SECURITY_ERROR
  162. * @static
  163. * @final
  164. */
  165. SECURITY_ERROR : -400,
  166. /**
  167. * Initialization error. Will be triggered if no runtime was initialized.
  168. *
  169. * @property INIT_ERROR
  170. * @static
  171. * @final
  172. */
  173. INIT_ERROR : -500,
  174. /**
  175. * File size error. If the user selects a file that is too large it will be blocked and an error of this type will be triggered.
  176. *
  177. * @property FILE_SIZE_ERROR
  178. * @static
  179. * @final
  180. */
  181. FILE_SIZE_ERROR : -600,
  182. /**
  183. * File extension error. If the user selects a file that isn't valid according to the filters setting.
  184. *
  185. * @property FILE_EXTENSION_ERROR
  186. * @static
  187. * @final
  188. */
  189. FILE_EXTENSION_ERROR : -601,
  190. /**
  191. * Duplicate file error. If prevent_duplicates is set to true and user selects the same file again.
  192. *
  193. * @property FILE_DUPLICATE_ERROR
  194. * @static
  195. * @final
  196. */
  197. FILE_DUPLICATE_ERROR : -602,
  198. /**
  199. * Runtime will try to detect if image is proper one. Otherwise will throw this error.
  200. *
  201. * @property IMAGE_FORMAT_ERROR
  202. * @static
  203. * @final
  204. */
  205. IMAGE_FORMAT_ERROR : -700,
  206. /**
  207. * While working on files runtime may run out of memory and will throw this error.
  208. *
  209. * @since 2.1.2
  210. * @property MEMORY_ERROR
  211. * @static
  212. * @final
  213. */
  214. MEMORY_ERROR : -701,
  215. /**
  216. * Each runtime has an upper limit on a dimension of the image it can handle. If bigger, will throw this error.
  217. *
  218. * @property IMAGE_DIMENSIONS_ERROR
  219. * @static
  220. * @final
  221. */
  222. IMAGE_DIMENSIONS_ERROR : -702,
  223. /**
  224. * Mime type lookup table.
  225. *
  226. * @property mimeTypes
  227. * @type Object
  228. * @final
  229. */
  230. mimeTypes : o.mimes,
  231. /**
  232. * In some cases sniffing is the only way around :(
  233. */
  234. ua: o.ua,
  235. /**
  236. * Gets the true type of the built-in object (better version of typeof).
  237. * @credits Angus Croll (http://javascriptweblog.wordpress.com/)
  238. *
  239. * @method typeOf
  240. * @static
  241. * @param {Object} o Object to check.
  242. * @return {String} Object [[Class]]
  243. */
  244. typeOf: o.typeOf,
  245. /**
  246. * Extends the specified object with another object.
  247. *
  248. * @method extend
  249. * @static
  250. * @param {Object} target Object to extend.
  251. * @param {Object..} obj Multiple objects to extend with.
  252. * @return {Object} Same as target, the extended object.
  253. */
  254. extend : o.extend,
  255. /**
  256. * Generates an unique ID. This is 99.99% unique since it takes the current time and 5 random numbers.
  257. * The only way a user would be able to get the same ID is if the two persons at the same exact millisecond manages
  258. * to get 5 the same random numbers between 0-65535 it also uses a counter so each call will be guaranteed to be page unique.
  259. * It's more probable for the earth to be hit with an asteriod. You can also if you want to be 100% sure set the plupload.guidPrefix property
  260. * to an user unique key.
  261. *
  262. * @method guid
  263. * @static
  264. * @return {String} Virtually unique id.
  265. */
  266. guid : o.guid,
  267. /**
  268. * Get array of DOM Elements by their ids.
  269. *
  270. * @method get
  271. * @param {String} id Identifier of the DOM Element
  272. * @return {Array}
  273. */
  274. getAll : function get(ids) {
  275. var els = [], el;
  276. if (plupload.typeOf(ids) !== 'array') {
  277. ids = [ids];
  278. }
  279. var i = ids.length;
  280. while (i--) {
  281. el = plupload.get(ids[i]);
  282. if (el) {
  283. els.push(el);
  284. }
  285. }
  286. return els.length ? els : null;
  287. },
  288. /**
  289. Get DOM element by id
  290. @method get
  291. @param {String} id Identifier of the DOM Element
  292. @return {Node}
  293. */
  294. get: o.get,
  295. /**
  296. * Executes the callback function for each item in array/object. If you return false in the
  297. * callback it will break the loop.
  298. *
  299. * @method each
  300. * @static
  301. * @param {Object} obj Object to iterate.
  302. * @param {function} callback Callback function to execute for each item.
  303. */
  304. each : o.each,
  305. /**
  306. * Returns the absolute x, y position of an Element. The position will be returned in a object with x, y fields.
  307. *
  308. * @method getPos
  309. * @static
  310. * @param {Element} node HTML element or element id to get x, y position from.
  311. * @param {Element} root Optional root element to stop calculations at.
  312. * @return {object} Absolute position of the specified element object with x, y fields.
  313. */
  314. getPos : o.getPos,
  315. /**
  316. * Returns the size of the specified node in pixels.
  317. *
  318. * @method getSize
  319. * @static
  320. * @param {Node} node Node to get the size of.
  321. * @return {Object} Object with a w and h property.
  322. */
  323. getSize : o.getSize,
  324. /**
  325. * Encodes the specified string.
  326. *
  327. * @method xmlEncode
  328. * @static
  329. * @param {String} s String to encode.
  330. * @return {String} Encoded string.
  331. */
  332. xmlEncode : function(str) {
  333. var xmlEncodeChars = {'<' : 'lt', '>' : 'gt', '&' : 'amp', '"' : 'quot', '\'' : '#39'}, xmlEncodeRegExp = /[<>&\"\']/g;
  334. return str ? ('' + str).replace(xmlEncodeRegExp, function(chr) {
  335. return xmlEncodeChars[chr] ? '&' + xmlEncodeChars[chr] + ';' : chr;
  336. }) : str;
  337. },
  338. /**
  339. * Forces anything into an array.
  340. *
  341. * @method toArray
  342. * @static
  343. * @param {Object} obj Object with length field.
  344. * @return {Array} Array object containing all items.
  345. */
  346. toArray : o.toArray,
  347. /**
  348. * Find an element in array and return its index if present, otherwise return -1.
  349. *
  350. * @method inArray
  351. * @static
  352. * @param {mixed} needle Element to find
  353. * @param {Array} array
  354. * @return {Int} Index of the element, or -1 if not found
  355. */
  356. inArray : o.inArray,
  357. /**
  358. * Extends the language pack object with new items.
  359. *
  360. * @method addI18n
  361. * @static
  362. * @param {Object} pack Language pack items to add.
  363. * @return {Object} Extended language pack object.
  364. */
  365. addI18n : o.addI18n,
  366. /**
  367. * Translates the specified string by checking for the english string in the language pack lookup.
  368. *
  369. * @method translate
  370. * @static
  371. * @param {String} str String to look for.
  372. * @return {String} Translated string or the input string if it wasn't found.
  373. */
  374. translate : o.translate,
  375. /**
  376. * Checks if object is empty.
  377. *
  378. * @method isEmptyObj
  379. * @static
  380. * @param {Object} obj Object to check.
  381. * @return {Boolean}
  382. */
  383. isEmptyObj : o.isEmptyObj,
  384. /**
  385. * Checks if specified DOM element has specified class.
  386. *
  387. * @method hasClass
  388. * @static
  389. * @param {Object} obj DOM element like object to add handler to.
  390. * @param {String} name Class name
  391. */
  392. hasClass : o.hasClass,
  393. /**
  394. * Adds specified className to specified DOM element.
  395. *
  396. * @method addClass
  397. * @static
  398. * @param {Object} obj DOM element like object to add handler to.
  399. * @param {String} name Class name
  400. */
  401. addClass : o.addClass,
  402. /**
  403. * Removes specified className from specified DOM element.
  404. *
  405. * @method removeClass
  406. * @static
  407. * @param {Object} obj DOM element like object to add handler to.
  408. * @param {String} name Class name
  409. */
  410. removeClass : o.removeClass,
  411. /**
  412. * Returns a given computed style of a DOM element.
  413. *
  414. * @method getStyle
  415. * @static
  416. * @param {Object} obj DOM element like object.
  417. * @param {String} name Style you want to get from the DOM element
  418. */
  419. getStyle : o.getStyle,
  420. /**
  421. * Adds an event handler to the specified object and store reference to the handler
  422. * in objects internal Plupload registry (@see removeEvent).
  423. *
  424. * @method addEvent
  425. * @static
  426. * @param {Object} obj DOM element like object to add handler to.
  427. * @param {String} name Name to add event listener to.
  428. * @param {Function} callback Function to call when event occurs.
  429. * @param {String} (optional) key that might be used to add specifity to the event record.
  430. */
  431. addEvent : o.addEvent,
  432. /**
  433. * Remove event handler from the specified object. If third argument (callback)
  434. * is not specified remove all events with the specified name.
  435. *
  436. * @method removeEvent
  437. * @static
  438. * @param {Object} obj DOM element to remove event listener(s) from.
  439. * @param {String} name Name of event listener to remove.
  440. * @param {Function|String} (optional) might be a callback or unique key to match.
  441. */
  442. removeEvent: o.removeEvent,
  443. /**
  444. * Remove all kind of events from the specified object
  445. *
  446. * @method removeAllEvents
  447. * @static
  448. * @param {Object} obj DOM element to remove event listeners from.
  449. * @param {String} (optional) unique key to match, when removing events.
  450. */
  451. removeAllEvents: o.removeAllEvents,
  452. /**
  453. * Cleans the specified name from national characters (diacritics). The result will be a name with only a-z, 0-9 and _.
  454. *
  455. * @method cleanName
  456. * @static
  457. * @param {String} s String to clean up.
  458. * @return {String} Cleaned string.
  459. */
  460. cleanName : function(name) {
  461. var i, lookup;
  462. // Replace diacritics
  463. lookup = [
  464. /[\300-\306]/g, 'A', /[\340-\346]/g, 'a',
  465. /\307/g, 'C', /\347/g, 'c',
  466. /[\310-\313]/g, 'E', /[\350-\353]/g, 'e',
  467. /[\314-\317]/g, 'I', /[\354-\357]/g, 'i',
  468. /\321/g, 'N', /\361/g, 'n',
  469. /[\322-\330]/g, 'O', /[\362-\370]/g, 'o',
  470. /[\331-\334]/g, 'U', /[\371-\374]/g, 'u'
  471. ];
  472. for (i = 0; i < lookup.length; i += 2) {
  473. name = name.replace(lookup[i], lookup[i + 1]);
  474. }
  475. // Replace whitespace
  476. name = name.replace(/\s+/g, '_');
  477. // Remove anything else
  478. name = name.replace(/[^a-z0-9_\-\.]+/gi, '');
  479. return name;
  480. },
  481. /**
  482. * Builds a full url out of a base URL and an object with items to append as query string items.
  483. *
  484. * @method buildUrl
  485. * @static
  486. * @param {String} url Base URL to append query string items to.
  487. * @param {Object} items Name/value object to serialize as a querystring.
  488. * @return {String} String with url + serialized query string items.
  489. */
  490. buildUrl : function(url, items) {
  491. var query = '';
  492. plupload.each(items, function(value, name) {
  493. query += (query ? '&' : '') + encodeURIComponent(name) + '=' + encodeURIComponent(value);
  494. });
  495. if (query) {
  496. url += (url.indexOf('?') > 0 ? '&' : '?') + query;
  497. }
  498. return url;
  499. },
  500. /**
  501. * Formats the specified number as a size string for example 1024 becomes 1 KB.
  502. *
  503. * @method formatSize
  504. * @static
  505. * @param {Number} size Size to format as string.
  506. * @return {String} Formatted size string.
  507. */
  508. formatSize : function(size) {
  509. if (size === undef || /\D/.test(size)) {
  510. return plupload.translate('N/A');
  511. }
  512. function round(num, precision) {
  513. return Math.round(num * Math.pow(10, precision)) / Math.pow(10, precision);
  514. }
  515. var boundary = Math.pow(1024, 4);
  516. // TB
  517. if (size > boundary) {
  518. return round(size / boundary, 1) + " " + plupload.translate('tb');
  519. }
  520. // GB
  521. if (size > (boundary/=1024)) {
  522. return round(size / boundary, 1) + " " + plupload.translate('gb');
  523. }
  524. // MB
  525. if (size > (boundary/=1024)) {
  526. return round(size / boundary, 1) + " " + plupload.translate('mb');
  527. }
  528. // KB
  529. if (size > 1024) {
  530. return Math.round(size / 1024) + " " + plupload.translate('kb');
  531. }
  532. return size + " " + plupload.translate('b');
  533. },
  534. /**
  535. * Parses the specified size string into a byte value. For example 10kb becomes 10240.
  536. *
  537. * @method parseSize
  538. * @static
  539. * @param {String|Number} size String to parse or number to just pass through.
  540. * @return {Number} Size in bytes.
  541. */
  542. parseSize : o.parseSizeStr,
  543. /**
  544. * A way to predict what runtime will be choosen in the current environment with the
  545. * specified settings.
  546. *
  547. * @method predictRuntime
  548. * @static
  549. * @param {Object|String} config Plupload settings to check
  550. * @param {String} [runtimes] Comma-separated list of runtimes to check against
  551. * @return {String} Type of compatible runtime
  552. */
  553. predictRuntime : function(config, runtimes) {
  554. var up, runtime;
  555. up = new plupload.Uploader(config);
  556. runtime = o.Runtime.thatCan(up.getOption().required_features, runtimes || config.runtimes);
  557. up.destroy();
  558. return runtime;
  559. },
  560. /**
  561. * Registers a filter that will be executed for each file added to the queue.
  562. * If callback returns false, file will not be added.
  563. *
  564. * Callback receives two arguments: a value for the filter as it was specified in settings.filters
  565. * and a file to be filtered. Callback is executed in the context of uploader instance.
  566. *
  567. * @method addFileFilter
  568. * @static
  569. * @param {String} name Name of the filter by which it can be referenced in settings.filters
  570. * @param {String} cb Callback - the actual routine that every added file must pass
  571. */
  572. addFileFilter: function(name, cb) {
  573. fileFilters[name] = cb;
  574. }
  575. };
  576. plupload.addFileFilter('mime_types', function(filters, file, cb) {
  577. if (filters.length && !filters.regexp.test(file.name)) {
  578. this.trigger('Error', {
  579. code : plupload.FILE_EXTENSION_ERROR,
  580. message : plupload.translate('File extension error.'),
  581. file : file
  582. });
  583. cb(false);
  584. } else {
  585. cb(true);
  586. }
  587. });
  588. plupload.addFileFilter('max_file_size', function(maxSize, file, cb) {
  589. var undef;
  590. maxSize = plupload.parseSize(maxSize);
  591. // Invalid file size
  592. if (file.size !== undef && maxSize && file.size > maxSize) {
  593. this.trigger('Error', {
  594. code : plupload.FILE_SIZE_ERROR,
  595. message : plupload.translate('File size error.'),
  596. file : file
  597. });
  598. cb(false);
  599. } else {
  600. cb(true);
  601. }
  602. });
  603. plupload.addFileFilter('prevent_duplicates', function(value, file, cb) {
  604. if (value) {
  605. var ii = this.files.length;
  606. while (ii--) {
  607. // Compare by name and size (size might be 0 or undefined, but still equivalent for both)
  608. if (file.name === this.files[ii].name && file.size === this.files[ii].size) {
  609. this.trigger('Error', {
  610. code : plupload.FILE_DUPLICATE_ERROR,
  611. message : plupload.translate('Duplicate file error.'),
  612. file : file
  613. });
  614. cb(false);
  615. return;
  616. }
  617. }
  618. }
  619. cb(true);
  620. });
  621. /**
  622. @class Uploader
  623. @constructor
  624. @param {Object} settings For detailed information about each option check documentation.
  625. @param {String|DOMElement} settings.browse_button id of the DOM element or DOM element itself to use as file dialog trigger.
  626. @param {String} settings.url URL of the server-side upload handler.
  627. @param {Number|String} [settings.chunk_size=0] Chunk size in bytes to slice the file into. Shorcuts with b, kb, mb, gb, tb suffixes also supported. `e.g. 204800 or "204800b" or "200kb"`. By default - disabled.
  628. @param {Boolean} [settings.send_chunk_number=true] Whether to send chunks and chunk numbers, or total and offset bytes.
  629. @param {String|DOMElement} [settings.container] id of the DOM element or DOM element itself that will be used to wrap uploader structures. Defaults to immediate parent of the `browse_button` element.
  630. @param {String|DOMElement} [settings.drop_element] id of the DOM element or DOM element itself to use as a drop zone for Drag-n-Drop.
  631. @param {String} [settings.file_data_name="file"] Name for the file field in Multipart formated message.
  632. @param {Object} [settings.filters={}] Set of file type filters.
  633. @param {Array} [settings.filters.mime_types=[]] List of file types to accept, each one defined by title and list of extensions. `e.g. {title : "Image files", extensions : "jpg,jpeg,gif,png"}`. Dispatches `plupload.FILE_EXTENSION_ERROR`
  634. @param {String|Number} [settings.filters.max_file_size=0] Maximum file size that the user can pick, in bytes. Optionally supports b, kb, mb, gb, tb suffixes. `e.g. "10mb" or "1gb"`. By default - not set. Dispatches `plupload.FILE_SIZE_ERROR`.
  635. @param {Boolean} [settings.filters.prevent_duplicates=false] Do not let duplicates into the queue. Dispatches `plupload.FILE_DUPLICATE_ERROR`.
  636. @param {String} [settings.flash_swf_url] URL of the Flash swf.
  637. @param {Object} [settings.headers] Custom headers to send with the upload. Hash of name/value pairs.
  638. @param {Number} [settings.max_retries=0] How many times to retry the chunk or file, before triggering Error event.
  639. @param {Boolean} [settings.multipart=true] Whether to send file and additional parameters as Multipart formated message.
  640. @param {Object} [settings.multipart_params] Hash of key/value pairs to send with every file upload.
  641. @param {Boolean} [settings.multi_selection=true] Enable ability to select multiple files at once in file dialog.
  642. @param {String|Object} [settings.required_features] Either comma-separated list or hash of required features that chosen runtime should absolutely possess.
  643. @param {Object} [settings.resize] Enable resizng of images on client-side. Applies to `image/jpeg` and `image/png` only. `e.g. {width : 200, height : 200, quality : 90, crop: true}`
  644. @param {Number} [settings.resize.width] If image is bigger, it will be resized.
  645. @param {Number} [settings.resize.height] If image is bigger, it will be resized.
  646. @param {Number} [settings.resize.quality=90] Compression quality for jpegs (1-100).
  647. @param {Boolean} [settings.resize.crop=false] Whether to crop images to exact dimensions. By default they will be resized proportionally.
  648. @param {String} [settings.runtimes="html5,flash,silverlight,html4"] Comma separated list of runtimes, that Plupload will try in turn, moving to the next if previous fails.
  649. @param {String} [settings.silverlight_xap_url] URL of the Silverlight xap.
  650. @param {Boolean} [settings.unique_names=false] If true will generate unique filenames for uploaded files.
  651. @param {Boolean} [settings.send_file_name=true] Whether to send file name as additional argument - 'name' (required for chunked uploads and some other cases where file name cannot be sent via normal ways).
  652. */
  653. plupload.Uploader = function(options) {
  654. /**
  655. Fires when the current RunTime has been initialized.
  656. @event Init
  657. @param {plupload.Uploader} uploader Uploader instance sending the event.
  658. */
  659. /**
  660. Fires after the init event incase you need to perform actions there.
  661. @event PostInit
  662. @param {plupload.Uploader} uploader Uploader instance sending the event.
  663. */
  664. /**
  665. Fires when the option is changed in via uploader.setOption().
  666. @event OptionChanged
  667. @since 2.1
  668. @param {plupload.Uploader} uploader Uploader instance sending the event.
  669. @param {String} name Name of the option that was changed
  670. @param {Mixed} value New value for the specified option
  671. @param {Mixed} oldValue Previous value of the option
  672. */
  673. /**
  674. Fires when the silverlight/flash or other shim needs to move.
  675. @event Refresh
  676. @param {plupload.Uploader} uploader Uploader instance sending the event.
  677. */
  678. /**
  679. Fires when the overall state is being changed for the upload queue.
  680. @event StateChanged
  681. @param {plupload.Uploader} uploader Uploader instance sending the event.
  682. */
  683. /**
  684. Fires when browse_button is clicked and browse dialog shows.
  685. @event Browse
  686. @since 2.1.2
  687. @param {plupload.Uploader} uploader Uploader instance sending the event.
  688. */
  689. /**
  690. Fires for every filtered file before it is added to the queue.
  691. @event FileFiltered
  692. @since 2.1
  693. @param {plupload.Uploader} uploader Uploader instance sending the event.
  694. @param {plupload.File} file Another file that has to be added to the queue.
  695. */
  696. /**
  697. Fires when the file queue is changed. In other words when files are added/removed to the files array of the uploader instance.
  698. @event QueueChanged
  699. @param {plupload.Uploader} uploader Uploader instance sending the event.
  700. */
  701. /**
  702. Fires after files were filtered and added to the queue.
  703. @event FilesAdded
  704. @param {plupload.Uploader} uploader Uploader instance sending the event.
  705. @param {Array} files Array of file objects that were added to queue by the user.
  706. */
  707. /**
  708. Fires when file is removed from the queue.
  709. @event FilesRemoved
  710. @param {plupload.Uploader} uploader Uploader instance sending the event.
  711. @param {Array} files Array of files that got removed.
  712. */
  713. /**
  714. Fires just before a file is uploaded. Can be used to cancel the upload for the specified file
  715. by returning false from the handler.
  716. @event BeforeUpload
  717. @param {plupload.Uploader} uploader Uploader instance sending the event.
  718. @param {plupload.File} file File to be uploaded.
  719. */
  720. /**
  721. Fires when a file is to be uploaded by the runtime.
  722. @event UploadFile
  723. @param {plupload.Uploader} uploader Uploader instance sending the event.
  724. @param {plupload.File} file File to be uploaded.
  725. */
  726. /**
  727. Fires while a file is being uploaded. Use this event to update the current file upload progress.
  728. @event UploadProgress
  729. @param {plupload.Uploader} uploader Uploader instance sending the event.
  730. @param {plupload.File} file File that is currently being uploaded.
  731. */
  732. /**
  733. Fires when file chunk is uploaded.
  734. @event ChunkUploaded
  735. @param {plupload.Uploader} uploader Uploader instance sending the event.
  736. @param {plupload.File} file File that the chunk was uploaded for.
  737. @param {Object} result Object with response properties.
  738. @param {Number} result.offset The amount of bytes the server has received so far, including this chunk.
  739. @param {Number} result.total The size of the file.
  740. @param {String} result.response The response body sent by the server.
  741. @param {Number} result.status The HTTP status code sent by the server.
  742. @param {String} result.responseHeaders All the response headers as a single string.
  743. */
  744. /**
  745. Fires when a file is successfully uploaded.
  746. @event FileUploaded
  747. @param {plupload.Uploader} uploader Uploader instance sending the event.
  748. @param {plupload.File} file File that was uploaded.
  749. @param {Object} result Object with response properties.
  750. @param {String} result.response The response body sent by the server.
  751. @param {Number} result.status The HTTP status code sent by the server.
  752. @param {String} result.responseHeaders All the response headers as a single string.
  753. */
  754. /**
  755. Fires when all files in a queue are uploaded.
  756. @event UploadComplete
  757. @param {plupload.Uploader} uploader Uploader instance sending the event.
  758. @param {Array} files Array of file objects that was added to queue/selected by the user.
  759. */
  760. /**
  761. Fires when a error occurs.
  762. @event Error
  763. @param {plupload.Uploader} uploader Uploader instance sending the event.
  764. @param {Object} error Contains code, message and sometimes file and other details.
  765. @param {Number} error.code The plupload error code.
  766. @param {String} error.message Description of the error (uses i18n).
  767. */
  768. /**
  769. Fires when destroy method is called.
  770. @event Destroy
  771. @param {plupload.Uploader} uploader Uploader instance sending the event.
  772. */
  773. var uid = plupload.guid()
  774. , settings
  775. , files = []
  776. , preferred_caps = {}
  777. , fileInputs = []
  778. , fileDrops = []
  779. , startTime
  780. , total
  781. , disabled = false
  782. , xhr
  783. ;
  784. // Private methods
  785. function uploadNext() {
  786. var file, count = 0, i;
  787. if (this.state == plupload.STARTED) {
  788. // Find first QUEUED file
  789. for (i = 0; i < files.length; i++) {
  790. if (!file && files[i].status == plupload.QUEUED) {
  791. file = files[i];
  792. if (this.trigger("BeforeUpload", file)) {
  793. file.status = plupload.UPLOADING;
  794. this.trigger("UploadFile", file);
  795. }
  796. } else {
  797. count++;
  798. }
  799. }
  800. // All files are DONE or FAILED
  801. if (count == files.length) {
  802. if (this.state !== plupload.STOPPED) {
  803. this.state = plupload.STOPPED;
  804. this.trigger("StateChanged");
  805. }
  806. this.trigger("UploadComplete", files);
  807. }
  808. }
  809. }
  810. function calcFile(file) {
  811. file.percent = file.size > 0 ? Math.ceil(file.loaded / file.size * 100) : 100;
  812. calc();
  813. }
  814. function calc() {
  815. var i, file;
  816. // Reset stats
  817. total.reset();
  818. // Check status, size, loaded etc on all files
  819. for (i = 0; i < files.length; i++) {
  820. file = files[i];
  821. if (file.size !== undef) {
  822. // We calculate totals based on original file size
  823. total.size += file.origSize;
  824. // Since we cannot predict file size after resize, we do opposite and
  825. // interpolate loaded amount to match magnitude of total
  826. total.loaded += file.loaded * file.origSize / file.size;
  827. } else {
  828. total.size = undef;
  829. }
  830. if (file.status == plupload.DONE) {
  831. total.uploaded++;
  832. } else if (file.status == plupload.FAILED) {
  833. total.failed++;
  834. } else {
  835. total.queued++;
  836. }
  837. }
  838. // If we couldn't calculate a total file size then use the number of files to calc percent
  839. if (total.size === undef) {
  840. total.percent = files.length > 0 ? Math.ceil(total.uploaded / files.length * 100) : 0;
  841. } else {
  842. total.bytesPerSec = Math.ceil(total.loaded / ((+new Date() - startTime || 1) / 1000.0));
  843. total.percent = total.size > 0 ? Math.ceil(total.loaded / total.size * 100) : 0;
  844. }
  845. }
  846. function getRUID() {
  847. var ctrl = fileInputs[0] || fileDrops[0];
  848. if (ctrl) {
  849. return ctrl.getRuntime().uid;
  850. }
  851. return false;
  852. }
  853. function runtimeCan(file, cap) {
  854. if (file.ruid) {
  855. var info = o.Runtime.getInfo(file.ruid);
  856. if (info) {
  857. return info.can(cap);
  858. }
  859. }
  860. return false;
  861. }
  862. function bindEventListeners() {
  863. this.bind('FilesAdded FilesRemoved', function(up) {
  864. up.trigger('QueueChanged');
  865. up.refresh();
  866. });
  867. this.bind('CancelUpload', onCancelUpload);
  868. this.bind('BeforeUpload', onBeforeUpload);
  869. this.bind('UploadFile', onUploadFile);
  870. this.bind('UploadProgress', onUploadProgress);
  871. this.bind('StateChanged', onStateChanged);
  872. this.bind('QueueChanged', calc);
  873. this.bind('Error', onError);
  874. this.bind('FileUploaded', onFileUploaded);
  875. this.bind('Destroy', onDestroy);
  876. }
  877. function initControls(settings, cb) {
  878. var self = this, inited = 0, queue = [];
  879. // common settings
  880. var options = {
  881. runtime_order: settings.runtimes,
  882. required_caps: settings.required_features,
  883. preferred_caps: preferred_caps,
  884. swf_url: settings.flash_swf_url,
  885. xap_url: settings.silverlight_xap_url
  886. };
  887. // add runtime specific options if any
  888. plupload.each(settings.runtimes.split(/\s*,\s*/), function(runtime) {
  889. if (settings[runtime]) {
  890. options[runtime] = settings[runtime];
  891. }
  892. });
  893. // initialize file pickers - there can be many
  894. if (settings.browse_button) {
  895. plupload.each(settings.browse_button, function(el) {
  896. queue.push(function(cb) {
  897. var fileInput = new o.FileInput(plupload.extend({}, options, {
  898. accept: settings.filters.mime_types,
  899. name: settings.file_data_name,
  900. multiple: settings.multi_selection,
  901. container: settings.container,
  902. browse_button: el
  903. }));
  904. fileInput.onready = function() {
  905. var info = o.Runtime.getInfo(this.ruid);
  906. // for backward compatibility
  907. o.extend(self.features, {
  908. chunks: info.can('slice_blob'),
  909. multipart: info.can('send_multipart'),
  910. multi_selection: info.can('select_multiple')
  911. });
  912. inited++;
  913. fileInputs.push(this);
  914. cb();
  915. };
  916. fileInput.onchange = function() {
  917. self.addFile(this.files);
  918. };
  919. fileInput.bind('mouseenter mouseleave mousedown mouseup', function(e) {
  920. if (!disabled) {
  921. if (settings.browse_button_hover) {
  922. if ('mouseenter' === e.type) {
  923. o.addClass(el, settings.browse_button_hover);
  924. } else if ('mouseleave' === e.type) {
  925. o.removeClass(el, settings.browse_button_hover);
  926. }
  927. }
  928. if (settings.browse_button_active) {
  929. if ('mousedown' === e.type) {
  930. o.addClass(el, settings.browse_button_active);
  931. } else if ('mouseup' === e.type) {
  932. o.removeClass(el, settings.browse_button_active);
  933. }
  934. }
  935. }
  936. });
  937. fileInput.bind('mousedown', function() {
  938. self.trigger('Browse');
  939. });
  940. fileInput.bind('error runtimeerror', function() {
  941. fileInput = null;
  942. cb();
  943. });
  944. fileInput.init();
  945. });
  946. });
  947. }
  948. // initialize drop zones
  949. if (settings.drop_element) {
  950. plupload.each(settings.drop_element, function(el) {
  951. queue.push(function(cb) {
  952. var fileDrop = new o.FileDrop(plupload.extend({}, options, {
  953. drop_zone: el
  954. }));
  955. fileDrop.onready = function() {
  956. var info = o.Runtime.getInfo(this.ruid);
  957. // for backward compatibility
  958. o.extend(self.features, {
  959. chunks: info.can('slice_blob'),
  960. multipart: info.can('send_multipart'),
  961. dragdrop: info.can('drag_and_drop')
  962. });
  963. inited++;
  964. fileDrops.push(this);
  965. cb();
  966. };
  967. fileDrop.ondrop = function() {
  968. self.addFile(this.files);
  969. };
  970. fileDrop.bind('error runtimeerror', function() {
  971. fileDrop = null;
  972. cb();
  973. });
  974. fileDrop.init();
  975. });
  976. });
  977. }
  978. o.inSeries(queue, function() {
  979. if (typeof(cb) === 'function') {
  980. cb(inited);
  981. }
  982. });
  983. }
  984. function resizeImage(blob, params, cb) {
  985. var img = new o.Image();
  986. try {
  987. img.onload = function() {
  988. // no manipulation required if...
  989. if (params.width > this.width &&
  990. params.height > this.height &&
  991. params.quality === undef &&
  992. params.preserve_headers &&
  993. !params.crop
  994. ) {
  995. this.destroy();
  996. return cb(blob);
  997. }
  998. // otherwise downsize
  999. img.downsize(params.width, params.height, params.crop, params.preserve_headers);
  1000. };
  1001. img.onresize = function() {
  1002. cb(this.getAsBlob(blob.type, params.quality));
  1003. this.destroy();
  1004. };
  1005. img.onerror = function() {
  1006. cb(blob);
  1007. };
  1008. img.load(blob);
  1009. } catch(ex) {
  1010. cb(blob);
  1011. }
  1012. }
  1013. function setOption(option, value, init) {
  1014. var self = this, reinitRequired = false;
  1015. function _setOption(option, value, init) {
  1016. var oldValue = settings[option];
  1017. switch (option) {
  1018. case 'max_file_size':
  1019. if (option === 'max_file_size') {
  1020. settings.max_file_size = settings.filters.max_file_size = value;
  1021. }
  1022. break;
  1023. case 'chunk_size':
  1024. if (value = plupload.parseSize(value)) {
  1025. settings[option] = value;
  1026. settings.send_file_name = true;
  1027. }
  1028. break;
  1029. case 'multipart':
  1030. settings[option] = value;
  1031. if (!value) {
  1032. settings.send_file_name = true;
  1033. }
  1034. break;
  1035. case 'unique_names':
  1036. settings[option] = value;
  1037. if (value) {
  1038. settings.send_file_name = true;
  1039. }
  1040. break;
  1041. case 'filters':
  1042. // for sake of backward compatibility
  1043. if (plupload.typeOf(value) === 'array') {
  1044. value = {
  1045. mime_types: value
  1046. };
  1047. }
  1048. if (init) {
  1049. plupload.extend(settings.filters, value);
  1050. } else {
  1051. settings.filters = value;
  1052. }
  1053. // if file format filters are being updated, regenerate the matching expressions
  1054. if (value.mime_types) {
  1055. settings.filters.mime_types.regexp = (function(filters) {
  1056. var extensionsRegExp = [];
  1057. plupload.each(filters, function(filter) {
  1058. plupload.each(filter.extensions.split(/,/), function(ext) {
  1059. if (/^\s*\*\s*$/.test(ext)) {
  1060. extensionsRegExp.push('\\.*');
  1061. } else {
  1062. extensionsRegExp.push('\\.' + ext.replace(new RegExp('[' + ('/^$.*+?|()[]{}\\'.replace(/./g, '\\$&')) + ']', 'g'), '\\$&'));
  1063. }
  1064. });
  1065. });
  1066. return new RegExp('(' + extensionsRegExp.join('|') + ')$', 'i');
  1067. }(settings.filters.mime_types));
  1068. }
  1069. break;
  1070. case 'resize':
  1071. if (init) {
  1072. plupload.extend(settings.resize, value, {
  1073. enabled: true
  1074. });
  1075. } else {
  1076. settings.resize = value;
  1077. }
  1078. break;
  1079. case 'prevent_duplicates':
  1080. settings.prevent_duplicates = settings.filters.prevent_duplicates = !!value;
  1081. break;
  1082. // options that require reinitialisation
  1083. case 'container':
  1084. case 'browse_button':
  1085. case 'drop_element':
  1086. value = 'container' === option
  1087. ? plupload.get(value)
  1088. : plupload.getAll(value)
  1089. ;
  1090. case 'runtimes':
  1091. case 'multi_selection':
  1092. case 'flash_swf_url':
  1093. case 'silverlight_xap_url':
  1094. settings[option] = value;
  1095. if (!init) {
  1096. reinitRequired = true;
  1097. }
  1098. break;
  1099. default:
  1100. settings[option] = value;
  1101. }
  1102. if (!init) {
  1103. self.trigger('OptionChanged', option, value, oldValue);
  1104. }
  1105. }
  1106. if (typeof(option) === 'object') {
  1107. plupload.each(option, function(value, option) {
  1108. _setOption(option, value, init);
  1109. });
  1110. } else {
  1111. _setOption(option, value, init);
  1112. }
  1113. if (init) {
  1114. // Normalize the list of required capabilities
  1115. settings.required_features = normalizeCaps(plupload.extend({}, settings));
  1116. // Come up with the list of capabilities that can affect default mode in a multi-mode runtimes
  1117. preferred_caps = normalizeCaps(plupload.extend({}, settings, {
  1118. required_features: true
  1119. }));
  1120. } else if (reinitRequired) {
  1121. self.trigger('Destroy');
  1122. initControls.call(self, settings, function(inited) {
  1123. if (inited) {
  1124. self.runtime = o.Runtime.getInfo(getRUID()).type;
  1125. self.trigger('Init', { runtime: self.runtime });
  1126. self.trigger('PostInit');
  1127. } else {
  1128. self.trigger('Error', {
  1129. code : plupload.INIT_ERROR,
  1130. message : plupload.translate('Init error.')
  1131. });
  1132. }
  1133. });
  1134. }
  1135. }
  1136. // Internal event handlers
  1137. function onBeforeUpload(up, file) {
  1138. // Generate unique target filenames
  1139. if (up.settings.unique_names) {
  1140. var matches = file.name.match(/\.([^.]+)$/), ext = "part";
  1141. if (matches) {
  1142. ext = matches[1];
  1143. }
  1144. file.target_name = file.id + '.' + ext;
  1145. }
  1146. }
  1147. function onUploadFile(up, file) {
  1148. var url = up.settings.url
  1149. , chunkSize = up.settings.chunk_size
  1150. , retries = up.settings.max_retries
  1151. , features = up.features
  1152. , offset = 0
  1153. , blob
  1154. ;
  1155. // make sure we start at a predictable offset
  1156. if (file.loaded) {
  1157. offset = file.loaded = chunkSize ? chunkSize * Math.floor(file.loaded / chunkSize) : 0;
  1158. }
  1159. function handleError() {
  1160. if (retries-- > 0) {
  1161. delay(uploadNextChunk, 1000);
  1162. } else {
  1163. file.loaded = offset; // reset all progress
  1164. up.trigger('Error', {
  1165. code : plupload.HTTP_ERROR,
  1166. message : plupload.translate('HTTP Error.'),
  1167. file : file,
  1168. response : xhr.responseText,
  1169. status : xhr.status,
  1170. responseHeaders: xhr.getAllResponseHeaders()
  1171. });
  1172. }
  1173. }
  1174. function uploadNextChunk() {
  1175. var chunkBlob, formData, args = {}, curChunkSize;
  1176. // make sure that file wasn't cancelled and upload is not stopped in general
  1177. if (file.status !== plupload.UPLOADING || up.state === plupload.STOPPED) {
  1178. return;
  1179. }
  1180. // send additional 'name' parameter only if required
  1181. if (up.settings.send_file_name) {
  1182. args.name = file.target_name || file.name;
  1183. }
  1184. if (chunkSize && features.chunks && blob.size > chunkSize) { // blob will be of type string if it was loaded in memory
  1185. curChunkSize = Math.min(chunkSize, blob.size - offset);
  1186. chunkBlob = blob.slice(offset, offset + curChunkSize);
  1187. } else {
  1188. curChunkSize = blob.size;
  1189. chunkBlob = blob;
  1190. }
  1191. // If chunking is enabled add corresponding args, no matter if file is bigger than chunk or smaller
  1192. if (chunkSize && features.chunks) {
  1193. // Setup query string arguments
  1194. if (up.settings.send_chunk_number) {
  1195. args.chunk = Math.ceil(offset / chunkSize);
  1196. args.chunks = Math.ceil(blob.size / chunkSize);
  1197. } else { // keep support for experimental chunk format, just in case
  1198. args.offset = offset;
  1199. args.total = blob.size;
  1200. }
  1201. }
  1202. xhr = new o.XMLHttpRequest();
  1203. // Do we have upload progress support
  1204. if (xhr.upload) {
  1205. xhr.upload.onprogress = function(e) {
  1206. file.loaded = Math.min(file.size, offset + e.loaded);
  1207. up.trigger('UploadProgress', file);
  1208. };
  1209. }
  1210. xhr.onload = function() {
  1211. // check if upload made itself through
  1212. if (xhr.status >= 400) {
  1213. handleError();
  1214. return;
  1215. }
  1216. retries = up.settings.max_retries; // reset the counter
  1217. // Handle chunk response
  1218. if (curChunkSize < blob.size) {
  1219. chunkBlob.destroy();
  1220. offset += curChunkSize;
  1221. file.loaded = Math.min(offset, blob.size);
  1222. up.trigger('ChunkUploaded', file, {
  1223. offset : file.loaded,
  1224. total : blob.size,
  1225. response : xhr.responseText,
  1226. status : xhr.status,
  1227. responseHeaders: xhr.getAllResponseHeaders()
  1228. });
  1229. // stock Android browser doesn't fire upload progress events, but in chunking mode we can fake them
  1230. if (o.Env.browser === 'Android Browser') {
  1231. // doesn't harm in general, but is not required anywhere else
  1232. up.trigger('UploadProgress', file);
  1233. }
  1234. } else {
  1235. file.loaded = file.size;
  1236. }
  1237. chunkBlob = formData = null; // Free memory
  1238. // Check if file is uploaded
  1239. if (!offset || offset >= blob.size) {
  1240. // If file was modified, destory the copy
  1241. if (file.size != file.origSize) {
  1242. blob.destroy();
  1243. blob = null;
  1244. }
  1245. up.trigger('UploadProgress', file);
  1246. file.status = plupload.DONE;
  1247. up.trigger('FileUploaded', file, {
  1248. response : xhr.responseText,
  1249. status : xhr.status,
  1250. responseHeaders: xhr.getAllResponseHeaders()
  1251. });
  1252. } else {
  1253. // Still chunks left
  1254. delay(uploadNextChunk, 1); // run detached, otherwise event handlers interfere
  1255. }
  1256. };
  1257. xhr.onerror = function() {
  1258. handleError();
  1259. };
  1260. xhr.onloadend = function() {
  1261. this.destroy();
  1262. xhr = null;
  1263. };
  1264. // Build multipart request
  1265. if (up.settings.multipart && features.multipart) {
  1266. xhr.open("post", url, true);
  1267. // Set custom headers
  1268. plupload.each(up.settings.headers, function(value, name) {
  1269. xhr.setRequestHeader(name, value);
  1270. });
  1271. formData = new o.FormData();
  1272. // Add multipart params
  1273. plupload.each(plupload.extend(args, up.settings.multipart_params), function(value, name) {
  1274. formData.append(name, value);
  1275. });
  1276. // Add file and send it
  1277. formData.append(up.settings.file_data_name, chunkBlob);
  1278. xhr.send(formData, {
  1279. runtime_order: up.settings.runtimes,
  1280. required_caps: up.settings.required_features,
  1281. preferred_caps: preferred_caps,
  1282. swf_url: up.settings.flash_swf_url,
  1283. xap_url: up.settings.silverlight_xap_url
  1284. });
  1285. } else {
  1286. // if no multipart, send as binary stream
  1287. url = plupload.buildUrl(up.settings.url, plupload.extend(args, up.settings.multipart_params));
  1288. xhr.open("post", url, true);
  1289. xhr.setRequestHeader('Content-Type', 'application/octet-stream'); // Binary stream header
  1290. // Set custom headers
  1291. plupload.each(up.settings.headers, function(value, name) {
  1292. xhr.setRequestHeader(name, value);
  1293. });
  1294. xhr.send(chunkBlob, {
  1295. runtime_order: up.settings.runtimes,
  1296. required_caps: up.settings.required_features,
  1297. preferred_caps: preferred_caps,
  1298. swf_url: up.settings.flash_swf_url,
  1299. xap_url: up.settings.silverlight_xap_url
  1300. });
  1301. }
  1302. }
  1303. blob = file.getSource();
  1304. // Start uploading chunks
  1305. if (up.settings.resize.enabled && runtimeCan(blob, 'send_binary_string') && !!~o.inArray(blob.type, ['image/jpeg', 'image/png'])) {
  1306. // Resize if required
  1307. resizeImage.call(this, blob, up.settings.resize, function(resizedBlob) {
  1308. blob = resizedBlob;
  1309. file.size = resizedBlob.size;
  1310. uploadNextChunk();
  1311. });
  1312. } else {
  1313. uploadNextChunk();
  1314. }
  1315. }
  1316. function onUploadProgress(up, file) {
  1317. calcFile(file);
  1318. }
  1319. function onStateChanged(up) {
  1320. if (up.state == plupload.STARTED) {
  1321. // Get start time to calculate bps
  1322. startTime = (+new Date());
  1323. } else if (up.state == plupload.STOPPED) {
  1324. // Reset currently uploading files
  1325. for (var i = up.files.length - 1; i >= 0; i--) {
  1326. if (up.files[i].status == plupload.UPLOADING) {
  1327. up.files[i].status = plupload.QUEUED;
  1328. calc();
  1329. }
  1330. }
  1331. }
  1332. }
  1333. function onCancelUpload() {
  1334. if (xhr) {
  1335. xhr.abort();
  1336. }
  1337. }
  1338. function onFileUploaded(up) {
  1339. calc();
  1340. // Upload next file but detach it from the error event
  1341. // since other custom listeners might want to stop the queue
  1342. delay(function() {
  1343. uploadNext.call(up);
  1344. }, 1);
  1345. }
  1346. function onError(up, err) {
  1347. if (err.code === plupload.INIT_ERROR) {
  1348. up.destroy();
  1349. }
  1350. // Set failed status if an error occured on a file
  1351. else if (err.code === plupload.HTTP_ERROR) {
  1352. err.file.status = plupload.FAILED;
  1353. calcFile(err.file);
  1354. // Upload next file but detach it from the error event
  1355. // since other custom listeners might want to stop the queue
  1356. if (up.state == plupload.STARTED) { // upload in progress
  1357. up.trigger('CancelUpload');
  1358. delay(function() {
  1359. uploadNext.call(up);
  1360. }, 1);
  1361. }
  1362. }
  1363. }
  1364. function onDestroy(up) {
  1365. up.stop();
  1366. // Purge the queue
  1367. plupload.each(files, function(file) {
  1368. file.destroy();
  1369. });
  1370. files = [];
  1371. if (fileInputs.length) {
  1372. plupload.each(fileInputs, function(fileInput) {
  1373. fileInput.destroy();
  1374. });
  1375. fileInputs = [];
  1376. }
  1377. if (fileDrops.length) {
  1378. plupload.each(fileDrops, function(fileDrop) {
  1379. fileDrop.destroy();
  1380. });
  1381. fileDrops = [];
  1382. }
  1383. preferred_caps = {};
  1384. disabled = false;
  1385. startTime = xhr = null;
  1386. total.reset();
  1387. }
  1388. // Default settings
  1389. settings = {
  1390. runtimes: o.Runtime.order,
  1391. max_retries: 0,
  1392. chunk_size: 0,
  1393. multipart: true,
  1394. multi_selection: true,
  1395. file_data_name: 'file',
  1396. flash_swf_url: 'js/Moxie.swf',
  1397. silverlight_xap_url: 'js/Moxie.xap',
  1398. filters: {
  1399. mime_types: [],
  1400. prevent_duplicates: false,
  1401. max_file_size: 0
  1402. },
  1403. resize: {
  1404. enabled: false,
  1405. preserve_headers: true,
  1406. crop: false
  1407. },
  1408. send_file_name: true,
  1409. send_chunk_number: true
  1410. };
  1411. setOption.call(this, options, null, true);
  1412. // Inital total state
  1413. total = new plupload.QueueProgress();
  1414. // Add public methods
  1415. plupload.extend(this, {
  1416. /**
  1417. * Unique id for the Uploader instance.
  1418. *
  1419. * @property id
  1420. * @type String
  1421. */
  1422. id : uid,
  1423. uid : uid, // mOxie uses this to differentiate between event targets
  1424. /**
  1425. * Current state of the total uploading progress. This one can either be plupload.STARTED or plupload.STOPPED.
  1426. * These states are controlled by the stop/start methods. The default value is STOPPED.
  1427. *
  1428. * @property state
  1429. * @type Number
  1430. */
  1431. state : plupload.STOPPED,
  1432. /**
  1433. * Map of features that are available for the uploader runtime. Features will be filled
  1434. * before the init event is called, these features can then be used to alter the UI for the end user.
  1435. * Some of the current features that might be in this map is: dragdrop, chunks, jpgresize, pngresize.
  1436. *
  1437. * @property features
  1438. * @type Object
  1439. */
  1440. features : {},
  1441. /**
  1442. * Current runtime name.
  1443. *
  1444. * @property runtime
  1445. * @type String
  1446. */
  1447. runtime : null,
  1448. /**
  1449. * Current upload queue, an array of File instances.
  1450. *
  1451. * @property files
  1452. * @type Array
  1453. * @see plupload.File
  1454. */
  1455. files : files,
  1456. /**
  1457. * Object with name/value settings.
  1458. *
  1459. * @property settings
  1460. * @type Object
  1461. */
  1462. settings : settings,
  1463. /**
  1464. * Total progess information. How many files has been uploaded, total percent etc.
  1465. *
  1466. * @property total
  1467. * @type plupload.QueueProgress
  1468. */
  1469. total : total,
  1470. /**
  1471. * Initializes the Uploader instance and adds internal event listeners.
  1472. *
  1473. * @method init
  1474. */
  1475. init : function() {
  1476. var self = this, opt, preinitOpt, err;
  1477. preinitOpt = self.getOption('preinit');
  1478. if (typeof(preinitOpt) == "function") {
  1479. preinitOpt(self);
  1480. } else {
  1481. plupload.each(preinitOpt, function(func, name) {
  1482. self.bind(name, func);
  1483. });
  1484. }
  1485. bindEventListeners.call(self);
  1486. // Check for required options
  1487. plupload.each(['container', 'browse_button', 'drop_element'], function(el) {
  1488. if (self.getOption(el) === null) {
  1489. err = {
  1490. code : plupload.INIT_ERROR,
  1491. message : plupload.translate("'%' specified, but cannot be found.")
  1492. }
  1493. return false;
  1494. }
  1495. });
  1496. if (err) {
  1497. return self.trigger('Error', err);
  1498. }
  1499. if (!settings.browse_button && !settings.drop_element) {
  1500. return self.trigger('Error', {
  1501. code : plupload.INIT_ERROR,
  1502. message : plupload.translate("You must specify either 'browse_button' or 'drop_element'.")
  1503. });
  1504. }
  1505. initControls.call(self, settings, function(inited) {
  1506. var initOpt = self.getOption('init');
  1507. if (typeof(initOpt) == "function") {
  1508. initOpt(self);
  1509. } else {
  1510. plupload.each(initOpt, function(func, name) {
  1511. self.bind(name, func);
  1512. });
  1513. }
  1514. if (inited) {
  1515. self.runtime = o.Runtime.getInfo(getRUID()).type;
  1516. self.trigger('Init', { runtime: self.runtime });
  1517. self.trigger('PostInit');
  1518. } else {
  1519. self.trigger('Error', {
  1520. code : plupload.INIT_ERROR,
  1521. message : plupload.translate('Init error.')
  1522. });
  1523. }
  1524. });
  1525. },
  1526. /**
  1527. * Set the value for the specified option(s).
  1528. *
  1529. * @method setOption
  1530. * @since 2.1
  1531. * @param {String|Object} option Name of the option to change or the set of key/value pairs
  1532. * @param {Mixed} [value] Value for the option (is ignored, if first argument is object)
  1533. */
  1534. setOption: function(option, value) {
  1535. setOption.call(this, option, value, !this.runtime); // until runtime not set we do not need to reinitialize
  1536. },
  1537. /**
  1538. * Get the value for the specified option or the whole configuration, if not specified.
  1539. *
  1540. * @method getOption
  1541. * @since 2.1
  1542. * @param {String} [option] Name of the option to get
  1543. * @return {Mixed} Value for the option or the whole set
  1544. */
  1545. getOption: function(option) {
  1546. if (!option) {
  1547. return settings;
  1548. }
  1549. return settings[option];
  1550. },
  1551. /**
  1552. * Refreshes the upload instance by dispatching out a refresh event to all runtimes.
  1553. * This would for example reposition flash/silverlight shims on the page.
  1554. *
  1555. * @method refresh
  1556. */
  1557. refresh : function() {
  1558. if (fileInputs.length) {
  1559. plupload.each(fileInputs, function(fileInput) {
  1560. fileInput.trigger('Refresh');
  1561. });
  1562. }
  1563. this.trigger('Refresh');
  1564. },
  1565. /**
  1566. * Starts uploading the queued files.
  1567. *
  1568. * @method start
  1569. */
  1570. start : function() {
  1571. if (this.state != plupload.STARTED) {
  1572. this.state = plupload.STARTED;
  1573. this.trigger('StateChanged');
  1574. uploadNext.call(this);
  1575. }
  1576. },
  1577. /**
  1578. * Stops the upload of the queued files.
  1579. *
  1580. * @method stop
  1581. */
  1582. stop : function() {
  1583. if (this.state != plupload.STOPPED) {
  1584. this.state = plupload.STOPPED;
  1585. this.trigger('StateChanged');
  1586. this.trigger('CancelUpload');
  1587. }
  1588. },
  1589. /**
  1590. * Disables/enables browse button on request.
  1591. *
  1592. * @method disableBrowse
  1593. * @param {Boolean} disable Whether to disable or enable (default: true)
  1594. */
  1595. disableBrowse : function() {
  1596. disabled = arguments[0] !== undef ? arguments[0] : true;
  1597. if (fileInputs.length) {
  1598. plupload.each(fileInputs, function(fileInput) {
  1599. fileInput.disable(disabled);
  1600. });
  1601. }
  1602. this.trigger('DisableBrowse', disabled);
  1603. },
  1604. /**
  1605. * Returns the specified file object by id.
  1606. *
  1607. * @method getFile
  1608. * @param {String} id File id to look for.
  1609. * @return {plupload.File} File object or undefined if it wasn't found;
  1610. */
  1611. getFile : function(id) {
  1612. var i;
  1613. for (i = files.length - 1; i >= 0; i--) {
  1614. if (files[i].id === id) {
  1615. return files[i];
  1616. }
  1617. }
  1618. },
  1619. /**
  1620. * Adds file to the queue programmatically. Can be native file, instance of Plupload.File,
  1621. * instance of mOxie.File, input[type="file"] element, or array of these. Fires FilesAdded,
  1622. * if any files were added to the queue. Otherwise nothing happens.
  1623. *
  1624. * @method addFile
  1625. * @since 2.0
  1626. * @param {plupload.File|mOxie.File|File|Node|Array} file File or files to add to the queue.
  1627. * @param {String} [fileName] If specified, will be used as a name for the file
  1628. */
  1629. addFile : function(file, fileName) {
  1630. var self = this
  1631. , queue = []
  1632. , filesAdded = []
  1633. , ruid
  1634. ;
  1635. function filterFile(file, cb) {
  1636. var queue = [];
  1637. o.each(self.settings.filters, function(rule, name) {
  1638. if (fileFilters[name]) {
  1639. queue.push(function(cb) {
  1640. fileFilters[name].call(self, rule, file, function(res) {
  1641. cb(!res);
  1642. });
  1643. });
  1644. }
  1645. });
  1646. o.inSeries(queue, cb);
  1647. }
  1648. /**
  1649. * @method resolveFile
  1650. * @private
  1651. * @param {o.File|o.Blob|plupload.File|File|Blob|input[type="file"]} file
  1652. */
  1653. function resolveFile(file) {
  1654. var type = o.typeOf(file);
  1655. // o.File
  1656. if (file instanceof o.File) {
  1657. if (!file.ruid && !file.isDetached()) {
  1658. if (!ruid) { // weird case
  1659. return false;
  1660. }
  1661. file.ruid = ruid;
  1662. file.connectRuntime(ruid);
  1663. }
  1664. resolveFile(new plupload.File(file));
  1665. }
  1666. // o.Blob
  1667. else if (file instanceof o.Blob) {
  1668. resolveFile(file.getSource());
  1669. file.destroy();
  1670. }
  1671. // plupload.File - final step for other branches
  1672. else if (file instanceof plupload.File) {
  1673. if (fileName) {
  1674. file.name = fileName;
  1675. }
  1676. queue.push(function(cb) {
  1677. // run through the internal and user-defined filters, if any
  1678. filterFile(file, function(err) {
  1679. if (!err) {
  1680. // make files available for the filters by updating the main queue directly
  1681. files.push(file);
  1682. // collect the files that will be passed to FilesAdded event
  1683. filesAdded.push(file);
  1684. self.trigger("FileFiltered", file);
  1685. }
  1686. delay(cb, 1); // do not build up recursions or eventually we might hit the limits
  1687. });
  1688. });
  1689. }
  1690. // native File or blob
  1691. else if (o.inArray(type, ['file', 'blob']) !== -1) {
  1692. resolveFile(new o.File(null, file));
  1693. }
  1694. // input[type="file"]
  1695. else if (type === 'node' && o.typeOf(file.files) === 'filelist') {
  1696. // if we are dealing with input[type="file"]
  1697. o.each(file.files, resolveFile);
  1698. }
  1699. // mixed array of any supported types (see above)
  1700. else if (type === 'array') {
  1701. fileName = null; // should never happen, but unset anyway to avoid funny situations
  1702. o.each(file, resolveFile);
  1703. }
  1704. }
  1705. ruid = getRUID();
  1706. resolveFile(file);
  1707. if (queue.length) {
  1708. o.inSeries(queue, function() {
  1709. // if any files left after filtration, trigger FilesAdded
  1710. if (filesAdded.length) {
  1711. self.trigger("FilesAdded", filesAdded);
  1712. }
  1713. });
  1714. }
  1715. },
  1716. /**
  1717. * Removes a specific file.
  1718. *
  1719. * @method removeFile
  1720. * @param {plupload.File|String} file File to remove from queue.
  1721. */
  1722. removeFile : function(file) {
  1723. var id = typeof(file) === 'string' ? file : file.id;
  1724. for (var i = files.length - 1; i >= 0; i--) {
  1725. if (files[i].id === id) {
  1726. return this.splice(i, 1)[0];
  1727. }
  1728. }
  1729. },
  1730. /**
  1731. * Removes part of the queue and returns the files removed. This will also trigger the FilesRemoved and QueueChanged events.
  1732. *
  1733. * @method splice
  1734. * @param {Number} start (Optional) Start index to remove from.
  1735. * @param {Number} length (Optional) Lengh of items to remove.
  1736. * @return {Array} Array of files that was removed.
  1737. */
  1738. splice : function(start, length) {
  1739. // Splice and trigger events
  1740. var removed = files.splice(start === undef ? 0 : start, length === undef ? files.length : length);
  1741. // if upload is in progress we need to stop it and restart after files are removed
  1742. var restartRequired = false;
  1743. if (this.state == plupload.STARTED) { // upload in progress
  1744. plupload.each(removed, function(file) {
  1745. if (file.status === plupload.UPLOADING) {
  1746. restartRequired = true; // do not restart, unless file that is being removed is uploading
  1747. return false;
  1748. }
  1749. });
  1750. if (restartRequired) {
  1751. this.stop();
  1752. }
  1753. }
  1754. this.trigger("FilesRemoved", removed);
  1755. // Dispose any resources allocated by those files
  1756. plupload.each(removed, function(file) {
  1757. file.destroy();
  1758. });
  1759. if (restartRequired) {
  1760. this.start();
  1761. }
  1762. return removed;
  1763. },
  1764. /**
  1765. Dispatches the specified event name and its arguments to all listeners.
  1766. @method trigger
  1767. @param {String} name Event name to fire.
  1768. @param {Object..} Multiple arguments to pass along to the listener functions.
  1769. */
  1770. // override the parent method to match Plupload-like event logic
  1771. dispatchEvent: function(type) {
  1772. var list, args, result;
  1773. type = type.toLowerCase();
  1774. list = this.hasEventListener(type);
  1775. if (list) {
  1776. // sort event list by priority
  1777. list.sort(function(a, b) { return b.priority - a.priority; });
  1778. // first argument should be current plupload.Uploader instance
  1779. args = [].slice.call(arguments);
  1780. args.shift();
  1781. args.unshift(this);
  1782. for (var i = 0; i < list.length; i++) {
  1783. // Fire event, break chain if false is returned
  1784. if (list[i].fn.apply(list[i].scope, args) === false) {
  1785. return false;
  1786. }
  1787. }
  1788. }
  1789. return true;
  1790. },
  1791. /**
  1792. Check whether uploader has any listeners to the specified event.
  1793. @method hasEventListener
  1794. @param {String} name Event name to check for.
  1795. */
  1796. /**
  1797. Adds an event listener by name.
  1798. @method bind
  1799. @param {String} name Event name to listen for.
  1800. @param {function} fn Function to call ones the event gets fired.
  1801. @param {Object} [scope] Optional scope to execute the specified function in.
  1802. @param {Number} [priority=0] Priority of the event handler - handlers with higher priorities will be called first
  1803. */
  1804. bind: function(name, fn, scope, priority) {
  1805. // adapt moxie EventTarget style to Plupload-like
  1806. plupload.Uploader.prototype.bind.call(this, name, fn, priority, scope);
  1807. },
  1808. /**
  1809. Removes the specified event listener.
  1810. @method unbind
  1811. @param {String} name Name of event to remove.
  1812. @param {function} fn Function to remove from listener.
  1813. */
  1814. /**
  1815. Removes all event listeners.
  1816. @method unbindAll
  1817. */
  1818. /**
  1819. * Destroys Plupload instance and cleans after itself.
  1820. *
  1821. * @method destroy
  1822. */
  1823. destroy : function() {
  1824. this.trigger('Destroy');
  1825. settings = total = null; // purge these exclusively
  1826. this.unbindAll();
  1827. }
  1828. });
  1829. };
  1830. plupload.Uploader.prototype = o.EventTarget.instance;
  1831. /**
  1832. * Constructs a new file instance.
  1833. *
  1834. * @class File
  1835. * @constructor
  1836. *
  1837. * @param {Object} file Object containing file properties
  1838. * @param {String} file.name Name of the file.
  1839. * @param {Number} file.size File size.
  1840. */
  1841. plupload.File = (function() {
  1842. var filepool = {};
  1843. function PluploadFile(file) {
  1844. plupload.extend(this, {
  1845. /**
  1846. * File id this is a globally unique id for the specific file.
  1847. *
  1848. * @property id
  1849. * @type String
  1850. */
  1851. id: plupload.guid(),
  1852. /**
  1853. * File name for example "myfile.gif".
  1854. *
  1855. * @property name
  1856. * @type String
  1857. */
  1858. name: file.name || file.fileName,
  1859. /**
  1860. * File type, `e.g image/jpeg`
  1861. *
  1862. * @property type
  1863. * @type String
  1864. */
  1865. type: file.type || '',
  1866. /**
  1867. * File size in bytes (may change after client-side manupilation).
  1868. *
  1869. * @property size
  1870. * @type Number
  1871. */
  1872. size: file.size || file.fileSize,
  1873. /**
  1874. * Original file size in bytes.
  1875. *
  1876. * @property origSize
  1877. * @type Number
  1878. */
  1879. origSize: file.size || file.fileSize,
  1880. /**
  1881. * Number of bytes uploaded of the files total size.
  1882. *
  1883. * @property loaded
  1884. * @type Number
  1885. */
  1886. loaded: 0,
  1887. /**
  1888. * Number of percentage uploaded of the file.
  1889. *
  1890. * @property percent
  1891. * @type Number
  1892. */
  1893. percent: 0,
  1894. /**
  1895. * Status constant matching the plupload states QUEUED, UPLOADING, FAILED, DONE.
  1896. *
  1897. * @property status
  1898. * @type Number
  1899. * @see plupload
  1900. */
  1901. status: plupload.QUEUED,
  1902. /**
  1903. * Date of last modification.
  1904. *
  1905. * @property lastModifiedDate
  1906. * @type {String}
  1907. */
  1908. lastModifiedDate: file.lastModifiedDate || (new Date()).toLocaleString(), // Thu Aug 23 2012 19:40:00 GMT+0400 (GET)
  1909. /**
  1910. * Returns native window.File object, when it's available.
  1911. *
  1912. * @method getNative
  1913. * @return {window.File} or null, if plupload.File is of different origin
  1914. */
  1915. getNative: function() {
  1916. var file = this.getSource().getSource();
  1917. return o.inArray(o.typeOf(file), ['blob', 'file']) !== -1 ? file : null;
  1918. },
  1919. /**
  1920. * Returns mOxie.File - unified wrapper object that can be used across runtimes.
  1921. *
  1922. * @method getSource
  1923. * @return {mOxie.File} or null
  1924. */
  1925. getSource: function() {
  1926. if (!filepool[this.id]) {
  1927. return null;
  1928. }
  1929. return filepool[this.id];
  1930. },
  1931. /**
  1932. * Destroys plupload.File object.
  1933. *
  1934. * @method destroy
  1935. */
  1936. destroy: function() {
  1937. var src = this.getSource();
  1938. if (src) {
  1939. src.destroy();
  1940. delete filepool[this.id];
  1941. }
  1942. }
  1943. });
  1944. filepool[this.id] = file;
  1945. }
  1946. return PluploadFile;
  1947. }());
  1948. /**
  1949. * Constructs a queue progress.
  1950. *
  1951. * @class QueueProgress
  1952. * @constructor
  1953. */
  1954. plupload.QueueProgress = function() {
  1955. var self = this; // Setup alias for self to reduce code size when it's compressed
  1956. /**
  1957. * Total queue file size.
  1958. *
  1959. * @property size
  1960. * @type Number
  1961. */
  1962. self.size = 0;
  1963. /**
  1964. * Total bytes uploaded.
  1965. *
  1966. * @property loaded
  1967. * @type Number
  1968. */
  1969. self.loaded = 0;
  1970. /**
  1971. * Number of files uploaded.
  1972. *
  1973. * @property uploaded
  1974. * @type Number
  1975. */
  1976. self.uploaded = 0;
  1977. /**
  1978. * Number of files failed to upload.
  1979. *
  1980. * @property failed
  1981. * @type Number
  1982. */
  1983. self.failed = 0;
  1984. /**
  1985. * Number of files yet to be uploaded.
  1986. *
  1987. * @property queued
  1988. * @type Number
  1989. */
  1990. self.queued = 0;
  1991. /**
  1992. * Total percent of the uploaded bytes.
  1993. *
  1994. * @property percent
  1995. * @type Number
  1996. */
  1997. self.percent = 0;
  1998. /**
  1999. * Bytes uploaded per second.
  2000. *
  2001. * @property bytesPerSec
  2002. * @type Number
  2003. */
  2004. self.bytesPerSec = 0;
  2005. /**
  2006. * Resets the progress to its initial values.
  2007. *
  2008. * @method reset
  2009. */
  2010. self.reset = function() {
  2011. self.size = self.loaded = self.uploaded = self.failed = self.queued = self.percent = self.bytesPerSec = 0;
  2012. };
  2013. };
  2014. window.plupload = plupload;
  2015. }(window, mOxie));