fileinput.js 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. /*!
  2. * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2015
  3. * @version 4.1.9
  4. *
  5. * File input styled for Bootstrap 3.0 that utilizes HTML5 File Input's advanced
  6. * features including the FileReader API.
  7. *
  8. * The plugin drastically enhances the HTML file input to preview multiple files on the client before
  9. * upload. In addition it provides the ability to preview content of images, text, videos, audio, html,
  10. * flash and other objects. It also offers the ability to upload and delete files using AJAX, and add
  11. * files in batches (i.e. preview, append, or remove before upload).
  12. *
  13. * Author: Kartik Visweswaran
  14. * Copyright: 2015, Kartik Visweswaran, Krajee.com
  15. * For more JQuery plugins visit http://plugins.krajee.com
  16. * For more Yii related demos visit http://demos.krajee.com
  17. */
  18. (function ($) {
  19. "use strict";
  20. String.prototype.repl = function (from, to) {
  21. return this.split(from).join(to);
  22. };
  23. var isIE = function (ver) {
  24. var div = document.createElement("div"), status;
  25. div.innerHTML = "<!--[if IE " + ver + "]><i></i><![endif]-->";
  26. status = (div.getElementsByTagName("i").length === 1);
  27. document.body.appendChild(div);
  28. div.parentNode.removeChild(div);
  29. return status;
  30. },
  31. previewCache = {
  32. data : {},
  33. init : function (obj) {
  34. var content = obj.initialPreview, id = obj.id;
  35. if (content.length > 0 && ! isArray(content)) {
  36. content = content.split(obj.initialPreviewDelimiter);
  37. }
  38. previewCache.data[id] = {
  39. content : content,
  40. config : obj.initialPreviewConfig,
  41. tags : obj.initialPreviewThumbTags,
  42. delimiter: obj.initialPreviewDelimiter,
  43. template : obj.previewGenericTemplate,
  44. msg : function (n) {
  45. return obj.getMsgSelected(n);
  46. },
  47. initId : obj.previewInitId,
  48. footer : obj.getLayoutTemplate('footer'),
  49. isDelete : obj.initialPreviewShowDelete,
  50. caption : obj.initialCaption,
  51. actions : function (showUpload, showDelete, disabled, url, key) {
  52. return obj.renderFileActions(showUpload, showDelete, disabled, url, key);
  53. }
  54. };
  55. },
  56. fetch : function (id) {
  57. return previewCache.data[id].content.filter(function (n) {
  58. return n !== null;
  59. });
  60. },
  61. count : function (id, all) {
  62. return ! ! previewCache.data[id] && ! ! previewCache.data[id].content ?
  63. (all ? previewCache.data[id].content.length : previewCache.fetch(id).length) : 0;
  64. },
  65. get : function (id, i, isDisabled) {
  66. var ind = 'init_' + i, data = previewCache.data[id],
  67. previewId = data.initId + '-' + ind, out;
  68. isDisabled = isDisabled === undefined ? true : isDisabled;
  69. if (data.content[i] === null) {
  70. return '';
  71. }
  72. out = data.template
  73. .repl('{previewId}', previewId)
  74. .repl('{frameClass}', ' file-preview-initial')
  75. .repl('{fileindex}', ind)
  76. .repl('{content}', data.content[i])
  77. .repl('{footer}', previewCache.footer(id, i, isDisabled));
  78. if (data.tags.length && data.tags[i]) {
  79. out = replaceTags(out, data.tags[i]);
  80. }
  81. return out;
  82. },
  83. add : function (id, content, config, tags, append) {
  84. var data = $.extend(true, {}, previewCache.data[id]), index;
  85. if (! isArray(content)) {
  86. content = content.split(data.delimiter);
  87. }
  88. if (append) {
  89. index = data.content.push(content) - 1;
  90. data.config[index] = config;
  91. data.tags[index] = tags;
  92. } else {
  93. index = content.length;
  94. data.content = content;
  95. data.config = config;
  96. data.tags = tags;
  97. }
  98. previewCache.data[id] = data;
  99. return index;
  100. },
  101. set : function (id, content, config, tags, append) {
  102. var data = $.extend(true, {}, previewCache.data[id]), i;
  103. if (! isArray(content)) {
  104. content = content.split(data.delimiter);
  105. }
  106. if (append) {
  107. for (i = 0; i < content.length; i ++) {
  108. data.content.push(content[i]);
  109. }
  110. for (i = 0; i < config.length; i ++) {
  111. data.config.push(config[i]);
  112. }
  113. for (i = 0; i < tags.length; i ++) {
  114. data.tags.push(tags[i]);
  115. }
  116. } else {
  117. data.content = content;
  118. data.config = config;
  119. data.tags = tags;
  120. }
  121. previewCache.data[id] = data;
  122. },
  123. unset : function (id, index) {
  124. var chk = previewCache.count(id);
  125. if (! chk) {
  126. return;
  127. }
  128. if (chk === 1) {
  129. previewCache.data[id].content = [];
  130. previewCache.data[id].config = [];
  131. return;
  132. }
  133. previewCache.data[id].content[index] = null;
  134. previewCache.data[id].config[index] = null;
  135. },
  136. out : function (id) {
  137. var html = '', data = previewCache.data[id], caption, len = previewCache.count(id, true);
  138. if (len === 0) {
  139. return {content: '', caption: ''};
  140. }
  141. for (var i = 0; i < len; i ++) {
  142. html += previewCache.get(id, i);
  143. }
  144. caption = data.msg(previewCache.count(id));
  145. return {content: html, caption: caption};
  146. },
  147. footer: function (id, i, isDisabled) {
  148. var data = previewCache.data[id];
  149. isDisabled = isDisabled === undefined ? true : isDisabled;
  150. if (data.config.length === 0 || isEmpty(data.config[i])) {
  151. return '';
  152. }
  153. var config = data.config[i],
  154. caption = isSet('caption', config) ? config.caption : '',
  155. width = isSet('width', config) ? config.width : 'auto',
  156. url = isSet('url', config) ? config.url : false,
  157. key = isSet('key', config) ? config.key : null,
  158. disabled = (url === false) && isDisabled,
  159. actions = data.isDelete ? data.actions(false, true, disabled, url, key) : '',
  160. footer = data.footer.repl('{actions}', actions);
  161. return footer
  162. .repl('{caption}', caption)
  163. .repl('{width}', width)
  164. .repl('{indicator}', '')
  165. .repl('{indicatorTitle}', '');
  166. }
  167. },
  168. getNum = function (num, def) {
  169. def = def || 0;
  170. if (typeof num === "number") {
  171. return num;
  172. }
  173. if (typeof num === "string") {
  174. num = parseFloat(num);
  175. }
  176. return isNaN(num) ? def : num;
  177. },
  178. hasFileAPISupport = function () {
  179. return window.File && window.FileReader;
  180. },
  181. hasDragDropSupport = function () {
  182. var $div = document.createElement('div');
  183. return ! isIE(9) && ($div.draggable !== undefined || ($div.ondragstart !== undefined && $div.ondrop !== undefined));
  184. },
  185. hasFileUploadSupport = function () {
  186. return hasFileAPISupport && window.FormData;
  187. },
  188. addCss = function ($el, css) {
  189. $el.removeClass(css).addClass(css);
  190. },
  191. STYLE_SETTING = 'style="width:{width};height:{height};"',
  192. OBJECT_PARAMS = ' <param name="controller" value="true" />\n' +
  193. ' <param name="allowFullScreen" value="true" />\n' +
  194. ' <param name="allowScriptAccess" value="always" />\n' +
  195. ' <param name="autoPlay" value="false" />\n' +
  196. ' <param name="autoStart" value="false" />\n' +
  197. ' <param name="quality" value="high" />\n',
  198. DEFAULT_PREVIEW = '<div class="file-preview-other">\n' +
  199. ' {previewFileIcon}\n' +
  200. ' </div>',
  201. defaultFileActionSettings = {
  202. removeIcon : '<i class="glyphicon glyphicon-trash text-danger"></i>',
  203. removeClass : 'btn btn-xs btn-default',
  204. removeTitle : 'Remove file',
  205. uploadIcon : '<i class="glyphicon glyphicon-upload text-info"></i>',
  206. uploadClass : 'btn btn-xs btn-default',
  207. uploadTitle : 'Upload file',
  208. indicatorNew : '<i class="glyphicon glyphicon-hand-down text-warning"></i>',
  209. indicatorSuccess : '<i class="glyphicon glyphicon-ok-sign file-icon-large text-success"></i>',
  210. indicatorError : '<i class="glyphicon glyphicon-exclamation-sign text-danger"></i>',
  211. indicatorLoading : '<i class="glyphicon glyphicon-hand-up text-muted"></i>',
  212. indicatorNewTitle : 'Not uploaded yet',
  213. indicatorSuccessTitle: 'Uploaded',
  214. indicatorErrorTitle : 'Upload Error',
  215. indicatorLoadingTitle: 'Uploading ...'
  216. },
  217. tMain1 = '{preview}\n' +
  218. '<div class="kv-upload-progress hide"></div>\n' +
  219. '<div class="input-group {class}">\n' +
  220. ' {caption}\n' +
  221. ' <div class="input-group-btn">\n' +
  222. ' {remove}\n' +
  223. ' {cancel}\n' +
  224. ' {upload}\n' +
  225. ' {browse}\n' +
  226. ' </div>\n' +
  227. '</div>',
  228. tMain2 = '{preview}\n<div class="kv-upload-progress hide"></div>\n{remove}\n{cancel}\n{upload}\n{browse}\n',
  229. tPreview = '<div class="file-preview {class}">\n' +
  230. ' <div class="close fileinput-remove">&times;</div>\n' +
  231. ' <div class="{dropClass}">\n' +
  232. ' <div class="file-preview-thumbnails">\n' +
  233. ' </div>\n' +
  234. ' <div class="clearfix"></div>' +
  235. ' <div class="file-preview-status text-center text-success"></div>\n' +
  236. ' <div class="kv-fileinput-error"></div>\n' +
  237. ' </div>\n' +
  238. '</div>',
  239. tIcon = '<span class="glyphicon glyphicon-file kv-caption-icon"></span>',
  240. tCaption = '<div tabindex="-1" class="form-control file-caption {class}">\n' +
  241. ' <span class="file-caption-ellipsis">&hellip;</span>\n' +
  242. ' <div class="file-caption-name"></div>\n' +
  243. '</div>',
  244. tModal = '<div id="{id}" class="modal fade">\n' +
  245. ' <div class="modal-dialog modal-lg">\n' +
  246. ' <div class="modal-content">\n' +
  247. ' <div class="modal-header">\n' +
  248. ' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\n' +
  249. ' <h3 class="modal-title">Detailed Preview <small>{title}</small></h3>\n' +
  250. ' </div>\n' +
  251. ' <div class="modal-body">\n' +
  252. ' <textarea class="form-control" style="font-family:Monaco,Consolas,monospace; height: {height}px;" readonly>{body}</textarea>\n' +
  253. ' </div>\n' +
  254. ' </div>\n' +
  255. ' </div>\n' +
  256. '</div>',
  257. tProgress = '<div class="progress">\n' +
  258. ' <div class="{class}" role="progressbar"' +
  259. ' aria-valuenow="{percent}" aria-valuemin="0" aria-valuemax="100" style="width:{percent}%;">\n' +
  260. ' {percent}%\n' +
  261. ' </div>\n' +
  262. '</div>',
  263. tFooter = '<div class="file-thumbnail-footer">\n' +
  264. ' <div class="file-caption-name">{caption}</div>\n' +
  265. ' {actions}\n' +
  266. '</div>',
  267. tActions = '<div class="file-actions">\n' +
  268. ' <div class="file-footer-buttons">\n' +
  269. ' {upload}{delete}{other}' +
  270. ' </div>\n' +
  271. ' <div class="file-upload-indicator" tabindex="-1" title="{indicatorTitle}">{indicator}</div>\n' +
  272. ' <div class="clearfix"></div>\n' +
  273. '</div>',
  274. tActionDelete = '<button type="button" class="kv-file-remove {removeClass}" ' +
  275. 'title="{removeTitle}"{dataUrl}{dataKey}>{removeIcon}</button>\n',
  276. tActionUpload = '<button type="button" class="kv-file-upload {uploadClass}" title="{uploadTitle}">' +
  277. ' {uploadIcon}\n</button>\n',
  278. tGeneric = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}">\n' +
  279. ' {content}\n' +
  280. ' {footer}\n' +
  281. '</div>\n',
  282. tHtml = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}">\n' +
  283. ' <object data="{data}" type="{type}" width="{width}" height="{height}">\n' +
  284. ' ' + DEFAULT_PREVIEW + '\n' +
  285. ' </object>\n' +
  286. ' {footer}\n' +
  287. '</div>',
  288. tImage = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}">\n' +
  289. ' <img src="{data}" class="file-preview-image" title="{caption}" alt="{caption}" ' + STYLE_SETTING + '>\n' +
  290. ' {footer}\n' +
  291. '</div>\n',
  292. tText = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}">\n' +
  293. ' <div class="file-preview-text" title="{caption}" ' + STYLE_SETTING + '>\n' +
  294. ' {data}\n' +
  295. ' </div>\n' +
  296. ' {footer}\n' +
  297. '</div>',
  298. tVideo = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}"' +
  299. ' title="{caption}" ' + STYLE_SETTING + '>\n' +
  300. ' <video width="{width}" height="{height}" controls>\n' +
  301. ' <source src="{data}" type="{type}">\n' +
  302. ' ' + DEFAULT_PREVIEW + '\n' +
  303. ' </video>\n' +
  304. ' {footer}\n' +
  305. '</div>\n',
  306. tAudio = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}"' +
  307. ' title="{caption}" ' + STYLE_SETTING + '>\n' +
  308. ' <audio controls>\n' +
  309. ' <source src="' + '{data}' + '" type="{type}">\n' +
  310. ' ' + DEFAULT_PREVIEW + '\n' +
  311. ' </audio>\n' +
  312. ' {footer}\n' +
  313. '</div>',
  314. tFlash = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}"' +
  315. ' title="{caption}" ' + STYLE_SETTING + '>\n' +
  316. ' <object type="application/x-shockwave-flash" width="{width}" height="{height}" data="{data}">\n' +
  317. OBJECT_PARAMS + ' ' + DEFAULT_PREVIEW + '\n' +
  318. ' </object>\n' +
  319. ' {footer}\n' +
  320. '</div>\n',
  321. tObject = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}"' +
  322. ' title="{caption}" ' + STYLE_SETTING + '>\n' +
  323. ' <object data="{data}" type="{type}" width="{width}" height="{height}">\n' +
  324. ' <param name="movie" value="{caption}" />\n' +
  325. OBJECT_PARAMS + ' ' + DEFAULT_PREVIEW + '\n' +
  326. ' </object>\n' +
  327. ' {footer}\n' +
  328. '</div>',
  329. tOther = '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}"' +
  330. ' title="{caption}" ' + STYLE_SETTING + '>\n' +
  331. ' ' + DEFAULT_PREVIEW + '\n' +
  332. ' {footer}\n' +
  333. '</div>',
  334. defaultLayoutTemplates = {
  335. main1 : tMain1,
  336. main2 : tMain2,
  337. preview : tPreview,
  338. icon : tIcon,
  339. caption : tCaption,
  340. modal : tModal,
  341. progress : tProgress,
  342. footer : tFooter,
  343. actions : tActions,
  344. actionDelete: tActionDelete,
  345. actionUpload: tActionUpload
  346. },
  347. defaultPreviewTemplates = {
  348. generic: tGeneric,
  349. html : tHtml,
  350. image : tImage,
  351. text : tText,
  352. video : tVideo,
  353. audio : tAudio,
  354. flash : tFlash,
  355. object : tObject,
  356. other : tOther
  357. },
  358. defaultPreviewTypes = ['image', 'html', 'text', 'video', 'audio', 'flash', 'object'],
  359. defaultPreviewSettings = {
  360. image : {width: "auto", height: "160px"},
  361. html : {width: "213px", height: "160px"},
  362. text : {width: "160px", height: "160px"},
  363. video : {width: "213px", height: "160px"},
  364. audio : {width: "213px", height: "80px"},
  365. flash : {width: "213px", height: "160px"},
  366. object: {width: "160px", height: "160px"},
  367. other : {width: "160px", height: "160px"}
  368. },
  369. defaultFileTypeSettings = {
  370. image : function (vType, vName) {
  371. return (vType !== undefined) ? vType.match('image.*') : vName.match(/\.(gif|png|jpe?g)$/i);
  372. },
  373. html : function (vType, vName) {
  374. return (vType !== undefined) ? vType === 'text/html' : vName.match(/\.(htm|html)$/i);
  375. },
  376. text : function (vType, vName) {
  377. return (vType !== undefined && vType.match('text.*')) || vName.match(/\.(txt|md|csv|nfo|php|ini)$/i);
  378. },
  379. video : function (vType, vName) {
  380. return (vType !== undefined && vType.match(/\.video\/(ogg|mp4|webm)$/i)) || vName.match(/\.(og?|mp4|webm)$/i);
  381. },
  382. audio : function (vType, vName) {
  383. return (vType !== undefined && vType.match(/\.audio\/(ogg|mp3|wav)$/i)) || vName.match(/\.(ogg|mp3|wav)$/i);
  384. },
  385. flash : function (vType, vName) {
  386. return (vType !== undefined && vType === 'application/x-shockwave-flash') || vName.match(/\.(swf)$/i);
  387. },
  388. object: function () {
  389. return true;
  390. },
  391. other : function () {
  392. return true;
  393. }
  394. },
  395. isEmpty = function (value, trim) {
  396. return value === null || value === undefined || value.length === 0 || (trim && $.trim(value) === '');
  397. },
  398. isArray = function (a) {
  399. return Array.isArray(a) || Object.prototype.toString.call(a) === '[object Array]';
  400. },
  401. isSet = function (needle, haystack) {
  402. return (typeof haystack === 'object' && needle in haystack);
  403. },
  404. getElement = function (options, param, value) {
  405. return (isEmpty(options) || isEmpty(options[param])) ? value : $(options[param]);
  406. },
  407. uniqId = function () {
  408. return Math.round(new Date().getTime() + (Math.random() * 100));
  409. },
  410. htmlEncode = function (str) {
  411. return String(str).repl('&', '&amp;')
  412. .repl('"', '&quot;')
  413. .repl("'", '&#39;')
  414. .repl('<', '&lt;')
  415. .repl('>', '&gt;');
  416. },
  417. replaceTags = function (str, tags) {
  418. var out = str;
  419. tags = tags || {};
  420. $.each(tags, function (key, value) {
  421. if (typeof value === "function") {
  422. value = value();
  423. }
  424. out = out.repl(key, value);
  425. });
  426. return out;
  427. },
  428. objUrl = window.URL || window.webkitURL,
  429. FileInput = function (element, options) {
  430. var self = this;
  431. self.$element = $(element);
  432. if (! self.validate()) {
  433. return;
  434. }
  435. if (hasFileAPISupport() || isIE(9)) {
  436. self.init(options);
  437. self.listen();
  438. } else {
  439. self.$element.removeClass('file-loading');
  440. }
  441. };
  442. FileInput.prototype = {
  443. constructor : FileInput,
  444. validate : function () {
  445. var self = this, $exception;
  446. if (self.$element.attr('type') === 'file') {
  447. return true;
  448. }
  449. $exception = '<div class="help-block alert alert-warning">' +
  450. '<h4>Invalid Input Type</h4>' +
  451. 'You must set an input <code>type = file</code> for <b>bootstrap-fileinput</b> plugin to initialize.' +
  452. '</div>';
  453. self.$element.after($exception);
  454. return false;
  455. },
  456. init : function (options) {
  457. var self = this, $el = self.$element, t;
  458. $.each(options, function (key, value) {
  459. self[key] = (key === 'maxFileCount' || key === 'maxFileSize') ? getNum(value) : value;
  460. });
  461. self.fileInputCleared = false;
  462. self.fileBatchCompleted = true;
  463. if (isEmpty(self.allowedPreviewTypes)) {
  464. self.allowedPreviewTypes = defaultPreviewTypes;
  465. }
  466. self.uploadFileAttr = ! isEmpty($el.attr('name')) ? $el.attr('name') : 'file_data';
  467. self.reader = null;
  468. self.formdata = {};
  469. self.isIE9 = isIE(9);
  470. self.isIE10 = isIE(10);
  471. self.filestack = [];
  472. self.ajaxRequests = [];
  473. self.isError = false;
  474. self.ajaxAborted = false;
  475. self.dropZoneEnabled = hasDragDropSupport() && self.dropZoneEnabled;
  476. self.isDisabled = self.$element.attr('disabled') || self.$element.attr('readonly');
  477. self.isUploadable = hasFileUploadSupport && ! isEmpty(self.uploadUrl);
  478. self.slug = typeof options.slugCallback === "function" ? options.slugCallback : self.slugDefault;
  479. self.mainTemplate = self.showCaption ? self.getLayoutTemplate('main1') : self.getLayoutTemplate('main2');
  480. self.captionTemplate = self.getLayoutTemplate('caption');
  481. self.previewGenericTemplate = self.getPreviewTemplate('generic');
  482. if (isEmpty(self.$element.attr('id'))) {
  483. self.$element.attr('id', uniqId());
  484. }
  485. if (self.$container === undefined) {
  486. self.$container = self.createContainer();
  487. } else {
  488. self.refreshContainer();
  489. }
  490. self.$progress = self.$container.find('.kv-upload-progress');
  491. self.$btnUpload = self.$container.find('.kv-fileinput-upload');
  492. self.$captionContainer = getElement(options, 'elCaptionContainer', self.$container.find('.file-caption'));
  493. self.$caption = getElement(options, 'elCaptionText', self.$container.find('.file-caption-name'));
  494. self.$previewContainer = getElement(options, 'elPreviewContainer', self.$container.find('.file-preview'));
  495. self.$preview = getElement(options, 'elPreviewImage', self.$container.find('.file-preview-thumbnails'));
  496. self.$previewStatus = getElement(options, 'elPreviewStatus', self.$container.find('.file-preview-status'));
  497. self.$errorContainer = getElement(options, 'elErrorContainer',
  498. self.$previewContainer.find('.kv-fileinput-error'));
  499. if (! isEmpty(self.msgErrorClass)) {
  500. addCss(self.$errorContainer, self.msgErrorClass);
  501. }
  502. self.$errorContainer.hide();
  503. self.fileActionSettings = $.extend(defaultFileActionSettings, options.fileActionSettings);
  504. self.previewInitId = "preview-" + uniqId();
  505. self.id = self.$element.attr('id');
  506. previewCache.init(self);
  507. self.initPreview(true);
  508. self.initPreviewDeletes();
  509. self.options = options;
  510. self.setFileDropZoneTitle();
  511. self.uploadCount = 0;
  512. self.uploadPercent = 0;
  513. self.$element.removeClass('file-loading');
  514. t = self.getLayoutTemplate('progress');
  515. self.progressTemplate = t.replace('{class}', self.progressClass);
  516. self.progressCompleteTemplate = t.replace('{class}', self.progressCompleteClass);
  517. self.setEllipsis();
  518. },
  519. parseError : function (jqXHR, errorThrown, fileName) {
  520. var self = this, errMsg = $.trim(errorThrown + ''),
  521. dot = errMsg.slice(- 1) === '.' ? '' : '.',
  522. text = $(jqXHR.responseText).text();
  523. if (self.showAjaxErrorDetails) {
  524. text = $.trim(text.replace(/\n\s*\n/g, '\n'));
  525. text = text.length > 0 ? '<pre>' + text + '</pre>' : '';
  526. errMsg += dot + text;
  527. } else {
  528. errMsg += dot;
  529. }
  530. return fileName ? '<b>' + fileName + ': </b>' + jqXHR : errMsg;
  531. },
  532. raise : function (event, params) {
  533. var self = this, e = $.Event(event), out = false;
  534. if (params !== undefined) {
  535. self.$element.trigger(e, params);
  536. } else {
  537. self.$element.trigger(e);
  538. }
  539. if (e.result) {
  540. out = true;
  541. }
  542. if (! out) {
  543. return;
  544. }
  545. switch (event) {
  546. // ignore these events
  547. case 'filebatchuploadcomplete':
  548. case 'filebatchuploadsuccess':
  549. case 'fileuploaded':
  550. case 'fileclear':
  551. case 'filecleared':
  552. case 'filereset':
  553. case 'fileerror':
  554. case 'filefoldererror':
  555. case 'fileuploaderror':
  556. case 'filebatchuploaderror':
  557. case 'filedeleteerror':
  558. case 'filecustomerror':
  559. break;
  560. // can trigger filecustomerror to abort upload
  561. default:
  562. self.ajaxAborted = out;
  563. break;
  564. }
  565. },
  566. getLayoutTemplate : function (t) {
  567. var self = this,
  568. template = isSet(t, self.layoutTemplates) ? self.layoutTemplates[t] : defaultLayoutTemplates[t];
  569. if (isEmpty(self.customLayoutTags)) {
  570. return template;
  571. }
  572. return replaceTags(template, self.customLayoutTags);
  573. },
  574. getPreviewTemplate : function (t) {
  575. var self = this,
  576. template = isSet(t, self.previewTemplates) ? self.previewTemplates[t] : defaultPreviewTemplates[t];
  577. template = template.repl('{previewFileIcon}', self.previewFileIcon);
  578. if (isEmpty(self.customPreviewTags)) {
  579. return template;
  580. }
  581. return replaceTags(template, self.customPreviewTags);
  582. },
  583. getOutData : function (jqXHR, responseData, filesData) {
  584. var self = this;
  585. jqXHR = jqXHR || {};
  586. responseData = responseData || {};
  587. filesData = filesData || self.filestack.slice(0) || {};
  588. return {
  589. form : self.formdata,
  590. files : filesData,
  591. extra : self.getExtraData(),
  592. response: responseData,
  593. reader : self.reader,
  594. jqXHR : jqXHR
  595. };
  596. },
  597. setEllipsis : function () {
  598. var self = this, $capCont = self.$captionContainer, $cap = self.$caption,
  599. $div = $cap.clone().css('height', 'auto').hide();
  600. $capCont.parent().before($div);
  601. $capCont.removeClass('kv-has-ellipsis');
  602. if ($div.outerWidth() > $cap.outerWidth()) {
  603. $capCont.addClass('kv-has-ellipsis');
  604. }
  605. $div.remove();
  606. },
  607. listen : function () {
  608. var self = this, $el = self.$element, $cap = self.$captionContainer, $btnFile = self.$btnFile,
  609. $form = $el.closest('form');
  610. $el.on('change', $.proxy(self.change, self));
  611. $(window).on('resize', function () {
  612. self.setEllipsis();
  613. });
  614. $btnFile.off('click').on('click', function () {
  615. self.raise('filebrowse');
  616. if (self.isError && ! self.isUploadable) {
  617. self.clear();
  618. }
  619. $cap.focus();
  620. });
  621. $form.off('reset').on('reset', $.proxy(self.reset, self));
  622. self.$container.off('click')
  623. .on('click', '.fileinput-remove:not([disabled])', $.proxy(self.clear, self))
  624. .on('click', '.fileinput-cancel', $.proxy(self.cancel, self));
  625. if (self.isUploadable && self.dropZoneEnabled && self.showPreview) {
  626. self.initDragDrop();
  627. }
  628. if (! self.isUploadable) {
  629. $form.on('submit', $.proxy(self.submitForm, self));
  630. }
  631. self.$container.find('.kv-fileinput-upload').off('click').on('click', function (e) {
  632. if (! self.isUploadable) {
  633. return;
  634. }
  635. e.preventDefault();
  636. if (! $(this).hasClass('disabled') && isEmpty($(this).attr('disabled'))) {
  637. self.upload();
  638. }
  639. });
  640. },
  641. submitForm : function () {
  642. var self = this, $el = self.$element, files = $el.get(0).files;
  643. if (files && files.length < self.minFileCount && self.minFileCount > 0) {
  644. self.noFilesError({});
  645. return false;
  646. }
  647. return ! self.abort({});
  648. },
  649. abort : function (params) {
  650. var self = this, data;
  651. if (self.ajaxAborted && typeof self.ajaxAborted === "object" && self.ajaxAborted.message !== undefined) {
  652. if (self.ajaxAborted.data !== undefined) {
  653. data = self.getOutData({}, self.ajaxAborted.data);
  654. } else {
  655. data = self.getOutData();
  656. }
  657. data = $.extend(data, params);
  658. self.showUploadError(self.ajaxAborted.message, data, 'filecustomerror');
  659. return true;
  660. }
  661. return false;
  662. },
  663. noFilesError : function (params) {
  664. var self = this, label = self.minFileCount > 1 ? self.filePlural : self.fileSingle,
  665. msg = self.msgFilesTooLess.repl('{n}', self.minFileCount).repl('{files}', label),
  666. $error = self.$errorContainer;
  667. $error.html(msg);
  668. self.isError = true;
  669. self.updateFileDetails(0);
  670. $error.fadeIn(800);
  671. self.raise('fileerror', [params]);
  672. self.clearFileInput();
  673. addCss(self.$container, 'has-error');
  674. },
  675. setProgress : function (p) {
  676. var self = this, pct = Math.min(p, 100),
  677. template = pct < 100 ? self.progressTemplate : self.progressCompleteTemplate;
  678. self.$progress.html(template.repl('{percent}', pct));
  679. },
  680. upload : function () {
  681. var self = this, totLen = self.getFileStack().length, params = {},
  682. i, outData, len, hasExtraData = ! $.isEmptyObject(self.getExtraData());
  683. if (totLen < self.minFileCount && self.minFileCount > 0) {
  684. self.noFilesError(params);
  685. return;
  686. }
  687. if (! self.isUploadable || self.isDisabled || (totLen === 0 && ! hasExtraData)) {
  688. return;
  689. }
  690. self.resetUpload();
  691. self.$progress.removeClass('hide');
  692. self.uploadCount = 0;
  693. self.uploadPercent = 0;
  694. self.lock();
  695. self.setProgress(0);
  696. if (totLen === 0 && hasExtraData) {
  697. self.uploadExtraOnly();
  698. return;
  699. }
  700. len = self.filestack.length;
  701. self.hasInitData = false;
  702. if (self.uploadAsync && self.showPreview) {
  703. outData = self.getOutData();
  704. self.raise('filebatchpreupload', [outData]);
  705. self.fileBatchCompleted = false;
  706. self.uploadCache = {content: [], config: [], tags: [], append: true};
  707. for (i = 0; i < len; i += 1) {
  708. if (self.filestack[i] !== undefined) {
  709. self.uploadSingle(i, self.filestack, true);
  710. }
  711. }
  712. return;
  713. }
  714. self.uploadBatch();
  715. },
  716. lock : function () {
  717. var self = this;
  718. self.resetErrors();
  719. self.disable();
  720. if (self.showRemove) {
  721. addCss(self.$container.find('.fileinput-remove'), 'hide');
  722. }
  723. if (self.showCancel) {
  724. self.$container.find('.fileinput-cancel').removeClass('hide');
  725. }
  726. self.raise('filelock', [self.filestack, self.getExtraData()]);
  727. },
  728. unlock : function (reset) {
  729. var self = this;
  730. if (reset === undefined) {
  731. reset = true;
  732. }
  733. self.enable();
  734. if (self.showCancel) {
  735. addCss(self.$container.find('.fileinput-cancel'), 'hide');
  736. }
  737. if (self.showRemove) {
  738. self.$container.find('.fileinput-remove').removeClass('hide');
  739. }
  740. if (reset) {
  741. self.resetFileStack();
  742. }
  743. self.raise('fileunlock', [self.filestack, self.getExtraData()]);
  744. },
  745. resetFileStack : function () {
  746. var self = this, i = 0, newstack = [];
  747. self.getThumbs().each(function () {
  748. var $thumb = $(this), ind = $thumb.attr('data-fileindex'),
  749. file = self.filestack[ind];
  750. if (ind === - 1) {
  751. return;
  752. }
  753. if (file !== undefined) {
  754. newstack[i] = file;
  755. $thumb.attr({
  756. 'id' : self.previewInitId + '-' + i,
  757. 'data-fileindex': i
  758. });
  759. i += 1;
  760. } else {
  761. $thumb.attr({
  762. 'id' : 'uploaded-' + uniqId(),
  763. 'data-fileindex': '-1'
  764. });
  765. }
  766. });
  767. self.filestack = newstack;
  768. },
  769. refresh : function (options) {
  770. var self = this, $el = self.$element, $zone,
  771. params = (arguments.length) ? $.extend(self.options, options) : self.options;
  772. $el.off();
  773. self.init(params);
  774. $zone = self.$container.find('.file-drop-zone');
  775. $zone.off('dragenter dragover drop');
  776. $(document).off('dragenter dragover drop');
  777. self.listen();
  778. self.setFileDropZoneTitle();
  779. },
  780. initDragDrop : function () {
  781. var self = this, $zone = self.$container.find('.file-drop-zone');
  782. $zone.off('dragenter dragover drop');
  783. $(document).off('dragenter dragover drop');
  784. $zone.on('dragenter dragover', function (e) {
  785. e.stopPropagation();
  786. e.preventDefault();
  787. if (self.isDisabled) {
  788. return;
  789. }
  790. addCss($(this), 'highlighted');
  791. });
  792. $zone.on('dragleave', function (e) {
  793. e.stopPropagation();
  794. e.preventDefault();
  795. if (self.isDisabled) {
  796. return;
  797. }
  798. $(this).removeClass('highlighted');
  799. });
  800. $zone.on('drop', function (e) {
  801. e.preventDefault();
  802. if (self.isDisabled) {
  803. return;
  804. }
  805. self.change(e, 'dragdrop');
  806. $(this).removeClass('highlighted');
  807. });
  808. $(document).on('dragenter dragover drop', function (e) {
  809. e.stopPropagation();
  810. e.preventDefault();
  811. });
  812. },
  813. setFileDropZoneTitle: function () {
  814. var self = this, $zone = self.$container.find('.file-drop-zone');
  815. $zone.find('.' + self.dropZoneTitleClass).remove();
  816. if (! self.isUploadable || ! self.showPreview || $zone.length === 0 || self.getFileStack().length > 0 || ! self.dropZoneEnabled) {
  817. return;
  818. }
  819. if ($zone.find('.file-preview-frame').length === 0) {
  820. $zone.prepend('<div class="' + self.dropZoneTitleClass + '">' + self.dropZoneTitle + '</div>');
  821. }
  822. self.$container.removeClass('file-input-new');
  823. addCss(self.$container, 'file-input-ajax-new');
  824. },
  825. initFileActions : function () {
  826. var self = this;
  827. self.$preview.find('.kv-file-remove').each(function () {
  828. var $el = $(this), $frame = $el.closest('.file-preview-frame'),
  829. ind = $frame.attr('data-fileindex'), n, cap;
  830. $el.off('click').on('click', function () {
  831. $frame.fadeOut('slow', function () {
  832. self.filestack[ind] = undefined;
  833. self.clearObjects($frame);
  834. $frame.remove();
  835. var filestack = self.getFileStack(), len = filestack.length,
  836. chk = previewCache.count(self.id);
  837. self.clearFileInput();
  838. if (len === 0 && chk === 0) {
  839. self.reset();
  840. } else {
  841. n = chk + len;
  842. cap = n > 1 ? self.getMsgSelected(n) : filestack[0].name;
  843. self.setCaption(cap);
  844. }
  845. });
  846. });
  847. });
  848. self.$preview.find('.kv-file-upload').each(function () {
  849. var $el = $(this);
  850. $el.off('click').on('click', function () {
  851. var $frame = $el.closest('.file-preview-frame'),
  852. ind = $frame.attr('data-fileindex');
  853. self.uploadSingle(ind, self.filestack, false);
  854. });
  855. });
  856. },
  857. getMsgSelected : function (n) {
  858. var self = this, strFiles = n === 1 ? self.fileSingle : self.filePlural;
  859. return self.msgSelected.repl('{n}', n).repl('{files}', strFiles);
  860. },
  861. renderFileFooter : function (caption, width) {
  862. var self = this, config = self.fileActionSettings, footer, out,
  863. template = self.getLayoutTemplate('footer');
  864. if (self.isUploadable) {
  865. footer = template.repl('{actions}', self.renderFileActions(true, true, false, false, false));
  866. out = footer.repl('{caption}', caption)
  867. .repl('{width}', width)
  868. .repl('{indicator}', config.indicatorNew)
  869. .repl('{indicatorTitle}', config.indicatorNewTitle);
  870. } else {
  871. out = template.repl('{actions}', '')
  872. .repl('{caption}', caption)
  873. .repl('{width}', width)
  874. .repl('{indicator}', '')
  875. .repl('{indicatorTitle}', '');
  876. }
  877. out = replaceTags(out, self.previewThumbTags);
  878. return out;
  879. },
  880. renderFileActions : function (showUpload, showDelete, disabled, url, key) {
  881. if (! showUpload && ! showDelete) {
  882. return '';
  883. }
  884. var self = this,
  885. vUrl = url === false ? '' : ' data-url="' + url + '"',
  886. vKey = key === false ? '' : ' data-key="' + key + '"',
  887. btnDelete = self.getLayoutTemplate('actionDelete'),
  888. btnUpload = '',
  889. template = self.getLayoutTemplate('actions'),
  890. otherButtons = self.otherActionButtons.repl('{dataKey}', vKey),
  891. config = self.fileActionSettings,
  892. removeClass = disabled ? config.removeClass + ' disabled' : config.removeClass;
  893. btnDelete = btnDelete
  894. .repl('{removeClass}', removeClass)
  895. .repl('{removeIcon}', config.removeIcon)
  896. .repl('{removeTitle}', config.removeTitle)
  897. .repl('{dataUrl}', vUrl)
  898. .repl('{dataKey}', vKey);
  899. if (showUpload) {
  900. btnUpload = self.getLayoutTemplate('actionUpload')
  901. .repl('{uploadClass}', config.uploadClass)
  902. .repl('{uploadIcon}', config.uploadIcon)
  903. .repl('{uploadTitle}', config.uploadTitle);
  904. }
  905. return template
  906. .repl('{delete}', btnDelete)
  907. .repl('{upload}', btnUpload)
  908. .repl('{other}', otherButtons);
  909. },
  910. initPreview : function (isInit) {
  911. var self = this, cap = self.initialCaption || '', out;
  912. if (! previewCache.count(self.id)) {
  913. self.$preview.html('');
  914. if (isInit) {
  915. self.setCaption(cap);
  916. } else {
  917. self.initCaption();
  918. }
  919. return;
  920. }
  921. out = previewCache.out(self.id);
  922. cap = isInit && self.initialCaption ? self.initialCaption : out.caption;
  923. self.$preview.html(out.content);
  924. self.setCaption(cap);
  925. if (! isEmpty(out.content)) {
  926. self.$container.removeClass('file-input-new');
  927. }
  928. },
  929. initPreviewDeletes : function () {
  930. var self = this, deleteExtraData = self.deleteExtraData || {},
  931. resetProgress = function () {
  932. if (self.$preview.find('.kv-file-remove').length === 0) {
  933. self.reset();
  934. self.initialCaption = '';
  935. }
  936. };
  937. self.$preview.find('.kv-file-remove').each(function () {
  938. var $el = $(this), vUrl = $el.data('url') || self.deleteUrl, vKey = $el.data('key');
  939. if (isEmpty(vUrl) || vKey === undefined) {
  940. return;
  941. }
  942. var $frame = $el.closest('.file-preview-frame'), cache = previewCache.data[self.id],
  943. settings, params, index = $frame.data('fileindex'), config, extraData;
  944. index = parseInt(index.replace('init_', ''));
  945. config = isEmpty(cache.config) && isEmpty(cache.config[index]) ? null : cache.config[index];
  946. extraData = isEmpty(config) || isEmpty(config.extra) ? deleteExtraData : config.extra;
  947. if (typeof extraData === "function") {
  948. extraData = extraData();
  949. }
  950. params = {id: $el.attr('id'), key: vKey, extra: extraData};
  951. settings = $.extend({
  952. url : vUrl,
  953. type : 'DELETE',
  954. dataType : 'json',
  955. data : $.extend({key: vKey}, extraData),
  956. beforeSend: function (jqXHR) {
  957. self.ajaxAborted = false;
  958. self.raise('filepredelete', [vKey, jqXHR, extraData]);
  959. if (self.ajaxAborted) {
  960. jqXHR.abort();
  961. } else {
  962. addCss($frame, 'file-uploading');
  963. addCss($el, 'disabled');
  964. }
  965. },
  966. success : function (data, textStatus, jqXHR) {
  967. var n, cap;
  968. if (data === undefined || data.error === undefined) {
  969. previewCache.unset(self.id, index);
  970. n = previewCache.count(self.id);
  971. cap = n > 0 ? self.getMsgSelected(n) : '';
  972. self.raise('filedeleted', [vKey, jqXHR, extraData]);
  973. self.setCaption(cap);
  974. } else {
  975. params.jqXHR = jqXHR;
  976. params.response = data;
  977. self.showError(data.error, params, 'filedeleteerror');
  978. $frame.removeClass('file-uploading');
  979. $el.removeClass('disabled');
  980. resetProgress();
  981. return;
  982. }
  983. $frame.removeClass('file-uploading').addClass('file-deleted');
  984. $frame.fadeOut('slow', function () {
  985. self.clearObjects($frame);
  986. $frame.remove();
  987. resetProgress();
  988. if (! n && self.getFileStack().length === 0) {
  989. self.setCaption('');
  990. self.reset();
  991. }
  992. });
  993. },
  994. error : function (jqXHR, textStatus, errorThrown) {
  995. var errMsg = self.parseError(jqXHR, errorThrown);
  996. params.jqXHR = jqXHR;
  997. params.response = {};
  998. self.showError(errMsg, params, 'filedeleteerror');
  999. $frame.removeClass('file-uploading');
  1000. resetProgress();
  1001. }
  1002. }, self.ajaxDeleteSettings);
  1003. $el.off('click').on('click', function () {
  1004. $.ajax(settings);
  1005. });
  1006. });
  1007. },
  1008. clearObjects : function ($el) {
  1009. $el.find('video audio').each(function () {
  1010. this.pause();
  1011. $(this).remove();
  1012. });
  1013. $el.find('img object div').each(function () {
  1014. $(this).remove();
  1015. });
  1016. },
  1017. clearFileInput : function () {
  1018. var self = this, $el = self.$element, $srcFrm, $tmpFrm, $tmpEl;
  1019. if (isEmpty($el.val())) {
  1020. return;
  1021. }
  1022. // Fix for IE ver < 11, that does not clear file inputs
  1023. // Requires a sequence of steps to prevent IE crashing but
  1024. // still allow clearing of the file input.
  1025. if (self.isIE9 || self.isIE10) {
  1026. $srcFrm = $el.closest('form');
  1027. $tmpFrm = $(document.createElement('form'));
  1028. $tmpEl = $(document.createElement('div'));
  1029. $el.before($tmpEl);
  1030. if ($srcFrm.length) {
  1031. $srcFrm.after($tmpFrm);
  1032. } else {
  1033. $tmpEl.after($tmpFrm);
  1034. }
  1035. $tmpFrm.append($el).trigger('reset');
  1036. $tmpEl.before($el).remove();
  1037. $tmpFrm.remove();
  1038. } else { // normal input clear behavior for other sane browsers
  1039. $el.val('');
  1040. }
  1041. self.fileInputCleared = true;
  1042. },
  1043. resetUpload : function () {
  1044. var self = this;
  1045. self.uploadCache = {content: [], config: [], tags: [], append: true};
  1046. self.uploadCount = 0;
  1047. self.uploadPercent = 0;
  1048. self.$btnUpload.removeAttr('disabled');
  1049. self.setProgress(0);
  1050. addCss(self.$progress, 'hide');
  1051. self.resetErrors(false);
  1052. self.ajaxAborted = false;
  1053. self.ajaxRequests = [];
  1054. },
  1055. cancel : function () {
  1056. var self = this, xhr = self.ajaxRequests, len = xhr.length, i;
  1057. if (len > 0) {
  1058. for (i = 0; i < len; i += 1) {
  1059. xhr[i].abort();
  1060. }
  1061. }
  1062. self.getThumbs().each(function () {
  1063. var $thumb = $(this), ind = $thumb.attr('data-fileindex');
  1064. $thumb.removeClass('file-uploading');
  1065. if (self.filestack[ind] !== undefined) {
  1066. $thumb.find('.kv-file-upload').removeClass('disabled').removeAttr('disabled');
  1067. $thumb.find('.kv-file-remove').removeClass('disabled').removeAttr('disabled');
  1068. }
  1069. self.unlock();
  1070. });
  1071. },
  1072. clear : function () {
  1073. var self = this, cap;
  1074. self.$btnUpload.removeAttr('disabled');
  1075. self.resetUpload();
  1076. self.filestack = [];
  1077. self.clearFileInput();
  1078. self.resetErrors(true);
  1079. self.raise('fileclear');
  1080. if (! self.overwriteInitial && previewCache.count(self.id)) {
  1081. self.showFileIcon();
  1082. self.resetPreview();
  1083. self.setEllipsis();
  1084. self.initPreviewDeletes();
  1085. self.$container.removeClass('file-input-new');
  1086. } else {
  1087. self.getThumbs().each(function () {
  1088. self.clearObjects($(this));
  1089. });
  1090. self.$preview.html('');
  1091. cap = (! self.overwriteInitial && self.initialCaption.length > 0) ? self.initialCaption : '';
  1092. self.setCaption(cap);
  1093. self.setEllipsis();
  1094. self.$caption.attr('title', '');
  1095. addCss(self.$container, 'file-input-new');
  1096. }
  1097. if (self.$container.find('.file-preview-frame').length === 0) {
  1098. if (! self.initCaption()) {
  1099. self.$captionContainer.find('.kv-caption-icon').hide();
  1100. }
  1101. self.setEllipsis();
  1102. }
  1103. self.hideFileIcon();
  1104. self.raise('filecleared');
  1105. self.$captionContainer.focus();
  1106. self.setFileDropZoneTitle();
  1107. },
  1108. resetPreview : function () {
  1109. var self = this, out;
  1110. if (previewCache.count(self.id)) {
  1111. out = previewCache.out(self.id);
  1112. self.$preview.html(out.content);
  1113. self.setCaption(out.caption);
  1114. } else {
  1115. self.$preview.html('');
  1116. self.initCaption();
  1117. }
  1118. },
  1119. reset : function () {
  1120. var self = this;
  1121. self.clear();
  1122. self.resetPreview();
  1123. self.setEllipsis();
  1124. self.$container.find('.fileinput-filename').text('');
  1125. self.raise('filereset');
  1126. if (self.initialPreview.length > 0) {
  1127. self.$container.removeClass('file-input-new');
  1128. }
  1129. self.setFileDropZoneTitle();
  1130. self.filestack = [];
  1131. self.formdata = {};
  1132. },
  1133. disable : function () {
  1134. var self = this;
  1135. self.isDisabled = true;
  1136. self.raise('filedisabled');
  1137. self.$element.attr('disabled', 'disabled');
  1138. self.$container.find(".kv-fileinput-caption").addClass("file-caption-disabled");
  1139. self.$container.find(".btn-file, .fileinput-remove, .kv-fileinput-upload").attr("disabled", true);
  1140. self.initDragDrop();
  1141. },
  1142. enable : function () {
  1143. var self = this;
  1144. self.isDisabled = false;
  1145. self.raise('fileenabled');
  1146. self.$element.removeAttr('disabled');
  1147. self.$container.find(".kv-fileinput-caption").removeClass("file-caption-disabled");
  1148. self.$container.find(".btn-file, .fileinput-remove, .kv-fileinput-upload").removeAttr("disabled");
  1149. self.initDragDrop();
  1150. },
  1151. getThumbs : function (css) {
  1152. css = css || '';
  1153. return this.$preview.find('.file-preview-frame:not(.file-preview-initial)' + css);
  1154. },
  1155. getExtraData : function () {
  1156. var self = this, data = self.uploadExtraData;
  1157. if (typeof self.uploadExtraData === "function") {
  1158. data = self.uploadExtraData();
  1159. }
  1160. return data;
  1161. },
  1162. uploadExtra : function () {
  1163. var self = this, data = self.getExtraData();
  1164. if (data.length === 0) {
  1165. return;
  1166. }
  1167. $.each(data, function (key, value) {
  1168. self.formdata.append(key, value);
  1169. });
  1170. },
  1171. initXhr : function (xhrobj, factor) {
  1172. var self = this;
  1173. if (xhrobj.upload) {
  1174. xhrobj.upload.addEventListener('progress', function (event) {
  1175. var pct = 0, position = event.loaded || event.position, total = event.total;
  1176. if (event.lengthComputable) {
  1177. pct = Math.ceil(position / total * factor);
  1178. }
  1179. self.uploadPercent = Math.max(pct, self.uploadPercent);
  1180. self.setProgress(self.uploadPercent);
  1181. }, false);
  1182. }
  1183. return xhrobj;
  1184. },
  1185. ajaxSubmit : function (fnBefore, fnSuccess, fnComplete, fnError) {
  1186. var self = this, settings;
  1187. self.uploadExtra();
  1188. settings = $.extend({
  1189. xhr : function () {
  1190. var xhrobj = $.ajaxSettings.xhr();
  1191. return self.initXhr(xhrobj, 98);
  1192. },
  1193. url : self.uploadUrl,
  1194. type : 'POST',
  1195. dataType : 'json',
  1196. data : self.formdata,
  1197. cache : false,
  1198. processData: false,
  1199. contentType: false,
  1200. beforeSend : fnBefore,
  1201. success : fnSuccess,
  1202. complete : fnComplete,
  1203. error : fnError
  1204. }, self.ajaxSettings);
  1205. self.ajaxRequests.push($.ajax(settings));
  1206. },
  1207. initUploadSuccess : function (out, $thumb, allFiles) {
  1208. var self = this, append, data, index, $newThumb, content, config, tags;
  1209. if (typeof out !== 'object' || $.isEmptyObject(out)) {
  1210. return;
  1211. }
  1212. if (out.initialPreview !== undefined && out.initialPreview.length > 0) {
  1213. self.hasInitData = true;
  1214. content = out.initialPreview || [];
  1215. config = out.initialPreviewConfig || [];
  1216. tags = out.initialPreviewThumbTags || [];
  1217. append = out.append === undefined || out.append ? true : false;
  1218. self.overwriteInitial = false;
  1219. if ($thumb !== undefined && ! allFiles) {
  1220. index = previewCache.add(self.id, content, config[0], tags[0], append);
  1221. data = previewCache.get(self.id, index, false);
  1222. $newThumb = $(data).hide();
  1223. $thumb.after($newThumb).fadeOut('slow', function () {
  1224. $newThumb.fadeIn('slow').css('display:inline-block');
  1225. self.initPreviewDeletes();
  1226. self.clearFileInput();
  1227. });
  1228. } else {
  1229. if (allFiles) {
  1230. self.uploadCache.content.push(content[0]);
  1231. self.uploadCache.config.push(config[0]);
  1232. self.uploadCache.tags.push(tags[0]);
  1233. self.uploadCache.append = append;
  1234. } else {
  1235. previewCache.set(self.id, content, config, tags, append);
  1236. self.initPreview();
  1237. self.initPreviewDeletes();
  1238. }
  1239. }
  1240. }
  1241. },
  1242. uploadSingle : function (i, files, allFiles) {
  1243. var self = this, total = self.getFileStack().length, formdata = new FormData(), outData,
  1244. previewId = self.previewInitId + "-" + i, $thumb = $('#' + previewId + ':not(.file-preview-initial)'),
  1245. pct, chkComplete, $btnUpload = $thumb.find('.kv-file-upload'), $btnDelete = $thumb.find('.kv-file-remove'),
  1246. $indicator = $thumb.find('.file-upload-indicator'), config = self.fileActionSettings,
  1247. hasPostData = self.filestack.length > 0 || ! $.isEmptyObject(self.uploadExtraData),
  1248. setIndicator, updateProgress, resetActions, fnBefore, fnSuccess, fnComplete, fnError,
  1249. params = {id: previewId, index: i};
  1250. self.formdata = formdata;
  1251. if (total === 0 || ! hasPostData || $btnUpload.hasClass('disabled') || self.abort(params)) {
  1252. return;
  1253. }
  1254. chkComplete = function () {
  1255. var $thumbs = self.getThumbs('.file-uploading');
  1256. if ($thumbs.length > 0 || self.fileBatchCompleted) {
  1257. return;
  1258. }
  1259. self.fileBatchCompleted = true;
  1260. setTimeout(function () {
  1261. previewCache.set(self.id, self.uploadCache.content, self.uploadCache.config, self.uploadCache.tags,
  1262. self.uploadCache.append);
  1263. if (self.hasInitData) {
  1264. self.initPreview();
  1265. self.initPreviewDeletes();
  1266. }
  1267. self.setProgress(100);
  1268. self.unlock();
  1269. self.clearFileInput();
  1270. self.raise('filebatchuploadcomplete', [self.filestack, self.getExtraData()]);
  1271. }, 100);
  1272. };
  1273. setIndicator = function (icon, msg) {
  1274. $indicator.html(config[icon]);
  1275. $indicator.attr('title', config[msg]);
  1276. };
  1277. updateProgress = function () {
  1278. if (! allFiles || total === 0 || self.uploadPercent >= 100) {
  1279. return;
  1280. }
  1281. self.uploadCount += 1;
  1282. pct = 80 + Math.ceil(self.uploadCount * 20 / total);
  1283. self.uploadPercent = Math.max(pct, self.uploadPercent);
  1284. self.setProgress(self.uploadPercent);
  1285. self.initPreviewDeletes();
  1286. };
  1287. resetActions = function () {
  1288. $btnUpload.removeAttr('disabled');
  1289. $btnDelete.removeAttr('disabled');
  1290. $thumb.removeClass('file-uploading');
  1291. };
  1292. fnBefore = function (jqXHR) {
  1293. outData = self.getOutData(jqXHR);
  1294. setIndicator('indicatorLoading', 'indicatorLoadingTitle');
  1295. addCss($thumb, 'file-uploading');
  1296. $btnUpload.attr('disabled', true);
  1297. $btnDelete.attr('disabled', true);
  1298. if (! allFiles) {
  1299. self.lock();
  1300. }
  1301. self.raise('filepreupload', [outData, previewId, i]);
  1302. params = $.extend(params, outData);
  1303. if (self.abort(params)) {
  1304. jqXHR.abort();
  1305. self.setProgress(100);
  1306. }
  1307. };
  1308. fnSuccess = function (data, textStatus, jqXHR) {
  1309. outData = self.getOutData(jqXHR, data);
  1310. params = $.extend(params, outData);
  1311. setTimeout(function () {
  1312. if (data.error === undefined) {
  1313. setIndicator('indicatorSuccess', 'indicatorSuccessTitle');
  1314. $btnUpload.hide();
  1315. $btnDelete.hide();
  1316. self.filestack[i] = undefined;
  1317. self.raise('fileuploaded', [outData, previewId, i]);
  1318. self.initUploadSuccess(data, $thumb, allFiles);
  1319. if (! allFiles) {
  1320. self.resetFileStack();
  1321. }
  1322. } else {
  1323. setIndicator('indicatorError', 'indicatorErrorTitle');
  1324. self.showUploadError(data.error, params);
  1325. }
  1326. }, 100);
  1327. };
  1328. fnComplete = function () {
  1329. setTimeout(function () {
  1330. updateProgress();
  1331. resetActions();
  1332. if (! allFiles) {
  1333. self.unlock(false);
  1334. } else {
  1335. chkComplete();
  1336. }
  1337. }, 100);
  1338. };
  1339. fnError = function (jqXHR, textStatus, errorThrown) {
  1340. var errMsg = self.parseError(jqXHR, errorThrown, (allFiles ? files[i].name : null));
  1341. setIndicator('indicatorError', 'indicatorErrorTitle');
  1342. params = $.extend(params, self.getOutData(jqXHR));
  1343. self.showUploadError(errMsg, params);
  1344. };
  1345. formdata.append(self.uploadFileAttr, files[i]);
  1346. formdata.append('file_id', i);
  1347. self.ajaxSubmit(fnBefore, fnSuccess, fnComplete, fnError);
  1348. },
  1349. uploadBatch : function () {
  1350. var self = this, files = self.filestack, total = files.length, config,
  1351. hasPostData = self.filestack.length > 0 || ! $.isEmptyObject(self.uploadExtraData),
  1352. setIndicator, setAllUploaded, enableActions, fnBefore, fnSuccess, fnComplete, fnError,
  1353. params = {};
  1354. self.formdata = new FormData();
  1355. if (total === 0 || ! hasPostData || self.abort(params)) {
  1356. return;
  1357. }
  1358. config = self.fileActionSettings;
  1359. setIndicator = function (i, icon, msg) {
  1360. var $indicator = $('#' + self.previewInitId + "-" + i).find('.file-upload-indicator');
  1361. $indicator.html(config[icon]);
  1362. $indicator.attr('title', config[msg]);
  1363. };
  1364. enableActions = function (i) {
  1365. var $thumb = $('#' + self.previewInitId + "-" + i + ':not(.file-preview-initial)'),
  1366. $btnUpload = $thumb.find('.kv-file-upload'),
  1367. $btnDelete = $thumb.find('.kv-file-delete');
  1368. $thumb.removeClass('file-uploading');
  1369. $btnUpload.removeAttr('disabled');
  1370. $btnDelete.removeAttr('disabled');
  1371. };
  1372. setAllUploaded = function () {
  1373. $.each(files, function (key) {
  1374. self.filestack[key] = undefined;
  1375. });
  1376. self.clearFileInput();
  1377. };
  1378. fnBefore = function (jqXHR) {
  1379. self.lock();
  1380. var outData = self.getOutData(jqXHR);
  1381. if (self.showPreview) {
  1382. self.getThumbs().each(function () {
  1383. var $thumb = $(this), $btnUpload = $thumb.find('.kv-file-upload'), $btnDelete = $thumb.find('.kv-file-remove');
  1384. addCss($thumb, 'file-uploading');
  1385. $btnUpload.attr('disabled', true);
  1386. $btnDelete.attr('disabled', true);
  1387. });
  1388. }
  1389. self.raise('filebatchpreupload', [outData]);
  1390. if (self.abort(outData)) {
  1391. jqXHR.abort();
  1392. }
  1393. };
  1394. fnSuccess = function (data, textStatus, jqXHR) {
  1395. var outData = self.getOutData(jqXHR, data), $thumbs = self.getThumbs(),
  1396. keys = isEmpty(data.errorkeys) ? [] : data.errorkeys;
  1397. if (data.error === undefined || isEmpty(data.error)) {
  1398. self.raise('filebatchuploadsuccess', [outData]);
  1399. setAllUploaded();
  1400. if (self.showPreview) {
  1401. $thumbs.find('.kv-file-upload').hide();
  1402. $thumbs.find('.kv-file-remove').hide();
  1403. $thumbs.each(function () {
  1404. var $thumb = $(this), key = $thumb.attr('data-fileindex');
  1405. setIndicator(key, 'indicatorSuccess', 'indicatorSuccessTitle');
  1406. enableActions(key);
  1407. });
  1408. self.initUploadSuccess(data);
  1409. } else {
  1410. self.reset();
  1411. }
  1412. } else {
  1413. if (self.showPreview) {
  1414. $thumbs.each(function () {
  1415. var $thumb = $(this), key = parseInt($thumb.attr('data-fileindex'), 10);
  1416. enableActions(key);
  1417. if (keys.length === 0) {
  1418. setIndicator(key, 'indicatorError', 'indicatorErrorTitle');
  1419. return;
  1420. }
  1421. if ($.inArray(key, keys) !== - 1) {
  1422. setIndicator(key, 'indicatorError', 'indicatorErrorTitle');
  1423. } else {
  1424. $thumb.find('.kv-file-upload').hide();
  1425. $thumb.find('.kv-file-remove').hide();
  1426. setIndicator(key, 'indicatorSuccess', 'indicatorSuccessTitle');
  1427. self.filestack[key] = undefined;
  1428. }
  1429. });
  1430. self.initUploadSuccess(data);
  1431. }
  1432. self.showUploadError(data.error, outData, 'filebatchuploaderror');
  1433. }
  1434. };
  1435. fnComplete = function () {
  1436. self.setProgress(100);
  1437. self.unlock();
  1438. self.raise('filebatchuploadcomplete', [self.filestack, self.getExtraData()]);
  1439. self.clearFileInput();
  1440. };
  1441. fnError = function (jqXHR, textStatus, errorThrown) {
  1442. var outData = self.getOutData(jqXHR), errMsg = self.parseError(jqXHR, errorThrown);
  1443. self.showUploadError(errMsg, outData, 'filebatchuploaderror');
  1444. self.uploadFileCount = total - 1;
  1445. if (! self.showPreview) {
  1446. return;
  1447. }
  1448. self.getThumbs().each(function () {
  1449. var $thumb = $(this), key = $thumb.attr('data-fileindex');
  1450. $thumb.removeClass('file-uploading');
  1451. if (self.filestack[key] !== undefined) {
  1452. setIndicator(key, 'indicatorError', 'indicatorErrorTitle');
  1453. }
  1454. });
  1455. self.getThumbs().removeClass('file-uploading');
  1456. self.getThumbs(' .kv-file-upload').removeAttr('disabled');
  1457. self.getThumbs(' .kv-file-delete').removeAttr('disabled');
  1458. };
  1459. $.each(files, function (key, data) {
  1460. if (! isEmpty(files[key])) {
  1461. self.formdata.append(self.uploadFileAttr, data);
  1462. }
  1463. });
  1464. self.ajaxSubmit(fnBefore, fnSuccess, fnComplete, fnError);
  1465. },
  1466. uploadExtraOnly : function () {
  1467. var self = this, params = {}, fnBefore, fnSuccess, fnComplete, fnError;
  1468. self.formdata = new FormData();
  1469. if (self.abort(params)) {
  1470. return;
  1471. }
  1472. fnBefore = function (jqXHR) {
  1473. self.lock();
  1474. var outData = self.getOutData(jqXHR);
  1475. self.raise('filebatchpreupload', [outData]);
  1476. self.setProgress(50);
  1477. params.data = outData;
  1478. params.xhr = jqXHR;
  1479. if (self.abort(params)) {
  1480. jqXHR.abort();
  1481. self.setProgress(100);
  1482. }
  1483. };
  1484. fnSuccess = function (data, textStatus, jqXHR) {
  1485. var outData = self.getOutData(jqXHR, data);
  1486. if (data.error === undefined || isEmpty(data.error)) {
  1487. self.raise('filebatchuploadsuccess', [outData]);
  1488. self.clearFileInput();
  1489. self.initUploadSuccess(data);
  1490. } else {
  1491. self.showUploadError(data.error, outData, 'filebatchuploaderror');
  1492. }
  1493. };
  1494. fnComplete = function () {
  1495. self.setProgress(100);
  1496. self.unlock();
  1497. self.raise('filebatchuploadcomplete', [self.filestack, self.getExtraData()]);
  1498. self.clearFileInput();
  1499. };
  1500. fnError = function (jqXHR, textStatus, errorThrown) {
  1501. var outData = self.getOutData(jqXHR), errMsg = self.parseError(jqXHR, errorThrown);
  1502. params.data = outData;
  1503. self.showUploadError(errMsg, outData, 'filebatchuploaderror');
  1504. };
  1505. self.ajaxSubmit(fnBefore, fnSuccess, fnComplete, fnError);
  1506. },
  1507. hideFileIcon : function () {
  1508. if (this.overwriteInitial) {
  1509. this.$captionContainer.find('.kv-caption-icon').hide();
  1510. }
  1511. },
  1512. showFileIcon : function () {
  1513. this.$captionContainer.find('.kv-caption-icon').show();
  1514. },
  1515. resetErrors : function (fade) {
  1516. var self = this, $error = self.$errorContainer;
  1517. self.isError = false;
  1518. self.$container.removeClass('has-error');
  1519. $error.html('');
  1520. if (fade) {
  1521. $error.fadeOut('slow');
  1522. } else {
  1523. $error.hide();
  1524. }
  1525. },
  1526. showFolderError : function (folders) {
  1527. var self = this, $error = self.$errorContainer;
  1528. if (! folders) {
  1529. return;
  1530. }
  1531. $error.html(self.msgFoldersNotAllowed.repl('{n}', folders));
  1532. $error.fadeIn(800);
  1533. addCss(self.$container, 'has-error');
  1534. self.raise('filefoldererror', [folders]);
  1535. },
  1536. showUploadError : function (msg, params, event) {
  1537. var self = this, $error = self.$errorContainer, ev = event || 'fileuploaderror';
  1538. if ($error.find('ul').length === 0) {
  1539. $error.html('<ul><li>' + msg + '</li></ul>');
  1540. } else {
  1541. $error.find('ul').append('<li>' + msg + '</li>');
  1542. }
  1543. $error.fadeIn(800);
  1544. self.raise(ev, [params]);
  1545. addCss(self.$container, 'has-error');
  1546. return true;
  1547. },
  1548. showError : function (msg, params, event) {
  1549. var self = this, $error = self.$errorContainer, ev = event || 'fileerror';
  1550. params = params || {};
  1551. params.reader = self.reader;
  1552. $error.html(msg);
  1553. $error.fadeIn(800);
  1554. self.raise(ev, [params]);
  1555. if (! self.isUploadable) {
  1556. self.clearFileInput();
  1557. }
  1558. addCss(self.$container, 'has-error');
  1559. self.$btnUpload.attr('disabled', true);
  1560. return true;
  1561. },
  1562. errorHandler : function (evt, caption) {
  1563. var self = this, err = evt.target.error;
  1564. switch (err.code) {
  1565. case err.NOT_FOUND_ERR:
  1566. self.showError(self.msgFileNotFound.repl('{name}', caption));
  1567. break;
  1568. case err.SECURITY_ERR:
  1569. self.showError(self.msgFileSecured.repl('{name}', caption));
  1570. break;
  1571. case err.NOT_READABLE_ERR:
  1572. self.showError(self.msgFileNotReadable.repl('{name}', caption));
  1573. break;
  1574. case err.ABORT_ERR:
  1575. self.showError(self.msgFilePreviewAborted.repl('{name}', caption));
  1576. break;
  1577. default:
  1578. self.showError(self.msgFilePreviewError.repl('{name}', caption));
  1579. }
  1580. },
  1581. parseFileType : function (file) {
  1582. var self = this, isValid, vType, cat, i;
  1583. for (i = 0; i < defaultPreviewTypes.length; i += 1) {
  1584. cat = defaultPreviewTypes[i];
  1585. isValid = isSet(cat, self.fileTypeSettings) ? self.fileTypeSettings[cat] : defaultFileTypeSettings[cat];
  1586. vType = isValid(file.type, file.name) ? cat : '';
  1587. if (! isEmpty(vType)) {
  1588. return vType;
  1589. }
  1590. }
  1591. return 'other';
  1592. },
  1593. previewDefault : function (file, previewId, isDisabled) {
  1594. if (! this.showPreview) {
  1595. return;
  1596. }
  1597. var self = this, data = objUrl.createObjectURL(file), $obj = $('#' + previewId),
  1598. config = self.previewSettings.other,
  1599. footer = self.renderFileFooter(file.name, config.width),
  1600. previewOtherTemplate = self.getPreviewTemplate('other'),
  1601. ind = previewId.slice(previewId.lastIndexOf('-') + 1),
  1602. frameClass = '';
  1603. if (isDisabled === true) {
  1604. frameClass = ' btn disabled';
  1605. footer += '<div class="file-other-error text-danger"><i class="glyphicon glyphicon-exclamation-sign"></i></div>';
  1606. }
  1607. self.$preview.append("\n" + previewOtherTemplate
  1608. .repl('{previewId}', previewId)
  1609. .repl('{frameClass}', frameClass)
  1610. .repl('{fileindex}', ind)
  1611. .repl('{caption}', self.slug(file.name))
  1612. .repl('{width}', config.width)
  1613. .repl('{height}', config.height)
  1614. .repl('{type}', file.type)
  1615. .repl('{data}', data)
  1616. .repl('{footer}', footer));
  1617. $obj.on('load', function () {
  1618. objUrl.revokeObjectURL($obj.attr('data'));
  1619. });
  1620. },
  1621. previewFile : function (file, theFile, previewId, data) {
  1622. if (! this.showPreview) {
  1623. return;
  1624. }
  1625. var self = this, cat = self.parseFileType(file), caption = self.slug(file.name), content, strText,
  1626. types = self.allowedPreviewTypes, mimes = self.allowedPreviewMimeTypes,
  1627. tmplt = self.getPreviewTemplate(cat),
  1628. config = isSet(cat, self.previewSettings) ? self.previewSettings[cat] : defaultPreviewSettings[cat],
  1629. wrapLen = parseInt(self.wrapTextLength, 10), wrapInd = self.wrapIndicator,
  1630. chkTypes = types.indexOf(cat) >= 0, id, height,
  1631. chkMimes = isEmpty(mimes) || (! isEmpty(mimes) && mimes.indexOf(file.type) !== - 1),
  1632. footer = self.renderFileFooter(caption, config.width), modal = '',
  1633. ind = previewId.slice(previewId.lastIndexOf('-') + 1);
  1634. if (chkTypes && chkMimes) {
  1635. if (cat === 'text') {
  1636. strText = htmlEncode(theFile.target.result);
  1637. objUrl.revokeObjectURL(data);
  1638. if (strText.length > wrapLen) {
  1639. id = 'text-' + uniqId();
  1640. height = window.innerHeight * 0.75;
  1641. modal = self.getLayoutTemplate('modal').repl('{id}', id)
  1642. .repl('{title}', caption)
  1643. .repl('{height}', height)
  1644. .repl('{body}', strText);
  1645. wrapInd = wrapInd
  1646. .repl('{title}', caption)
  1647. .repl('{dialog}', "$('#" + id + "').modal('show')");
  1648. strText = strText.substring(0, (wrapLen - 1)) + wrapInd;
  1649. }
  1650. content = tmplt.repl('{previewId}', previewId).repl('{caption}', caption)
  1651. .repl('{frameClass}', '')
  1652. .repl('{type}', file.type).repl('{width}', config.width)
  1653. .repl('{height}', config.height).repl('{data}', strText)
  1654. .repl('{footer}', footer).repl('{fileindex}', ind) + modal;
  1655. } else {
  1656. content = tmplt.repl('{previewId}', previewId).repl('{caption}', caption)
  1657. .repl('{frameClass}', '')
  1658. .repl('{type}', file.type).repl('{data}', data)
  1659. .repl('{width}', config.width).repl('{height}', config.height)
  1660. .repl('{footer}', footer).repl('{fileindex}', ind);
  1661. }
  1662. self.$preview.append("\n" + content);
  1663. self.autoSizeImage(previewId);
  1664. } else {
  1665. self.previewDefault(file, previewId);
  1666. }
  1667. },
  1668. slugDefault : function (text) {
  1669. return isEmpty(text) ? '' : text.split(/(\\|\/)/g).pop().replace(/[^\w\u00C0-\u017F\-.\\\/ ]+/g, '');
  1670. },
  1671. getFileStack : function () {
  1672. var self = this;
  1673. return self.filestack.filter(function (n) {
  1674. return n !== undefined;
  1675. });
  1676. },
  1677. readFiles : function (files) {
  1678. this.reader = new FileReader();
  1679. var self = this, $el = self.$element, $preview = self.$preview, reader = self.reader,
  1680. $container = self.$previewContainer, $status = self.$previewStatus, msgLoading = self.msgLoading,
  1681. msgProgress = self.msgProgress, previewInitId = self.previewInitId, numFiles = files.length,
  1682. settings = self.fileTypeSettings, ctr = self.filestack.length,
  1683. throwError = function (msg, file, previewId, index) {
  1684. var p1 = $.extend(self.getOutData({}, {}, files), {id: previewId, index: index}),
  1685. p2 = {id: previewId, index: index, file: file, files: files};
  1686. self.previewDefault(file, previewId, true);
  1687. return self.isUploadable ? self.showUploadError(msg, p1) : self.showError(msg, p2);
  1688. };
  1689. function readFile(i) {
  1690. if (isEmpty($el.attr('multiple'))) {
  1691. numFiles = 1;
  1692. }
  1693. if (i >= numFiles) {
  1694. if (self.isUploadable && self.filestack.length > 0) {
  1695. self.raise('filebatchselected', [self.getFileStack()]);
  1696. } else {
  1697. self.raise('filebatchselected', [files]);
  1698. }
  1699. $container.removeClass('loading');
  1700. $status.html('');
  1701. return;
  1702. }
  1703. var node = ctr + i, previewId = previewInitId + "-" + node, isText, file = files[i],
  1704. caption = self.slug(file.name), fileSize = (file.size || 0) / 1000, checkFile, fileExtExpr = '',
  1705. previewData = objUrl.createObjectURL(file), fileCount = 0, j, msg, typ, chk,
  1706. fileTypes = self.allowedFileTypes, strTypes = isEmpty(fileTypes) ? '' : fileTypes.join(', '),
  1707. fileExt = self.allowedFileExtensions, strExt = isEmpty(fileExt) ? '' : fileExt.join(', ');
  1708. if (! isEmpty(fileExt)) {
  1709. fileExtExpr = new RegExp('\\.(' + fileExt.join('|') + ')$', 'i');
  1710. }
  1711. fileSize = fileSize.toFixed(2);
  1712. if (self.maxFileSize > 0 && fileSize > self.maxFileSize) {
  1713. msg = self.msgSizeTooLarge.repl('{name}', caption)
  1714. .repl('{size}', fileSize)
  1715. .repl('{maxSize}', self.maxFileSize);
  1716. self.isError = throwError(msg, file, previewId, i);
  1717. return;
  1718. }
  1719. if (! isEmpty(fileTypes) && isArray(fileTypes)) {
  1720. for (j = 0; j < fileTypes.length; j += 1) {
  1721. typ = fileTypes[j];
  1722. checkFile = settings[typ];
  1723. chk = (checkFile !== undefined && checkFile(file.type, caption));
  1724. fileCount += isEmpty(chk) ? 0 : chk.length;
  1725. }
  1726. if (fileCount === 0) {
  1727. msg = self.msgInvalidFileType.repl('{name}', caption).repl('{types}', strTypes);
  1728. self.isError = throwError(msg, file, previewId, i);
  1729. return;
  1730. }
  1731. }
  1732. if (fileCount === 0 && ! isEmpty(fileExt) && isArray(fileExt) && ! isEmpty(fileExtExpr)) {
  1733. chk = caption.match(fileExtExpr);
  1734. fileCount += isEmpty(chk) ? 0 : chk.length;
  1735. if (fileCount === 0) {
  1736. msg = self.msgInvalidFileExtension.repl('{name}', caption).repl('{extensions}',
  1737. strExt);
  1738. self.isError = throwError(msg, file, previewId, i);
  1739. return;
  1740. }
  1741. }
  1742. if (! self.showPreview) {
  1743. self.filestack.push(file);
  1744. setTimeout(readFile(i + 1), 100);
  1745. self.raise('fileloaded', [file, previewId, i, reader]);
  1746. return;
  1747. }
  1748. if ($preview.length > 0 && FileReader !== undefined) {
  1749. $status.html(msgLoading.repl('{index}', i + 1).repl('{files}', numFiles));
  1750. $container.addClass('loading');
  1751. reader.onerror = function (evt) {
  1752. self.errorHandler(evt, caption);
  1753. };
  1754. reader.onload = function (theFile) {
  1755. self.previewFile(file, theFile, previewId, previewData);
  1756. self.initFileActions();
  1757. };
  1758. reader.onloadend = function () {
  1759. msg = msgProgress
  1760. .repl('{index}', i + 1).repl('{files}', numFiles)
  1761. .repl('{percent}', 50).repl('{name}', caption);
  1762. setTimeout(function () {
  1763. $status.html(msg);
  1764. objUrl.revokeObjectURL(previewData);
  1765. }, 100);
  1766. setTimeout(function () {
  1767. readFile(i + 1);
  1768. self.updateFileDetails(numFiles);
  1769. }, 100);
  1770. self.raise('fileloaded', [file, previewId, i, reader]);
  1771. };
  1772. reader.onprogress = function (data) {
  1773. if (data.lengthComputable) {
  1774. var fact = (data.loaded / data.total) * 100, progress = Math.ceil(fact);
  1775. msg = msgProgress.repl('{index}', i + 1).repl('{files}', numFiles)
  1776. .repl('{percent}', progress).repl('{name}', caption);
  1777. setTimeout(function () {
  1778. $status.html(msg);
  1779. }, 100);
  1780. }
  1781. };
  1782. isText = isSet('text', settings) ? settings.text : defaultFileTypeSettings.text;
  1783. if (isText(file.type, caption)) {
  1784. reader.readAsText(file, self.textEncoding);
  1785. } else {
  1786. reader.readAsArrayBuffer(file);
  1787. }
  1788. } else {
  1789. self.previewDefault(file, previewId);
  1790. setTimeout(function () {
  1791. readFile(i + 1);
  1792. self.updateFileDetails(numFiles);
  1793. }, 100);
  1794. self.raise('fileloaded', [file, previewId, i, reader]);
  1795. }
  1796. self.filestack.push(file);
  1797. }
  1798. readFile(0);
  1799. self.updateFileDetails(numFiles, false);
  1800. },
  1801. updateFileDetails : function (numFiles) {
  1802. var self = this, $el = self.$element, fileStack = self.getFileStack(),
  1803. name = $el.val() || (fileStack.length && fileStack[0].name) || '', label = self.slug(name),
  1804. n = self.isUploadable ? fileStack.length : numFiles,
  1805. nFiles = previewCache.count(self.id) + n,
  1806. log = n > 1 ? self.getMsgSelected(nFiles) : label;
  1807. if (self.isError) {
  1808. self.$previewContainer.removeClass('loading');
  1809. self.$previewStatus.html('');
  1810. self.$captionContainer.find('.kv-caption-icon').hide();
  1811. } else {
  1812. self.showFileIcon();
  1813. }
  1814. self.setCaption(log, self.isError);
  1815. self.$container.removeClass('file-input-new file-input-ajax-new');
  1816. if (arguments.length === 1) {
  1817. self.raise('fileselect', [numFiles, label]);
  1818. }
  1819. if (previewCache.count(self.id)) {
  1820. self.initPreviewDeletes();
  1821. }
  1822. },
  1823. change : function (e) {
  1824. var self = this, $el = self.$element;
  1825. if (! self.isUploadable && isEmpty($el.val()) && self.fileInputCleared) { // IE 11 fix
  1826. self.fileInputCleared = false;
  1827. return;
  1828. }
  1829. self.fileInputCleared = false;
  1830. var tfiles, msg, total, $preview = self.$preview, isDragDrop = arguments.length > 1,
  1831. files = isDragDrop ? e.originalEvent.dataTransfer.files : $el.get(0).files,
  1832. isSingleUpload = isEmpty($el.attr('multiple')), i = 0, f, m, folders = 0,
  1833. ctr = self.filestack.length, isAjaxUpload = self.isUploadable,
  1834. throwError = function (mesg, file, previewId, index) {
  1835. var p1 = $.extend(self.getOutData({}, {}, files), {id: previewId, index: index}),
  1836. p2 = {id: previewId, index: index, file: file, files: files};
  1837. return self.isUploadable ? self.showUploadError(mesg, p1) : self.showError(mesg, p2);
  1838. };
  1839. self.reader = null;
  1840. self.resetUpload();
  1841. self.hideFileIcon();
  1842. if (self.isUploadable) {
  1843. self.$container.find('.file-drop-zone .' + self.dropZoneTitleClass).remove();
  1844. }
  1845. if (isDragDrop) {
  1846. tfiles = [];
  1847. while (files[i]) {
  1848. f = files[i];
  1849. if (! f.type && f.size % 4096 === 0) {
  1850. folders ++;
  1851. } else {
  1852. tfiles.push(f);
  1853. }
  1854. i ++;
  1855. }
  1856. } else {
  1857. if (e.target.files === undefined) {
  1858. tfiles = e.target && e.target.value ? [
  1859. {name: e.target.value.replace(/^.+\\/, '')}
  1860. ] : [];
  1861. } else {
  1862. tfiles = e.target.files;
  1863. }
  1864. }
  1865. if (isEmpty(tfiles) || tfiles.length === 0) {
  1866. if (! isAjaxUpload) {
  1867. self.clear();
  1868. }
  1869. self.showFolderError(folders);
  1870. self.raise('fileselectnone');
  1871. return;
  1872. }
  1873. self.resetErrors();
  1874. if (! isAjaxUpload || (isSingleUpload && ctr > 0)) {
  1875. if (! self.overwriteInitial && previewCache.count(self.id)) {
  1876. var out = previewCache.out(self.id);
  1877. $preview.html(out.content);
  1878. self.setCaption(out.caption);
  1879. self.initPreviewDeletes();
  1880. } else {
  1881. $preview.html('');
  1882. }
  1883. if (isSingleUpload && ctr > 0) {
  1884. self.filestack = [];
  1885. }
  1886. }
  1887. total = self.isUploadable ? self.getFileStack().length + tfiles.length : tfiles.length;
  1888. if (self.maxFileCount > 0 && total > self.maxFileCount) {
  1889. msg = self.msgFilesTooMany.repl('{m}', self.maxFileCount).repl('{n}', total);
  1890. self.isError = throwError(msg, null, null, null);
  1891. self.$captionContainer.find('.kv-caption-icon').hide();
  1892. self.$caption.html(self.msgValidationError);
  1893. self.setEllipsis();
  1894. self.$container.removeClass('file-input-new file-input-ajax-new');
  1895. return;
  1896. }
  1897. if (! self.isIE9) {
  1898. self.readFiles(tfiles);
  1899. } else {
  1900. self.updateFileDetails(1);
  1901. }
  1902. self.showFolderError(folders);
  1903. },
  1904. autoSizeImage : function (previewId) {
  1905. var self = this, $preview = self.$preview,
  1906. $thumb = $preview.find("#" + previewId),
  1907. $img = $thumb.find('img'), w1, w2, $cap;
  1908. if (! $img.length) {
  1909. return;
  1910. }
  1911. $img.on('load', function () {
  1912. w1 = $thumb.width();
  1913. w2 = $preview.width();
  1914. if (w1 > w2) {
  1915. $img.css('width', '100%');
  1916. $thumb.css('width', '97%');
  1917. }
  1918. $cap = $img.closest('.file-preview-frame').find('.file-caption-name');
  1919. if ($cap.length) {
  1920. $cap.width($img.width());
  1921. $cap.attr('title', $cap.text());
  1922. }
  1923. self.raise('fileimageloaded', previewId);
  1924. });
  1925. },
  1926. initCaption : function () {
  1927. var self = this, cap = self.initialCaption || '';
  1928. if (self.overwriteInitial || isEmpty(cap)) {
  1929. self.$caption.html('');
  1930. return false;
  1931. }
  1932. self.setCaption(cap);
  1933. return true;
  1934. },
  1935. setCaption : function (content, isError) {
  1936. var self = this, err = isError || false, title, out;
  1937. if (err) {
  1938. title = $('<div>' + self.msgValidationError + '</div>').text();
  1939. out = '<span class="' + self.msgValidationErrorClass + '">' +
  1940. self.msgValidationErrorIcon + title + '</span>';
  1941. } else {
  1942. if (isEmpty(content) || self.$caption.length === 0) {
  1943. return;
  1944. }
  1945. title = $('<div>' + content + '</div>').text();
  1946. out = self.getLayoutTemplate('icon') + title;
  1947. }
  1948. self.$caption.html(out);
  1949. self.$caption.attr('title', title);
  1950. self.$captionContainer.find('.file-caption-ellipsis').attr('title', title);
  1951. self.setEllipsis();
  1952. },
  1953. initBrowse : function ($container) {
  1954. var self = this;
  1955. self.$btnFile = $container.find('.btn-file');
  1956. self.$btnFile.append(self.$element);
  1957. },
  1958. createContainer : function () {
  1959. var self = this,
  1960. $container = $(document.createElement("span"))
  1961. .attr({"class": 'file-input file-input-new'})
  1962. .html(self.renderMain());
  1963. self.$element.before($container);
  1964. self.initBrowse($container);
  1965. return $container;
  1966. },
  1967. refreshContainer : function () {
  1968. var self = this, $container = self.$container;
  1969. $container.before(self.$element);
  1970. $container.html(self.renderMain());
  1971. self.initBrowse($container);
  1972. },
  1973. renderMain : function () {
  1974. var self = this, dropCss = (self.isUploadable && self.dropZoneEnabled) ? ' file-drop-zone' : '',
  1975. preview = self.showPreview ? self.getLayoutTemplate('preview').repl('{class}', self.previewClass)
  1976. .repl('{dropClass}', dropCss) : '',
  1977. css = self.isDisabled ? self.captionClass + ' file-caption-disabled' : self.captionClass,
  1978. caption = self.captionTemplate.repl('{class}', css + ' kv-fileinput-caption');
  1979. return self.mainTemplate.repl('{class}', self.mainClass)
  1980. .repl('{preview}', preview)
  1981. .repl('{caption}', caption)
  1982. .repl('{upload}', self.renderUpload())
  1983. .repl('{remove}', self.renderRemove())
  1984. .repl('{cancel}', self.renderCancel())
  1985. .repl('{browse}', self.renderBrowse());
  1986. },
  1987. renderBrowse : function () {
  1988. var self = this, css = self.browseClass + ' btn-file', status = '';
  1989. if (self.isDisabled) {
  1990. status = ' disabled ';
  1991. }
  1992. return '<div class="' + css + '"' + status + '> ' + self.browseIcon + self.browseLabel + ' </div>';
  1993. },
  1994. renderRemove : function () {
  1995. var self = this, css = self.removeClass + ' fileinput-remove fileinput-remove-button', status = '';
  1996. if (! self.showRemove) {
  1997. return '';
  1998. }
  1999. if (self.isDisabled) {
  2000. status = ' disabled ';
  2001. }
  2002. return '<button type="button" title="' + self.removeTitle + '" class="' + css + '"' + status + '>' + self.removeIcon + self.removeLabel + '</button>';
  2003. },
  2004. renderCancel : function () {
  2005. var self = this, css = self.cancelClass + ' fileinput-cancel fileinput-cancel-button';
  2006. if (! self.showCancel) {
  2007. return '';
  2008. }
  2009. return '<button type="button" title="' + self.cancelTitle + '" class="hide ' + css + '">' + self.cancelIcon + self.cancelLabel + '</button>';
  2010. },
  2011. renderUpload : function () {
  2012. var self = this, css = self.uploadClass + ' kv-fileinput-upload fileinput-upload-button', content = '', status = '';
  2013. if (! self.showUpload) {
  2014. return '';
  2015. }
  2016. if (self.isDisabled) {
  2017. status = ' disabled ';
  2018. }
  2019. if (! self.isUploadable || self.isDisabled) {
  2020. content = '<button type="submit" title="' + self.uploadTitle + '"class="' + css + '"' + status + '>' + self.uploadIcon + self.uploadLabel + '</button>';
  2021. } else {
  2022. content = '<a href="' + self.uploadUrl + '" title="' + self.uploadTitle + '" class="' + css + '"' + status + '>' + self.uploadIcon + self.uploadLabel + '</a>';
  2023. }
  2024. return content;
  2025. }
  2026. };
  2027. //FileInput plugin definition
  2028. $.fn.fileinput = function (option) {
  2029. if (! hasFileAPISupport() && ! isIE(9)) {
  2030. return;
  2031. }
  2032. var args = Array.apply(null, arguments);
  2033. args.shift();
  2034. return this.each(function () {
  2035. var $this = $(this),
  2036. data = $this.data('fileinput'),
  2037. options = typeof option === 'object' && option;
  2038. if (! data) {
  2039. data = new FileInput(this, $.extend({}, $.fn.fileinput.defaults, options, $(this).data()));
  2040. $this.data('fileinput', data);
  2041. }
  2042. if (typeof option === 'string') {
  2043. data[option].apply(data, args);
  2044. }
  2045. });
  2046. };
  2047. $.fn.fileinput.defaults = {
  2048. showCaption : true,
  2049. showPreview : true,
  2050. showRemove : true,
  2051. showUpload : true,
  2052. showCancel : true,
  2053. mainClass : '',
  2054. previewClass : '',
  2055. captionClass : '',
  2056. mainTemplate : null,
  2057. initialCaption : '',
  2058. initialPreview : [],
  2059. initialPreviewDelimiter : '*$$*',
  2060. initialPreviewConfig : [],
  2061. initialPreviewThumbTags : [],
  2062. previewThumbTags : {},
  2063. initialPreviewShowDelete: true,
  2064. deleteUrl : '',
  2065. deleteExtraData : {},
  2066. overwriteInitial : true,
  2067. layoutTemplates : defaultLayoutTemplates,
  2068. previewTemplates : defaultPreviewTemplates,
  2069. allowedPreviewTypes : defaultPreviewTypes,
  2070. allowedPreviewMimeTypes : null,
  2071. allowedFileTypes : null,
  2072. allowedFileExtensions : null,
  2073. customLayoutTags : {},
  2074. customPreviewTags : {},
  2075. previewSettings : defaultPreviewSettings,
  2076. fileTypeSettings : defaultFileTypeSettings,
  2077. previewFileIcon : '<i class="glyphicon glyphicon-file"></i>',
  2078. browseIcon : '<i class="glyphicon glyphicon-folder-open"></i> &nbsp;',
  2079. browseClass : 'btn btn-primary',
  2080. removeIcon : '<i class="glyphicon glyphicon-trash"></i> ',
  2081. removeClass : 'btn btn-default',
  2082. cancelIcon : '<i class="glyphicon glyphicon-ban-circle"></i> ',
  2083. cancelClass : 'btn btn-default',
  2084. uploadIcon : '<i class="glyphicon glyphicon-upload"></i> ',
  2085. uploadClass : 'btn btn-default',
  2086. uploadUrl : null,
  2087. uploadAsync : true,
  2088. uploadExtraData : {},
  2089. maxFileSize : 0,
  2090. minFileCount : 0,
  2091. maxFileCount : 0,
  2092. msgValidationErrorClass : 'text-danger',
  2093. msgValidationErrorIcon : '<i class="glyphicon glyphicon-exclamation-sign"></i> ',
  2094. msgErrorClass : 'file-error-message',
  2095. progressClass : "progress-bar progress-bar-success progress-bar-striped active",
  2096. progressCompleteClass : "progress-bar progress-bar-success",
  2097. previewFileType : 'image',
  2098. wrapTextLength : 250,
  2099. wrapIndicator : ' <span class="wrap-indicator" title="{title}" onclick="{dialog}">[&hellip;]</span>',
  2100. elCaptionContainer : null,
  2101. elCaptionText : null,
  2102. elPreviewContainer : null,
  2103. elPreviewImage : null,
  2104. elPreviewStatus : null,
  2105. elErrorContainer : null,
  2106. slugCallback : null,
  2107. dropZoneEnabled : true,
  2108. dropZoneTitleClass : 'file-drop-zone-title',
  2109. fileActionSettings : {},
  2110. otherActionButtons : '',
  2111. textEncoding : 'UTF-8',
  2112. ajaxSettings : {},
  2113. ajaxDeleteSettings : {},
  2114. showAjaxErrorDetails : true
  2115. };
  2116. $.fn.fileinput.locales = {};
  2117. $.fn.fileinput.locales.en = {
  2118. fileSingle : 'file',
  2119. filePlural : 'files',
  2120. browseLabel : 'Browse &hellip;',
  2121. removeLabel : 'Remove',
  2122. removeTitle : 'Clear selected files',
  2123. cancelLabel : 'Cancel',
  2124. cancelTitle : 'Abort ongoing upload',
  2125. uploadLabel : 'Upload',
  2126. uploadTitle : 'Upload selected files',
  2127. msgSizeTooLarge : 'File "{name}" (<b>{size} KB</b>) exceeds maximum allowed upload size of <b>{maxSize} KB</b>. Please retry your upload!',
  2128. msgFilesTooLess : 'You must select at least <b>{n}</b> {files} to upload. Please retry your upload!',
  2129. msgFilesTooMany : 'Number of files selected for upload <b>({n})</b> exceeds maximum allowed limit of <b>{m}</b>. Please retry your upload!',
  2130. msgFileNotFound : 'File "{name}" not found!',
  2131. msgFileSecured : 'Security restrictions prevent reading the file "{name}".',
  2132. msgFileNotReadable : 'File "{name}" is not readable.',
  2133. msgFilePreviewAborted : 'File preview aborted for "{name}".',
  2134. msgFilePreviewError : 'An error occurred while reading the file "{name}".',
  2135. msgInvalidFileType : 'Invalid type for file "{name}". Only "{types}" files are supported.',
  2136. msgInvalidFileExtension: 'Invalid extension for file "{name}". Only "{extensions}" files are supported.',
  2137. msgValidationError : 'File Upload Error',
  2138. msgLoading : 'Loading file {index} of {files} &hellip;',
  2139. msgProgress : 'Loading file {index} of {files} - {name} - {percent}% completed.',
  2140. msgSelected : '{n} {files} selected',
  2141. msgFoldersNotAllowed : 'Drag & drop files only! {n} folder(s) dropped were skipped.',
  2142. dropZoneTitle : 'Drag & drop files here &hellip;'
  2143. };
  2144. $.extend($.fn.fileinput.defaults, $.fn.fileinput.locales.en);
  2145. $.fn.fileinput.Constructor = FileInput;
  2146. /**
  2147. * Convert automatically file inputs with class 'file'
  2148. * into a bootstrap fileinput control.
  2149. */
  2150. $(document).ready(function () {
  2151. var $input = $('input.file[type=file]'), count = $input.attr('type') ? $input.length : 0;
  2152. if (count > 0) {
  2153. $input.fileinput();
  2154. }
  2155. });
  2156. })(window.jQuery);