fileinput.js 76 KB

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