webuploader.flashonly.js 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623
  1. /*! WebUploader 0.1.5 */
  2. /**
  3. * @fileOverview 让内部各个部件的代码可以用[amd](https://github.com/amdjs/amdjs-api/wiki/AMD)模块定义方式组织起来。
  4. *
  5. * AMD API 内部的简单不完全实现,请忽略。只有当WebUploader被合并成一个文件的时候才会引入。
  6. */
  7. (function (root, factory) {
  8. var modules = {},
  9. // 内部require, 简单不完全实现。
  10. // https://github.com/amdjs/amdjs-api/wiki/require
  11. _require = function (deps, callback) {
  12. var args, len, i;
  13. // 如果deps不是数组,则直接返回指定module
  14. if (typeof deps === 'string') {
  15. return getModule(deps);
  16. } else {
  17. args = [];
  18. for (len = deps.length, i = 0; i < len; i++) {
  19. args.push(getModule(deps[i]));
  20. }
  21. return callback.apply(null, args);
  22. }
  23. },
  24. // 内部define,暂时不支持不指定id.
  25. _define = function (id, deps, factory) {
  26. if (arguments.length === 2) {
  27. factory = deps;
  28. deps = null;
  29. }
  30. _require(deps || [], function () {
  31. setModule(id, factory, arguments);
  32. });
  33. },
  34. // 设置module, 兼容CommonJs写法。
  35. setModule = function (id, factory, args) {
  36. var module = {
  37. exports: factory
  38. },
  39. returned;
  40. if (typeof factory === 'function') {
  41. args.length || (args = [_require, module.exports, module]);
  42. returned = factory.apply(null, args);
  43. returned !== undefined && (module.exports = returned);
  44. }
  45. modules[id] = module.exports;
  46. },
  47. // 根据id获取module
  48. getModule = function (id) {
  49. var module = modules[id] || root[id];
  50. if (!module) {
  51. throw new Error('`' + id + '` is undefined');
  52. }
  53. return module;
  54. },
  55. // 将所有modules,将路径ids装换成对象。
  56. exportsTo = function (obj) {
  57. var key, host, parts, part, last, ucFirst;
  58. // make the first character upper case.
  59. ucFirst = function (str) {
  60. return str && (str.charAt(0).toUpperCase() + str.substr(1));
  61. };
  62. for (key in modules) {
  63. host = obj;
  64. if (!modules.hasOwnProperty(key)) {
  65. continue;
  66. }
  67. parts = key.split('/');
  68. last = ucFirst(parts.pop());
  69. while ((part = ucFirst(parts.shift()))) {
  70. host[part] = host[part] || {};
  71. host = host[part];
  72. }
  73. host[last] = modules[key];
  74. }
  75. return obj;
  76. },
  77. makeExport = function (dollar) {
  78. root.__dollar = dollar;
  79. // exports every module.
  80. return exportsTo(factory(root, _define, _require));
  81. },
  82. origin;
  83. if (typeof module === 'object' && typeof module.exports === 'object') {
  84. // For CommonJS and CommonJS-like environments where a proper window is present,
  85. module.exports = makeExport();
  86. } else if (typeof define === 'function' && define.amd) {
  87. // Allow using this built library as an AMD module
  88. // in another project. That other project will only
  89. // see this AMD call, not the internal modules in
  90. // the closure below.
  91. define(['jquery'], makeExport);
  92. } else {
  93. // Browser globals case. Just assign the
  94. // result to a property on the global.
  95. origin = root.WebUploader;
  96. root.WebUploader = makeExport();
  97. root.WebUploader.noConflict = function () {
  98. root.WebUploader = origin;
  99. };
  100. }
  101. })(window, function (window, define, require) {
  102. /**
  103. * @fileOverview jQuery or Zepto
  104. */
  105. define('dollar-third', [], function () {
  106. var $ = window.__dollar || window.jQuery || window.Zepto;
  107. if (!$) {
  108. throw new Error('jQuery or Zepto not found!');
  109. }
  110. return $;
  111. });
  112. /**
  113. * @fileOverview Dom 操作相关
  114. */
  115. define('dollar', [
  116. 'dollar-third'
  117. ], function (_) {
  118. return _;
  119. });
  120. /**
  121. * @fileOverview 使用jQuery的Promise
  122. */
  123. define('promise-third', [
  124. 'dollar'
  125. ], function ($) {
  126. return {
  127. Deferred: $.Deferred,
  128. when: $.when,
  129. isPromise: function (anything) {
  130. return anything && typeof anything.then === 'function';
  131. }
  132. };
  133. });
  134. /**
  135. * @fileOverview Promise/A+
  136. */
  137. define('promise', [
  138. 'promise-third'
  139. ], function (_) {
  140. return _;
  141. });
  142. /**
  143. * @fileOverview 基础类方法。
  144. */
  145. /**
  146. * Web Uploader内部类的详细说明,以下提及的功能类,都可以在`WebUploader`这个变量中访问到。
  147. *
  148. * As you know, Web Uploader的每个文件都是用过[AMD](https://github.com/amdjs/amdjs-api/wiki/AMD)规范中的`define`组织起来的, 每个Module都会有个module id.
  149. * 默认module id为该文件的路径,而此路径将会转化成名字空间存放在WebUploader中。如:
  150. *
  151. * * module `base`:WebUploader.Base
  152. * * module `file`: WebUploader.File
  153. * * module `lib/dnd`: WebUploader.Lib.Dnd
  154. * * module `runtime/html5/dnd`: WebUploader.Runtime.Html5.Dnd
  155. *
  156. *
  157. * 以下文档中对类的使用可能省略掉了`WebUploader`前缀。
  158. * @module WebUploader
  159. * @title WebUploader API文档
  160. */
  161. define('base', [
  162. 'dollar',
  163. 'promise'
  164. ], function ($, promise) {
  165. var noop = function () {
  166. },
  167. call = Function.call;
  168. // http://jsperf.com/uncurrythis
  169. // 反科里化
  170. function uncurryThis(fn) {
  171. return function () {
  172. return call.apply(fn, arguments);
  173. };
  174. }
  175. function bindFn(fn, context) {
  176. return function () {
  177. return fn.apply(context, arguments);
  178. };
  179. }
  180. function createObject(proto) {
  181. var f;
  182. if (Object.create) {
  183. return Object.create(proto);
  184. } else {
  185. f = function () {
  186. };
  187. f.prototype = proto;
  188. return new f();
  189. }
  190. }
  191. /**
  192. * 基础类,提供一些简单常用的方法。
  193. * @class Base
  194. */
  195. return {
  196. /**
  197. * @property {String} version 当前版本号。
  198. */
  199. version: '0.1.5',
  200. /**
  201. * @property {jQuery|Zepto} $ 引用依赖的jQuery或者Zepto对象。
  202. */
  203. $: $,
  204. Deferred: promise.Deferred,
  205. isPromise: promise.isPromise,
  206. when: promise.when,
  207. /**
  208. * @description 简单的浏览器检查结果。
  209. *
  210. * * `webkit` webkit版本号,如果浏览器为非webkit内核,此属性为`undefined`。
  211. * * `chrome` chrome浏览器版本号,如果浏览器为chrome,此属性为`undefined`。
  212. * * `ie` ie浏览器版本号,如果浏览器为非ie,此属性为`undefined`。**暂不支持ie10+**
  213. * * `firefox` firefox浏览器版本号,如果浏览器为非firefox,此属性为`undefined`。
  214. * * `safari` safari浏览器版本号,如果浏览器为非safari,此属性为`undefined`。
  215. * * `opera` opera浏览器版本号,如果浏览器为非opera,此属性为`undefined`。
  216. *
  217. * @property {Object} [browser]
  218. */
  219. browser: (function (ua) {
  220. var ret = {},
  221. webkit = ua.match(/WebKit\/([\d.]+)/),
  222. chrome = ua.match(/Chrome\/([\d.]+)/) ||
  223. ua.match(/CriOS\/([\d.]+)/),
  224. ie = ua.match(/MSIE\s([\d\.]+)/) ||
  225. ua.match(/(?:trident)(?:.*rv:([\w.]+))?/i),
  226. firefox = ua.match(/Firefox\/([\d.]+)/),
  227. safari = ua.match(/Safari\/([\d.]+)/),
  228. opera = ua.match(/OPR\/([\d.]+)/);
  229. webkit && (ret.webkit = parseFloat(webkit[1]));
  230. chrome && (ret.chrome = parseFloat(chrome[1]));
  231. ie && (ret.ie = parseFloat(ie[1]));
  232. firefox && (ret.firefox = parseFloat(firefox[1]));
  233. safari && (ret.safari = parseFloat(safari[1]));
  234. opera && (ret.opera = parseFloat(opera[1]));
  235. return ret;
  236. })(navigator.userAgent),
  237. /**
  238. * @description 操作系统检查结果。
  239. *
  240. * * `android` 如果在android浏览器环境下,此值为对应的android版本号,否则为`undefined`。
  241. * * `ios` 如果在ios浏览器环境下,此值为对应的ios版本号,否则为`undefined`。
  242. * @property {Object} [os]
  243. */
  244. os: (function (ua) {
  245. var ret = {},
  246. // osx = !!ua.match( /\(Macintosh\; Intel / ),
  247. android = ua.match(/(?:Android);?[\s\/]+([\d.]+)?/),
  248. ios = ua.match(/(?:iPad|iPod|iPhone).*OS\s([\d_]+)/);
  249. // osx && (ret.osx = true);
  250. android && (ret.android = parseFloat(android[1]));
  251. ios && (ret.ios = parseFloat(ios[1].replace(/_/g, '.')));
  252. return ret;
  253. })(navigator.userAgent),
  254. /**
  255. * 实现类与类之间的继承。
  256. * @method inherits
  257. * @grammar Base.inherits( super ) => child
  258. * @grammar Base.inherits( super, protos ) => child
  259. * @grammar Base.inherits( super, protos, statics ) => child
  260. * @param {Class} super 父类
  261. * @param {Object | Function} [protos] 子类或者对象。如果对象中包含constructor,子类将是用此属性值。
  262. * @param {Function} [protos.constructor] 子类构造器,不指定的话将创建个临时的直接执行父类构造器的方法。
  263. * @param {Object} [statics] 静态属性或方法。
  264. * @return {Class} 返回子类。
  265. * @example
  266. * function Person() {
  267. * console.log( 'Super' );
  268. * }
  269. * Person.prototype.hello = function() {
  270. * console.log( 'hello' );
  271. * };
  272. *
  273. * var Manager = Base.inherits( Person, {
  274. * world: function() {
  275. * console.log( 'World' );
  276. * }
  277. * });
  278. *
  279. * // 因为没有指定构造器,父类的构造器将会执行。
  280. * var instance = new Manager(); // => Super
  281. *
  282. * // 继承子父类的方法
  283. * instance.hello(); // => hello
  284. * instance.world(); // => World
  285. *
  286. * // 子类的__super__属性指向父类
  287. * console.log( Manager.__super__ === Person ); // => true
  288. */
  289. inherits: function (Super, protos, staticProtos) {
  290. var child;
  291. if (typeof protos === 'function') {
  292. child = protos;
  293. protos = null;
  294. } else if (protos && protos.hasOwnProperty('constructor')) {
  295. child = protos.constructor;
  296. } else {
  297. child = function () {
  298. return Super.apply(this, arguments);
  299. };
  300. }
  301. // 复制静态方法
  302. $.extend(true, child, Super, staticProtos || {});
  303. /* jshint camelcase: false */
  304. // 让子类的__super__属性指向父类。
  305. child.__super__ = Super.prototype;
  306. // 构建原型,添加原型方法或属性。
  307. // 暂时用Object.create实现。
  308. child.prototype = createObject(Super.prototype);
  309. protos && $.extend(true, child.prototype, protos);
  310. return child;
  311. },
  312. /**
  313. * 一个不做任何事情的方法。可以用来赋值给默认的callback.
  314. * @method noop
  315. */
  316. noop: noop,
  317. /**
  318. * 返回一个新的方法,此方法将已指定的`context`来执行。
  319. * @grammar Base.bindFn( fn, context ) => Function
  320. * @method bindFn
  321. * @example
  322. * var doSomething = function() {
  323. * console.log( this.name );
  324. * },
  325. * obj = {
  326. * name: 'Object Name'
  327. * },
  328. * aliasFn = Base.bind( doSomething, obj );
  329. *
  330. * aliasFn(); // => Object Name
  331. *
  332. */
  333. bindFn: bindFn,
  334. /**
  335. * 引用Console.log如果存在的话,否则引用一个[空函数noop](#WebUploader:Base.noop)。
  336. * @grammar Base.log( args... ) => undefined
  337. * @method log
  338. */
  339. log: (function () {
  340. if (window.console) {
  341. return bindFn(console.log, console);
  342. }
  343. return noop;
  344. })(),
  345. nextTick: (function () {
  346. return function (cb) {
  347. setTimeout(cb, 1);
  348. };
  349. // @bug 当浏览器不在当前窗口时就停了。
  350. // var next = window.requestAnimationFrame ||
  351. // window.webkitRequestAnimationFrame ||
  352. // window.mozRequestAnimationFrame ||
  353. // function( cb ) {
  354. // window.setTimeout( cb, 1000 / 60 );
  355. // };
  356. // // fix: Uncaught TypeError: Illegal invocation
  357. // return bindFn( next, window );
  358. })(),
  359. /**
  360. * 被[uncurrythis](http://www.2ality.com/2011/11/uncurrying-this.html)的数组slice方法。
  361. * 将用来将非数组对象转化成数组对象。
  362. * @grammar Base.slice( target, start[, end] ) => Array
  363. * @method slice
  364. * @example
  365. * function doSomthing() {
  366. * var args = Base.slice( arguments, 1 );
  367. * console.log( args );
  368. * }
  369. *
  370. * doSomthing( 'ignored', 'arg2', 'arg3' ); // => Array ["arg2", "arg3"]
  371. */
  372. slice: uncurryThis([].slice),
  373. /**
  374. * 生成唯一的ID
  375. * @method guid
  376. * @grammar Base.guid() => String
  377. * @grammar Base.guid( prefx ) => String
  378. */
  379. guid: (function () {
  380. var counter = 0;
  381. return function (prefix) {
  382. var guid = (+new Date()).toString(32),
  383. i = 0;
  384. for (; i < 5; i++) {
  385. guid += Math.floor(Math.random() * 65535).toString(32);
  386. }
  387. return (prefix || 'wu_') + guid + (counter++).toString(32);
  388. };
  389. })(),
  390. /**
  391. * 格式化文件大小, 输出成带单位的字符串
  392. * @method formatSize
  393. * @grammar Base.formatSize( size ) => String
  394. * @grammar Base.formatSize( size, pointLength ) => String
  395. * @grammar Base.formatSize( size, pointLength, units ) => String
  396. * @param {Number} size 文件大小
  397. * @param {Number} [pointLength=2] 精确到的小数点数。
  398. * @param {Array} [units=[ 'B', 'K', 'M', 'G', 'TB' ]] 单位数组。从字节,到千字节,一直往上指定。如果单位数组里面只指定了到了K(千字节),同时文件大小大于M, 此方法的输出将还是显示成多少K.
  399. * @example
  400. * console.log( Base.formatSize( 100 ) ); // => 100B
  401. * console.log( Base.formatSize( 1024 ) ); // => 1.00K
  402. * console.log( Base.formatSize( 1024, 0 ) ); // => 1K
  403. * console.log( Base.formatSize( 1024 * 1024 ) ); // => 1.00M
  404. * console.log( Base.formatSize( 1024 * 1024 * 1024 ) ); // => 1.00G
  405. * console.log( Base.formatSize( 1024 * 1024 * 1024, 0, ['B', 'KB', 'MB'] ) ); // => 1024MB
  406. */
  407. formatSize: function (size, pointLength, units) {
  408. var unit;
  409. units = units || ['B', 'K', 'M', 'G', 'TB'];
  410. while ((unit = units.shift()) && size > 1024) {
  411. size = size / 1024;
  412. }
  413. return (unit === 'B' ? size : size.toFixed(pointLength || 2)) +
  414. unit;
  415. }
  416. };
  417. });
  418. /**
  419. * 事件处理类,可以独立使用,也可以扩展给对象使用。
  420. * @fileOverview Mediator
  421. */
  422. define('mediator', [
  423. 'base'
  424. ], function (Base) {
  425. var $ = Base.$,
  426. slice = [].slice,
  427. separator = /\s+/,
  428. protos;
  429. // 根据条件过滤出事件handlers.
  430. function findHandlers(arr, name, callback, context) {
  431. return $.grep(arr, function (handler) {
  432. return handler &&
  433. (!name || handler.e === name) &&
  434. (!callback || handler.cb === callback ||
  435. handler.cb._cb === callback) &&
  436. (!context || handler.ctx === context);
  437. });
  438. }
  439. function eachEvent(events, callback, iterator) {
  440. // 不支持对象,只支持多个event用空格隔开
  441. $.each((events || '').split(separator), function (_, key) {
  442. iterator(key, callback);
  443. });
  444. }
  445. function triggerHanders(events, args) {
  446. var stoped = false,
  447. i = -1,
  448. len = events.length,
  449. handler;
  450. while (++i < len) {
  451. handler = events[i];
  452. if (handler.cb.apply(handler.ctx2, args) === false) {
  453. stoped = true;
  454. break;
  455. }
  456. }
  457. return !stoped;
  458. }
  459. protos = {
  460. /**
  461. * 绑定事件。
  462. *
  463. * `callback`方法在执行时,arguments将会来源于trigger的时候携带的参数。如
  464. * ```javascript
  465. * var obj = {};
  466. *
  467. * // 使得obj有事件行为
  468. * Mediator.installTo( obj );
  469. *
  470. * obj.on( 'testa', function( arg1, arg2 ) {
  471. * console.log( arg1, arg2 ); // => 'arg1', 'arg2'
  472. * });
  473. *
  474. * obj.trigger( 'testa', 'arg1', 'arg2' );
  475. * ```
  476. *
  477. * 如果`callback`中,某一个方法`return false`了,则后续的其他`callback`都不会被执行到。
  478. * 切会影响到`trigger`方法的返回值,为`false`。
  479. *
  480. * `on`还可以用来添加一个特殊事件`all`, 这样所有的事件触发都会响应到。同时此类`callback`中的arguments有一个不同处,
  481. * 就是第一个参数为`type`,记录当前是什么事件在触发。此类`callback`的优先级比脚低,会再正常`callback`执行完后触发。
  482. * ```javascript
  483. * obj.on( 'all', function( type, arg1, arg2 ) {
  484. * console.log( type, arg1, arg2 ); // => 'testa', 'arg1', 'arg2'
  485. * });
  486. * ```
  487. *
  488. * @method on
  489. * @grammar on( name, callback[, context] ) => self
  490. * @param {String} name 事件名,支持多个事件用空格隔开
  491. * @param {Function} callback 事件处理器
  492. * @param {Object} [context] 事件处理器的上下文。
  493. * @return {self} 返回自身,方便链式
  494. * @chainable
  495. * @class Mediator
  496. */
  497. on: function (name, callback, context) {
  498. var me = this,
  499. set;
  500. if (!callback) {
  501. return this;
  502. }
  503. set = this._events || (this._events = []);
  504. eachEvent(name, callback, function (name, callback) {
  505. var handler = {e: name};
  506. handler.cb = callback;
  507. handler.ctx = context;
  508. handler.ctx2 = context || me;
  509. handler.id = set.length;
  510. set.push(handler);
  511. });
  512. return this;
  513. },
  514. /**
  515. * 绑定事件,且当handler执行完后,自动解除绑定。
  516. * @method once
  517. * @grammar once( name, callback[, context] ) => self
  518. * @param {String} name 事件名
  519. * @param {Function} callback 事件处理器
  520. * @param {Object} [context] 事件处理器的上下文。
  521. * @return {self} 返回自身,方便链式
  522. * @chainable
  523. */
  524. once: function (name, callback, context) {
  525. var me = this;
  526. if (!callback) {
  527. return me;
  528. }
  529. eachEvent(name, callback, function (name, callback) {
  530. var once = function () {
  531. me.off(name, once);
  532. return callback.apply(context || me, arguments);
  533. };
  534. once._cb = callback;
  535. me.on(name, once, context);
  536. });
  537. return me;
  538. },
  539. /**
  540. * 解除事件绑定
  541. * @method off
  542. * @grammar off( [name[, callback[, context] ] ] ) => self
  543. * @param {String} [name] 事件名
  544. * @param {Function} [callback] 事件处理器
  545. * @param {Object} [context] 事件处理器的上下文。
  546. * @return {self} 返回自身,方便链式
  547. * @chainable
  548. */
  549. off: function (name, cb, ctx) {
  550. var events = this._events;
  551. if (!events) {
  552. return this;
  553. }
  554. if (!name && !cb && !ctx) {
  555. this._events = [];
  556. return this;
  557. }
  558. eachEvent(name, cb, function (name, cb) {
  559. $.each(findHandlers(events, name, cb, ctx), function () {
  560. delete events[this.id];
  561. });
  562. });
  563. return this;
  564. },
  565. /**
  566. * 触发事件
  567. * @method trigger
  568. * @grammar trigger( name[, args...] ) => self
  569. * @param {String} type 事件名
  570. * @param {*} [...] 任意参数
  571. * @return {Boolean} 如果handler中return false了,则返回false, 否则返回true
  572. */
  573. trigger: function (type) {
  574. var args, events, allEvents;
  575. if (!this._events || !type) {
  576. return this;
  577. }
  578. args = slice.call(arguments, 1);
  579. events = findHandlers(this._events, type);
  580. allEvents = findHandlers(this._events, 'all');
  581. return triggerHanders(events, args) &&
  582. triggerHanders(allEvents, arguments);
  583. }
  584. };
  585. /**
  586. * 中介者,它本身是个单例,但可以通过[installTo](#WebUploader:Mediator:installTo)方法,使任何对象具备事件行为。
  587. * 主要目的是负责模块与模块之间的合作,降低耦合度。
  588. *
  589. * @class Mediator
  590. */
  591. return $.extend({
  592. /**
  593. * 可以通过这个接口,使任何对象具备事件功能。
  594. * @method installTo
  595. * @param {Object} obj 需要具备事件行为的对象。
  596. * @return {Object} 返回obj.
  597. */
  598. installTo: function (obj) {
  599. return $.extend(obj, protos);
  600. }
  601. }, protos);
  602. });
  603. /**
  604. * @fileOverview Uploader上传类
  605. */
  606. define('uploader', [
  607. 'base',
  608. 'mediator'
  609. ], function (Base, Mediator) {
  610. var $ = Base.$;
  611. /**
  612. * 上传入口类。
  613. * @class Uploader
  614. * @constructor
  615. * @grammar new Uploader( opts ) => Uploader
  616. * @example
  617. * var uploader = WebUploader.Uploader({
  618. * swf: 'path_of_swf/Uploader.swf',
  619. *
  620. * // 开起分片上传。
  621. * chunked: true
  622. * });
  623. */
  624. function Uploader(opts) {
  625. this.options = $.extend(true, {}, Uploader.options, opts);
  626. this._init(this.options);
  627. }
  628. // default Options
  629. // widgets中有相应扩展
  630. Uploader.options = {};
  631. Mediator.installTo(Uploader.prototype);
  632. // 批量添加纯命令式方法。
  633. $.each({
  634. upload: 'start-upload',
  635. stop: 'stop-upload',
  636. getFile: 'get-file',
  637. getFiles: 'get-files',
  638. addFile: 'add-file',
  639. addFiles: 'add-file',
  640. sort: 'sort-files',
  641. removeFile: 'remove-file',
  642. cancelFile: 'cancel-file',
  643. skipFile: 'skip-file',
  644. retry: 'retry',
  645. isInProgress: 'is-in-progress',
  646. makeThumb: 'make-thumb',
  647. md5File: 'md5-file',
  648. getDimension: 'get-dimension',
  649. addButton: 'add-btn',
  650. predictRuntimeType: 'predict-runtime-type',
  651. refresh: 'refresh',
  652. disable: 'disable',
  653. enable: 'enable',
  654. reset: 'reset'
  655. }, function (fn, command) {
  656. Uploader.prototype[fn] = function () {
  657. return this.request(command, arguments);
  658. };
  659. });
  660. $.extend(Uploader.prototype, {
  661. state: 'pending',
  662. _init: function (opts) {
  663. var me = this;
  664. me.request('init', opts, function () {
  665. me.state = 'ready';
  666. me.trigger('ready');
  667. });
  668. },
  669. /**
  670. * 获取或者设置Uploader配置项。
  671. * @method option
  672. * @grammar option( key ) => *
  673. * @grammar option( key, val ) => self
  674. * @example
  675. *
  676. * // 初始状态图片上传前不会压缩
  677. * var uploader = new WebUploader.Uploader({
  678. * compress: null;
  679. * });
  680. *
  681. * // 修改后图片上传前,尝试将图片压缩到1600 * 1600
  682. * uploader.option( 'compress', {
  683. * width: 1600,
  684. * height: 1600
  685. * });
  686. */
  687. option: function (key, val) {
  688. var opts = this.options;
  689. // setter
  690. if (arguments.length > 1) {
  691. if ($.isPlainObject(val) &&
  692. $.isPlainObject(opts[key])) {
  693. $.extend(opts[key], val);
  694. } else {
  695. opts[key] = val;
  696. }
  697. } else { // getter
  698. return key ? opts[key] : opts;
  699. }
  700. },
  701. /**
  702. * 获取文件统计信息。返回一个包含一下信息的对象。
  703. * * `successNum` 上传成功的文件数
  704. * * `progressNum` 上传中的文件数
  705. * * `cancelNum` 被删除的文件数
  706. * * `invalidNum` 无效的文件数
  707. * * `uploadFailNum` 上传失败的文件数
  708. * * `queueNum` 还在队列中的文件数
  709. * * `interruptNum` 被暂停的文件数
  710. * @method getStats
  711. * @grammar getStats() => Object
  712. */
  713. getStats: function () {
  714. // return this._mgr.getStats.apply( this._mgr, arguments );
  715. var stats = this.request('get-stats');
  716. return stats ? {
  717. successNum: stats.numOfSuccess,
  718. progressNum: stats.numOfProgress,
  719. // who care?
  720. // queueFailNum: 0,
  721. cancelNum: stats.numOfCancel,
  722. invalidNum: stats.numOfInvalid,
  723. uploadFailNum: stats.numOfUploadFailed,
  724. queueNum: stats.numOfQueue,
  725. interruptNum: stats.numofInterrupt
  726. } : {};
  727. },
  728. // 需要重写此方法来来支持opts.onEvent和instance.onEvent的处理器
  729. trigger: function (type/*, args...*/) {
  730. var args = [].slice.call(arguments, 1),
  731. opts = this.options,
  732. name = 'on' + type.substring(0, 1).toUpperCase() +
  733. type.substring(1);
  734. if (
  735. // 调用通过on方法注册的handler.
  736. Mediator.trigger.apply(this, arguments) === false ||
  737. // 调用opts.onEvent
  738. $.isFunction(opts[name]) &&
  739. opts[name].apply(this, args) === false ||
  740. // 调用this.onEvent
  741. $.isFunction(this[name]) &&
  742. this[name].apply(this, args) === false ||
  743. // 广播所有uploader的事件。
  744. Mediator.trigger.apply(Mediator,
  745. [this, type].concat(args)) === false) {
  746. return false;
  747. }
  748. return true;
  749. },
  750. /**
  751. * 销毁 webuploader 实例
  752. * @method destroy
  753. * @grammar destroy() => undefined
  754. */
  755. destroy: function () {
  756. this.request('destroy', arguments);
  757. this.off();
  758. },
  759. // widgets/widget.js将补充此方法的详细文档。
  760. request: Base.noop
  761. });
  762. /**
  763. * 创建Uploader实例,等同于new Uploader( opts );
  764. * @method create
  765. * @class Base
  766. * @static
  767. * @grammar Base.create( opts ) => Uploader
  768. */
  769. Base.create = Uploader.create = function (opts) {
  770. return new Uploader(opts);
  771. };
  772. // 暴露Uploader,可以通过它来扩展业务逻辑。
  773. Base.Uploader = Uploader;
  774. return Uploader;
  775. });
  776. /**
  777. * @fileOverview Runtime管理器,负责Runtime的选择, 连接
  778. */
  779. define('runtime/runtime', [
  780. 'base',
  781. 'mediator'
  782. ], function (Base, Mediator) {
  783. var $ = Base.$,
  784. factories = {},
  785. // 获取对象的第一个key
  786. getFirstKey = function (obj) {
  787. for (var key in obj) {
  788. if (obj.hasOwnProperty(key)) {
  789. return key;
  790. }
  791. }
  792. return null;
  793. };
  794. // 接口类。
  795. function Runtime(options) {
  796. this.options = $.extend({
  797. container: document.body
  798. }, options);
  799. this.uid = Base.guid('rt_');
  800. }
  801. $.extend(Runtime.prototype, {
  802. getContainer: function () {
  803. var opts = this.options,
  804. parent, container;
  805. if (this._container) {
  806. return this._container;
  807. }
  808. parent = $(opts.container || document.body);
  809. container = $(document.createElement('div'));
  810. container.attr('id', 'rt_' + this.uid);
  811. container.css({
  812. position: 'absolute',
  813. top: '0px',
  814. left: '0px',
  815. width: '1px',
  816. height: '1px',
  817. overflow: 'hidden'
  818. });
  819. parent.append(container);
  820. parent.addClass('webuploader-container');
  821. this._container = container;
  822. this._parent = parent;
  823. return container;
  824. },
  825. init: Base.noop,
  826. exec: Base.noop,
  827. destroy: function () {
  828. this._container && this._container.remove();
  829. this._parent && this._parent.removeClass('webuploader-container');
  830. this.off();
  831. }
  832. });
  833. Runtime.orders = 'html5,flash';
  834. /**
  835. * 添加Runtime实现。
  836. * @param {String} type 类型
  837. * @param {Runtime} factory 具体Runtime实现。
  838. */
  839. Runtime.addRuntime = function (type, factory) {
  840. factories[type] = factory;
  841. };
  842. Runtime.hasRuntime = function (type) {
  843. return !!(type ? factories[type] : getFirstKey(factories));
  844. };
  845. Runtime.create = function (opts, orders) {
  846. var type, runtime;
  847. orders = orders || Runtime.orders;
  848. $.each(orders.split(/\s*,\s*/g), function () {
  849. if (factories[this]) {
  850. type = this;
  851. return false;
  852. }
  853. });
  854. type = type || getFirstKey(factories);
  855. if (!type) {
  856. throw new Error('Runtime Error');
  857. }
  858. runtime = new factories[type](opts);
  859. return runtime;
  860. };
  861. Mediator.installTo(Runtime.prototype);
  862. return Runtime;
  863. });
  864. /**
  865. * @fileOverview Runtime管理器,负责Runtime的选择, 连接
  866. */
  867. define('runtime/client', [
  868. 'base',
  869. 'mediator',
  870. 'runtime/runtime'
  871. ], function (Base, Mediator, Runtime) {
  872. var cache;
  873. cache = (function () {
  874. var obj = {};
  875. return {
  876. add: function (runtime) {
  877. obj[runtime.uid] = runtime;
  878. },
  879. get: function (ruid, standalone) {
  880. var i;
  881. if (ruid) {
  882. return obj[ruid];
  883. }
  884. for (i in obj) {
  885. // 有些类型不能重用,比如filepicker.
  886. if (standalone && obj[i].__standalone) {
  887. continue;
  888. }
  889. return obj[i];
  890. }
  891. return null;
  892. },
  893. remove: function (runtime) {
  894. delete obj[runtime.uid];
  895. }
  896. };
  897. })();
  898. function RuntimeClient(component, standalone) {
  899. var deferred = Base.Deferred(),
  900. runtime;
  901. this.uid = Base.guid('client_');
  902. // 允许runtime没有初始化之前,注册一些方法在初始化后执行。
  903. this.runtimeReady = function (cb) {
  904. return deferred.done(cb);
  905. };
  906. this.connectRuntime = function (opts, cb) {
  907. // already connected.
  908. if (runtime) {
  909. throw new Error('already connected!');
  910. }
  911. deferred.done(cb);
  912. if (typeof opts === 'string' && cache.get(opts)) {
  913. runtime = cache.get(opts);
  914. }
  915. // 像filePicker只能独立存在,不能公用。
  916. runtime = runtime || cache.get(null, standalone);
  917. // 需要创建
  918. if (!runtime) {
  919. runtime = Runtime.create(opts, opts.runtimeOrder);
  920. runtime.__promise = deferred.promise();
  921. runtime.once('ready', deferred.resolve);
  922. runtime.init();
  923. cache.add(runtime);
  924. runtime.__client = 1;
  925. } else {
  926. // 来自cache
  927. Base.$.extend(runtime.options, opts);
  928. runtime.__promise.then(deferred.resolve);
  929. runtime.__client++;
  930. }
  931. standalone && (runtime.__standalone = standalone);
  932. return runtime;
  933. };
  934. this.getRuntime = function () {
  935. return runtime;
  936. };
  937. this.disconnectRuntime = function () {
  938. if (!runtime) {
  939. return;
  940. }
  941. runtime.__client--;
  942. if (runtime.__client <= 0) {
  943. cache.remove(runtime);
  944. delete runtime.__promise;
  945. runtime.destroy();
  946. }
  947. runtime = null;
  948. };
  949. this.exec = function () {
  950. if (!runtime) {
  951. return;
  952. }
  953. var args = Base.slice(arguments);
  954. component && args.unshift(component);
  955. return runtime.exec.apply(this, args);
  956. };
  957. this.getRuid = function () {
  958. return runtime && runtime.uid;
  959. };
  960. this.destroy = (function (destroy) {
  961. return function () {
  962. destroy && destroy.apply(this, arguments);
  963. this.trigger('destroy');
  964. this.off();
  965. this.exec('destroy');
  966. this.disconnectRuntime();
  967. };
  968. })(this.destroy);
  969. }
  970. Mediator.installTo(RuntimeClient.prototype);
  971. return RuntimeClient;
  972. });
  973. /**
  974. * @fileOverview Blob
  975. */
  976. define('lib/blob', [
  977. 'base',
  978. 'runtime/client'
  979. ], function (Base, RuntimeClient) {
  980. function Blob(ruid, source) {
  981. var me = this;
  982. me.source = source;
  983. me.ruid = ruid;
  984. this.size = source.size || 0;
  985. // 如果没有指定 mimetype, 但是知道文件后缀。
  986. if (!source.type && this.ext &&
  987. ~'jpg,jpeg,png,gif,bmp'.indexOf(this.ext)) {
  988. this.type = 'image/' + (this.ext === 'jpg' ? 'jpeg' : this.ext);
  989. } else {
  990. this.type = source.type || 'application/octet-stream';
  991. }
  992. RuntimeClient.call(me, 'Blob');
  993. this.uid = source.uid || this.uid;
  994. if (ruid) {
  995. me.connectRuntime(ruid);
  996. }
  997. }
  998. Base.inherits(RuntimeClient, {
  999. constructor: Blob,
  1000. slice: function (start, end) {
  1001. return this.exec('slice', start, end);
  1002. },
  1003. getSource: function () {
  1004. return this.source;
  1005. }
  1006. });
  1007. return Blob;
  1008. });
  1009. /**
  1010. * 为了统一化Flash的File和HTML5的File而存在。
  1011. * 以至于要调用Flash里面的File,也可以像调用HTML5版本的File一下。
  1012. * @fileOverview File
  1013. */
  1014. define('lib/file', [
  1015. 'base',
  1016. 'lib/blob'
  1017. ], function (Base, Blob) {
  1018. var uid = 1,
  1019. rExt = /\.([^.]+)$/;
  1020. function File(ruid, file) {
  1021. var ext;
  1022. this.name = file.name || ('untitled' + uid++);
  1023. ext = rExt.exec(file.name) ? RegExp.$1.toLowerCase() : '';
  1024. // todo 支持其他类型文件的转换。
  1025. // 如果有 mimetype, 但是文件名里面没有找出后缀规律
  1026. if (!ext && file.type) {
  1027. ext = /\/(jpg|jpeg|png|gif|bmp)$/i.exec(file.type) ?
  1028. RegExp.$1.toLowerCase() : '';
  1029. this.name += '.' + ext;
  1030. }
  1031. this.ext = ext;
  1032. this.lastModifiedDate = file.lastModifiedDate ||
  1033. (new Date()).toLocaleString();
  1034. Blob.apply(this, arguments);
  1035. }
  1036. return Base.inherits(Blob, File);
  1037. });
  1038. /**
  1039. * @fileOverview 错误信息
  1040. */
  1041. define('lib/filepicker', [
  1042. 'base',
  1043. 'runtime/client',
  1044. 'lib/file'
  1045. ], function (Base, RuntimeClent, File) {
  1046. var $ = Base.$;
  1047. function FilePicker(opts) {
  1048. opts = this.options = $.extend({}, FilePicker.options, opts);
  1049. opts.container = $(opts.id);
  1050. if (!opts.container.length) {
  1051. throw new Error('按钮指定错误');
  1052. }
  1053. opts.innerHTML = opts.innerHTML || opts.label ||
  1054. opts.container.html() || '';
  1055. opts.button = $(opts.button || document.createElement('div'));
  1056. opts.button.html(opts.innerHTML);
  1057. opts.container.html(opts.button);
  1058. RuntimeClent.call(this, 'FilePicker', true);
  1059. }
  1060. FilePicker.options = {
  1061. button: null,
  1062. container: null,
  1063. label: null,
  1064. innerHTML: null,
  1065. multiple: true,
  1066. accept: null,
  1067. name: 'file'
  1068. };
  1069. Base.inherits(RuntimeClent, {
  1070. constructor: FilePicker,
  1071. init: function () {
  1072. var me = this,
  1073. opts = me.options,
  1074. button = opts.button;
  1075. button.addClass('webuploader-pick');
  1076. me.on('all', function (type) {
  1077. var files;
  1078. switch (type) {
  1079. case 'mouseenter':
  1080. button.addClass('webuploader-pick-hover');
  1081. break;
  1082. case 'mouseleave':
  1083. button.removeClass('webuploader-pick-hover');
  1084. break;
  1085. case 'change':
  1086. files = me.exec('getFiles');
  1087. me.trigger('select', $.map(files, function (file) {
  1088. file = new File(me.getRuid(), file);
  1089. // 记录来源。
  1090. file._refer = opts.container;
  1091. return file;
  1092. }), opts.container);
  1093. break;
  1094. }
  1095. });
  1096. me.connectRuntime(opts, function () {
  1097. me.refresh();
  1098. me.exec('init', opts);
  1099. me.trigger('ready');
  1100. });
  1101. this._resizeHandler = Base.bindFn(this.refresh, this);
  1102. $(window).on('resize', this._resizeHandler);
  1103. },
  1104. refresh: function () {
  1105. var shimContainer = this.getRuntime().getContainer(),
  1106. button = this.options.button,
  1107. width = button.outerWidth ?
  1108. button.outerWidth() : button.width(),
  1109. height = button.outerHeight ?
  1110. button.outerHeight() : button.height(),
  1111. pos = button.offset();
  1112. width && height && shimContainer.css({
  1113. bottom: 'auto',
  1114. right: 'auto',
  1115. width: width + 'px',
  1116. height: height + 'px'
  1117. }).offset(pos);
  1118. },
  1119. enable: function () {
  1120. var btn = this.options.button;
  1121. btn.removeClass('webuploader-pick-disable');
  1122. this.refresh();
  1123. },
  1124. disable: function () {
  1125. var btn = this.options.button;
  1126. this.getRuntime().getContainer().css({
  1127. top: '-99999px'
  1128. });
  1129. btn.addClass('webuploader-pick-disable');
  1130. },
  1131. destroy: function () {
  1132. var btn = this.options.button;
  1133. $(window).off('resize', this._resizeHandler);
  1134. btn.removeClass('webuploader-pick-disable webuploader-pick-hover ' +
  1135. 'webuploader-pick');
  1136. }
  1137. });
  1138. return FilePicker;
  1139. });
  1140. /**
  1141. * @fileOverview 组件基类。
  1142. */
  1143. define('widgets/widget', [
  1144. 'base',
  1145. 'uploader'
  1146. ], function (Base, Uploader) {
  1147. var $ = Base.$,
  1148. _init = Uploader.prototype._init,
  1149. _destroy = Uploader.prototype.destroy,
  1150. IGNORE = {},
  1151. widgetClass = [];
  1152. function isArrayLike(obj) {
  1153. if (!obj) {
  1154. return false;
  1155. }
  1156. var length = obj.length,
  1157. type = $.type(obj);
  1158. if (obj.nodeType === 1 && length) {
  1159. return true;
  1160. }
  1161. return type === 'array' || type !== 'function' && type !== 'string' &&
  1162. (length === 0 || typeof length === 'number' && length > 0 &&
  1163. (length - 1) in obj);
  1164. }
  1165. function Widget(uploader) {
  1166. this.owner = uploader;
  1167. this.options = uploader.options;
  1168. }
  1169. $.extend(Widget.prototype, {
  1170. init: Base.noop,
  1171. // 类Backbone的事件监听声明,监听uploader实例上的事件
  1172. // widget直接无法监听事件,事件只能通过uploader来传递
  1173. invoke: function (apiName, args) {
  1174. /*
  1175. {
  1176. 'make-thumb': 'makeThumb'
  1177. }
  1178. */
  1179. var map = this.responseMap;
  1180. // 如果无API响应声明则忽略
  1181. if (!map || !(apiName in map) || !(map[apiName] in this) ||
  1182. !$.isFunction(this[map[apiName]])) {
  1183. return IGNORE;
  1184. }
  1185. return this[map[apiName]].apply(this, args);
  1186. },
  1187. /**
  1188. * 发送命令。当传入`callback`或者`handler`中返回`promise`时。返回一个当所有`handler`中的promise都完成后完成的新`promise`。
  1189. * @method request
  1190. * @grammar request( command, args ) => * | Promise
  1191. * @grammar request( command, args, callback ) => Promise
  1192. * @for Uploader
  1193. */
  1194. request: function () {
  1195. return this.owner.request.apply(this.owner, arguments);
  1196. }
  1197. });
  1198. // 扩展Uploader.
  1199. $.extend(Uploader.prototype, {
  1200. /**
  1201. * @property {String | Array} [disableWidgets=undefined]
  1202. * @namespace options
  1203. * @for Uploader
  1204. * @description 默认所有 Uploader.register 了的 widget 都会被加载,如果禁用某一部分,请通过此 option 指定黑名单。
  1205. */
  1206. // 覆写_init用来初始化widgets
  1207. _init: function () {
  1208. var me = this,
  1209. widgets = me._widgets = [],
  1210. deactives = me.options.disableWidgets || '';
  1211. $.each(widgetClass, function (_, klass) {
  1212. (!deactives || !~deactives.indexOf(klass._name)) &&
  1213. widgets.push(new klass(me));
  1214. });
  1215. return _init.apply(me, arguments);
  1216. },
  1217. request: function (apiName, args, callback) {
  1218. var i = 0,
  1219. widgets = this._widgets,
  1220. len = widgets && widgets.length,
  1221. rlts = [],
  1222. dfds = [],
  1223. widget, rlt, promise, key;
  1224. args = isArrayLike(args) ? args : [args];
  1225. for (; i < len; i++) {
  1226. widget = widgets[i];
  1227. rlt = widget.invoke(apiName, args);
  1228. if (rlt !== IGNORE) {
  1229. // Deferred对象
  1230. if (Base.isPromise(rlt)) {
  1231. dfds.push(rlt);
  1232. } else {
  1233. rlts.push(rlt);
  1234. }
  1235. }
  1236. }
  1237. // 如果有callback,则用异步方式。
  1238. if (callback || dfds.length) {
  1239. promise = Base.when.apply(Base, dfds);
  1240. key = promise.pipe ? 'pipe' : 'then';
  1241. // 很重要不能删除。删除了会死循环。
  1242. // 保证执行顺序。让callback总是在下一个 tick 中执行。
  1243. return promise[key](function () {
  1244. var deferred = Base.Deferred(),
  1245. args = arguments;
  1246. if (args.length === 1) {
  1247. args = args[0];
  1248. }
  1249. setTimeout(function () {
  1250. deferred.resolve(args);
  1251. }, 1);
  1252. return deferred.promise();
  1253. })[callback ? key : 'done'](callback || Base.noop);
  1254. } else {
  1255. return rlts[0];
  1256. }
  1257. },
  1258. destroy: function () {
  1259. _destroy.apply(this, arguments);
  1260. this._widgets = null;
  1261. }
  1262. });
  1263. /**
  1264. * 添加组件
  1265. * @grammar Uploader.register(proto);
  1266. * @grammar Uploader.register(map, proto);
  1267. * @param {object} responseMap API 名称与函数实现的映射
  1268. * @param {object} proto 组件原型,构造函数通过 constructor 属性定义
  1269. * @method Uploader.register
  1270. * @for Uploader
  1271. * @example
  1272. * Uploader.register({
  1273. * 'make-thumb': 'makeThumb'
  1274. * }, {
  1275. * init: function( options ) {},
  1276. * makeThumb: function() {}
  1277. * });
  1278. *
  1279. * Uploader.register({
  1280. * 'make-thumb': function() {
  1281. *
  1282. * }
  1283. * });
  1284. */
  1285. Uploader.register = Widget.register = function (responseMap, widgetProto) {
  1286. var map = {init: 'init', destroy: 'destroy', name: 'anonymous'},
  1287. klass;
  1288. if (arguments.length === 1) {
  1289. widgetProto = responseMap;
  1290. // 自动生成 map 表。
  1291. $.each(widgetProto, function (key) {
  1292. if (key[0] === '_' || key === 'name') {
  1293. key === 'name' && (map.name = widgetProto.name);
  1294. return;
  1295. }
  1296. map[key.replace(/[A-Z]/g, '-$&').toLowerCase()] = key;
  1297. });
  1298. } else {
  1299. map = $.extend(map, responseMap);
  1300. }
  1301. widgetProto.responseMap = map;
  1302. klass = Base.inherits(Widget, widgetProto);
  1303. klass._name = map.name;
  1304. widgetClass.push(klass);
  1305. return klass;
  1306. };
  1307. /**
  1308. * 删除插件,只有在注册时指定了名字的才能被删除。
  1309. * @grammar Uploader.unRegister(name);
  1310. * @param {string} name 组件名字
  1311. * @method Uploader.unRegister
  1312. * @for Uploader
  1313. * @example
  1314. *
  1315. * Uploader.register({
  1316. * name: 'custom',
  1317. *
  1318. * 'make-thumb': function() {
  1319. *
  1320. * }
  1321. * });
  1322. *
  1323. * Uploader.unRegister('custom');
  1324. */
  1325. Uploader.unRegister = Widget.unRegister = function (name) {
  1326. if (!name || name === 'anonymous') {
  1327. return;
  1328. }
  1329. // 删除指定的插件。
  1330. for (var i = widgetClass.length; i--;) {
  1331. if (widgetClass[i]._name === name) {
  1332. widgetClass.splice(i, 1)
  1333. }
  1334. }
  1335. };
  1336. return Widget;
  1337. });
  1338. /**
  1339. * @fileOverview 文件选择相关
  1340. */
  1341. define('widgets/filepicker', [
  1342. 'base',
  1343. 'uploader',
  1344. 'lib/filepicker',
  1345. 'widgets/widget'
  1346. ], function (Base, Uploader, FilePicker) {
  1347. var $ = Base.$;
  1348. $.extend(Uploader.options, {
  1349. /**
  1350. * @property {Selector | Object} [pick=undefined]
  1351. * @namespace options
  1352. * @for Uploader
  1353. * @description 指定选择文件的按钮容器,不指定则不创建按钮。
  1354. *
  1355. * * `id` {Seletor|dom} 指定选择文件的按钮容器,不指定则不创建按钮。**注意** 这里虽然写的是 id, 但是不是只支持 id, 还支持 class, 或者 dom 节点。
  1356. * * `label` {String} 请采用 `innerHTML` 代替
  1357. * * `innerHTML` {String} 指定按钮文字。不指定时优先从指定的容器中看是否自带文字。
  1358. * * `multiple` {Boolean} 是否开起同时选择多个文件能力。
  1359. */
  1360. pick: null,
  1361. /**
  1362. * @property {Arroy} [accept=null]
  1363. * @namespace options
  1364. * @for Uploader
  1365. * @description 指定接受哪些类型的文件。 由于目前还有ext转mimeType表,所以这里需要分开指定。
  1366. *
  1367. * * `title` {String} 文字描述
  1368. * * `extensions` {String} 允许的文件后缀,不带点,多个用逗号分割。
  1369. * * `mimeTypes` {String} 多个用逗号分割。
  1370. *
  1371. * 如:
  1372. *
  1373. * ```
  1374. * {
  1375. * title: 'Images',
  1376. * extensions: 'gif,jpg,jpeg,bmp,png',
  1377. * mimeTypes: 'image/*'
  1378. * }
  1379. * ```
  1380. */
  1381. accept: null/*{
  1382. title: 'Images',
  1383. extensions: 'gif,jpg,jpeg,bmp,png',
  1384. mimeTypes: 'image/*'
  1385. }*/
  1386. });
  1387. return Uploader.register({
  1388. name: 'picker',
  1389. init: function (opts) {
  1390. this.pickers = [];
  1391. return opts.pick && this.addBtn(opts.pick);
  1392. },
  1393. refresh: function () {
  1394. $.each(this.pickers, function () {
  1395. this.refresh();
  1396. });
  1397. },
  1398. /**
  1399. * @method addButton
  1400. * @for Uploader
  1401. * @grammar addButton( pick ) => Promise
  1402. * @description
  1403. * 添加文件选择按钮,如果一个按钮不够,需要调用此方法来添加。参数跟[options.pick](#WebUploader:Uploader:options)一致。
  1404. * @example
  1405. * uploader.addButton({
  1406. * id: '#btnContainer',
  1407. * innerHTML: '选择文件'
  1408. * });
  1409. */
  1410. addBtn: function (pick) {
  1411. var me = this,
  1412. opts = me.options,
  1413. accept = opts.accept,
  1414. promises = [];
  1415. if (!pick) {
  1416. return;
  1417. }
  1418. $.isPlainObject(pick) || (pick = {
  1419. id: pick
  1420. });
  1421. $(pick.id).each(function () {
  1422. var options, picker, deferred;
  1423. deferred = Base.Deferred();
  1424. options = $.extend({}, pick, {
  1425. accept: $.isPlainObject(accept) ? [accept] : accept,
  1426. swf: opts.swf,
  1427. runtimeOrder: opts.runtimeOrder,
  1428. id: this
  1429. });
  1430. picker = new FilePicker(options);
  1431. picker.once('ready', deferred.resolve);
  1432. picker.on('select', function (files) {
  1433. me.owner.request('add-file', [files]);
  1434. });
  1435. picker.init();
  1436. me.pickers.push(picker);
  1437. promises.push(deferred.promise());
  1438. });
  1439. return Base.when.apply(Base, promises);
  1440. },
  1441. disable: function () {
  1442. $.each(this.pickers, function () {
  1443. this.disable();
  1444. });
  1445. },
  1446. enable: function () {
  1447. $.each(this.pickers, function () {
  1448. this.enable();
  1449. });
  1450. },
  1451. destroy: function () {
  1452. $.each(this.pickers, function () {
  1453. this.destroy();
  1454. });
  1455. this.pickers = null;
  1456. }
  1457. });
  1458. });
  1459. /**
  1460. * @fileOverview Image
  1461. */
  1462. define('lib/image', [
  1463. 'base',
  1464. 'runtime/client',
  1465. 'lib/blob'
  1466. ], function (Base, RuntimeClient, Blob) {
  1467. var $ = Base.$;
  1468. // 构造器。
  1469. function Image(opts) {
  1470. this.options = $.extend({}, Image.options, opts);
  1471. RuntimeClient.call(this, 'Image');
  1472. this.on('load', function () {
  1473. this._info = this.exec('info');
  1474. this._meta = this.exec('meta');
  1475. });
  1476. }
  1477. // 默认选项。
  1478. Image.options = {
  1479. // 默认的图片处理质量
  1480. quality: 90,
  1481. // 是否裁剪
  1482. crop: false,
  1483. // 是否保留头部信息
  1484. preserveHeaders: false,
  1485. // 是否允许放大。
  1486. allowMagnify: false
  1487. };
  1488. // 继承RuntimeClient.
  1489. Base.inherits(RuntimeClient, {
  1490. constructor: Image,
  1491. info: function (val) {
  1492. // setter
  1493. if (val) {
  1494. this._info = val;
  1495. return this;
  1496. }
  1497. // getter
  1498. return this._info;
  1499. },
  1500. meta: function (val) {
  1501. // setter
  1502. if (val) {
  1503. this._meta = val;
  1504. return this;
  1505. }
  1506. // getter
  1507. return this._meta;
  1508. },
  1509. loadFromBlob: function (blob) {
  1510. var me = this,
  1511. ruid = blob.getRuid();
  1512. this.connectRuntime(ruid, function () {
  1513. me.exec('init', me.options);
  1514. me.exec('loadFromBlob', blob);
  1515. });
  1516. },
  1517. resize: function () {
  1518. var args = Base.slice(arguments);
  1519. return this.exec.apply(this, ['resize'].concat(args));
  1520. },
  1521. crop: function () {
  1522. var args = Base.slice(arguments);
  1523. return this.exec.apply(this, ['crop'].concat(args));
  1524. },
  1525. getAsDataUrl: function (type) {
  1526. return this.exec('getAsDataUrl', type);
  1527. },
  1528. getAsBlob: function (type) {
  1529. var blob = this.exec('getAsBlob', type);
  1530. return new Blob(this.getRuid(), blob);
  1531. }
  1532. });
  1533. return Image;
  1534. });
  1535. /**
  1536. * @fileOverview 图片操作, 负责预览图片和上传前压缩图片
  1537. */
  1538. define('widgets/image', [
  1539. 'base',
  1540. 'uploader',
  1541. 'lib/image',
  1542. 'widgets/widget'
  1543. ], function (Base, Uploader, Image) {
  1544. var $ = Base.$,
  1545. throttle;
  1546. // 根据要处理的文件大小来节流,一次不能处理太多,会卡。
  1547. throttle = (function (max) {
  1548. var occupied = 0,
  1549. waiting = [],
  1550. tick = function () {
  1551. var item;
  1552. while (waiting.length && occupied < max) {
  1553. item = waiting.shift();
  1554. occupied += item[0];
  1555. item[1]();
  1556. }
  1557. };
  1558. return function (emiter, size, cb) {
  1559. waiting.push([size, cb]);
  1560. emiter.once('destroy', function () {
  1561. occupied -= size;
  1562. setTimeout(tick, 1);
  1563. });
  1564. setTimeout(tick, 1);
  1565. };
  1566. })(5 * 1024 * 1024);
  1567. $.extend(Uploader.options, {
  1568. /**
  1569. * @property {Object} [thumb]
  1570. * @namespace options
  1571. * @for Uploader
  1572. * @description 配置生成缩略图的选项。
  1573. *
  1574. * 默认为:
  1575. *
  1576. * ```javascript
  1577. * {
  1578. * width: 110,
  1579. * height: 110,
  1580. *
  1581. * // 图片质量,只有type为`image/jpeg`的时候才有效。
  1582. * quality: 70,
  1583. *
  1584. * // 是否允许放大,如果想要生成小图的时候不失真,此选项应该设置为false.
  1585. * allowMagnify: true,
  1586. *
  1587. * // 是否允许裁剪。
  1588. * crop: true,
  1589. *
  1590. * // 为空的话则保留原有图片格式。
  1591. * // 否则强制转换成指定的类型。
  1592. * type: 'image/jpeg'
  1593. * }
  1594. * ```
  1595. */
  1596. thumb: {
  1597. width: 110,
  1598. height: 110,
  1599. quality: 70,
  1600. allowMagnify: true,
  1601. crop: true,
  1602. preserveHeaders: false,
  1603. // 为空的话则保留原有图片格式。
  1604. // 否则强制转换成指定的类型。
  1605. // IE 8下面 base64 大小不能超过 32K 否则预览失败,而非 jpeg 编码的图片很可
  1606. // 能会超过 32k, 所以这里设置成预览的时候都是 image/jpeg
  1607. type: 'image/jpeg'
  1608. },
  1609. /**
  1610. * @property {Object} [compress]
  1611. * @namespace options
  1612. * @for Uploader
  1613. * @description 配置压缩的图片的选项。如果此选项为`false`, 则图片在上传前不进行压缩。
  1614. *
  1615. * 默认为:
  1616. *
  1617. * ```javascript
  1618. * {
  1619. * width: 1600,
  1620. * height: 1600,
  1621. *
  1622. * // 图片质量,只有type为`image/jpeg`的时候才有效。
  1623. * quality: 90,
  1624. *
  1625. * // 是否允许放大,如果想要生成小图的时候不失真,此选项应该设置为false.
  1626. * allowMagnify: false,
  1627. *
  1628. * // 是否允许裁剪。
  1629. * crop: false,
  1630. *
  1631. * // 是否保留头部meta信息。
  1632. * preserveHeaders: true,
  1633. *
  1634. * // 如果发现压缩后文件大小比原来还大,则使用原来图片
  1635. * // 此属性可能会影响图片自动纠正功能
  1636. * noCompressIfLarger: false,
  1637. *
  1638. * // 单位字节,如果图片大小小于此值,不会采用压缩。
  1639. * compressSize: 0
  1640. * }
  1641. * ```
  1642. */
  1643. compress: {
  1644. width: 1600,
  1645. height: 1600,
  1646. quality: 90,
  1647. allowMagnify: false,
  1648. crop: false,
  1649. preserveHeaders: true
  1650. }
  1651. });
  1652. return Uploader.register({
  1653. name: 'image',
  1654. /**
  1655. * 生成缩略图,此过程为异步,所以需要传入`callback`。
  1656. * 通常情况在图片加入队里后调用此方法来生成预览图以增强交互效果。
  1657. *
  1658. * 当 width 或者 height 的值介于 0 - 1 时,被当成百分比使用。
  1659. *
  1660. * `callback`中可以接收到两个参数。
  1661. * * 第一个为error,如果生成缩略图有错误,此error将为真。
  1662. * * 第二个为ret, 缩略图的Data URL值。
  1663. *
  1664. * **注意**
  1665. * Date URL在IE6/7中不支持,所以不用调用此方法了,直接显示一张暂不支持预览图片好了。
  1666. * 也可以借助服务端,将 base64 数据传给服务端,生成一个临时文件供预览。
  1667. *
  1668. * @method makeThumb
  1669. * @grammar makeThumb( file, callback ) => undefined
  1670. * @grammar makeThumb( file, callback, width, height ) => undefined
  1671. * @for Uploader
  1672. * @example
  1673. *
  1674. * uploader.on( 'fileQueued', function( file ) {
  1675. * var $li = ...;
  1676. *
  1677. * uploader.makeThumb( file, function( error, ret ) {
  1678. * if ( error ) {
  1679. * $li.text('预览错误');
  1680. * } else {
  1681. * $li.append('<img alt="" src="' + ret + '" />');
  1682. * }
  1683. * });
  1684. *
  1685. * });
  1686. */
  1687. makeThumb: function (file, cb, width, height) {
  1688. var opts, image;
  1689. file = this.request('get-file', file);
  1690. // 只预览图片格式。
  1691. if (!file.type.match(/^image/)) {
  1692. cb(true);
  1693. return;
  1694. }
  1695. opts = $.extend({}, this.options.thumb);
  1696. // 如果传入的是object.
  1697. if ($.isPlainObject(width)) {
  1698. opts = $.extend(opts, width);
  1699. width = null;
  1700. }
  1701. width = width || opts.width;
  1702. height = height || opts.height;
  1703. image = new Image(opts);
  1704. image.once('load', function () {
  1705. file._info = file._info || image.info();
  1706. file._meta = file._meta || image.meta();
  1707. // 如果 width 的值介于 0 - 1
  1708. // 说明设置的是百分比。
  1709. if (width <= 1 && width > 0) {
  1710. width = file._info.width * width;
  1711. }
  1712. // 同样的规则应用于 height
  1713. if (height <= 1 && height > 0) {
  1714. height = file._info.height * height;
  1715. }
  1716. image.resize(width, height);
  1717. });
  1718. // 当 resize 完后
  1719. image.once('complete', function () {
  1720. cb(false, image.getAsDataUrl(opts.type));
  1721. image.destroy();
  1722. });
  1723. image.once('error', function (reason) {
  1724. cb(reason || true);
  1725. image.destroy();
  1726. });
  1727. throttle(image, file.source.size, function () {
  1728. file._info && image.info(file._info);
  1729. file._meta && image.meta(file._meta);
  1730. image.loadFromBlob(file.source);
  1731. });
  1732. },
  1733. beforeSendFile: function (file) {
  1734. var opts = this.options.compress || this.options.resize,
  1735. compressSize = opts && opts.compressSize || 0,
  1736. noCompressIfLarger = opts && opts.noCompressIfLarger || false,
  1737. image, deferred;
  1738. file = this.request('get-file', file);
  1739. // 只压缩 jpeg 图片格式。
  1740. // gif 可能会丢失针
  1741. // bmp png 基本上尺寸都不大,且压缩比比较小。
  1742. if (!opts || !~'image/jpeg,image/jpg'.indexOf(file.type) ||
  1743. file.size < compressSize ||
  1744. file._compressed) {
  1745. return;
  1746. }
  1747. opts = $.extend({}, opts);
  1748. deferred = Base.Deferred();
  1749. image = new Image(opts);
  1750. deferred.always(function () {
  1751. image.destroy();
  1752. image = null;
  1753. });
  1754. image.once('error', deferred.reject);
  1755. image.once('load', function () {
  1756. var width = opts.width,
  1757. height = opts.height;
  1758. file._info = file._info || image.info();
  1759. file._meta = file._meta || image.meta();
  1760. // 如果 width 的值介于 0 - 1
  1761. // 说明设置的是百分比。
  1762. if (width <= 1 && width > 0) {
  1763. width = file._info.width * width;
  1764. }
  1765. // 同样的规则应用于 height
  1766. if (height <= 1 && height > 0) {
  1767. height = file._info.height * height;
  1768. }
  1769. image.resize(width, height);
  1770. });
  1771. image.once('complete', function () {
  1772. var blob, size;
  1773. // 移动端 UC / qq 浏览器的无图模式下
  1774. // ctx.getImageData 处理大图的时候会报 Exception
  1775. // INDEX_SIZE_ERR: DOM Exception 1
  1776. try {
  1777. blob = image.getAsBlob(opts.type);
  1778. size = file.size;
  1779. // 如果压缩后,比原来还大则不用压缩后的。
  1780. if (!noCompressIfLarger || blob.size < size) {
  1781. // file.source.destroy && file.source.destroy();
  1782. file.source = blob;
  1783. file.size = blob.size;
  1784. file.trigger('resize', blob.size, size);
  1785. }
  1786. // 标记,避免重复压缩。
  1787. file._compressed = true;
  1788. deferred.resolve();
  1789. } catch (e) {
  1790. // 出错了直接继续,让其上传原始图片
  1791. deferred.resolve();
  1792. }
  1793. });
  1794. file._info && image.info(file._info);
  1795. file._meta && image.meta(file._meta);
  1796. image.loadFromBlob(file.source);
  1797. return deferred.promise();
  1798. }
  1799. });
  1800. });
  1801. /**
  1802. * @fileOverview 文件属性封装
  1803. */
  1804. define('file', [
  1805. 'base',
  1806. 'mediator'
  1807. ], function (Base, Mediator) {
  1808. var $ = Base.$,
  1809. idPrefix = 'WU_FILE_',
  1810. idSuffix = 0,
  1811. rExt = /\.([^.]+)$/,
  1812. statusMap = {};
  1813. function gid() {
  1814. return idPrefix + idSuffix++;
  1815. }
  1816. /**
  1817. * 文件类
  1818. * @class File
  1819. * @constructor 构造函数
  1820. * @grammar new File( source ) => File
  1821. * @param {Lib.File} source [lib.File](#Lib.File)实例, 此source对象是带有Runtime信息的。
  1822. */
  1823. function WUFile(source) {
  1824. /**
  1825. * 文件名,包括扩展名(后缀)
  1826. * @property name
  1827. * @type {string}
  1828. */
  1829. this.name = source.name || 'Untitled';
  1830. /**
  1831. * 文件体积(字节)
  1832. * @property size
  1833. * @type {uint}
  1834. * @default 0
  1835. */
  1836. this.size = source.size || 0;
  1837. /**
  1838. * 文件MIMETYPE类型,与文件类型的对应关系请参考[http://t.cn/z8ZnFny](http://t.cn/z8ZnFny)
  1839. * @property type
  1840. * @type {string}
  1841. * @default 'application/octet-stream'
  1842. */
  1843. this.type = source.type || 'application/octet-stream';
  1844. /**
  1845. * 文件最后修改日期
  1846. * @property lastModifiedDate
  1847. * @type {int}
  1848. * @default 当前时间戳
  1849. */
  1850. this.lastModifiedDate = source.lastModifiedDate || (new Date() * 1);
  1851. /**
  1852. * 文件ID,每个对象具有唯一ID,与文件名无关
  1853. * @property id
  1854. * @type {string}
  1855. */
  1856. this.id = gid();
  1857. /**
  1858. * 文件扩展名,通过文件名获取,例如test.png的扩展名为png
  1859. * @property ext
  1860. * @type {string}
  1861. */
  1862. this.ext = rExt.exec(this.name) ? RegExp.$1 : '';
  1863. /**
  1864. * 状态文字说明。在不同的status语境下有不同的用途。
  1865. * @property statusText
  1866. * @type {string}
  1867. */
  1868. this.statusText = '';
  1869. // 存储文件状态,防止通过属性直接修改
  1870. statusMap[this.id] = WUFile.Status.INITED;
  1871. this.source = source;
  1872. this.loaded = 0;
  1873. this.on('error', function (msg) {
  1874. this.setStatus(WUFile.Status.ERROR, msg);
  1875. });
  1876. }
  1877. $.extend(WUFile.prototype, {
  1878. /**
  1879. * 设置状态,状态变化时会触发`change`事件。
  1880. * @method setStatus
  1881. * @grammar setStatus( status[, statusText] );
  1882. * @param {File.Status|String} status [文件状态值](#WebUploader:File:File.Status)
  1883. * @param {String} [statusText=''] 状态说明,常在error时使用,用http, abort,server等来标记是由于什么原因导致文件错误。
  1884. */
  1885. setStatus: function (status, text) {
  1886. var prevStatus = statusMap[this.id];
  1887. typeof text !== 'undefined' && (this.statusText = text);
  1888. if (status !== prevStatus) {
  1889. statusMap[this.id] = status;
  1890. /**
  1891. * 文件状态变化
  1892. * @event statuschange
  1893. */
  1894. this.trigger('statuschange', status, prevStatus);
  1895. }
  1896. },
  1897. /**
  1898. * 获取文件状态
  1899. * @return {File.Status}
  1900. * @example
  1901. 文件状态具体包括以下几种类型:
  1902. {
  1903. // 初始化
  1904. INITED: 0,
  1905. // 已入队列
  1906. QUEUED: 1,
  1907. // 正在上传
  1908. PROGRESS: 2,
  1909. // 上传出错
  1910. ERROR: 3,
  1911. // 上传成功
  1912. COMPLETE: 4,
  1913. // 上传取消
  1914. CANCELLED: 5
  1915. }
  1916. */
  1917. getStatus: function () {
  1918. return statusMap[this.id];
  1919. },
  1920. /**
  1921. * 获取文件原始信息。
  1922. * @return {*}
  1923. */
  1924. getSource: function () {
  1925. return this.source;
  1926. },
  1927. destroy: function () {
  1928. this.off();
  1929. delete statusMap[this.id];
  1930. }
  1931. });
  1932. Mediator.installTo(WUFile.prototype);
  1933. /**
  1934. * 文件状态值,具体包括以下几种类型:
  1935. * * `inited` 初始状态
  1936. * * `queued` 已经进入队列, 等待上传
  1937. * * `progress` 上传中
  1938. * * `complete` 上传完成。
  1939. * * `error` 上传出错,可重试
  1940. * * `interrupt` 上传中断,可续传。
  1941. * * `invalid` 文件不合格,不能重试上传。会自动从队列中移除。
  1942. * * `cancelled` 文件被移除。
  1943. * @property {Object} Status
  1944. * @namespace File
  1945. * @class File
  1946. * @static
  1947. */
  1948. WUFile.Status = {
  1949. INITED: 'inited', // 初始状态
  1950. QUEUED: 'queued', // 已经进入队列, 等待上传
  1951. PROGRESS: 'progress', // 上传中
  1952. ERROR: 'error', // 上传出错,可重试
  1953. COMPLETE: 'complete', // 上传完成。
  1954. CANCELLED: 'cancelled', // 上传取消。
  1955. INTERRUPT: 'interrupt', // 上传中断,可续传。
  1956. INVALID: 'invalid' // 文件不合格,不能重试上传。
  1957. };
  1958. return WUFile;
  1959. });
  1960. /**
  1961. * @fileOverview 文件队列
  1962. */
  1963. define('queue', [
  1964. 'base',
  1965. 'mediator',
  1966. 'file'
  1967. ], function (Base, Mediator, WUFile) {
  1968. var $ = Base.$,
  1969. STATUS = WUFile.Status;
  1970. /**
  1971. * 文件队列, 用来存储各个状态中的文件。
  1972. * @class Queue
  1973. * @extends Mediator
  1974. */
  1975. function Queue() {
  1976. /**
  1977. * 统计文件数。
  1978. * * `numOfQueue` 队列中的文件数。
  1979. * * `numOfSuccess` 上传成功的文件数
  1980. * * `numOfCancel` 被取消的文件数
  1981. * * `numOfProgress` 正在上传中的文件数
  1982. * * `numOfUploadFailed` 上传错误的文件数。
  1983. * * `numOfInvalid` 无效的文件数。
  1984. * * `numofDeleted` 被移除的文件数。
  1985. * @property {Object} stats
  1986. */
  1987. this.stats = {
  1988. numOfQueue: 0,
  1989. numOfSuccess: 0,
  1990. numOfCancel: 0,
  1991. numOfProgress: 0,
  1992. numOfUploadFailed: 0,
  1993. numOfInvalid: 0,
  1994. numofDeleted: 0,
  1995. numofInterrupt: 0
  1996. };
  1997. // 上传队列,仅包括等待上传的文件
  1998. this._queue = [];
  1999. // 存储所有文件
  2000. this._map = {};
  2001. }
  2002. $.extend(Queue.prototype, {
  2003. /**
  2004. * 将新文件加入对队列尾部
  2005. *
  2006. * @method append
  2007. * @param {File} file 文件对象
  2008. */
  2009. append: function (file) {
  2010. this._queue.push(file);
  2011. this._fileAdded(file);
  2012. return this;
  2013. },
  2014. /**
  2015. * 将新文件加入对队列头部
  2016. *
  2017. * @method prepend
  2018. * @param {File} file 文件对象
  2019. */
  2020. prepend: function (file) {
  2021. this._queue.unshift(file);
  2022. this._fileAdded(file);
  2023. return this;
  2024. },
  2025. /**
  2026. * 获取文件对象
  2027. *
  2028. * @method getFile
  2029. * @param {String} fileId 文件ID
  2030. * @return {File}
  2031. */
  2032. getFile: function (fileId) {
  2033. if (typeof fileId !== 'string') {
  2034. return fileId;
  2035. }
  2036. return this._map[fileId];
  2037. },
  2038. /**
  2039. * 从队列中取出一个指定状态的文件。
  2040. * @grammar fetch( status ) => File
  2041. * @method fetch
  2042. * @param {String} status [文件状态值](#WebUploader:File:File.Status)
  2043. * @return {File} [File](#WebUploader:File)
  2044. */
  2045. fetch: function (status) {
  2046. var len = this._queue.length,
  2047. i, file;
  2048. status = status || STATUS.QUEUED;
  2049. for (i = 0; i < len; i++) {
  2050. file = this._queue[i];
  2051. if (status === file.getStatus()) {
  2052. return file;
  2053. }
  2054. }
  2055. return null;
  2056. },
  2057. /**
  2058. * 对队列进行排序,能够控制文件上传顺序。
  2059. * @grammar sort( fn ) => undefined
  2060. * @method sort
  2061. * @param {Function} fn 排序方法
  2062. */
  2063. sort: function (fn) {
  2064. if (typeof fn === 'function') {
  2065. this._queue.sort(fn);
  2066. }
  2067. },
  2068. /**
  2069. * 获取指定类型的文件列表, 列表中每一个成员为[File](#WebUploader:File)对象。
  2070. * @grammar getFiles( [status1[, status2 ...]] ) => Array
  2071. * @method getFiles
  2072. * @param {String} [status] [文件状态值](#WebUploader:File:File.Status)
  2073. */
  2074. getFiles: function () {
  2075. var sts = [].slice.call(arguments, 0),
  2076. ret = [],
  2077. i = 0,
  2078. len = this._queue.length,
  2079. file;
  2080. for (; i < len; i++) {
  2081. file = this._queue[i];
  2082. if (sts.length && !~$.inArray(file.getStatus(), sts)) {
  2083. continue;
  2084. }
  2085. ret.push(file);
  2086. }
  2087. return ret;
  2088. },
  2089. /**
  2090. * 在队列中删除文件。
  2091. * @grammar removeFile( file ) => Array
  2092. * @method removeFile
  2093. * @param {File} 文件对象。
  2094. */
  2095. removeFile: function (file) {
  2096. var me = this,
  2097. existing = this._map[file.id];
  2098. if (existing) {
  2099. delete this._map[file.id];
  2100. file.destroy();
  2101. this.stats.numofDeleted++;
  2102. }
  2103. },
  2104. _fileAdded: function (file) {
  2105. var me = this,
  2106. existing = this._map[file.id];
  2107. if (!existing) {
  2108. this._map[file.id] = file;
  2109. file.on('statuschange', function (cur, pre) {
  2110. me._onFileStatusChange(cur, pre);
  2111. });
  2112. }
  2113. },
  2114. _onFileStatusChange: function (curStatus, preStatus) {
  2115. var stats = this.stats;
  2116. switch (preStatus) {
  2117. case STATUS.PROGRESS:
  2118. stats.numOfProgress--;
  2119. break;
  2120. case STATUS.QUEUED:
  2121. stats.numOfQueue--;
  2122. break;
  2123. case STATUS.ERROR:
  2124. stats.numOfUploadFailed--;
  2125. break;
  2126. case STATUS.INVALID:
  2127. stats.numOfInvalid--;
  2128. break;
  2129. case STATUS.INTERRUPT:
  2130. stats.numofInterrupt--;
  2131. break;
  2132. }
  2133. switch (curStatus) {
  2134. case STATUS.QUEUED:
  2135. stats.numOfQueue++;
  2136. break;
  2137. case STATUS.PROGRESS:
  2138. stats.numOfProgress++;
  2139. break;
  2140. case STATUS.ERROR:
  2141. stats.numOfUploadFailed++;
  2142. break;
  2143. case STATUS.COMPLETE:
  2144. stats.numOfSuccess++;
  2145. break;
  2146. case STATUS.CANCELLED:
  2147. stats.numOfCancel++;
  2148. break;
  2149. case STATUS.INVALID:
  2150. stats.numOfInvalid++;
  2151. break;
  2152. case STATUS.INTERRUPT:
  2153. stats.numofInterrupt++;
  2154. break;
  2155. }
  2156. }
  2157. });
  2158. Mediator.installTo(Queue.prototype);
  2159. return Queue;
  2160. });
  2161. /**
  2162. * @fileOverview 队列
  2163. */
  2164. define('widgets/queue', [
  2165. 'base',
  2166. 'uploader',
  2167. 'queue',
  2168. 'file',
  2169. 'lib/file',
  2170. 'runtime/client',
  2171. 'widgets/widget'
  2172. ], function (Base, Uploader, Queue, WUFile, File, RuntimeClient) {
  2173. var $ = Base.$,
  2174. rExt = /\.\w+$/,
  2175. Status = WUFile.Status;
  2176. return Uploader.register({
  2177. name: 'queue',
  2178. init: function (opts) {
  2179. var me = this,
  2180. deferred, len, i, item, arr, accept, runtime;
  2181. if ($.isPlainObject(opts.accept)) {
  2182. opts.accept = [opts.accept];
  2183. }
  2184. // accept中的中生成匹配正则。
  2185. if (opts.accept) {
  2186. arr = [];
  2187. for (i = 0, len = opts.accept.length; i < len; i++) {
  2188. item = opts.accept[i].extensions;
  2189. item && arr.push(item);
  2190. }
  2191. if (arr.length) {
  2192. accept = '\\.' + arr.join(',')
  2193. .replace(/,/g, '$|\\.')
  2194. .replace(/\*/g, '.*') + '$';
  2195. }
  2196. me.accept = new RegExp(accept, 'i');
  2197. }
  2198. me.queue = new Queue();
  2199. me.stats = me.queue.stats;
  2200. // 如果当前不是html5运行时,那就算了。
  2201. // 不执行后续操作
  2202. if (this.request('predict-runtime-type') !== 'html5') {
  2203. return;
  2204. }
  2205. // 创建一个 html5 运行时的 placeholder
  2206. // 以至于外部添加原生 File 对象的时候能正确包裹一下供 webuploader 使用。
  2207. deferred = Base.Deferred();
  2208. this.placeholder = runtime = new RuntimeClient('Placeholder');
  2209. runtime.connectRuntime({
  2210. runtimeOrder: 'html5'
  2211. }, function () {
  2212. me._ruid = runtime.getRuid();
  2213. deferred.resolve();
  2214. });
  2215. return deferred.promise();
  2216. },
  2217. // 为了支持外部直接添加一个原生File对象。
  2218. _wrapFile: function (file) {
  2219. if (!(file instanceof WUFile)) {
  2220. if (!(file instanceof File)) {
  2221. if (!this._ruid) {
  2222. throw new Error('Can\'t add external files.');
  2223. }
  2224. file = new File(this._ruid, file);
  2225. }
  2226. file = new WUFile(file);
  2227. }
  2228. return file;
  2229. },
  2230. // 判断文件是否可以被加入队列
  2231. acceptFile: function (file) {
  2232. var invalid = !file || !file.size || this.accept &&
  2233. // 如果名字中有后缀,才做后缀白名单处理。
  2234. rExt.exec(file.name) && !this.accept.test(file.name);
  2235. return !invalid;
  2236. },
  2237. /**
  2238. * @event beforeFileQueued
  2239. * @param {File} file File对象
  2240. * @description 当文件被加入队列之前触发,此事件的handler返回值为`false`,则此文件不会被添加进入队列。
  2241. * @for Uploader
  2242. */
  2243. /**
  2244. * @event fileQueued
  2245. * @param {File} file File对象
  2246. * @description 当文件被加入队列以后触发。
  2247. * @for Uploader
  2248. */
  2249. _addFile: function (file) {
  2250. var me = this;
  2251. file = me._wrapFile(file);
  2252. // 不过类型判断允许不允许,先派送 `beforeFileQueued`
  2253. if (!me.owner.trigger('beforeFileQueued', file)) {
  2254. return;
  2255. }
  2256. // 类型不匹配,则派送错误事件,并返回。
  2257. if (!me.acceptFile(file)) {
  2258. me.owner.trigger('error', 'Q_TYPE_DENIED', file);
  2259. return;
  2260. }
  2261. me.queue.append(file);
  2262. me.owner.trigger('fileQueued', file);
  2263. return file;
  2264. },
  2265. getFile: function (fileId) {
  2266. return this.queue.getFile(fileId);
  2267. },
  2268. /**
  2269. * @event filesQueued
  2270. * @param {File} files 数组,内容为原始File(lib/File)对象。
  2271. * @description 当一批文件添加进队列以后触发。
  2272. * @for Uploader
  2273. */
  2274. /**
  2275. * @property {Boolean} [auto=false]
  2276. * @namespace options
  2277. * @for Uploader
  2278. * @description 设置为 true 后,不需要手动调用上传,有文件选择即开始上传。
  2279. *
  2280. */
  2281. /**
  2282. * @method addFiles
  2283. * @grammar addFiles( file ) => undefined
  2284. * @grammar addFiles( [file1, file2 ...] ) => undefined
  2285. * @param {Array of File or File} [files] Files 对象 数组
  2286. * @description 添加文件到队列
  2287. * @for Uploader
  2288. */
  2289. addFile: function (files) {
  2290. var me = this;
  2291. if (!files.length) {
  2292. files = [files];
  2293. }
  2294. files = $.map(files, function (file) {
  2295. return me._addFile(file);
  2296. });
  2297. me.owner.trigger('filesQueued', files);
  2298. if (me.options.auto) {
  2299. setTimeout(function () {
  2300. me.request('start-upload');
  2301. }, 20);
  2302. }
  2303. },
  2304. getStats: function () {
  2305. return this.stats;
  2306. },
  2307. /**
  2308. * @event fileDequeued
  2309. * @param {File} file File对象
  2310. * @description 当文件被移除队列后触发。
  2311. * @for Uploader
  2312. */
  2313. /**
  2314. * @method removeFile
  2315. * @grammar removeFile( file ) => undefined
  2316. * @grammar removeFile( id ) => undefined
  2317. * @grammar removeFile( file, true ) => undefined
  2318. * @grammar removeFile( id, true ) => undefined
  2319. * @param {File|id} file File对象或这File对象的id
  2320. * @description 移除某一文件, 默认只会标记文件状态为已取消,如果第二个参数为 `true` 则会从 queue 中移除。
  2321. * @for Uploader
  2322. * @example
  2323. *
  2324. * $li.on('click', '.remove-this', function() {
  2325. * uploader.removeFile( file );
  2326. * })
  2327. */
  2328. removeFile: function (file, remove) {
  2329. var me = this;
  2330. file = file.id ? file : me.queue.getFile(file);
  2331. this.request('cancel-file', file);
  2332. if (remove) {
  2333. this.queue.removeFile(file);
  2334. }
  2335. },
  2336. /**
  2337. * @method getFiles
  2338. * @grammar getFiles() => Array
  2339. * @grammar getFiles( status1, status2, status... ) => Array
  2340. * @description 返回指定状态的文件集合,不传参数将返回所有状态的文件。
  2341. * @for Uploader
  2342. * @example
  2343. * console.log( uploader.getFiles() ); // => all files
  2344. * console.log( uploader.getFiles('error') ) // => all error files.
  2345. */
  2346. getFiles: function () {
  2347. return this.queue.getFiles.apply(this.queue, arguments);
  2348. },
  2349. fetchFile: function () {
  2350. return this.queue.fetch.apply(this.queue, arguments);
  2351. },
  2352. /**
  2353. * @method retry
  2354. * @grammar retry() => undefined
  2355. * @grammar retry( file ) => undefined
  2356. * @description 重试上传,重试指定文件,或者从出错的文件开始重新上传。
  2357. * @for Uploader
  2358. * @example
  2359. * function retry() {
  2360. * uploader.retry();
  2361. * }
  2362. */
  2363. retry: function (file, noForceStart) {
  2364. var me = this,
  2365. files, i, len;
  2366. if (file) {
  2367. file = file.id ? file : me.queue.getFile(file);
  2368. file.setStatus(Status.QUEUED);
  2369. noForceStart || me.request('start-upload');
  2370. return;
  2371. }
  2372. files = me.queue.getFiles(Status.ERROR);
  2373. i = 0;
  2374. len = files.length;
  2375. for (; i < len; i++) {
  2376. file = files[i];
  2377. file.setStatus(Status.QUEUED);
  2378. }
  2379. me.request('start-upload');
  2380. },
  2381. /**
  2382. * @method sort
  2383. * @grammar sort( fn ) => undefined
  2384. * @description 排序队列中的文件,在上传之前调整可以控制上传顺序。
  2385. * @for Uploader
  2386. */
  2387. sortFiles: function () {
  2388. return this.queue.sort.apply(this.queue, arguments);
  2389. },
  2390. /**
  2391. * @event reset
  2392. * @description 当 uploader 被重置的时候触发。
  2393. * @for Uploader
  2394. */
  2395. /**
  2396. * @method reset
  2397. * @grammar reset() => undefined
  2398. * @description 重置uploader。目前只重置了队列。
  2399. * @for Uploader
  2400. * @example
  2401. * uploader.reset();
  2402. */
  2403. reset: function () {
  2404. this.owner.trigger('reset');
  2405. this.queue = new Queue();
  2406. this.stats = this.queue.stats;
  2407. },
  2408. destroy: function () {
  2409. this.reset();
  2410. this.placeholder && this.placeholder.destroy();
  2411. }
  2412. });
  2413. });
  2414. /**
  2415. * @fileOverview 添加获取Runtime相关信息的方法。
  2416. */
  2417. define('widgets/runtime', [
  2418. 'uploader',
  2419. 'runtime/runtime',
  2420. 'widgets/widget'
  2421. ], function (Uploader, Runtime) {
  2422. Uploader.support = function () {
  2423. return Runtime.hasRuntime.apply(Runtime, arguments);
  2424. };
  2425. /**
  2426. * @property {Object} [runtimeOrder=html5,flash]
  2427. * @namespace options
  2428. * @for Uploader
  2429. * @description 指定运行时启动顺序。默认会想尝试 html5 是否支持,如果支持则使用 html5, 否则则使用 flash.
  2430. *
  2431. * 可以将此值设置成 `flash`,来强制使用 flash 运行时。
  2432. */
  2433. return Uploader.register({
  2434. name: 'runtime',
  2435. init: function () {
  2436. if (!this.predictRuntimeType()) {
  2437. throw Error('Runtime Error');
  2438. }
  2439. },
  2440. /**
  2441. * 预测Uploader将采用哪个`Runtime`
  2442. * @grammar predictRuntimeType() => String
  2443. * @method predictRuntimeType
  2444. * @for Uploader
  2445. */
  2446. predictRuntimeType: function () {
  2447. var orders = this.options.runtimeOrder || Runtime.orders,
  2448. type = this.type,
  2449. i, len;
  2450. if (!type) {
  2451. orders = orders.split(/\s*,\s*/g);
  2452. for (i = 0, len = orders.length; i < len; i++) {
  2453. if (Runtime.hasRuntime(orders[i])) {
  2454. this.type = type = orders[i];
  2455. break;
  2456. }
  2457. }
  2458. }
  2459. return type;
  2460. }
  2461. });
  2462. });
  2463. /**
  2464. * @fileOverview Transport
  2465. */
  2466. define('lib/transport', [
  2467. 'base',
  2468. 'runtime/client',
  2469. 'mediator'
  2470. ], function (Base, RuntimeClient, Mediator) {
  2471. var $ = Base.$;
  2472. function Transport(opts) {
  2473. var me = this;
  2474. opts = me.options = $.extend(true, {}, Transport.options, opts || {});
  2475. RuntimeClient.call(this, 'Transport');
  2476. this._blob = null;
  2477. this._formData = opts.formData || {};
  2478. this._headers = opts.headers || {};
  2479. this.on('progress', this._timeout);
  2480. this.on('load error', function () {
  2481. me.trigger('progress', 1);
  2482. clearTimeout(me._timer);
  2483. });
  2484. }
  2485. Transport.options = {
  2486. server: '',
  2487. method: 'POST',
  2488. // 跨域时,是否允许携带cookie, 只有html5 runtime才有效
  2489. withCredentials: false,
  2490. fileVal: 'file',
  2491. timeout: 2 * 60 * 1000, // 2分钟
  2492. formData: {},
  2493. headers: {},
  2494. sendAsBinary: false
  2495. };
  2496. $.extend(Transport.prototype, {
  2497. // 添加Blob, 只能添加一次,最后一次有效。
  2498. appendBlob: function (key, blob, filename) {
  2499. var me = this,
  2500. opts = me.options;
  2501. if (me.getRuid()) {
  2502. me.disconnectRuntime();
  2503. }
  2504. // 连接到blob归属的同一个runtime.
  2505. me.connectRuntime(blob.ruid, function () {
  2506. me.exec('init');
  2507. });
  2508. me._blob = blob;
  2509. opts.fileVal = key || opts.fileVal;
  2510. opts.filename = filename || opts.filename;
  2511. },
  2512. // 添加其他字段
  2513. append: function (key, value) {
  2514. if (typeof key === 'object') {
  2515. $.extend(this._formData, key);
  2516. } else {
  2517. this._formData[key] = value;
  2518. }
  2519. },
  2520. setRequestHeader: function (key, value) {
  2521. if (typeof key === 'object') {
  2522. $.extend(this._headers, key);
  2523. } else {
  2524. this._headers[key] = value;
  2525. }
  2526. },
  2527. send: function (method) {
  2528. this.exec('send', method);
  2529. this._timeout();
  2530. },
  2531. abort: function () {
  2532. clearTimeout(this._timer);
  2533. return this.exec('abort');
  2534. },
  2535. destroy: function () {
  2536. this.trigger('destroy');
  2537. this.off();
  2538. this.exec('destroy');
  2539. this.disconnectRuntime();
  2540. },
  2541. getResponse: function () {
  2542. return this.exec('getResponse');
  2543. },
  2544. getResponseAsJson: function () {
  2545. return this.exec('getResponseAsJson');
  2546. },
  2547. getStatus: function () {
  2548. return this.exec('getStatus');
  2549. },
  2550. _timeout: function () {
  2551. var me = this,
  2552. duration = me.options.timeout;
  2553. if (!duration) {
  2554. return;
  2555. }
  2556. clearTimeout(me._timer);
  2557. me._timer = setTimeout(function () {
  2558. me.abort();
  2559. me.trigger('error', 'timeout');
  2560. }, duration);
  2561. }
  2562. });
  2563. // 让Transport具备事件功能。
  2564. Mediator.installTo(Transport.prototype);
  2565. return Transport;
  2566. });
  2567. /**
  2568. * @fileOverview 负责文件上传相关。
  2569. */
  2570. define('widgets/upload', [
  2571. 'base',
  2572. 'uploader',
  2573. 'file',
  2574. 'lib/transport',
  2575. 'widgets/widget'
  2576. ], function (Base, Uploader, WUFile, Transport) {
  2577. var $ = Base.$,
  2578. isPromise = Base.isPromise,
  2579. Status = WUFile.Status;
  2580. // 添加默认配置项
  2581. $.extend(Uploader.options, {
  2582. /**
  2583. * @property {Boolean} [prepareNextFile=false]
  2584. * @namespace options
  2585. * @for Uploader
  2586. * @description 是否允许在文件传输时提前把下一个文件准备好。
  2587. * 对于一个文件的准备工作比较耗时,比如图片压缩,md5序列化。
  2588. * 如果能提前在当前文件传输期处理,可以节省总体耗时。
  2589. */
  2590. prepareNextFile: false,
  2591. /**
  2592. * @property {Boolean} [chunked=false]
  2593. * @namespace options
  2594. * @for Uploader
  2595. * @description 是否要分片处理大文件上传。
  2596. */
  2597. chunked: false,
  2598. /**
  2599. * @property {Boolean} [chunkSize=5242880]
  2600. * @namespace options
  2601. * @for Uploader
  2602. * @description 如果要分片,分多大一片? 默认大小为5M.
  2603. */
  2604. chunkSize: 5 * 1024 * 1024,
  2605. /**
  2606. * @property {Boolean} [chunkRetry=2]
  2607. * @namespace options
  2608. * @for Uploader
  2609. * @description 如果某个分片由于网络问题出错,允许自动重传多少次?
  2610. */
  2611. chunkRetry: 2,
  2612. /**
  2613. * @property {Boolean} [threads=3]
  2614. * @namespace options
  2615. * @for Uploader
  2616. * @description 上传并发数。允许同时最大上传进程数。
  2617. */
  2618. threads: 3,
  2619. /**
  2620. * @property {Object} [formData={}]
  2621. * @namespace options
  2622. * @for Uploader
  2623. * @description 文件上传请求的参数表,每次发送都会发送此对象中的参数。
  2624. */
  2625. formData: {}
  2626. /**
  2627. * @property {Object} [fileVal='file']
  2628. * @namespace options
  2629. * @for Uploader
  2630. * @description 设置文件上传域的name。
  2631. */
  2632. /**
  2633. * @property {Object} [method='POST']
  2634. * @namespace options
  2635. * @for Uploader
  2636. * @description 文件上传方式,`POST`或者`GET`。
  2637. */
  2638. /**
  2639. * @property {Object} [sendAsBinary=false]
  2640. * @namespace options
  2641. * @for Uploader
  2642. * @description 是否已二进制的流的方式发送文件,这样整个上传内容`php://input`都为文件内容,
  2643. * 其他参数在$_GET数组中。
  2644. */
  2645. });
  2646. // 负责将文件切片。
  2647. function CuteFile(file, chunkSize) {
  2648. var pending = [],
  2649. blob = file.source,
  2650. total = blob.size,
  2651. chunks = chunkSize ? Math.ceil(total / chunkSize) : 1,
  2652. start = 0,
  2653. index = 0,
  2654. len, api;
  2655. api = {
  2656. file: file,
  2657. has: function () {
  2658. return !!pending.length;
  2659. },
  2660. shift: function () {
  2661. return pending.shift();
  2662. },
  2663. unshift: function (block) {
  2664. pending.unshift(block);
  2665. }
  2666. };
  2667. while (index < chunks) {
  2668. len = Math.min(chunkSize, total - start);
  2669. pending.push({
  2670. file: file,
  2671. start: start,
  2672. end: chunkSize ? (start + len) : total,
  2673. total: total,
  2674. chunks: chunks,
  2675. chunk: index++,
  2676. cuted: api
  2677. });
  2678. start += len;
  2679. }
  2680. file.blocks = pending.concat();
  2681. file.remaning = pending.length;
  2682. return api;
  2683. }
  2684. Uploader.register({
  2685. name: 'upload',
  2686. init: function () {
  2687. var owner = this.owner,
  2688. me = this;
  2689. this.runing = false;
  2690. this.progress = false;
  2691. owner
  2692. .on('startUpload', function () {
  2693. me.progress = true;
  2694. })
  2695. .on('uploadFinished', function () {
  2696. me.progress = false;
  2697. });
  2698. // 记录当前正在传的数据,跟threads相关
  2699. this.pool = [];
  2700. // 缓存分好片的文件。
  2701. this.stack = [];
  2702. // 缓存即将上传的文件。
  2703. this.pending = [];
  2704. // 跟踪还有多少分片在上传中但是没有完成上传。
  2705. this.remaning = 0;
  2706. this.__tick = Base.bindFn(this._tick, this);
  2707. owner.on('uploadComplete', function (file) {
  2708. // 把其他块取消了。
  2709. file.blocks && $.each(file.blocks, function (_, v) {
  2710. v.transport && (v.transport.abort(), v.transport.destroy());
  2711. delete v.transport;
  2712. });
  2713. delete file.blocks;
  2714. delete file.remaning;
  2715. });
  2716. },
  2717. reset: function () {
  2718. this.request('stop-upload', true);
  2719. this.runing = false;
  2720. this.pool = [];
  2721. this.stack = [];
  2722. this.pending = [];
  2723. this.remaning = 0;
  2724. this._trigged = false;
  2725. this._promise = null;
  2726. },
  2727. /**
  2728. * @event startUpload
  2729. * @description 当开始上传流程时触发。
  2730. * @for Uploader
  2731. */
  2732. /**
  2733. * 开始上传。此方法可以从初始状态调用开始上传流程,也可以从暂停状态调用,继续上传流程。
  2734. *
  2735. * 可以指定开始某一个文件。
  2736. * @grammar upload() => undefined
  2737. * @grammar upload( file | fileId) => undefined
  2738. * @method upload
  2739. * @for Uploader
  2740. */
  2741. startUpload: function (file) {
  2742. var me = this;
  2743. // 移出invalid的文件
  2744. $.each(me.request('get-files', Status.INVALID), function () {
  2745. me.request('remove-file', this);
  2746. });
  2747. // 如果指定了开始某个文件,则只开始指定文件。
  2748. if (file) {
  2749. file = file.id ? file : me.request('get-file', file);
  2750. if (file.getStatus() === Status.INTERRUPT) {
  2751. $.each(me.pool, function (_, v) {
  2752. // 之前暂停过。
  2753. if (v.file !== file) {
  2754. return;
  2755. }
  2756. v.transport && v.transport.send();
  2757. });
  2758. file.setStatus(Status.QUEUED);
  2759. } else if (file.getStatus() === Status.PROGRESS) {
  2760. return;
  2761. } else {
  2762. file.setStatus(Status.QUEUED);
  2763. }
  2764. } else {
  2765. $.each(me.request('get-files', [Status.INITED]), function () {
  2766. this.setStatus(Status.QUEUED);
  2767. });
  2768. }
  2769. if (me.runing) {
  2770. return;
  2771. }
  2772. me.runing = true;
  2773. var files = [];
  2774. // 如果有暂停的,则续传
  2775. $.each(me.pool, function (_, v) {
  2776. var file = v.file;
  2777. if (file.getStatus() === Status.INTERRUPT) {
  2778. files.push(file);
  2779. me._trigged = false;
  2780. v.transport && v.transport.send();
  2781. }
  2782. });
  2783. var file;
  2784. while ((file = files.shift())) {
  2785. file.setStatus(Status.PROGRESS);
  2786. }
  2787. file || $.each(me.request('get-files',
  2788. Status.INTERRUPT), function () {
  2789. this.setStatus(Status.PROGRESS);
  2790. });
  2791. me._trigged = false;
  2792. Base.nextTick(me.__tick);
  2793. me.owner.trigger('startUpload');
  2794. },
  2795. /**
  2796. * @event stopUpload
  2797. * @description 当开始上传流程暂停时触发。
  2798. * @for Uploader
  2799. */
  2800. /**
  2801. * 暂停上传。第一个参数为是否中断上传当前正在上传的文件。
  2802. *
  2803. * 如果第一个参数是文件,则只暂停指定文件。
  2804. * @grammar stop() => undefined
  2805. * @grammar stop( true ) => undefined
  2806. * @grammar stop( file ) => undefined
  2807. * @method stop
  2808. * @for Uploader
  2809. */
  2810. stopUpload: function (file, interrupt) {
  2811. var me = this;
  2812. if (file === true) {
  2813. interrupt = file;
  2814. file = null;
  2815. }
  2816. if (me.runing === false) {
  2817. return;
  2818. }
  2819. // 如果只是暂停某个文件。
  2820. if (file) {
  2821. file = file.id ? file : me.request('get-file', file);
  2822. if (file.getStatus() !== Status.PROGRESS &&
  2823. file.getStatus() !== Status.QUEUED) {
  2824. return;
  2825. }
  2826. file.setStatus(Status.INTERRUPT);
  2827. $.each(me.pool, function (_, v) {
  2828. // 只 abort 指定的文件。
  2829. if (v.file !== file) {
  2830. return;
  2831. }
  2832. v.transport && v.transport.abort();
  2833. me._putback(v);
  2834. me._popBlock(v);
  2835. });
  2836. return Base.nextTick(me.__tick);
  2837. }
  2838. me.runing = false;
  2839. if (this._promise && this._promise.file) {
  2840. this._promise.file.setStatus(Status.INTERRUPT);
  2841. }
  2842. interrupt && $.each(me.pool, function (_, v) {
  2843. v.transport && v.transport.abort();
  2844. v.file.setStatus(Status.INTERRUPT);
  2845. });
  2846. me.owner.trigger('stopUpload');
  2847. },
  2848. /**
  2849. * @method cancelFile
  2850. * @grammar cancelFile( file ) => undefined
  2851. * @grammar cancelFile( id ) => undefined
  2852. * @param {File|id} file File对象或这File对象的id
  2853. * @description 标记文件状态为已取消, 同时将中断文件传输。
  2854. * @for Uploader
  2855. * @example
  2856. *
  2857. * $li.on('click', '.remove-this', function() {
  2858. * uploader.cancelFile( file );
  2859. * })
  2860. */
  2861. cancelFile: function (file) {
  2862. file = file.id ? file : this.request('get-file', file);
  2863. // 如果正在上传。
  2864. file.blocks && $.each(file.blocks, function (_, v) {
  2865. var _tr = v.transport;
  2866. if (_tr) {
  2867. _tr.abort();
  2868. _tr.destroy();
  2869. delete v.transport;
  2870. }
  2871. });
  2872. file.setStatus(Status.CANCELLED);
  2873. this.owner.trigger('fileDequeued', file);
  2874. },
  2875. /**
  2876. * 判断`Uplaode`r是否正在上传中。
  2877. * @grammar isInProgress() => Boolean
  2878. * @method isInProgress
  2879. * @for Uploader
  2880. */
  2881. isInProgress: function () {
  2882. return !!this.progress;
  2883. },
  2884. _getStats: function () {
  2885. return this.request('get-stats');
  2886. },
  2887. /**
  2888. * 掉过一个文件上传,直接标记指定文件为已上传状态。
  2889. * @grammar skipFile( file ) => undefined
  2890. * @method skipFile
  2891. * @for Uploader
  2892. */
  2893. skipFile: function (file, status) {
  2894. file = file.id ? file : this.request('get-file', file);
  2895. file.setStatus(status || Status.COMPLETE);
  2896. file.skipped = true;
  2897. // 如果正在上传。
  2898. file.blocks && $.each(file.blocks, function (_, v) {
  2899. var _tr = v.transport;
  2900. if (_tr) {
  2901. _tr.abort();
  2902. _tr.destroy();
  2903. delete v.transport;
  2904. }
  2905. });
  2906. this.owner.trigger('uploadSkip', file);
  2907. },
  2908. /**
  2909. * @event uploadFinished
  2910. * @description 当所有文件上传结束时触发。
  2911. * @for Uploader
  2912. */
  2913. _tick: function () {
  2914. var me = this,
  2915. opts = me.options,
  2916. fn, val;
  2917. // 上一个promise还没有结束,则等待完成后再执行。
  2918. if (me._promise) {
  2919. return me._promise.always(me.__tick);
  2920. }
  2921. // 还有位置,且还有文件要处理的话。
  2922. if (me.pool.length < opts.threads && (val = me._nextBlock())) {
  2923. me._trigged = false;
  2924. fn = function (val) {
  2925. me._promise = null;
  2926. // 有可能是reject过来的,所以要检测val的类型。
  2927. val && val.file && me._startSend(val);
  2928. Base.nextTick(me.__tick);
  2929. };
  2930. me._promise = isPromise(val) ? val.always(fn) : fn(val);
  2931. // 没有要上传的了,且没有正在传输的了。
  2932. } else if (!me.remaning && !me._getStats().numOfQueue &&
  2933. !me._getStats().numofInterrupt) {
  2934. me.runing = false;
  2935. me._trigged || Base.nextTick(function () {
  2936. me.owner.trigger('uploadFinished');
  2937. });
  2938. me._trigged = true;
  2939. }
  2940. },
  2941. _putback: function (block) {
  2942. var idx;
  2943. block.cuted.unshift(block);
  2944. idx = this.stack.indexOf(block.cuted);
  2945. if (!~idx) {
  2946. this.stack.unshift(block.cuted);
  2947. }
  2948. },
  2949. _getStack: function () {
  2950. var i = 0,
  2951. act;
  2952. while ((act = this.stack[i++])) {
  2953. if (act.has() && act.file.getStatus() === Status.PROGRESS) {
  2954. return act;
  2955. } else if (!act.has() ||
  2956. act.file.getStatus() !== Status.PROGRESS &&
  2957. act.file.getStatus() !== Status.INTERRUPT) {
  2958. // 把已经处理完了的,或者,状态为非 progress(上传中)、
  2959. // interupt(暂停中) 的移除。
  2960. this.stack.splice(--i, 1);
  2961. }
  2962. }
  2963. return null;
  2964. },
  2965. _nextBlock: function () {
  2966. var me = this,
  2967. opts = me.options,
  2968. act, next, done, preparing;
  2969. // 如果当前文件还有没有需要传输的,则直接返回剩下的。
  2970. if ((act = this._getStack())) {
  2971. // 是否提前准备下一个文件
  2972. if (opts.prepareNextFile && !me.pending.length) {
  2973. me._prepareNextFile();
  2974. }
  2975. return act.shift();
  2976. // 否则,如果正在运行,则准备下一个文件,并等待完成后返回下个分片。
  2977. } else if (me.runing) {
  2978. // 如果缓存中有,则直接在缓存中取,没有则去queue中取。
  2979. if (!me.pending.length && me._getStats().numOfQueue) {
  2980. me._prepareNextFile();
  2981. }
  2982. next = me.pending.shift();
  2983. done = function (file) {
  2984. if (!file) {
  2985. return null;
  2986. }
  2987. act = CuteFile(file, opts.chunked ? opts.chunkSize : 0);
  2988. me.stack.push(act);
  2989. return act.shift();
  2990. };
  2991. // 文件可能还在prepare中,也有可能已经完全准备好了。
  2992. if (isPromise(next)) {
  2993. preparing = next.file;
  2994. next = next[next.pipe ? 'pipe' : 'then'](done);
  2995. next.file = preparing;
  2996. return next;
  2997. }
  2998. return done(next);
  2999. }
  3000. },
  3001. /**
  3002. * @event uploadStart
  3003. * @param {File} file File对象
  3004. * @description 某个文件开始上传前触发,一个文件只会触发一次。
  3005. * @for Uploader
  3006. */
  3007. _prepareNextFile: function () {
  3008. var me = this,
  3009. file = me.request('fetch-file'),
  3010. pending = me.pending,
  3011. promise;
  3012. if (file) {
  3013. promise = me.request('before-send-file', file, function () {
  3014. // 有可能文件被skip掉了。文件被skip掉后,状态坑定不是Queued.
  3015. if (file.getStatus() === Status.PROGRESS ||
  3016. file.getStatus() === Status.INTERRUPT) {
  3017. return file;
  3018. }
  3019. return me._finishFile(file);
  3020. });
  3021. me.owner.trigger('uploadStart', file);
  3022. file.setStatus(Status.PROGRESS);
  3023. promise.file = file;
  3024. // 如果还在pending中,则替换成文件本身。
  3025. promise.done(function () {
  3026. var idx = $.inArray(promise, pending);
  3027. ~idx && pending.splice(idx, 1, file);
  3028. });
  3029. // befeore-send-file的钩子就有错误发生。
  3030. promise.fail(function (reason) {
  3031. file.setStatus(Status.ERROR, reason);
  3032. me.owner.trigger('uploadError', file, reason);
  3033. me.owner.trigger('uploadComplete', file);
  3034. });
  3035. pending.push(promise);
  3036. }
  3037. },
  3038. // 让出位置了,可以让其他分片开始上传
  3039. _popBlock: function (block) {
  3040. var idx = $.inArray(block, this.pool);
  3041. this.pool.splice(idx, 1);
  3042. block.file.remaning--;
  3043. this.remaning--;
  3044. },
  3045. // 开始上传,可以被掉过。如果promise被reject了,则表示跳过此分片。
  3046. _startSend: function (block) {
  3047. var me = this,
  3048. file = block.file,
  3049. promise;
  3050. // 有可能在 before-send-file 的 promise 期间改变了文件状态。
  3051. // 如:暂停,取消
  3052. // 我们不能中断 promise, 但是可以在 promise 完后,不做上传操作。
  3053. if (file.getStatus() !== Status.PROGRESS) {
  3054. // 如果是中断,则还需要放回去。
  3055. if (file.getStatus() === Status.INTERRUPT) {
  3056. me._putback(block);
  3057. }
  3058. return;
  3059. }
  3060. me.pool.push(block);
  3061. me.remaning++;
  3062. // 如果没有分片,则直接使用原始的。
  3063. // 不会丢失content-type信息。
  3064. block.blob = block.chunks === 1 ? file.source :
  3065. file.source.slice(block.start, block.end);
  3066. // hook, 每个分片发送之前可能要做些异步的事情。
  3067. promise = me.request('before-send', block, function () {
  3068. // 有可能文件已经上传出错了,所以不需要再传输了。
  3069. if (file.getStatus() === Status.PROGRESS) {
  3070. me._doSend(block);
  3071. } else {
  3072. me._popBlock(block);
  3073. Base.nextTick(me.__tick);
  3074. }
  3075. });
  3076. // 如果为fail了,则跳过此分片。
  3077. promise.fail(function () {
  3078. if (file.remaning === 1) {
  3079. me._finishFile(file).always(function () {
  3080. block.percentage = 1;
  3081. me._popBlock(block);
  3082. me.owner.trigger('uploadComplete', file);
  3083. Base.nextTick(me.__tick);
  3084. });
  3085. } else {
  3086. block.percentage = 1;
  3087. me.updateFileProgress(file);
  3088. me._popBlock(block);
  3089. Base.nextTick(me.__tick);
  3090. }
  3091. });
  3092. },
  3093. /**
  3094. * @event uploadBeforeSend
  3095. * @param {Object} object
  3096. * @param {Object} data 默认的上传参数,可以扩展此对象来控制上传参数。
  3097. * @param {Object} headers 可以扩展此对象来控制上传头部。
  3098. * @description 当某个文件的分块在发送前触发,主要用来询问是否要添加附带参数,大文件在开起分片上传的前提下此事件可能会触发多次。
  3099. * @for Uploader
  3100. */
  3101. /**
  3102. * @event uploadAccept
  3103. * @param {Object} object
  3104. * @param {Object} ret 服务端的返回数据,json格式,如果服务端不是json格式,从ret._raw中取数据,自行解析。
  3105. * @description 当某个文件上传到服务端响应后,会派送此事件来询问服务端响应是否有效。如果此事件handler返回值为`false`, 则此文件将派送`server`类型的`uploadError`事件。
  3106. * @for Uploader
  3107. */
  3108. /**
  3109. * @event uploadProgress
  3110. * @param {File} file File对象
  3111. * @param {Number} percentage 上传进度
  3112. * @description 上传过程中触发,携带上传进度。
  3113. * @for Uploader
  3114. */
  3115. /**
  3116. * @event uploadError
  3117. * @param {File} file File对象
  3118. * @param {String} reason 出错的code
  3119. * @description 当文件上传出错时触发。
  3120. * @for Uploader
  3121. */
  3122. /**
  3123. * @event uploadSuccess
  3124. * @param {File} file File对象
  3125. * @param {Object} response 服务端返回的数据
  3126. * @description 当文件上传成功时触发。
  3127. * @for Uploader
  3128. */
  3129. /**
  3130. * @event uploadComplete
  3131. * @param {File} [file] File对象
  3132. * @description 不管成功或者失败,文件上传完成时触发。
  3133. * @for Uploader
  3134. */
  3135. // 做上传操作。
  3136. _doSend: function (block) {
  3137. var me = this,
  3138. owner = me.owner,
  3139. opts = me.options,
  3140. file = block.file,
  3141. tr = new Transport(opts),
  3142. data = $.extend({}, opts.formData),
  3143. headers = $.extend({}, opts.headers),
  3144. requestAccept, ret;
  3145. block.transport = tr;
  3146. tr.on('destroy', function () {
  3147. delete block.transport;
  3148. me._popBlock(block);
  3149. Base.nextTick(me.__tick);
  3150. });
  3151. // 广播上传进度。以文件为单位。
  3152. tr.on('progress', function (percentage) {
  3153. block.percentage = percentage;
  3154. me.updateFileProgress(file);
  3155. });
  3156. // 用来询问,是否返回的结果是有错误的。
  3157. requestAccept = function (reject) {
  3158. var fn;
  3159. ret = tr.getResponseAsJson() || {};
  3160. ret._raw = tr.getResponse();
  3161. fn = function (value) {
  3162. reject = value;
  3163. };
  3164. // 服务端响应了,不代表成功了,询问是否响应正确。
  3165. if (!owner.trigger('uploadAccept', block, ret, fn)) {
  3166. reject = reject || 'server';
  3167. }
  3168. return reject;
  3169. };
  3170. // 尝试重试,然后广播文件上传出错。
  3171. tr.on('error', function (type, flag) {
  3172. block.retried = block.retried || 0;
  3173. // 自动重试
  3174. if (block.chunks > 1 && ~'http,abort'.indexOf(type) &&
  3175. block.retried < opts.chunkRetry) {
  3176. block.retried++;
  3177. tr.send();
  3178. } else {
  3179. // http status 500 ~ 600
  3180. if (!flag && type === 'server') {
  3181. type = requestAccept(type);
  3182. }
  3183. file.setStatus(Status.ERROR, type);
  3184. owner.trigger('uploadError', file, type);
  3185. owner.trigger('uploadComplete', file);
  3186. }
  3187. });
  3188. // 上传成功
  3189. tr.on('load', function () {
  3190. var reason;
  3191. // 如果非预期,转向上传出错。
  3192. if ((reason = requestAccept())) {
  3193. tr.trigger('error', reason, true);
  3194. return;
  3195. }
  3196. // 全部上传完成。
  3197. if (file.remaning === 1) {
  3198. me._finishFile(file, ret);
  3199. } else {
  3200. tr.destroy();
  3201. }
  3202. });
  3203. // 配置默认的上传字段。
  3204. data = $.extend(data, {
  3205. id: file.id,
  3206. name: file.name,
  3207. type: file.type,
  3208. lastModifiedDate: file.lastModifiedDate,
  3209. size: file.size
  3210. });
  3211. block.chunks > 1 && $.extend(data, {
  3212. chunks: block.chunks,
  3213. chunk: block.chunk
  3214. });
  3215. // 在发送之间可以添加字段什么的。。。
  3216. // 如果默认的字段不够使用,可以通过监听此事件来扩展
  3217. owner.trigger('uploadBeforeSend', block, data, headers);
  3218. // 开始发送。
  3219. tr.appendBlob(opts.fileVal, block.blob, file.name);
  3220. tr.append(data);
  3221. tr.setRequestHeader(headers);
  3222. tr.send();
  3223. },
  3224. // 完成上传。
  3225. _finishFile: function (file, ret, hds) {
  3226. var owner = this.owner;
  3227. return owner
  3228. .request('after-send-file', arguments, function () {
  3229. file.setStatus(Status.COMPLETE);
  3230. owner.trigger('uploadSuccess', file, ret, hds);
  3231. })
  3232. .fail(function (reason) {
  3233. // 如果外部已经标记为invalid什么的,不再改状态。
  3234. if (file.getStatus() === Status.PROGRESS) {
  3235. file.setStatus(Status.ERROR, reason);
  3236. }
  3237. owner.trigger('uploadError', file, reason);
  3238. })
  3239. .always(function () {
  3240. owner.trigger('uploadComplete', file);
  3241. });
  3242. },
  3243. updateFileProgress: function (file) {
  3244. var totalPercent = 0,
  3245. uploaded = 0;
  3246. if (!file.blocks) {
  3247. return;
  3248. }
  3249. $.each(file.blocks, function (_, v) {
  3250. uploaded += (v.percentage || 0) * (v.end - v.start);
  3251. });
  3252. totalPercent = uploaded / file.size;
  3253. this.owner.trigger('uploadProgress', file, totalPercent || 0);
  3254. }
  3255. });
  3256. });
  3257. /**
  3258. * @fileOverview 各种验证,包括文件总大小是否超出、单文件是否超出和文件是否重复。
  3259. */
  3260. define('widgets/validator', [
  3261. 'base',
  3262. 'uploader',
  3263. 'file',
  3264. 'widgets/widget'
  3265. ], function (Base, Uploader, WUFile) {
  3266. var $ = Base.$,
  3267. validators = {},
  3268. api;
  3269. /**
  3270. * @event error
  3271. * @param {String} type 错误类型。
  3272. * @description 当validate不通过时,会以派送错误事件的形式通知调用者。通过`upload.on('error', handler)`可以捕获到此类错误,目前有以下错误会在特定的情况下派送错来。
  3273. *
  3274. * * `Q_EXCEED_NUM_LIMIT` 在设置了`fileNumLimit`且尝试给`uploader`添加的文件数量超出这个值时派送。
  3275. * * `Q_EXCEED_SIZE_LIMIT` 在设置了`Q_EXCEED_SIZE_LIMIT`且尝试给`uploader`添加的文件总大小超出这个值时派送。
  3276. * * `Q_TYPE_DENIED` 当文件类型不满足时触发。。
  3277. * @for Uploader
  3278. */
  3279. // 暴露给外面的api
  3280. api = {
  3281. // 添加验证器
  3282. addValidator: function (type, cb) {
  3283. validators[type] = cb;
  3284. },
  3285. // 移除验证器
  3286. removeValidator: function (type) {
  3287. delete validators[type];
  3288. }
  3289. };
  3290. // 在Uploader初始化的时候启动Validators的初始化
  3291. Uploader.register({
  3292. name: 'validator',
  3293. init: function () {
  3294. var me = this;
  3295. Base.nextTick(function () {
  3296. $.each(validators, function () {
  3297. this.call(me.owner);
  3298. });
  3299. });
  3300. }
  3301. });
  3302. /**
  3303. * @property {int} [fileNumLimit=undefined]
  3304. * @namespace options
  3305. * @for Uploader
  3306. * @description 验证文件总数量, 超出则不允许加入队列。
  3307. */
  3308. api.addValidator('fileNumLimit', function () {
  3309. var uploader = this,
  3310. opts = uploader.options,
  3311. count = 0,
  3312. max = parseInt(opts.fileNumLimit, 10),
  3313. flag = true;
  3314. if (!max) {
  3315. return;
  3316. }
  3317. uploader.on('beforeFileQueued', function (file) {
  3318. if (count >= max && flag) {
  3319. flag = false;
  3320. this.trigger('error', 'Q_EXCEED_NUM_LIMIT', max, file);
  3321. setTimeout(function () {
  3322. flag = true;
  3323. }, 1);
  3324. }
  3325. return count >= max ? false : true;
  3326. });
  3327. uploader.on('fileQueued', function () {
  3328. count++;
  3329. });
  3330. uploader.on('fileDequeued', function () {
  3331. count--;
  3332. });
  3333. uploader.on('reset', function () {
  3334. count = 0;
  3335. });
  3336. });
  3337. /**
  3338. * @property {int} [fileSizeLimit=undefined]
  3339. * @namespace options
  3340. * @for Uploader
  3341. * @description 验证文件总大小是否超出限制, 超出则不允许加入队列。
  3342. */
  3343. api.addValidator('fileSizeLimit', function () {
  3344. var uploader = this,
  3345. opts = uploader.options,
  3346. count = 0,
  3347. max = parseInt(opts.fileSizeLimit, 10),
  3348. flag = true;
  3349. if (!max) {
  3350. return;
  3351. }
  3352. uploader.on('beforeFileQueued', function (file) {
  3353. var invalid = count + file.size > max;
  3354. if (invalid && flag) {
  3355. flag = false;
  3356. this.trigger('error', 'Q_EXCEED_SIZE_LIMIT', max, file);
  3357. setTimeout(function () {
  3358. flag = true;
  3359. }, 1);
  3360. }
  3361. return invalid ? false : true;
  3362. });
  3363. uploader.on('fileQueued', function (file) {
  3364. count += file.size;
  3365. });
  3366. uploader.on('fileDequeued', function (file) {
  3367. count -= file.size;
  3368. });
  3369. uploader.on('reset', function () {
  3370. count = 0;
  3371. });
  3372. });
  3373. /**
  3374. * @property {int} [fileSingleSizeLimit=undefined]
  3375. * @namespace options
  3376. * @for Uploader
  3377. * @description 验证单个文件大小是否超出限制, 超出则不允许加入队列。
  3378. */
  3379. api.addValidator('fileSingleSizeLimit', function () {
  3380. var uploader = this,
  3381. opts = uploader.options,
  3382. max = opts.fileSingleSizeLimit;
  3383. if (!max) {
  3384. return;
  3385. }
  3386. uploader.on('beforeFileQueued', function (file) {
  3387. if (file.size > max) {
  3388. file.setStatus(WUFile.Status.INVALID, 'exceed_size');
  3389. this.trigger('error', 'F_EXCEED_SIZE', max, file);
  3390. return false;
  3391. }
  3392. });
  3393. });
  3394. /**
  3395. * @property {Boolean} [duplicate=undefined]
  3396. * @namespace options
  3397. * @for Uploader
  3398. * @description 去重, 根据文件名字、文件大小和最后修改时间来生成hash Key.
  3399. */
  3400. api.addValidator('duplicate', function () {
  3401. var uploader = this,
  3402. opts = uploader.options,
  3403. mapping = {};
  3404. if (opts.duplicate) {
  3405. return;
  3406. }
  3407. function hashString(str) {
  3408. var hash = 0,
  3409. i = 0,
  3410. len = str.length,
  3411. _char;
  3412. for (; i < len; i++) {
  3413. _char = str.charCodeAt(i);
  3414. hash = _char + (hash << 6) + (hash << 16) - hash;
  3415. }
  3416. return hash;
  3417. }
  3418. uploader.on('beforeFileQueued', function (file) {
  3419. var hash = file.__hash || (file.__hash = hashString(file.name +
  3420. file.size + file.lastModifiedDate));
  3421. // 已经重复了
  3422. if (mapping[hash]) {
  3423. this.trigger('error', 'F_DUPLICATE', file);
  3424. return false;
  3425. }
  3426. });
  3427. uploader.on('fileQueued', function (file) {
  3428. var hash = file.__hash;
  3429. hash && (mapping[hash] = true);
  3430. });
  3431. uploader.on('fileDequeued', function (file) {
  3432. var hash = file.__hash;
  3433. hash && (delete mapping[hash]);
  3434. });
  3435. uploader.on('reset', function () {
  3436. mapping = {};
  3437. });
  3438. });
  3439. return api;
  3440. });
  3441. /**
  3442. * @fileOverview Runtime管理器,负责Runtime的选择, 连接
  3443. */
  3444. define('runtime/compbase', [], function () {
  3445. function CompBase(owner, runtime) {
  3446. this.owner = owner;
  3447. this.options = owner.options;
  3448. this.getRuntime = function () {
  3449. return runtime;
  3450. };
  3451. this.getRuid = function () {
  3452. return runtime.uid;
  3453. };
  3454. this.trigger = function () {
  3455. return owner.trigger.apply(owner, arguments);
  3456. };
  3457. }
  3458. return CompBase;
  3459. });
  3460. /**
  3461. * @fileOverview FlashRuntime
  3462. */
  3463. define('runtime/flash/runtime', [
  3464. 'base',
  3465. 'runtime/runtime',
  3466. 'runtime/compbase'
  3467. ], function (Base, Runtime, CompBase) {
  3468. var $ = Base.$,
  3469. type = 'flash',
  3470. components = {};
  3471. function getFlashVersion() {
  3472. var version;
  3473. try {
  3474. version = navigator.plugins['Shockwave Flash'];
  3475. version = version.description;
  3476. } catch (ex) {
  3477. try {
  3478. version = new ActiveXObject('ShockwaveFlash.ShockwaveFlash')
  3479. .GetVariable('$version');
  3480. } catch (ex2) {
  3481. version = '0.0';
  3482. }
  3483. }
  3484. version = version.match(/\d+/g);
  3485. return parseFloat(version[0] + '.' + version[1], 10);
  3486. }
  3487. function FlashRuntime() {
  3488. var pool = {},
  3489. clients = {},
  3490. destroy = this.destroy,
  3491. me = this,
  3492. jsreciver = Base.guid('webuploader_');
  3493. Runtime.apply(me, arguments);
  3494. me.type = type;
  3495. // 这个方法的调用者,实际上是RuntimeClient
  3496. me.exec = function (comp, fn/*, args...*/) {
  3497. var client = this,
  3498. uid = client.uid,
  3499. args = Base.slice(arguments, 2),
  3500. instance;
  3501. clients[uid] = client;
  3502. if (components[comp]) {
  3503. if (!pool[uid]) {
  3504. pool[uid] = new components[comp](client, me);
  3505. }
  3506. instance = pool[uid];
  3507. if (instance[fn]) {
  3508. return instance[fn].apply(instance, args);
  3509. }
  3510. }
  3511. return me.flashExec.apply(client, arguments);
  3512. };
  3513. function handler(evt, obj) {
  3514. var type = evt.type || evt,
  3515. parts, uid;
  3516. parts = type.split('::');
  3517. uid = parts[0];
  3518. type = parts[1];
  3519. // console.log.apply( console, arguments );
  3520. if (type === 'Ready' && uid === me.uid) {
  3521. me.trigger('ready');
  3522. } else if (clients[uid]) {
  3523. clients[uid].trigger(type.toLowerCase(), evt, obj);
  3524. }
  3525. // Base.log( evt, obj );
  3526. }
  3527. // flash的接受器。
  3528. window[jsreciver] = function () {
  3529. var args = arguments;
  3530. // 为了能捕获得到。
  3531. setTimeout(function () {
  3532. handler.apply(null, args);
  3533. }, 1);
  3534. };
  3535. this.jsreciver = jsreciver;
  3536. this.destroy = function () {
  3537. // @todo 删除池子中的所有实例
  3538. return destroy && destroy.apply(this, arguments);
  3539. };
  3540. this.flashExec = function (comp, fn) {
  3541. var flash = me.getFlash(),
  3542. args = Base.slice(arguments, 2);
  3543. return flash.exec(this.uid, comp, fn, args);
  3544. };
  3545. // @todo
  3546. }
  3547. Base.inherits(Runtime, {
  3548. constructor: FlashRuntime,
  3549. init: function () {
  3550. var container = this.getContainer(),
  3551. opts = this.options,
  3552. html;
  3553. // if not the minimal height, shims are not initialized
  3554. // in older browsers (e.g FF3.6, IE6,7,8, Safari 4.0,5.0, etc)
  3555. container.css({
  3556. position: 'absolute',
  3557. top: '-8px',
  3558. left: '-8px',
  3559. width: '9px',
  3560. height: '9px',
  3561. overflow: 'hidden'
  3562. });
  3563. // insert flash object
  3564. html = '<object id="' + this.uid + '" type="application/' +
  3565. 'x-shockwave-flash" data="' + opts.swf + '" ';
  3566. if (Base.browser.ie) {
  3567. html += 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
  3568. }
  3569. html += 'width="100%" height="100%" style="outline:0">' +
  3570. '<param name="movie" value="' + opts.swf + '" />' +
  3571. '<param name="flashvars" value="uid=' + this.uid +
  3572. '&jsreciver=' + this.jsreciver + '" />' +
  3573. '<param name="wmode" value="transparent" />' +
  3574. '<param name="allowscriptaccess" value="always" />' +
  3575. '</object>';
  3576. container.html(html);
  3577. },
  3578. getFlash: function () {
  3579. if (this._flash) {
  3580. return this._flash;
  3581. }
  3582. this._flash = $('#' + this.uid).get(0);
  3583. return this._flash;
  3584. }
  3585. });
  3586. FlashRuntime.register = function (name, component) {
  3587. component = components[name] = Base.inherits(CompBase, $.extend({
  3588. // @todo fix this later
  3589. flashExec: function () {
  3590. var owner = this.owner,
  3591. runtime = this.getRuntime();
  3592. return runtime.flashExec.apply(owner, arguments);
  3593. }
  3594. }, component));
  3595. return component;
  3596. };
  3597. if (getFlashVersion() >= 11.4) {
  3598. Runtime.addRuntime(type, FlashRuntime);
  3599. }
  3600. return FlashRuntime;
  3601. });
  3602. /**
  3603. * @fileOverview FilePicker
  3604. */
  3605. define('runtime/flash/filepicker', [
  3606. 'base',
  3607. 'runtime/flash/runtime'
  3608. ], function (Base, FlashRuntime) {
  3609. var $ = Base.$;
  3610. return FlashRuntime.register('FilePicker', {
  3611. init: function (opts) {
  3612. var copy = $.extend({}, opts),
  3613. len, i;
  3614. // 修复Flash再没有设置title的情况下无法弹出flash文件选择框的bug.
  3615. len = copy.accept && copy.accept.length;
  3616. for (i = 0; i < len; i++) {
  3617. if (!copy.accept[i].title) {
  3618. copy.accept[i].title = 'Files';
  3619. }
  3620. }
  3621. delete copy.button;
  3622. delete copy.id;
  3623. delete copy.container;
  3624. this.flashExec('FilePicker', 'init', copy);
  3625. },
  3626. destroy: function () {
  3627. this.flashExec('FilePicker', 'destroy');
  3628. }
  3629. });
  3630. });
  3631. /**
  3632. * @fileOverview 图片压缩
  3633. */
  3634. define('runtime/flash/image', [
  3635. 'runtime/flash/runtime'
  3636. ], function (FlashRuntime) {
  3637. return FlashRuntime.register('Image', {
  3638. // init: function( options ) {
  3639. // var owner = this.owner;
  3640. // this.flashExec( 'Image', 'init', options );
  3641. // owner.on( 'load', function() {
  3642. // });
  3643. // },
  3644. loadFromBlob: function (blob) {
  3645. var owner = this.owner;
  3646. owner.info() && this.flashExec('Image', 'info', owner.info());
  3647. owner.meta() && this.flashExec('Image', 'meta', owner.meta());
  3648. this.flashExec('Image', 'loadFromBlob', blob.uid);
  3649. }
  3650. });
  3651. });
  3652. /**
  3653. * @fileOverview Blob Html实现
  3654. */
  3655. define('runtime/flash/blob', [
  3656. 'runtime/flash/runtime',
  3657. 'lib/blob'
  3658. ], function (FlashRuntime, Blob) {
  3659. return FlashRuntime.register('Blob', {
  3660. slice: function (start, end) {
  3661. var blob = this.flashExec('Blob', 'slice', start, end);
  3662. return new Blob(blob.uid, blob);
  3663. }
  3664. });
  3665. });
  3666. /**
  3667. * @fileOverview Transport flash实现
  3668. */
  3669. define('runtime/flash/transport', [
  3670. 'base',
  3671. 'runtime/flash/runtime',
  3672. 'runtime/client'
  3673. ], function (Base, FlashRuntime, RuntimeClient) {
  3674. var $ = Base.$;
  3675. return FlashRuntime.register('Transport', {
  3676. init: function () {
  3677. this._status = 0;
  3678. this._response = null;
  3679. this._responseJson = null;
  3680. },
  3681. send: function () {
  3682. var owner = this.owner,
  3683. opts = this.options,
  3684. xhr = this._initAjax(),
  3685. blob = owner._blob,
  3686. server = opts.server,
  3687. binary;
  3688. xhr.connectRuntime(blob.ruid);
  3689. if (opts.sendAsBinary) {
  3690. server += (/\?/.test(server) ? '&' : '?') +
  3691. $.param(owner._formData);
  3692. binary = blob.uid;
  3693. } else {
  3694. $.each(owner._formData, function (k, v) {
  3695. xhr.exec('append', k, v);
  3696. });
  3697. xhr.exec('appendBlob', opts.fileVal, blob.uid,
  3698. opts.filename || owner._formData.name || '');
  3699. }
  3700. this._setRequestHeader(xhr, opts.headers);
  3701. xhr.exec('send', {
  3702. method: opts.method,
  3703. url: server,
  3704. forceURLStream: opts.forceURLStream,
  3705. mimeType: 'application/octet-stream'
  3706. }, binary);
  3707. },
  3708. getStatus: function () {
  3709. return this._status;
  3710. },
  3711. getResponse: function () {
  3712. return this._response || '';
  3713. },
  3714. getResponseAsJson: function () {
  3715. return this._responseJson;
  3716. },
  3717. abort: function () {
  3718. var xhr = this._xhr;
  3719. if (xhr) {
  3720. xhr.exec('abort');
  3721. xhr.destroy();
  3722. this._xhr = xhr = null;
  3723. }
  3724. },
  3725. destroy: function () {
  3726. this.abort();
  3727. },
  3728. _initAjax: function () {
  3729. var me = this,
  3730. xhr = new RuntimeClient('XMLHttpRequest');
  3731. xhr.on('uploadprogress progress', function (e) {
  3732. var percent = e.loaded / e.total;
  3733. percent = Math.min(1, Math.max(0, percent));
  3734. return me.trigger('progress', percent);
  3735. });
  3736. xhr.on('load', function () {
  3737. var status = xhr.exec('getStatus'),
  3738. readBody = false,
  3739. err = '',
  3740. p;
  3741. xhr.off();
  3742. me._xhr = null;
  3743. if (status >= 200 && status < 300) {
  3744. readBody = true;
  3745. } else if (status >= 500 && status < 600) {
  3746. readBody = true;
  3747. err = 'server';
  3748. } else {
  3749. err = 'http';
  3750. }
  3751. if (readBody) {
  3752. me._response = xhr.exec('getResponse');
  3753. me._response = decodeURIComponent(me._response);
  3754. // flash 处理可能存在 bug, 没辙只能靠 js 了
  3755. // try {
  3756. // me._responseJson = xhr.exec('getResponseAsJson');
  3757. // } catch ( error ) {
  3758. p = window.JSON && window.JSON.parse || function (s) {
  3759. try {
  3760. return new Function('return ' + s).call();
  3761. } catch (err) {
  3762. return {};
  3763. }
  3764. };
  3765. me._responseJson = me._response ? p(me._response) : {};
  3766. // }
  3767. }
  3768. xhr.destroy();
  3769. xhr = null;
  3770. return err ? me.trigger('error', err) : me.trigger('load');
  3771. });
  3772. xhr.on('error', function () {
  3773. xhr.off();
  3774. me._xhr = null;
  3775. me.trigger('error', 'http');
  3776. });
  3777. me._xhr = xhr;
  3778. return xhr;
  3779. },
  3780. _setRequestHeader: function (xhr, headers) {
  3781. $.each(headers, function (key, val) {
  3782. xhr.exec('setRequestHeader', key, val);
  3783. });
  3784. }
  3785. });
  3786. });
  3787. /**
  3788. * @fileOverview 只有flash实现的文件版本。
  3789. */
  3790. define('preset/flashonly', [
  3791. 'base',
  3792. // widgets
  3793. 'widgets/filepicker',
  3794. 'widgets/image',
  3795. 'widgets/queue',
  3796. 'widgets/runtime',
  3797. 'widgets/upload',
  3798. 'widgets/validator',
  3799. // runtimes
  3800. // flash
  3801. 'runtime/flash/filepicker',
  3802. 'runtime/flash/image',
  3803. 'runtime/flash/blob',
  3804. 'runtime/flash/transport'
  3805. ], function (Base) {
  3806. return Base;
  3807. });
  3808. define('webuploader', [
  3809. 'preset/flashonly'
  3810. ], function (preset) {
  3811. return preset;
  3812. });
  3813. return require('webuploader');
  3814. });