fileinput.js 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. /*!
  2. * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
  3. * @version 4.0.0
  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: 2014, 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. var isIE = function(ver) {
  20. var div = document.createElement("div"), status;
  21. div.innerHTML = "<!--[if IE " + ver + "]><i></i><![endif]-->";
  22. status = (div.getElementsByTagName("i").length == 1);
  23. document.body.appendChild(div);
  24. div.parentNode.removeChild(div);
  25. return status;
  26. },
  27. hasFileAPISupport = function () {
  28. return window.File && window.FileReader;
  29. },
  30. hasFileUploadSupport = function () {
  31. return hasFileAPISupport && window.FormData;
  32. },
  33. addCss = function($el, css) {
  34. $el.removeClass(css).addClass(css);
  35. },
  36. STYLE_SETTING = 'style="width:{width};height:{height};"',
  37. OBJECT_PARAMS = ' <param name="controller" value="true" />\n' +
  38. ' <param name="allowFullScreen" value="true" />\n' +
  39. ' <param name="allowScriptAccess" value="always" />\n' +
  40. ' <param name="autoPlay" value="false" />\n' +
  41. ' <param name="autoStart" value="false" />\n'+
  42. ' <param name="quality" value="high" />\n',
  43. DEFAULT_PREVIEW = '<div class="file-preview-other" ' + STYLE_SETTING + '>\n' +
  44. ' <h2><i class="glyphicon glyphicon-file"></i></h2>\n' +
  45. ' </div>';
  46. var defaultFileActionSettings = {
  47. removeIcon: '<i class="glyphicon glyphicon-trash text-danger"></i>',
  48. removeClass: 'btn btn-xs btn-default',
  49. removeTitle: 'Remove file',
  50. uploadIcon: '<i class="glyphicon glyphicon-upload text-info"></i>',
  51. uploadClass: 'btn btn-xs btn-default',
  52. uploadTitle: 'Upload file',
  53. indicatorNew: '<i class="glyphicon glyphicon-hand-down text-warning"></i>',
  54. indicatorSuccess: '<i class="glyphicon glyphicon-ok-sign file-icon-large text-success"></i>',
  55. indicatorError: '<i class="glyphicon glyphicon-exclamation-sign text-danger"></i>',
  56. indicatorLoading: '<i class="glyphicon glyphicon-hand-up text-muted"></i>',
  57. indicatorNewTitle: 'Not uploaded yet',
  58. indicatorSuccessTitle: 'Uploaded',
  59. indicatorErrorTitle: 'Upload Error',
  60. indicatorLoadingTitle: 'Uploading ...'
  61. };
  62. var defaultLayoutTemplates = {
  63. main1: '{preview}\n' +
  64. '<div class="kv-upload-progress hide"></div>\n' +
  65. '<div class="input-group {class}">\n' +
  66. ' {caption}\n' +
  67. ' <div class="input-group-btn">\n' +
  68. ' {remove}\n' +
  69. ' {cancel}\n' +
  70. ' {upload}\n' +
  71. ' {browse}\n' +
  72. ' </div>\n' +
  73. '</div>',
  74. main2: '{preview}\n<div class="kv-upload-progress hide"></div>\n{remove}\n{cancel}\n{upload}\n{browse}\n',
  75. preview: '<div class="file-preview {class}">\n' +
  76. ' <div class="close fileinput-remove">&times;</div>\n' +
  77. ' <div class="{dropClass}">\n' +
  78. ' <div class="file-preview-thumbnails">\n' +
  79. ' </div>\n' +
  80. ' <div class="clearfix"></div>' +
  81. ' <div class="file-preview-status text-center text-success"></div>\n' +
  82. ' <div class="kv-fileinput-error"></div>\n' +
  83. ' </div>\n' +
  84. '</div>',
  85. icon: '<span class="glyphicon glyphicon-file kv-caption-icon"></span>',
  86. caption: '<div tabindex="-1" class="form-control file-caption {class}">\n' +
  87. ' <div class="file-caption-name"></div>\n' +
  88. '</div>',
  89. modal: '<div id="{id}" class="modal fade">\n' +
  90. ' <div class="modal-dialog modal-lg">\n' +
  91. ' <div class="modal-content">\n' +
  92. ' <div class="modal-header">\n' +
  93. ' <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\n' +
  94. ' <h3 class="modal-title">Detailed Preview <small>{title}</small></h3>\n' +
  95. ' </div>\n' +
  96. ' <div class="modal-body">\n' +
  97. ' <textarea class="form-control" style="font-family:Monaco,Consolas,monospace; height: {height}px;" readonly>{body}</textarea>\n' +
  98. ' </div>\n' +
  99. ' </div>\n' +
  100. ' </div>\n' +
  101. '</div>',
  102. progress: '<div class="progress">\n' +
  103. ' <div class="progress-bar progress-bar-success progress-bar-striped text-center" role="progressbar" aria-valuenow="{percent}" aria-valuemin="0" aria-valuemax="100" style="width:{percent}%;">\n' +
  104. ' {percent}%\n' +
  105. ' </div>\n' +
  106. '</div>',
  107. footer: '<div class="file-thumbnail-footer">\n' +
  108. ' <div class="file-caption-name" style="width:{width}">{caption}</div>\n' +
  109. ' {actions}\n' +
  110. '</div>',
  111. actions: '<div class="file-actions">\n' +
  112. ' <div class="file-footer-buttons">\n' +
  113. ' {upload}{delete}' +
  114. ' </div>\n' +
  115. ' <div class="file-upload-indicator" tabindex="-1" title="{indicatorTitle}">{indicator}</div>\n' +
  116. ' <div class="clearfix"></div>\n' +
  117. '</div>',
  118. actionDelete: '<button type="button" class="kv-file-remove {removeClass}" title="{removeTitle}"{dataUrl}{dataKey}>{removeIcon}</button>\n',
  119. actionUpload: '<button type="button" class="kv-file-upload {uploadClass}" title="{uploadTitle}">{uploadIcon}</button>\n'
  120. };
  121. var defaultPreviewTypes = ['image', 'html', 'text', 'video', 'audio', 'flash', 'object'];
  122. var defaultPreviewTemplates = {
  123. generic: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}">\n' +
  124. ' {content}\n' +
  125. ' {footer}\n' +
  126. '</div>\n',
  127. html: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}">\n' +
  128. ' <object data="{data}" type="{type}" width="{width}" height="{height}">\n' +
  129. ' ' + DEFAULT_PREVIEW + '\n' +
  130. ' </object>\n' +
  131. ' {footer}\n' +
  132. '</div>',
  133. image: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}">\n' +
  134. ' <img src="{data}" class="file-preview-image" title="{caption}" alt="{caption}" ' + STYLE_SETTING + '>\n' +
  135. ' {footer}\n' +
  136. '</div>\n',
  137. text: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}">\n' +
  138. ' <div class="file-preview-text" title="{caption}" ' + STYLE_SETTING + '>\n' +
  139. ' {data}\n' +
  140. ' </div>\n' +
  141. ' {footer}\n' +
  142. '</div>\n',
  143. video: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}" title="{caption}" ' + STYLE_SETTING + '>\n' +
  144. ' <video width="{width}" height="{height}" controls>\n' +
  145. ' <source src="{data}" type="{type}">\n' +
  146. ' ' + DEFAULT_PREVIEW + '\n' +
  147. ' </video>\n' +
  148. ' {footer}\n' +
  149. '</div>\n',
  150. audio: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}" title="{caption}" ' + STYLE_SETTING + '>\n' +
  151. ' <audio controls>\n' +
  152. ' <source src="{data}" type="{type}">\n' +
  153. ' ' + DEFAULT_PREVIEW + '\n' +
  154. ' </audio>\n' +
  155. ' {footer}\n' +
  156. '</div>\n',
  157. flash: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}" title="{caption}" ' + STYLE_SETTING + '>\n' +
  158. ' <object type="application/x-shockwave-flash" width="{width}" height="{height}" data="{data}">\n' +
  159. OBJECT_PARAMS + ' ' + DEFAULT_PREVIEW + '\n' +
  160. ' </object>\n' +
  161. ' {footer}\n' +
  162. '</div>\n',
  163. object: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}" title="{caption}" ' + STYLE_SETTING + '>\n' +
  164. ' <object data="{data}" type="{type}" width="{width}" height="{height}">\n' +
  165. ' <param name="movie" value="{caption}" />\n' +
  166. OBJECT_PARAMS + ' ' + DEFAULT_PREVIEW + '\n' +
  167. ' </object>\n' +
  168. ' {footer}\n' +
  169. '</div>',
  170. other: '<div class="file-preview-frame{frameClass}" id="{previewId}" data-fileindex="{fileindex}" title="{caption}" ' + STYLE_SETTING + '>\n' +
  171. ' ' + DEFAULT_PREVIEW + '\n' +
  172. ' {footer}\n' +
  173. '</div>',
  174. };
  175. var defaultPreviewSettings = {
  176. image: {width: "auto", height: "160px"},
  177. html: {width: "213px", height: "160px"},
  178. text: {width: "160px", height: "160px"},
  179. video: {width: "213px", height: "160px"},
  180. audio: {width: "213px", height: "80px"},
  181. flash: {width: "213px", height: "160px"},
  182. object: {width: "213px", height: "160px"},
  183. other: {width: "160px", height: "160px"}
  184. };
  185. var defaultFileTypeSettings = {
  186. image: function(vType, vName) {
  187. return (typeof vType !== "undefined") ? vType.match('image.*') : vName.match(/\.(gif|png|jpe?g)$/i);
  188. },
  189. html: function(vType, vName) {
  190. return (typeof vType !== "undefined") ? vType == 'text/html' : vName.match(/\.(htm|html)$/i);
  191. },
  192. text: function(vType, vName) {
  193. return typeof vType !== "undefined" && vType.match('text.*') || vName.match(/\.(txt|md|csv|nfo|php|ini)$/i);
  194. },
  195. video: function (vType, vName) {
  196. return typeof vType !== "undefined" && vType.match(/\.video\/(ogg|mp4|webm)$/i) || vName.match(/\.(og?|mp4|webm)$/i);
  197. },
  198. audio: function (vType, vName) {
  199. return typeof vType !== "undefined" && vType.match(/\.audio\/(ogg|mp3|wav)$/i) || vName.match(/\.(ogg|mp3|wav)$/i);
  200. },
  201. flash: function (vType, vName) {
  202. return typeof vType !== "undefined" && vType == 'application/x-shockwave-flash' || vName.match(/\.(swf)$/i);
  203. },
  204. object: function (vType, vName) {
  205. return true;
  206. },
  207. other: function (vType, vName) {
  208. return true;
  209. },
  210. };
  211. var isEmpty = function (value, trim) {
  212. return value === null || value === undefined || value == []
  213. || value === '' || trim && $.trim(value) === '';
  214. },
  215. isArray = function (a) {
  216. return Array.isArray(a) || Object.prototype.toString.call(a) === '[object Array]';
  217. },
  218. isSet = function (needle, haystack) {
  219. return (typeof haystack == 'object' && needle in haystack);
  220. },
  221. getValue = function (options, param, value) {
  222. return (isEmpty(options) || isEmpty(options[param])) ? value : options[param];
  223. },
  224. getElement = function (options, param, value) {
  225. return (isEmpty(options) || isEmpty(options[param])) ? value : $(options[param]);
  226. },
  227. uniqId = function () {
  228. return Math.round(new Date().getTime() + (Math.random() * 100));
  229. },
  230. htmlEncode = function(str) {
  231. return String(str)
  232. .replace(/&/g, '&amp;')
  233. .replace(/"/g, '&quot;')
  234. .replace(/'/g, '&#39;')
  235. .replace(/</g, '&lt;')
  236. .replace(/>/g, '&gt;');
  237. },
  238. vUrl = window.URL || window.webkitURL;
  239. var FileInput = function (element, options) {
  240. this.$element = $(element);
  241. if (hasFileAPISupport() || isIE(9)) {
  242. this.init(options);
  243. this.listen();
  244. } else {
  245. this.$element.removeClass('file-loading');
  246. }
  247. };
  248. FileInput.prototype = {
  249. constructor: FileInput,
  250. init: function (options) {
  251. var self = this, $el = self.$element;
  252. for (key in options) {
  253. self[key] = options[key];
  254. }
  255. if (isEmpty(self.allowedPreviewTypes)) {
  256. self.allowedPreviewTypes = defaultPreviewTypes;
  257. }
  258. self.uploadFileAttr = !isEmpty($el.attr('name')) ? $el.attr('name') : 'file_data';
  259. self.reader = null;
  260. self.isIE9 = isIE(9);
  261. self.isIE10 = isIE(10);
  262. self.filestack = [];
  263. self.ajaxRequests = [];
  264. self.isError = false;
  265. self.isDisabled = self.$element.attr('disabled') || self.$element.attr('readonly');
  266. self.isUploadable = hasFileUploadSupport && !isEmpty(self.uploadUrl);
  267. self.slug = typeof options.slugCallback == "function" ? options.slugCallback : self.slugDefault;
  268. self.mainTemplate = self.showCaption ? self.getLayoutTemplate('main1') : self.getLayoutTemplate('main2');
  269. self.captionTemplate = self.getLayoutTemplate('caption');
  270. self.previewGenericTemplate = self.getPreviewTemplate('generic');
  271. if (isEmpty(self.$element.attr('id'))) {
  272. self.$element.attr('id', uniqId());
  273. }
  274. if (typeof self.$container == 'undefined') {
  275. self.$container = self.createContainer();
  276. } else {
  277. self.refreshContainer();
  278. }
  279. self.$progress = self.$container.find('.kv-upload-progress');
  280. self.$captionContainer = getElement(options, 'elCaptionContainer', self.$container.find('.file-caption'));
  281. self.$caption = getElement(options, 'elCaptionText', self.$container.find('.file-caption-name'));
  282. self.$previewContainer = getElement(options, 'elPreviewContainer', self.$container.find('.file-preview'));
  283. self.$preview = getElement(options, 'elPreviewImage', self.$container.find('.file-preview-thumbnails'));
  284. self.$previewStatus = getElement(options, 'elPreviewStatus', self.$container.find('.file-preview-status'));
  285. self.$errorContainer = getElement(options, 'elErrorContainer', self.$previewContainer.find('.kv-fileinput-error'));
  286. if (!isEmpty(self.msgErrorClass)) {
  287. addCss(self.$errorContainer, self.msgErrorClass);
  288. }
  289. self.$errorContainer.hide();
  290. self.initialPreviewContent = '';
  291. var content = self.initialPreview;
  292. self.initialPreviewCount = isArray(content) ? content.length : (content.length > 0 ? content.split(self.initialPreviewDelimiter).length : 0);
  293. self.fileActionSettings = $.extend(defaultFileActionSettings, options.fileActionSettings);
  294. self.previewInitId = "preview-" + uniqId();
  295. self.initPreview();
  296. self.initPreviewDeletes();
  297. self.original = {
  298. preview: self.$preview.html(),
  299. caption: self.$caption.html()
  300. };
  301. self.autoSizeCaption();
  302. self.options = options;
  303. self.setFileDropZoneTitle();
  304. self.uploadCount = 0;
  305. self.uploadPercent = 0;
  306. self.$element.removeClass('file-loading');
  307. },
  308. getLayoutTemplate: function(t) {
  309. var self = this;
  310. return isSet(t, self.layoutTemplates) ? self.layoutTemplates[t] : defaultLayoutTemplates[t];
  311. },
  312. getPreviewTemplate: function(t) {
  313. var self = this;
  314. return isSet(t, self.previewTemplates) ? self.previewTemplates[t] : defaultPreviewTemplates[t];
  315. },
  316. listen: function () {
  317. var self = this, $el = self.$element, $cap = self.$captionContainer, $btnFile = self.$btnFile;
  318. $el.on('change', $.proxy(self.change, self));
  319. $(window).on('resize', function() {
  320. setTimeout(function() {
  321. self.autoSizeCaption();
  322. }, 100);
  323. });
  324. $btnFile.on('click', function (ev) {
  325. self.$element.trigger('filebrowse');
  326. $cap.focus();
  327. });
  328. $el.closest('form').on('reset', $.proxy(self.reset, self));
  329. self.$container.on('click', '.fileinput-remove:not([disabled])', $.proxy(self.clear, self));
  330. self.$container.on('click', '.fileinput-cancel', $.proxy(self.cancel, self));
  331. if (self.isUploadable && self.dropZoneEnabled && self.showPreview) {
  332. self.initDragDrop();
  333. }
  334. if (!self.isUploadable) {
  335. return;
  336. }
  337. self.$container.find('.kv-fileinput-upload').on('click', function(e) {
  338. if (!self.isUploadable) {
  339. return;
  340. }
  341. e.preventDefault();
  342. var totLen = self.getFileStack().length;
  343. if (self.isDisabled || $(this).hasClass('disabled') || !isEmpty($(this).attr('disabled')) || totLen == 0) {
  344. return;
  345. }
  346. self.$progress.removeClass('hide');
  347. self.uploadCount = 0;
  348. self.uploadPercent = 0;
  349. var i, len = self.filestack.length, template = self.getLayoutTemplate('progress');
  350. setTimeout(function() {
  351. self.lock();
  352. self.setProgress(0);
  353. if ((self.uploadAsync || totLen == 1) && self.showPreview) {
  354. for (i = 0; i < len; i++) {
  355. if (self.filestack[i] !== undefined) {
  356. self.upload(i, true);
  357. }
  358. }
  359. return;
  360. }
  361. self.uploadBatch();
  362. }, 100);
  363. });
  364. },
  365. setProgress: function(percent) {
  366. var self = this, template = self.getLayoutTemplate('progress'), pct = Math.min(percent, 100);
  367. self.$progress.html(template.replace(/\{percent\}/g, pct));
  368. },
  369. lock: function() {
  370. var self = this;
  371. self.resetErrors();
  372. self.disable();
  373. if (self.showRemove) {
  374. addCss(self.$container.find('.fileinput-remove'), 'hide');
  375. }
  376. if (self.showCancel) {
  377. self.$container.find('.fileinput-cancel').removeClass('hide');
  378. }
  379. self.$element.trigger('filelock', [self.filestack]);
  380. },
  381. unlock: function() {
  382. var self = this;
  383. self.enable();
  384. if (self.showCancel) {
  385. addCss(self.$container.find('.fileinput-cancel'), 'hide');
  386. }
  387. if (self.showRemove) {
  388. self.$container.find('.fileinput-remove').removeClass('hide');
  389. }
  390. self.$element.trigger('fileunlock', [self.filestack]);
  391. },
  392. refresh: function (options) {
  393. var self = this, $el = self.$element,
  394. params = (arguments.length) ? $.extend(self.options, options) : self.options;
  395. $el.off();
  396. self.init(params);
  397. var $zone = self.$container.find('.file-drop-zone');
  398. $el.on('change', $.proxy(self.change, self));
  399. $zone.off('dragenter dragover drop');
  400. self.$(document).off('dragenter dragover drop');
  401. self.setFileDropZoneTitle();
  402. },
  403. initDragDrop: function() {
  404. var self = this, $zone = self.$container.find('.file-drop-zone');
  405. $zone.off('dragenter dragover drop');
  406. $(document).off('dragenter dragover drop');
  407. $zone.on('dragenter dragover', function (e) {
  408. e.stopPropagation();
  409. e.preventDefault();
  410. if (self.isDisabled) {
  411. return;
  412. }
  413. addCss($(this), 'highlighted');
  414. });
  415. $zone.on('dragleave', function (e) {
  416. e.stopPropagation();
  417. e.preventDefault();
  418. if (self.isDisabled) {
  419. return;
  420. }
  421. $(this).removeClass('highlighted');
  422. });
  423. $zone.on('drop', function (e) {
  424. e.preventDefault();
  425. if (self.isDisabled) {
  426. return;
  427. }
  428. self.change(e, 'dragdrop');
  429. $(this).removeClass('highlighted');
  430. });
  431. $(document).on('dragenter dragover drop', function (e) {
  432. e.stopPropagation();
  433. e.preventDefault();
  434. });
  435. },
  436. setFileDropZoneTitle: function() {
  437. var self = this, $zone = self.$container.find('.file-drop-zone');
  438. $zone.find('.' + self.dropZoneTitleClass).remove();
  439. if (!self.isUploadable || !self.showPreview ||
  440. $zone.length == 0 || self.getFileStack().length > 0 ||
  441. !self.dropZoneEnabled) {
  442. return;
  443. }
  444. if ($zone.find('.file-preview-frame').length == 0) {
  445. $zone.prepend('<div class="' + self.dropZoneTitleClass + '">' + self.dropZoneTitle + '</div>');
  446. }
  447. self.$container.removeClass('file-input-new');
  448. },
  449. initFileActions: function() {
  450. var self = this;
  451. self.$preview.find('.kv-file-remove').each(function() {
  452. var $el = $(this), $frame = $el.closest('.file-preview-frame'),
  453. ind = $frame.attr('data-fileindex');
  454. $el.off('click').on('click', function() {
  455. $frame.fadeOut('slow', function() {
  456. self.filestack[ind] = undefined;
  457. self.clearObjects($frame);
  458. $frame.remove();
  459. var filestack = self.getFileStack(), len = filestack.length,
  460. chk = self.$container.find('.file-preview-initial').length;
  461. if (len == 0 && chk == 0) {
  462. self.original.preview = '';
  463. self.reset();
  464. } else {
  465. var n = self.initialPreviewCount + len,
  466. cap = n > 1 ? self.msgSelected.replace(/\{n\}/g, n) : filestack[0].name;
  467. self.setCaption(cap);
  468. }
  469. });
  470. });
  471. });
  472. self.$preview.find('.kv-file-upload').each(function() {
  473. var $el = $(this);
  474. $el.off('click').on('click', function() {
  475. var $frame = $el.closest('.file-preview-frame'),
  476. ind = $frame.attr('data-fileindex');
  477. self.upload(ind);
  478. });
  479. });
  480. },
  481. renderInitFileFooter: function(i) {
  482. var self = this, hasConfig = self.initialPreviewConfig.length > 0,
  483. template = self.getLayoutTemplate('footer');
  484. if (hasConfig && !isEmpty(self.initialPreviewConfig[i])) {
  485. var config = self.initialPreviewConfig[i],
  486. caption = ('caption' in config) ? config.caption : '',
  487. width = ('width' in config) ? config.width : 'auto',
  488. url = ('url' in config) ? config.url : false,
  489. key = ('key' in config) ? config.key : null,
  490. disabled = url === false ? true : false,
  491. actions = self.initialPreviewShowDelete ? self.renderFileActions(false, true, disabled, url, key) : '',
  492. footer = template.replace(/\{actions\}/g, actions);
  493. return footer.replace(/\{caption\}/g, caption).replace(/\{width\}/g, width)
  494. .replace(/\{indicator\}/g, '').replace(/\{indicatorTitle\}/g, '');
  495. }
  496. return '';
  497. },
  498. renderFileFooter: function(caption, width, indicator) {
  499. var self = this, config = self.fileActionSettings,
  500. template = self.getLayoutTemplate('footer');
  501. if (self.isUploadable) {
  502. var footer = template.replace(/\{actions\}/g, self.renderFileActions(true, true, false, false, false));
  503. return footer.replace(/\{caption\}/g, caption).replace(/\{width\}/g, width)
  504. .replace(/\{indicator\}/g, config.indicatorNew).replace(/\{indicatorTitle\}/g, config.indicatorNewTitle);
  505. }
  506. return '';
  507. },
  508. renderFileActions: function(showUpload, showDelete, disabled, url, key) {
  509. if (!showUpload && !showDelete) {
  510. return '';
  511. }
  512. var self = this,
  513. vUrl = url == false ? '' : ' data-url="' + url + '"',
  514. vKey = key == false ? '' : ' data-key="' + key + '"',
  515. btnDelete = self.getLayoutTemplate('actionDelete'),
  516. btnUpload = '',
  517. template = self.getLayoutTemplate('actions'),
  518. config = self.fileActionSettings,
  519. removeClass = disabled ? config.removeClass + ' disabled' : config.removeClass;
  520. btnDelete = btnDelete
  521. .replace(/\{removeClass\}/g, removeClass)
  522. .replace(/\{removeIcon\}/g, config.removeIcon)
  523. .replace(/\{removeTitle\}/g, config.removeTitle)
  524. .replace(/\{dataUrl\}/g, vUrl)
  525. .replace(/\{dataKey\}/g, vKey);
  526. if (showUpload) {
  527. btnUpload = self.getLayoutTemplate('actionUpload')
  528. .replace(/\{uploadClass\}/g, config.uploadClass)
  529. .replace(/\{uploadIcon\}/g, config.uploadIcon)
  530. .replace(/\{uploadTitle\}/g, config.uploadTitle);
  531. }
  532. return template
  533. .replace(/\{delete\}/g, btnDelete)
  534. .replace(/\{upload\}/g, btnUpload);
  535. },
  536. getInitialPreview: function(template, content, i) {
  537. var self = this, ind = 'init_' + i,
  538. previewId = self.previewInitId + '-' + ind;
  539. footer = self.renderInitFileFooter(i, false);
  540. return template
  541. .replace(/\{previewId\}/g, previewId)
  542. .replace(/\{frameClass\}/g, ' file-preview-initial')
  543. .replace(/\{fileindex\}/g, ind)
  544. .replace(/\{content\}/g, content)
  545. .replace(/\{footer\}/g, footer);
  546. },
  547. initPreview: function () {
  548. var self = this, html = '', content = self.initialPreview, len = self.initialPreviewCount,
  549. cap = self.initialCaption.length, previewId = self.previewInitId + '-init_' + i;
  550. caption = (cap > 0) ? self.initialCaption : self.msgSelected.replace(/\{n\}/g, len);
  551. if (isArray(content) && len > 0) {
  552. for (var i = 0; i < len; i++) {
  553. html += self.getInitialPreview(self.previewGenericTemplate, content[i], i);
  554. }
  555. if (len > 1 && cap == 0) {
  556. caption = self.msgSelected.replace(/\{n\}/g, len);
  557. }
  558. } else {
  559. if (len > 0) {
  560. var fileList = content.split(self.initialPreviewDelimiter);
  561. for (var i = 0; i < len; i++) {
  562. html += self.getInitialPreview(self.previewGenericTemplate, content[i], i);
  563. }
  564. if (len > 1 && cap == 0) {
  565. caption = self.msgSelected.replace(/\{n\}/g, len);
  566. }
  567. } else {
  568. if (cap > 0) {
  569. self.setCaption(caption);
  570. return;
  571. } else {
  572. return;
  573. }
  574. }
  575. }
  576. self.initialPreviewContent = html;
  577. self.$preview.html(html);
  578. self.setCaption(caption);
  579. self.$container.removeClass('file-input-new');
  580. },
  581. initPreviewDeletes: function() {
  582. var self = this,
  583. resetProgress = function() {
  584. if (self.$preview.find('.kv-file-remove').length == 0) {
  585. self.reset();
  586. }
  587. };
  588. self.$preview.find('.kv-file-remove').each(function() {
  589. var $el = $(this), $frame = $el.closest('.file-preview-frame'),
  590. vUrl = $el.attr('data-url'), vKey = $el.attr('data-key'),
  591. $content = $(self.initialPreviewContent);
  592. if (vUrl === undefined || vKey === undefined) {
  593. return;
  594. }
  595. $el.off('click').on('click', function() {
  596. $.ajax({
  597. url: vUrl,
  598. type: 'POST',
  599. dataType: 'json',
  600. data: {key: vKey},
  601. beforeSend: function() {
  602. addCss($frame, 'file-uploading');
  603. addCss($el, 'disabled');
  604. self.$element.trigger('filepredelete', [vKey]);
  605. },
  606. success: function(data, textStatus, jqXHR) {
  607. setTimeout(function() {
  608. if(typeof data.error === 'undefined') {
  609. self.$element.trigger('filedeleted', [vKey]);
  610. } else {
  611. self.showError(data.error, null, $el.attr('id'), key, 'filedeleteerror');
  612. resetProgress();
  613. }
  614. $frame.removeClass('file-uploading').addClass('file-deleted');
  615. $frame.fadeOut('slow', function() {
  616. self.clearObjects($frame);
  617. $frame.remove();
  618. var $content = $(document.createElement('div')).html(self.original.preview);
  619. $content.find('.file-preview-frame').each(function() {
  620. var $el = $(this);
  621. if ($el.find('.kv-file-remove').attr('data-key') == vKey) {
  622. $el.remove();
  623. }
  624. });
  625. self.initialPreviewContent = $content.html();
  626. if (self.initialPreviewCount > 0) {
  627. self.initialPreviewCount--;
  628. }
  629. var caption = (self.initialCaption.length > 0) ?
  630. self.initialCaption :
  631. self.msgSelected.replace(/\{n\}/g, self.initialPreviewCount);
  632. self.original.preview = $content.html();
  633. self.setCaption(caption);
  634. self.original.caption = self.$caption.html();
  635. $content.remove();
  636. resetProgress();
  637. });
  638. }, 500);
  639. },
  640. error: function(jqXHR, textStatus, errorThrown) {
  641. self.showError(errorThrown, null, $el.attr('id'), key, 'filedeleteerror');
  642. $frame.removeClass('file-uploading');
  643. resetProgress();
  644. }
  645. });
  646. });
  647. });
  648. },
  649. clearObjects: function($el) {
  650. $el.find('video audio').each(function() {
  651. this.pause();
  652. delete(this);
  653. $(this).remove();
  654. });
  655. $el.find('img object div').each(function() {
  656. delete(this);
  657. $(this).remove();
  658. });
  659. },
  660. clearFileInput: function() {
  661. var self = this, $el = self.$element;
  662. if (isEmpty($el.val())) {
  663. return;
  664. }
  665. // Fix for IE ver < 11, that does not clear file inputs
  666. // Requires a sequence of steps to prevent IE crashing but
  667. // still allow clearing of the file input.
  668. if (self.isIE9 || self.isIE10) {
  669. var $srcFrm = $el.closest('form'),
  670. $tmpFrm = $(document.createElement('form')),
  671. $tmpEl = $(document.createElement('div'));
  672. $el.before($tmpEl);
  673. if ($srcFrm.length) {
  674. $srcFrm.after($tmpFrm);
  675. } else {
  676. $tmpEl.after($tmpFrm);
  677. }
  678. $tmpFrm.append($el).trigger('reset');
  679. $tmpEl.before($el).remove();
  680. $tmpFrm.remove();
  681. } else { // normal input clear behavior for other sane browsers
  682. $el.val('');
  683. }
  684. },
  685. resetUpload: function() {
  686. var self = this;
  687. self.uploadCount = 0;
  688. self.uploadPercent = 0;
  689. self.setProgress(0);
  690. addCss(self.$progress, 'hide');
  691. self.ajaxRequests = [];
  692. },
  693. cancel: function() {
  694. var self = this, xhr = self.ajaxRequests, len = xhr.length;
  695. if (len > 0) {
  696. for (i = 0; i < len; i++) {
  697. xhr[i].abort();
  698. }
  699. self.$preview.find('file-preview-frame').each(function() {
  700. $thumb = $(this), ind = $thumb.attr('data-fileindex');
  701. $thumb.removeClass('file-uploading');
  702. if (self.filestack[ind] !== undefined) {
  703. $thumb.find('.kv-file-upload').removeClass('disabled');
  704. $thumb.find('.kv-file-upload').removeClass('disabled');
  705. }
  706. });
  707. self.unlock();
  708. }
  709. },
  710. clear: function () {
  711. var self = this, e = arguments.length && arguments[0];
  712. if (!self.isIE9 && self.reader instanceof FileReader) {
  713. self.reader.abort();
  714. }
  715. self.resetUpload();
  716. self.filestack = [];
  717. self.autoSizeCaption();
  718. self.clearFileInput();
  719. self.resetErrors(true);
  720. if (e !== false) {
  721. self.$element.trigger('change');
  722. self.$element.trigger('fileclear');
  723. }
  724. if (self.overwriteInitial) {
  725. self.initialPreviewCount = 0;
  726. self.initialPreviewContent = '';
  727. }
  728. if (!self.overwriteInitial && self.initialPreviewContent.length > 0) {
  729. self.showFileIcon();
  730. self.$preview.html(self.original.preview);
  731. self.$caption.html(self.original.caption);
  732. self.initPreviewDeletes();
  733. self.$container.removeClass('file-input-new');
  734. } else {
  735. self.$preview.find('.file-preview-frame').each(function() {
  736. self.clearObjects($(this));
  737. });
  738. self.$preview.html('');
  739. var cap = (!self.overwriteInitial && self.initialCaption.length > 0) ?
  740. self.original.caption : '';
  741. self.$caption.html(cap);
  742. self.$caption.attr('title', '');
  743. addCss(self.$container, 'file-input-new');
  744. }
  745. if (self.$container.find('.file-preview-frame').length == 0) {
  746. self.initialCaption = '';
  747. self.original.caption = '';
  748. self.$caption.html('');
  749. self.$captionContainer.find('.kv-caption-icon').hide();
  750. }
  751. self.hideFileIcon();
  752. self.$element.trigger('filecleared');
  753. self.$captionContainer.focus();
  754. self.setFileDropZoneTitle();
  755. },
  756. reset: function () {
  757. var self = this;
  758. self.clear(false);
  759. self.$preview.html(self.original.preview);
  760. self.$caption.html(self.original.caption);
  761. self.$container.find('.fileinput-filename').text('');
  762. self.$element.trigger('filereset');
  763. if (self.initialPreview.length > 0) {
  764. self.$container.removeClass('file-input-new');
  765. }
  766. self.setFileDropZoneTitle();
  767. self.resetUpload();
  768. self.filestack = [];
  769. },
  770. disable: function (e) {
  771. var self = this;
  772. self.isDisabled = true;
  773. self.$element.attr('disabled', 'disabled');
  774. self.$container.find(".kv-fileinput-caption").addClass("file-caption-disabled");
  775. self.$container.find(".btn-file, .fileinput-remove, .kv-fileinput-upload").attr("disabled", true);
  776. self.initDragDrop();
  777. },
  778. enable: function (e) {
  779. var self = this;
  780. self.isDisabled = false;
  781. self.$element.removeAttr('disabled');
  782. self.$container.find(".kv-fileinput-caption").removeClass("file-caption-disabled");
  783. self.$container.find(".btn-file, .fileinput-remove, .kv-fileinput-upload").removeAttr("disabled");
  784. self.initDragDrop();
  785. },
  786. uploadExtra: function(fd) {
  787. var self = this;
  788. if (self.uploadExtraData.length == 0) {
  789. return;
  790. }
  791. $.each(self.uploadExtraData, function(j, data) {
  792. if (!isEmpty(data.id) && !isEmpty(data.value)) {
  793. fd.append(data.id, data.value);
  794. }
  795. });
  796. },
  797. initXhr: function(xhrobj) {
  798. var self = this;
  799. if (xhrobj.upload) {
  800. xhrobj.upload.addEventListener('progress', function(event) {
  801. var pct = 0, position = event.loaded || event.position, total = event.total;
  802. if (event.lengthComputable) {
  803. pct = Math.ceil(position / total * 98);
  804. }
  805. self.uploadPercent = Math.max(pct, self.uploadPercent);
  806. //Set progress
  807. self.setProgress(self.uploadPercent);
  808. }, false);
  809. }
  810. return xhrobj;
  811. },
  812. upload: function(i) {
  813. var self = this, files = self.getFileStack(), formdata = new FormData(),
  814. previewId = self.previewInitId + "-" + i, $thumb = $('#' + previewId),
  815. $btnUpload = $thumb.find('.kv-file-upload'), $btnDelete = $thumb.find('.kv-file-remove'),
  816. $indicator = $thumb.find('.file-upload-indicator'), config = self.fileActionSettings;
  817. if ($btnUpload.hasClass('disabled')) {
  818. return;
  819. }
  820. var percent,
  821. total = files.length,
  822. allFiles = arguments.length > 1,
  823. setIndicator = function (icon, msg) {
  824. $indicator.html(config[icon]);
  825. $indicator.attr('title', config[msg]);
  826. },
  827. updateProgress = function() {
  828. if (self.$preview.find('file-uploading').length == 0) {
  829. self.unlock();
  830. }
  831. if (!allFiles) {
  832. return;
  833. }
  834. self.uploadCount++;
  835. var pct = total > 0 ? Math.ceil(self.uploadCount * 100/total) : 0;
  836. self.uploadPercent = Math.max(pct, self.uploadPercent);
  837. self.setProgress(self.uploadPercent);
  838. self.initPreviewDeletes();
  839. },
  840. resetActions = function() {
  841. $btnUpload.removeClass('disabled');
  842. $btnDelete.removeClass('disabled');
  843. $thumb.removeClass('file-uploading');
  844. };
  845. formdata.append(self.uploadFileAttr, files[i]);
  846. formdata.append('file_id', i);
  847. self.uploadExtra(formdata);
  848. self.ajaxRequests.push($.ajax({
  849. xhr: function() {
  850. var xhrobj = $.ajaxSettings.xhr();
  851. return self.initXhr(xhrobj);
  852. },
  853. url: self.uploadUrl,
  854. type: 'POST',
  855. dataType: 'json',
  856. data: formdata,
  857. cache: false,
  858. processData: false,
  859. contentType: false,
  860. beforeSend: function() {
  861. setIndicator('indicatorLoading', 'indicatorLoadingTitle');
  862. addCss($thumb, 'file-uploading');
  863. addCss($btnUpload, 'disabled');
  864. addCss($btnDelete, 'disabled');
  865. if (!allFiles) {
  866. self.lock();
  867. }
  868. self.$element.trigger('filepreupload', [formdata, previewId, i])
  869. },
  870. success: function(data, textStatus, jqXHR) {
  871. setTimeout(function() {
  872. if(typeof data.error === 'undefined') {
  873. setIndicator('indicatorSuccess', 'indicatorSuccessTitle');
  874. $btnUpload.hide();
  875. $btnDelete.hide();
  876. self.filestack[i] = undefined;
  877. self.$element.trigger('fileuploaded', [formdata, previewId, i]);
  878. } else {
  879. setIndicator('indicatorError', 'indicatorErrorTitle');
  880. self.showError(data.error, formdata, previewId, i, 'fileuploaderror');
  881. }
  882. updateProgress();
  883. resetActions();
  884. }, 500);
  885. },
  886. error: function(jqXHR, textStatus, errorThrown) {
  887. setIndicator('indicatorError', 'indicatorErrorTitle');
  888. if (allFiles) {
  889. var cap = files[i].name;
  890. self.showUploadError('<b>' + cap + '</b>: ' + errorThrown, formdata, previewId, i, 'fileuploaderror');
  891. } else {
  892. self.showError(errorThrown, formdata, previewId, i, 'fileuploaderror');
  893. }
  894. updateProgress();
  895. resetActions();
  896. }
  897. }));
  898. },
  899. uploadBatch: function() {
  900. var self = this, files = self.filestack, total = files.length,
  901. config = self.fileActionSettings; formdata = new FormData(),
  902. setIndicator = function (i, icon, msg) {
  903. $indicator = $('#' + self.previewInitId + "-" + i).find('.file-upload-indicator'),
  904. $indicator.html(config[icon]);
  905. $indicator.attr('title', config[msg]);
  906. },
  907. setAllUploaded = function() {
  908. $.each(files, function(key, data) {
  909. self.filestack[key] = undefined;
  910. });
  911. };
  912. $.each(files, function(key, data) {
  913. if (files[key] !== undefined) {
  914. formdata.append(self.uploadFileAttr, files[key]);
  915. }
  916. });
  917. self.uploadExtra(formdata);
  918. $.ajax({
  919. xhr: function() {
  920. var xhrobj = $.ajaxSettings.xhr();
  921. return self.initXhr(xhrobj);
  922. },
  923. url: self.uploadUrl,
  924. type: 'POST',
  925. dataType: 'json',
  926. data: formdata,
  927. cache: false,
  928. processData: false,
  929. contentType: false,
  930. beforeSend: function() {
  931. addCss(self.$preview.find('.file-preview-frame'), 'file-uploading');
  932. self.lock();
  933. },
  934. success: function(data, textStatus, jqXHR) {
  935. setTimeout(function() {
  936. var keys = isEmpty(data.errorkeys) ? [] : data.errorkeys;
  937. if(typeof data.error === 'undefined' || isEmpty(data.error)) {
  938. setAllUploaded();
  939. if (self.showPreview) {
  940. self.$preview.find('.kv-file-upload').hide();
  941. self.$preview.find('.kv-file-remove').hide();
  942. self.$preview.find('.file-preview-frame').each(function() {
  943. var $thumb = $(this), key = $thumb.attr('data-fileindex');
  944. setIndicator(key, 'indicatorSuccess', 'indicatorSuccessTitle');
  945. $thumb.removeClass('file-uploading');
  946. });
  947. } else {
  948. self.reset();
  949. }
  950. } else {
  951. self.$preview.find('.file-preview-frame').each(function() {
  952. var $thumb = $(this), key = $thumb.attr('data-fileindex');
  953. if (keys.length == 0) {
  954. $thumb.removeClass('file-uploading');
  955. setIndicator(key, 'indicatorError', 'indicatorErrorTitle');
  956. return;
  957. }
  958. if ($.inArray(key, keys)) {
  959. setIndicator(key, 'indicatorError', 'indicatorErrorTitle');
  960. } else {
  961. var $upload = $thumb.find('.kv-file-upload'), $remove = $thumb.find('.kv-file-remove');
  962. addCss($upload, 'disabled');
  963. addCss($remove, 'disabled');
  964. $upload.hide();
  965. $remove.hide();
  966. $thumb.removeClass('file-uploading');
  967. setIndicator(key, 'indicatorSuccess', 'indicatorSuccessTitle');
  968. self.filestack[key] = undefined;
  969. }
  970. });
  971. self.showUploadError(data.error, formdata, null, null, 'filebatchuploaderror');
  972. }
  973. }, 100);
  974. },
  975. complete: function () {
  976. self.setProgress(100);
  977. self.unlock();
  978. },
  979. error: function(jqXHR, textStatus, errorThrown) {
  980. self.showError(errorThrown, formdata, null, null, 'filebatchuploaderror');
  981. self.uploadFileCount = total - 1;
  982. self.$preview.find('.file-preview-frame').removeClass('file-uploading');
  983. }
  984. });
  985. },
  986. hideFileIcon: function () {
  987. if (this.overwriteInitial) {
  988. this.$captionContainer.find('.kv-caption-icon').hide();
  989. }
  990. },
  991. showFileIcon: function () {
  992. this.$captionContainer.find('.kv-caption-icon').show();
  993. },
  994. resetErrors: function (fade) {
  995. var self = this, $error = self.$errorContainer;
  996. self.isError = false;
  997. self.$container.removeClass('has-error');
  998. if (fade) {
  999. $error.fadeOut('slow');
  1000. } else {
  1001. $error.hide();
  1002. }
  1003. },
  1004. showUploadError: function (msg, file, previewId, index) {
  1005. var self = this, $error = self.$errorContainer, $el = self.$element,
  1006. ev = arguments.length > 4 ? arguments[4] : 'fileerror';
  1007. if ($error.find('ul').length == 0) {
  1008. $error.html('<ul class="text-left"><li>' + msg + '</li></ul>');
  1009. } else {
  1010. $error.find('ul').append('<li>' + msg + '</li>');
  1011. }
  1012. $error.fadeIn(800);
  1013. $el.trigger(ev, [file, previewId, index]);
  1014. addCss(self.$container, 'has-error');
  1015. return true;
  1016. },
  1017. showError: function (msg, file, previewId, index) {
  1018. var self = this, $error = self.$errorContainer, $el = self.$element,
  1019. ev = arguments.length > 4 ? arguments[4] : 'fileerror';
  1020. $error.html(msg);
  1021. $error.fadeIn(800);
  1022. $el.trigger(ev, [file, previewId, index]);
  1023. if (!self.isUploadable) {
  1024. self.clearFileInput();
  1025. }
  1026. addCss(self.$container, 'has-error');
  1027. return true;
  1028. },
  1029. errorHandler: function (evt, caption) {
  1030. var self = this;
  1031. switch (evt.target.error.code) {
  1032. case evt.target.error.NOT_FOUND_ERR:
  1033. self.addError(self.msgFileNotFound.replace(/\{name\}/g, caption));
  1034. break;
  1035. case evt.target.error.NOT_READABLE_ERR:
  1036. self.addError(self.msgFileNotReadable.replace(/\{name\}/g, caption));
  1037. break;
  1038. case evt.target.error.ABORT_ERR:
  1039. self.addError(self.msgFilePreviewAborted.replace(/\{name\}/g, caption));
  1040. break;
  1041. default:
  1042. self.addError(self.msgFilePreviewError.replace(/\{name\}/g, caption));
  1043. }
  1044. },
  1045. parseFileType: function(file) {
  1046. var isValid, vType;
  1047. for (var i = 0; i < defaultPreviewTypes.length; i++) {
  1048. cat = defaultPreviewTypes[i];
  1049. isValid = isSet(cat, self.fileTypeSettings) ? self.fileTypeSettings[cat] : defaultFileTypeSettings[cat];
  1050. vType = isValid(file.type, file.name) ? cat : '';
  1051. if (vType != '') {
  1052. return vType;
  1053. }
  1054. }
  1055. return 'other';
  1056. },
  1057. previewDefault: function(file, previewId) {
  1058. var self = this, data = vUrl.createObjectURL(file), $obj = $('#' + previewId),
  1059. footer = self.renderFileFooter(file.name, self.previewSettings.other.width),
  1060. previewOtherTemplate = self.getPreviewTemplate('other'),
  1061. ind = previewId.slice(previewId.lastIndexOf('-') + 1);
  1062. self.$preview.append("\n" + previewOtherTemplate
  1063. .replace(/\{previewId\}/g, previewId)
  1064. .replace(/\{frameClass\}/g, '')
  1065. .replace(/\{fileindex\}/g, ind)
  1066. .replace(/\{caption\}/g, self.slug(file.name))
  1067. .replace(/\{type\}/g, file.type)
  1068. .replace(/\{data\}/g, data)
  1069. .replace(/\{footer\}/g, footer));
  1070. $obj.on('load', function(e) {
  1071. vUrl.revokeObjectURL($obj.attr('data'));
  1072. });
  1073. },
  1074. previewFile: function(file, theFile, previewId, data) {
  1075. var self = this, cat = self.parseFileType(file), caption = self.slug(file.name), data, obj, content,
  1076. types = self.allowedPreviewTypes, mimes = self.allowedPreviewMimeTypes, fType = file.type,
  1077. template = isSet(cat, self.previewTemplates) ? self.previewTemplates[cat] : defaultPreviewTemplates[cat],
  1078. config = isSet(cat, self.previewSettings) ? self.previewSettings[cat] : defaultPreviewSettings[cat],
  1079. wrapLen = parseInt(self.wrapTextLength), wrapInd = self.wrapIndicator, $preview = self.$preview,
  1080. chkTypes = types.indexOf(cat) >=0, chkMimes = isEmpty(mimes) || (!isEmpty(mimes) && isSet(file.type, mimes)),
  1081. footer = self.renderFileFooter(caption, config.width),
  1082. ind = previewId.slice(previewId.lastIndexOf('-') + 1);
  1083. if (chkTypes && chkMimes) {
  1084. if (cat == 'text') {
  1085. var strText = htmlEncode(theFile.target.result);
  1086. vUrl.revokeObjectURL(data);
  1087. if (strText.length > wrapLen) {
  1088. var id = 'text-' + uniqId(), height = window.innerHeight * .75,
  1089. modal = self.getLayoutTemplate('modal')
  1090. .replace(/\{id\}/g, id)
  1091. .replace(/\{title\}/g, caption)
  1092. .replace(/\{height\}/g, height)
  1093. .replace(/\{body\}/g, strText);
  1094. wrapInd = wrapInd
  1095. .replace(/\{title\}/g, caption)
  1096. .replace(/\{dialog\}/g, "$('#" + id + "').modal('show')");
  1097. strText = strText.substring(0, (wrapLen - 1)) + wrapInd;
  1098. }
  1099. content = template
  1100. .replace(/\{previewId\}/g, previewId).replace(/\{caption\}/g, caption)
  1101. .replace(/\{frameClass\}/g, '')
  1102. .replace(/\{type\}/g, file.type).replace(/\{width\}/g, config.width)
  1103. .replace(/\{height\}/g, config.height).replace(/\{data\}/g, strText)
  1104. .replace(/\{footer\}/g, footer).replace(/\{fileindex\}/g, ind) + modal;
  1105. } else {
  1106. content = template
  1107. .replace(/\{previewId\}/g, previewId).replace(/\{caption\}/g, caption)
  1108. .replace(/\{frameClass\}/g, '')
  1109. .replace(/\{type\}/g, file.type).replace(/\{data\}/g, data)
  1110. .replace(/\{width\}/g, config.width).replace(/\{height\}/g, config.height)
  1111. .replace(/\{footer\}/g, footer).replace(/\{fileindex\}/g, ind);
  1112. }
  1113. $preview.append("\n" + content);
  1114. self.autoSizeImage(previewId);
  1115. } else {
  1116. self.previewDefault(file, previewId);
  1117. }
  1118. },
  1119. slugDefault: function (text) {
  1120. return isEmpty(text) ? '' : text.split(/(\\|\/)/g).pop().replace(/[^\w-.\\\/ ]+/g,'');
  1121. },
  1122. getFileStack: function() {
  1123. var size = 0, self = this;
  1124. return self.filestack.filter(function(n){ return n != undefined });
  1125. },
  1126. readFiles: function (files) {
  1127. this.reader = new FileReader();
  1128. var self = this, $el = self.$element, $preview = self.$preview, reader = self.reader,
  1129. $container = self.$previewContainer, $status = self.$previewStatus, msgLoading = self.msgLoading,
  1130. msgProgress = self.msgProgress, msgSelected = self.msgSelected, fileType = self.previewFileType,
  1131. wrapLen = parseInt(self.wrapTextLength), wrapInd = self.wrapIndicator,
  1132. previewInitId = self.previewInitId, numFiles = files.length, settings = self.fileTypeSettings,
  1133. isText = isSet('text', settings) ? settings['text'] : defaultFileTypeSettings['text'],
  1134. ctr = self.filestack.length;
  1135. function readFile(i) {
  1136. if (isEmpty($el.attr('multiple'))) {
  1137. numFiles = 1;
  1138. }
  1139. if (i >= numFiles) {
  1140. $container.removeClass('loading');
  1141. $status.html('');
  1142. return;
  1143. }
  1144. var node = ctr + i, previewId = previewInitId + "-" + node, file = files[i], caption = self.slug(file.name),
  1145. fileSize = (file.size ? file.size : 0) / 1000, checkFile,
  1146. previewData = vUrl.createObjectURL(file), fileCount = 0, j, msg, typ, chk,
  1147. fileTypes = self.allowedFileTypes, strTypes = isEmpty(fileTypes) ? '' : fileTypes.join(', '),
  1148. fileExt = self.allowedFileExtensions, strExt = isEmpty(fileExt) ? '' : fileExt.join(', '),
  1149. fileExtExpr = isEmpty(fileExt) ? '' : new RegExp('\\.(' + fileExt.join('|') + ')$', 'i');
  1150. fileSize = fileSize.toFixed(2);
  1151. if (self.maxFileSize > 0 && fileSize > self.maxFileSize) {
  1152. msg = self.msgSizeTooLarge.replace(/\{name\}/g, caption).replace(/\{size\}/g,
  1153. fileSize).replace(/\{maxSize\}/g, self.maxFileSize);
  1154. self.isError = self.showError(msg, file, previewId, i);
  1155. return;
  1156. }
  1157. if (!isEmpty(fileTypes) && isArray(fileTypes)) {
  1158. for (j = 0; j < fileTypes.length; j++) {
  1159. typ = fileTypes[j];
  1160. checkFile = settings[typ];
  1161. chk = (checkFile !== undefined && checkFile(file.type, caption));
  1162. fileCount += isEmpty(chk) ? 0 : chk.length;
  1163. }
  1164. if (fileCount == 0) {
  1165. msg = self.msgInvalidFileType.replace(/\{name\}/g, caption).replace(/\{types\}/g, strTypes);
  1166. self.isError = self.showError(msg, file, previewId, i);
  1167. return;
  1168. }
  1169. }
  1170. if (fileCount == 0 && !isEmpty(fileExt) && isArray(fileExt) && !isEmpty(fileExtExpr)) {
  1171. chk = caption.match(fileExtExpr);
  1172. fileCount += isEmpty(chk) ? 0 : chk.length;
  1173. if (fileCount == 0) {
  1174. msg = self.msgInvalidFileExtension.replace(/\{name\}/g, caption).replace(/\{extensions\}/g, strExt);
  1175. self.isError = self.showError(msg, file, previewId, i);
  1176. return;
  1177. }
  1178. }
  1179. if (!self.showPreview) {
  1180. $el.trigger('fileloaded', [file, previewId, i]);
  1181. setTimeout(readFile(i + 1), 500);
  1182. return;
  1183. }
  1184. if ($preview.length > 0 && typeof FileReader !== "undefined") {
  1185. $status.html(msgLoading.replace(/\{index\}/g, i + 1).replace(/\{files\}/g, numFiles));
  1186. $container.addClass('loading');
  1187. reader.onerror = function (evt) {
  1188. self.errorHandler(evt, caption);
  1189. };
  1190. reader.onload = function (theFile) {
  1191. self.previewFile(file, theFile, previewId, previewData);
  1192. self.initFileActions();
  1193. };
  1194. reader.onloadend = function (e) {
  1195. var msg = msgProgress
  1196. .replace(/\{index\}/g, i + 1).replace(/\{files\}/g, numFiles)
  1197. .replace(/\{percent\}/g, 100).replace(/\{name\}/g, caption);
  1198. setTimeout(function () {
  1199. $status.html(msg);
  1200. vUrl.revokeObjectURL(previewData);
  1201. }, 500);
  1202. setTimeout(function () {
  1203. readFile(i + 1);
  1204. self.updateFileDetails(numFiles);
  1205. }, 500);
  1206. $el.trigger('fileloaded', [file, previewId, i]);
  1207. };
  1208. reader.onprogress = function (data) {
  1209. if (data.lengthComputable) {
  1210. var progress = parseInt(((data.loaded / data.total) * 100), 10);
  1211. var msg = msgProgress
  1212. .replace(/\{index\}/g, i + 1).replace(/\{files\}/g, numFiles)
  1213. .replace(/\{percent\}/g, progress).replace(/\{name\}/g, caption);
  1214. setTimeout(function () {
  1215. $status.html(msg);
  1216. }, 500);
  1217. }
  1218. };
  1219. if (isText(file.type, caption)) {
  1220. reader.readAsText(file);
  1221. } else {
  1222. reader.readAsArrayBuffer(file);
  1223. }
  1224. } else {
  1225. self.previewDefault(file, previewId, i);
  1226. setTimeout(function() {
  1227. readFile(i + 1);
  1228. self.updateFileDetails(numFiles);
  1229. }, 500);
  1230. $el.trigger('fileloaded', [file, previewId, i]);
  1231. }
  1232. self.filestack.push(file);
  1233. }
  1234. readFile(0);
  1235. self.updateFileDetails(numFiles, false);
  1236. },
  1237. updateFileDetails: function(numFiles) {
  1238. var self = this, msgSelected = self.msgSelected, $el = self.$element, fileStack = self.getFileStack(),
  1239. name = $el.val() || (fileStack.length && fileStack[0].name) || '', label = self.slug(name),
  1240. n = self.isUploadable ? fileStack.length : numFiles;
  1241. numFiles = self.initialPreviewCount + n,
  1242. log = n > 1 ? msgSelected.replace(/\{n\}/g, numFiles) : label;
  1243. if (self.isError) {
  1244. self.$previewContainer.removeClass('loading');
  1245. self.$previewStatus.html('');
  1246. self.$captionContainer.find('.kv-caption-icon').hide();
  1247. log = self.msgValidationError;
  1248. } else {
  1249. self.showFileIcon();
  1250. }
  1251. self.setCaption(log);
  1252. self.$container.removeClass('file-input-new');
  1253. if (arguments.length == 1) {
  1254. $el.trigger('fileselect', [numFiles, label]);
  1255. }
  1256. },
  1257. change: function (e) {
  1258. var self = this, $el = self.$element, label = self.slug($el.val()),
  1259. total = 0, $preview = self.$preview, isDragDrop = arguments.length > 1,
  1260. files = isDragDrop ? e.originalEvent.dataTransfer.files : $el.get(0).files,
  1261. msgSelected = self.msgSelected,
  1262. numFiles = !isEmpty(files) ? (files.length + self.initialPreviewCount) : 1, tfiles,
  1263. ctr = self.filestack.length, isAjaxUpload = (self.isUploadable && ctr != 0);
  1264. if (self.isError && !isAjaxUpload) {
  1265. return;
  1266. }
  1267. self.resetUpload();
  1268. self.hideFileIcon();
  1269. self.$container.find('.file-drop-zone .' + self.dropZoneTitleClass).remove();
  1270. if (isDragDrop) {
  1271. tfiles = files;
  1272. } else {
  1273. if (e.target.files === undefined) {
  1274. tfiles = e.target && e.target.value ? [
  1275. {name: e.target.value.replace(/^.+\\/, '')}
  1276. ] : [];
  1277. } else {
  1278. tfiles = e.target.files;
  1279. }
  1280. }
  1281. if (isEmpty(tfiles) || tfiles.length === 0) {
  1282. if (!isAjaxUpload) {
  1283. self.clear(false);
  1284. }
  1285. $el.trigger('fileselectnone');
  1286. return;
  1287. }
  1288. self.resetErrors();
  1289. if (!isAjaxUpload) {
  1290. if (!self.overwriteInitial) {
  1291. $preview.html(self.initialPreviewContent);
  1292. } else {
  1293. $preview.html('');
  1294. }
  1295. }
  1296. var total = self.isUploadable ? self.getFileStack().length + tfiles.length : tfiles.length;
  1297. if (self.maxFileCount > 0 && total > self.maxFileCount) {
  1298. var msg = self.msgFilesTooMany.replace(/\{m\}/g, self.maxFileCount).replace(/\{n\}/g, total);
  1299. self.isError = self.showError(msg, null, null, null);
  1300. self.$captionContainer.find('.kv-caption-icon').hide();
  1301. self.$caption.html(self.msgValidationError);
  1302. self.$container.removeClass('file-input-new');
  1303. return;
  1304. }
  1305. if (!self.isIE9) {
  1306. self.readFiles(files);
  1307. } else {
  1308. self.updateFileDetails(1);
  1309. }
  1310. self.reader = null;
  1311. },
  1312. autoSizeImage: function(previewId) {
  1313. var self = this, $preview = self.$preview,
  1314. $thumb = $preview.find("#" + previewId),
  1315. $img = $thumb.find('img');
  1316. if (!$img.length) {
  1317. return;
  1318. }
  1319. $img.on('load', function() {
  1320. var w1 = $thumb.width(), w2 = $preview.width();
  1321. if (w1 > w2) {
  1322. $img.css('width', '100%');
  1323. $thumb.css('width', '97%');
  1324. }
  1325. var $cap = $img.closest('.file-preview-frame').find('.file-caption-name');
  1326. if ($cap.length) {
  1327. $cap.width($img.width());
  1328. $cap.attr('title', $cap.text());
  1329. }
  1330. self.$element.trigger('fileimageloaded', previewId);
  1331. });
  1332. },
  1333. autoSizeCaption: function() {
  1334. var self = this;
  1335. if (self.$caption.length == 0 || !self.autoFitCaption) {
  1336. return;
  1337. }
  1338. self.$caption.css('width', 0);
  1339. setTimeout(function() {
  1340. var w = self.$captionContainer.width();
  1341. self.$caption.css('width', 0.98 * w);
  1342. }, 100);
  1343. },
  1344. setCaption: function(content) {
  1345. var self = this, title = $('<div>' + content + '</div>').text(),
  1346. icon = self.layoutTemplates['icon'],
  1347. out = icon + title;
  1348. if (self.$caption.length == 0) {
  1349. return;
  1350. }
  1351. self.$caption.html(out);
  1352. self.$caption.attr('title', title);
  1353. self.autoSizeCaption();
  1354. },
  1355. initBrowse: function ($container) {
  1356. var self = this;
  1357. self.$btnFile = $container.find('.btn-file');
  1358. self.$btnFile.append(self.$element);
  1359. },
  1360. createContainer: function () {
  1361. var self = this;
  1362. var $container = $(document.createElement("span")).attr({"class": 'file-input file-input-new'}).html(self.renderMain());
  1363. self.$element.before($container);
  1364. self.initBrowse($container);
  1365. return $container;
  1366. },
  1367. refreshContainer: function () {
  1368. var self = this, $container = self.$container;
  1369. $container.before(self.$element);
  1370. $container.html(self.renderMain());
  1371. self.initBrowse($container);
  1372. },
  1373. renderMain: function () {
  1374. var self = this, dropCss = (self.isUploadable && self.dropZoneEnabled) ? ' file-drop-zone' : '';;
  1375. var preview = self.showPreview ? self.getLayoutTemplate('preview')
  1376. .replace(/\{class\}/g, self.previewClass)
  1377. .replace(/\{dropClass\}/g, dropCss)
  1378. : '';
  1379. var css = self.isDisabled ? self.captionClass + ' file-caption-disabled' : self.captionClass;
  1380. var caption = self.captionTemplate.replace(/\{class\}/g, css + ' kv-fileinput-caption');
  1381. return self.mainTemplate.replace(/\{class\}/g, self.mainClass).
  1382. replace(/\{preview\}/g, preview).
  1383. replace(/\{caption\}/g, caption).
  1384. replace(/\{upload\}/g, self.renderUpload()).
  1385. replace(/\{remove\}/g, self.renderRemove()).
  1386. replace(/\{cancel\}/g, self.renderCancel()).
  1387. replace(/\{browse\}/g, self.renderBrowse());
  1388. },
  1389. renderBrowse: function () {
  1390. var self = this, css = self.browseClass + ' btn-file', status = '';
  1391. if (self.isDisabled) {
  1392. status = ' disabled ';
  1393. }
  1394. return '<div class="' + css + '"' + status + '> ' + self.browseIcon + self.browseLabel + ' </div>';
  1395. },
  1396. renderRemove: function () {
  1397. var self = this, css = self.removeClass + ' fileinput-remove fileinput-remove-button', status = '';
  1398. if (!self.showRemove) {
  1399. return '';
  1400. }
  1401. if (self.isDisabled) {
  1402. status = ' disabled ';
  1403. }
  1404. return '<button type="button" title="' + self.removeTitle + '" class="' + css + '"' + status + '>' + self.removeIcon + self.removeLabel + '</button>';
  1405. },
  1406. renderCancel: function () {
  1407. var self = this, css = self.cancelClass + ' fileinput-cancel fileinput-cancel-button', status = '';
  1408. if (!self.showCancel) {
  1409. return '';
  1410. }
  1411. return '<button type="button" title="' + self.cancelTitle + '" class="hide ' + css + '">' + self.cancelIcon + self.cancelLabel + '</button>';
  1412. },
  1413. renderUpload: function () {
  1414. var self = this, css = self.uploadClass + ' kv-fileinput-upload', content = '', status = '';
  1415. if (!self.showUpload) {
  1416. return '';
  1417. }
  1418. if (self.isDisabled) {
  1419. status = ' disabled ';
  1420. }
  1421. if (!self.isUploadable || self.isDisabled) {
  1422. content = '<button type="submit" title="' + self.uploadTitle + '"class="' + css + '"' + status + '>' + self.uploadIcon + self.uploadLabel + '</button>';
  1423. } else {
  1424. content = '<a href="' + self.uploadUrl + '" title="' + self.uploadTitle + '" class="' + css + '"' + status + '>' + self.uploadIcon + self.uploadLabel + '</a>';
  1425. }
  1426. return content;
  1427. }
  1428. }
  1429. //FileInput plugin definition
  1430. $.fn.fileinput = function (option) {
  1431. if (!hasFileAPISupport() && !isIE(9)) {
  1432. return;
  1433. }
  1434. var args = Array.apply(null, arguments);
  1435. args.shift();
  1436. return this.each(function () {
  1437. var $this = $(this),
  1438. data = $this.data('fileinput'),
  1439. options = typeof option === 'object' && option;
  1440. if (!data) {
  1441. $this.data('fileinput',
  1442. (data = new FileInput(this, $.extend({}, $.fn.fileinput.defaults, options, $(this).data()))));
  1443. }
  1444. if (typeof option === 'string') {
  1445. data[option].apply(data, args);
  1446. }
  1447. });
  1448. };
  1449. $.fn.fileinput.defaults = {
  1450. showCaption: true,
  1451. showPreview: true,
  1452. showRemove: true,
  1453. showUpload: true,
  1454. showCancel: true,
  1455. autoFitCaption: true,
  1456. mainClass: '',
  1457. previewClass: '',
  1458. captionClass: '',
  1459. mainTemplate: null,
  1460. initialCaption: '',
  1461. initialPreview: '',
  1462. initialPreviewCount: 0,
  1463. initialPreviewDelimiter: '*$$*',
  1464. initialPreviewConfig: [],
  1465. initialPreviewShowDelete: true,
  1466. overwriteInitial: true,
  1467. layoutTemplates: defaultLayoutTemplates,
  1468. previewTemplates: defaultPreviewTemplates,
  1469. allowedPreviewTypes: defaultPreviewTypes,
  1470. allowedPreviewMimeTypes: null,
  1471. allowedFileTypes: null,
  1472. allowedFileExtensions: null,
  1473. previewSettings: defaultPreviewSettings,
  1474. fileTypeSettings: defaultFileTypeSettings,
  1475. browseLabel: 'Browse &hellip;',
  1476. browseIcon: '<i class="glyphicon glyphicon-folder-open"></i> &nbsp;',
  1477. browseClass: 'btn btn-primary',
  1478. removeLabel: 'Remove',
  1479. removeTitle: 'Clear selected files',
  1480. removeIcon: '<i class="glyphicon glyphicon-trash"></i> ',
  1481. removeClass: 'btn btn-default',
  1482. cancelLabel: 'Cancel',
  1483. cancelTitle: 'Abort ongoing upload',
  1484. cancelIcon: '<i class="glyphicon glyphicon-ban-circle"></i> ',
  1485. cancelClass: 'btn btn-default',
  1486. uploadLabel: 'Upload',
  1487. uploadTitle: 'Upload selected files',
  1488. uploadIcon: '<i class="glyphicon glyphicon-upload"></i> ',
  1489. uploadClass: 'btn btn-default',
  1490. uploadUrl: null,
  1491. uploadExtraData: [],
  1492. uploadAsync: true,
  1493. maxFileSize: 0,
  1494. maxFileCount: 0,
  1495. msgSizeTooLarge: 'File "{name}" (<b>{size} KB</b>) exceeds maximum allowed upload size of <b>{maxSize} KB</b>. Please retry your upload!',
  1496. msgFilesTooMany: 'Number of files selected for upload <b>({n})</b> exceeds maximum allowed limit of <b>{m}</b>. Please retry your upload!',
  1497. msgFileNotFound: 'File "{name}" not found!',
  1498. msgFileNotReadable: 'File "{name}" is not readable.',
  1499. msgFilePreviewAborted: 'File preview aborted for "{name}".',
  1500. msgFilePreviewError: 'An error occurred while reading the file "{name}".',
  1501. msgInvalidFileType: 'Invalid type for file "{name}". Only "{types}" files are supported.',
  1502. msgInvalidFileExtension: 'Invalid extension for file "{name}". Only "{extensions}" files are supported.',
  1503. msgValidationError: '<span class="text-danger"><i class="glyphicon glyphicon-exclamation-sign"></i> File Upload Error</span>',
  1504. msgErrorClass: 'file-error-message',
  1505. msgLoading: 'Loading file {index} of {files} &hellip;',
  1506. msgProgress: 'Loading file {index} of {files} - {name} - {percent}% completed.',
  1507. msgSelected: '{n} files selected',
  1508. previewFileType: 'image',
  1509. wrapTextLength: 250,
  1510. wrapIndicator: ' <span class="wrap-indicator" title="{title}" onclick="{dialog}">[&hellip;]</span>',
  1511. elCaptionContainer: null,
  1512. elCaptionText: null,
  1513. elPreviewContainer: null,
  1514. elPreviewImage: null,
  1515. elPreviewStatus: null,
  1516. elErrorContainer: null,
  1517. slugCallback: null,
  1518. dropZoneEnabled: true,
  1519. dropZoneTitle: 'Drag & drop files here &hellip;',
  1520. dropZoneTitleClass: 'file-drop-zone-title',
  1521. fileActionSettings: {}
  1522. };
  1523. /**
  1524. * Convert automatically file inputs with class 'file'
  1525. * into a bootstrap fileinput control.
  1526. */
  1527. $(document).ready(function () {
  1528. var $input = $('input.file[type=file]'), count = $input.attr('type') != null ? $input.length : 0;
  1529. if (count > 0) {
  1530. $input.fileinput();
  1531. }
  1532. });
  1533. $.fn.fileinput.Constructor = FileInput;
  1534. })(window.jQuery);