fileinput.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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(5);
  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. }
  744. if (self.$container.find('.file-preview-frame').length == 0) {
  745. self.initialCaption = '';
  746. self.original.caption = '';
  747. self.$caption.html('');
  748. self.$captionContainer.find('.kv-caption-icon').hide();
  749. }
  750. self.hideFileIcon();
  751. self.$element.trigger('filecleared');
  752. self.$captionContainer.focus();
  753. self.setFileDropZoneTitle();
  754. },
  755. reset: function () {
  756. var self = this;
  757. self.clear(false);
  758. self.$preview.html(self.original.preview);
  759. self.$caption.html(self.original.caption);
  760. self.$container.find('.fileinput-filename').text('');
  761. self.$element.trigger('filereset');
  762. if (self.initialPreview.length > 0) {
  763. self.$container.removeClass('file-input-new');
  764. }
  765. self.setFileDropZoneTitle();
  766. self.resetUpload();
  767. self.filestack = [];
  768. },
  769. disable: function (e) {
  770. var self = this;
  771. self.isDisabled = true;
  772. self.$element.attr('disabled', 'disabled');
  773. self.$container.find(".kv-fileinput-caption").addClass("file-caption-disabled");
  774. self.$container.find(".btn-file, .fileinput-remove, .kv-fileinput-upload").attr("disabled", true);
  775. self.initDragDrop();
  776. },
  777. enable: function (e) {
  778. var self = this;
  779. self.isDisabled = false;
  780. self.$element.removeAttr('disabled');
  781. self.$container.find(".kv-fileinput-caption").removeClass("file-caption-disabled");
  782. self.$container.find(".btn-file, .fileinput-remove, .kv-fileinput-upload").removeAttr("disabled");
  783. self.initDragDrop();
  784. },
  785. uploadExtra: function(fd) {
  786. var self = this;
  787. if (self.uploadExtraData.length == 0) {
  788. return;
  789. }
  790. $.each(self.uploadExtraData, function(j, data) {
  791. if (!isEmpty(data.id) && !isEmpty(data.value)) {
  792. fd.append(data.id, data.value);
  793. }
  794. });
  795. },
  796. initXhr: function(xhrobj) {
  797. var self = this;
  798. if (xhrobj.upload) {
  799. xhrobj.upload.addEventListener('progress', function(event) {
  800. var pct = 0, position = event.loaded || event.position, total = event.total;
  801. if (event.lengthComputable) {
  802. pct = Math.ceil(position / total * 98);
  803. }
  804. self.uploadPercent = Math.max(pct, self.uploadPercent);
  805. //Set progress
  806. self.setProgress(self.uploadPercent);
  807. }, false);
  808. }
  809. return xhrobj;
  810. },
  811. upload: function(i) {
  812. var self = this, files = self.getFileStack(), formdata = new FormData(),
  813. previewId = self.previewInitId + "-" + i, $thumb = $('#' + previewId),
  814. $btnUpload = $thumb.find('.kv-file-upload'), $btnDelete = $thumb.find('.kv-file-remove'),
  815. $indicator = $thumb.find('.file-upload-indicator'), config = self.fileActionSettings;
  816. if ($btnUpload.hasClass('disabled')) {
  817. return;
  818. }
  819. var percent,
  820. total = files.length,
  821. allFiles = arguments.length > 1,
  822. setIndicator = function (icon, msg) {
  823. $indicator.html(config[icon]);
  824. $indicator.attr('title', config[msg]);
  825. },
  826. updateProgress = function() {
  827. if (self.$preview.find('file-uploading').length == 0) {
  828. self.unlock();
  829. }
  830. if (!allFiles) {
  831. return;
  832. }
  833. self.uploadCount++;
  834. var pct = total > 0 ? Math.ceil(self.uploadCount * 100/total) : 0;
  835. self.uploadPercent = Math.max(pct, self.uploadPercent);
  836. self.setProgress(self.uploadPercent);
  837. self.initPreviewDeletes();
  838. },
  839. resetActions = function() {
  840. $btnUpload.removeClass('disabled');
  841. $btnDelete.removeClass('disabled');
  842. $thumb.removeClass('file-uploading');
  843. };
  844. formdata.append(self.uploadFileAttr, files[i]);
  845. formdata.append('file_id', i);
  846. self.uploadExtra(formdata);
  847. self.ajaxRequests.push($.ajax({
  848. xhr: function() {
  849. var xhrobj = $.ajaxSettings.xhr();
  850. return self.initXhr(xhrobj);
  851. },
  852. url: self.uploadUrl,
  853. type: 'POST',
  854. dataType: 'json',
  855. data: formdata,
  856. cache: false,
  857. processData: false,
  858. contentType: false,
  859. beforeSend: function() {
  860. setIndicator('indicatorLoading', 'indicatorLoadingTitle');
  861. addCss($thumb, 'file-uploading');
  862. addCss($btnUpload, 'disabled');
  863. addCss($btnDelete, 'disabled');
  864. if (!allFiles) {
  865. self.lock();
  866. }
  867. self.$element.trigger('filepreupload', [formdata, previewId, i])
  868. },
  869. success: function(data, textStatus, jqXHR) {
  870. setTimeout(function() {
  871. if(typeof data.error === 'undefined') {
  872. setIndicator('indicatorSuccess', 'indicatorSuccessTitle');
  873. $btnUpload.hide();
  874. $btnDelete.hide();
  875. self.filestack[i] = undefined;
  876. self.$element.trigger('fileuploaded', [formdata, previewId, i]);
  877. } else {
  878. setIndicator('indicatorError', 'indicatorErrorTitle');
  879. self.showError(data.error, formdata, previewId, i, 'fileuploaderror');
  880. }
  881. updateProgress();
  882. resetActions();
  883. }, 500);
  884. },
  885. error: function(jqXHR, textStatus, errorThrown) {
  886. setIndicator('indicatorError', 'indicatorErrorTitle');
  887. if (allFiles) {
  888. var cap = files[i].name;
  889. self.showUploadError('<b>' + cap + '</b>: ' + errorThrown, formdata, previewId, i, 'fileuploaderror');
  890. } else {
  891. self.showError(errorThrown, formdata, previewId, i, 'fileuploaderror');
  892. }
  893. updateProgress();
  894. resetActions();
  895. }
  896. }));
  897. },
  898. uploadBatch: function() {
  899. var self = this, files = self.filestack, total = files.length,
  900. config = self.fileActionSettings; formdata = new FormData(),
  901. setIndicator = function (i, icon, msg) {
  902. $indicator = $('#' + self.previewInitId + "-" + i).find('.file-upload-indicator'),
  903. $indicator.html(config[icon]);
  904. $indicator.attr('title', config[msg]);
  905. },
  906. setAllUploaded = function() {
  907. $.each(files, function(key, data) {
  908. self.filestack[key] = undefined;
  909. });
  910. };
  911. $.each(files, function(key, data) {
  912. if (files[key] !== undefined) {
  913. formdata.append(self.uploadFileAttr, files[key]);
  914. }
  915. });
  916. self.uploadExtra(formdata);
  917. $.ajax({
  918. xhr: function() {
  919. var xhrobj = $.ajaxSettings.xhr();
  920. return self.initXhr(xhrobj);
  921. },
  922. url: self.uploadUrl,
  923. type: 'POST',
  924. dataType: 'json',
  925. data: formdata,
  926. cache: false,
  927. processData: false,
  928. contentType: false,
  929. beforeSend: function() {
  930. addCss(self.$preview.find('.file-preview-frame'), 'file-uploading');
  931. self.lock();
  932. },
  933. success: function(data, textStatus, jqXHR) {
  934. setTimeout(function() {
  935. var keys = isEmpty(data.errorkeys) ? [] : data.errorkeys;
  936. if(typeof data.error === 'undefined' || isEmpty(data.error)) {
  937. setAllUploaded();
  938. if (self.showPreview) {
  939. self.$preview.find('.kv-file-upload').hide();
  940. self.$preview.find('.kv-file-remove').hide();
  941. self.$preview.find('.file-preview-frame').each(function() {
  942. var $thumb = $(this), key = $thumb.attr('data-fileindex');
  943. setIndicator(key, 'indicatorSuccess', 'indicatorSuccessTitle');
  944. $thumb.removeClass('file-uploading');
  945. });
  946. } else {
  947. self.reset();
  948. }
  949. } else {
  950. self.$preview.find('.file-preview-frame').each(function() {
  951. var $thumb = $(this), key = $thumb.attr('data-fileindex');
  952. if (keys.length == 0) {
  953. $thumb.removeClass('file-uploading');
  954. setIndicator(key, 'indicatorError', 'indicatorErrorTitle');
  955. return;
  956. }
  957. if ($.inArray(key, keys)) {
  958. setIndicator(key, 'indicatorError', 'indicatorErrorTitle');
  959. } else {
  960. var $upload = $thumb.find('.kv-file-upload'), $remove = $thumb.find('.kv-file-remove');
  961. addCss($upload, 'disabled');
  962. addCss($remove, 'disabled');
  963. $upload.hide();
  964. $remove.hide();
  965. $thumb.removeClass('file-uploading');
  966. setIndicator(key, 'indicatorSuccess', 'indicatorSuccessTitle');
  967. self.filestack[key] = undefined;
  968. }
  969. });
  970. self.showUploadError(data.error, formdata, null, null, 'filebatchuploaderror');
  971. }
  972. }, 100);
  973. },
  974. complete: function () {
  975. self.setProgress(100);
  976. self.unlock();
  977. },
  978. error: function(jqXHR, textStatus, errorThrown) {
  979. self.showError(errorThrown, formdata, null, null, 'filebatchuploaderror');
  980. self.uploadFileCount = total - 1;
  981. self.$preview.find('.file-preview-frame').removeClass('file-uploading');
  982. }
  983. });
  984. },
  985. hideFileIcon: function () {
  986. if (this.overwriteInitial) {
  987. this.$captionContainer.find('.kv-caption-icon').hide();
  988. }
  989. },
  990. showFileIcon: function () {
  991. this.$captionContainer.find('.kv-caption-icon').show();
  992. },
  993. resetErrors: function (fade) {
  994. var self = this, $error = self.$errorContainer;
  995. self.isError = false;
  996. self.$container.removeClass('has-error');
  997. if (fade) {
  998. $error.fadeOut('slow');
  999. } else {
  1000. $error.hide();
  1001. }
  1002. },
  1003. showUploadError: function (msg, file, previewId, index) {
  1004. var self = this, $error = self.$errorContainer, $el = self.$element,
  1005. ev = arguments.length > 4 ? arguments[4] : 'fileerror';
  1006. if ($error.find('ul').length == 0) {
  1007. $error.html('<ul class="text-left"><li>' + msg + '</li></ul>');
  1008. } else {
  1009. $error.find('ul').append('<li>' + msg + '</li>');
  1010. }
  1011. $error.fadeIn(800);
  1012. $el.trigger(ev, [file, previewId, index]);
  1013. addCss(self.$container, 'has-error');
  1014. return true;
  1015. },
  1016. showError: function (msg, file, previewId, index) {
  1017. var self = this, $error = self.$errorContainer, $el = self.$element,
  1018. ev = arguments.length > 4 ? arguments[4] : 'fileerror';
  1019. $error.html(msg);
  1020. $error.fadeIn(800);
  1021. $el.trigger(ev, [file, previewId, index]);
  1022. if (!self.isUploadable) {
  1023. self.clearFileInput();
  1024. }
  1025. addCss(self.$container, 'has-error');
  1026. return true;
  1027. },
  1028. errorHandler: function (evt, caption) {
  1029. var self = this;
  1030. switch (evt.target.error.code) {
  1031. case evt.target.error.NOT_FOUND_ERR:
  1032. self.addError(self.msgFileNotFound.replace(/\{name\}/g, caption));
  1033. break;
  1034. case evt.target.error.NOT_READABLE_ERR:
  1035. self.addError(self.msgFileNotReadable.replace(/\{name\}/g, caption));
  1036. break;
  1037. case evt.target.error.ABORT_ERR:
  1038. self.addError(self.msgFilePreviewAborted.replace(/\{name\}/g, caption));
  1039. break;
  1040. default:
  1041. self.addError(self.msgFilePreviewError.replace(/\{name\}/g, caption));
  1042. }
  1043. },
  1044. parseFileType: function(file) {
  1045. var isValid, vType;
  1046. for (var i = 0; i < defaultPreviewTypes.length; i++) {
  1047. cat = defaultPreviewTypes[i];
  1048. isValid = isSet(cat, self.fileTypeSettings) ? self.fileTypeSettings[cat] : defaultFileTypeSettings[cat];
  1049. vType = isValid(file.type, file.name) ? cat : '';
  1050. if (vType != '') {
  1051. return vType;
  1052. }
  1053. }
  1054. return 'other';
  1055. },
  1056. previewDefault: function(file, previewId) {
  1057. var self = this, data = vUrl.createObjectURL(file), $obj = $('#' + previewId),
  1058. footer = self.renderFileFooter(file.name, self.previewSettings.other.width),
  1059. previewOtherTemplate = self.getPreviewTemplate('other'),
  1060. ind = previewId.slice(previewId.lastIndexOf('-') + 1);
  1061. self.$preview.append("\n" + previewOtherTemplate
  1062. .replace(/\{previewId\}/g, previewId)
  1063. .replace(/\{frameClass\}/g, '')
  1064. .replace(/\{fileindex\}/g, ind)
  1065. .replace(/\{caption\}/g, self.slug(file.name))
  1066. .replace(/\{type\}/g, file.type)
  1067. .replace(/\{data\}/g, data)
  1068. .replace(/\{footer\}/g, footer));
  1069. $obj.on('load', function(e) {
  1070. vUrl.revokeObjectURL($obj.attr('data'));
  1071. });
  1072. },
  1073. previewFile: function(file, theFile, previewId, data) {
  1074. var self = this, cat = self.parseFileType(file), caption = self.slug(file.name), data, obj, content,
  1075. types = self.allowedPreviewTypes, mimes = self.allowedPreviewMimeTypes, fType = file.type,
  1076. template = isSet(cat, self.previewTemplates) ? self.previewTemplates[cat] : defaultPreviewTemplates[cat],
  1077. config = isSet(cat, self.previewSettings) ? self.previewSettings[cat] : defaultPreviewSettings[cat],
  1078. wrapLen = parseInt(self.wrapTextLength), wrapInd = self.wrapIndicator, $preview = self.$preview,
  1079. chkTypes = types.indexOf(cat) >=0, chkMimes = isEmpty(mimes) || (!isEmpty(mimes) && isSet(file.type, mimes)),
  1080. footer = self.renderFileFooter(caption, config.width),
  1081. ind = previewId.slice(previewId.lastIndexOf('-') + 1);
  1082. if (chkTypes && chkMimes) {
  1083. if (cat == 'text') {
  1084. var strText = htmlEncode(theFile.target.result);
  1085. vUrl.revokeObjectURL(data);
  1086. if (strText.length > wrapLen) {
  1087. var id = 'text-' + uniqId(), height = window.innerHeight * .75,
  1088. modal = self.getLayoutTemplate('modal')
  1089. .replace(/\{id\}/g, id)
  1090. .replace(/\{title\}/g, caption)
  1091. .replace(/\{height\}/g, height)
  1092. .replace(/\{body\}/g, strText);
  1093. wrapInd = wrapInd
  1094. .replace(/\{title\}/g, caption)
  1095. .replace(/\{dialog\}/g, "$('#" + id + "').modal('show')");
  1096. strText = strText.substring(0, (wrapLen - 1)) + wrapInd;
  1097. }
  1098. content = template
  1099. .replace(/\{previewId\}/g, previewId).replace(/\{caption\}/g, caption)
  1100. .replace(/\{frameClass\}/g, '')
  1101. .replace(/\{type\}/g, file.type).replace(/\{width\}/g, config.width)
  1102. .replace(/\{height\}/g, config.height).replace(/\{data\}/g, strText)
  1103. .replace(/\{footer\}/g, footer).replace(/\{fileindex\}/g, ind) + modal;
  1104. } else {
  1105. content = template
  1106. .replace(/\{previewId\}/g, previewId).replace(/\{caption\}/g, caption)
  1107. .replace(/\{frameClass\}/g, '')
  1108. .replace(/\{type\}/g, file.type).replace(/\{data\}/g, data)
  1109. .replace(/\{width\}/g, config.width).replace(/\{height\}/g, config.height)
  1110. .replace(/\{footer\}/g, footer).replace(/\{fileindex\}/g, ind);
  1111. }
  1112. $preview.append("\n" + content);
  1113. self.autoSizeImage(previewId);
  1114. } else {
  1115. self.previewDefault(file, previewId);
  1116. }
  1117. },
  1118. slugDefault: function (text) {
  1119. return isEmpty(text) ? '' : text.split(/(\\|\/)/g).pop().replace(/[^\w-.\\\/ ]+/g,'');
  1120. },
  1121. getFileStack: function() {
  1122. var size = 0, self = this;
  1123. return self.filestack.filter(function(n){ return n != undefined });
  1124. },
  1125. readFiles: function (files) {
  1126. this.reader = new FileReader();
  1127. var self = this, $el = self.$element, $preview = self.$preview, reader = self.reader,
  1128. $container = self.$previewContainer, $status = self.$previewStatus, msgLoading = self.msgLoading,
  1129. msgProgress = self.msgProgress, msgSelected = self.msgSelected, fileType = self.previewFileType,
  1130. wrapLen = parseInt(self.wrapTextLength), wrapInd = self.wrapIndicator,
  1131. previewInitId = self.previewInitId, numFiles = files.length, settings = self.fileTypeSettings,
  1132. isText = isSet('text', settings) ? settings['text'] : defaultFileTypeSettings['text'],
  1133. ctr = self.filestack.length;
  1134. function readFile(i) {
  1135. if (isEmpty($el.attr('multiple'))) {
  1136. numFiles = 1;
  1137. }
  1138. if (i >= numFiles) {
  1139. $container.removeClass('loading');
  1140. $status.html('');
  1141. return;
  1142. }
  1143. var node = ctr + i, previewId = previewInitId + "-" + node, file = files[i], caption = self.slug(file.name),
  1144. fileSize = (file.size ? file.size : 0) / 1000, checkFile,
  1145. previewData = vUrl.createObjectURL(file), fileCount = 0, j, msg, typ, chk,
  1146. fileTypes = self.allowedFileTypes, strTypes = isEmpty(fileTypes) ? '' : fileTypes.join(', '),
  1147. fileExt = self.allowedFileExtensions, strExt = isEmpty(fileExt) ? '' : fileExt.join(', '),
  1148. fileExtExpr = isEmpty(fileExt) ? '' : new RegExp('\\.(' + fileExt.join('|') + ')$', 'i');
  1149. fileSize = fileSize.toFixed(2);
  1150. if (self.maxFileSize > 0 && fileSize > self.maxFileSize) {
  1151. msg = self.msgSizeTooLarge.replace(/\{name\}/g, caption).replace(/\{size\}/g,
  1152. fileSize).replace(/\{maxSize\}/g, self.maxFileSize);
  1153. self.isError = self.showError(msg, file, previewId, i);
  1154. return;
  1155. }
  1156. if (!isEmpty(fileTypes) && isArray(fileTypes)) {
  1157. for (j = 0; j < fileTypes.length; j++) {
  1158. typ = fileTypes[j];
  1159. checkFile = settings[typ];
  1160. chk = (checkFile !== undefined && checkFile(file.type, caption));
  1161. fileCount += isEmpty(chk) ? 0 : chk.length;
  1162. }
  1163. if (fileCount == 0) {
  1164. msg = self.msgInvalidFileType.replace(/\{name\}/g, caption).replace(/\{types\}/g, strTypes);
  1165. self.isError = self.showError(msg, file, previewId, i);
  1166. return;
  1167. }
  1168. }
  1169. if (fileCount == 0 && !isEmpty(fileExt) && isArray(fileExt) && !isEmpty(fileExtExpr)) {
  1170. chk = caption.match(fileExtExpr);
  1171. fileCount += isEmpty(chk) ? 0 : chk.length;
  1172. if (fileCount == 0) {
  1173. msg = self.msgInvalidFileExtension.replace(/\{name\}/g, caption).replace(/\{extensions\}/g, strExt);
  1174. self.isError = self.showError(msg, file, previewId, i);
  1175. return;
  1176. }
  1177. }
  1178. if (!self.showPreview) {
  1179. $el.trigger('fileloaded', [file, previewId, i]);
  1180. setTimeout(readFile(i + 1), 500);
  1181. return;
  1182. }
  1183. if ($preview.length > 0 && typeof FileReader !== "undefined") {
  1184. $status.html(msgLoading.replace(/\{index\}/g, i + 1).replace(/\{files\}/g, numFiles));
  1185. $container.addClass('loading');
  1186. reader.onerror = function (evt) {
  1187. self.errorHandler(evt, caption);
  1188. };
  1189. reader.onload = function (theFile) {
  1190. self.previewFile(file, theFile, previewId, previewData);
  1191. self.initFileActions();
  1192. };
  1193. reader.onloadend = function (e) {
  1194. var msg = msgProgress
  1195. .replace(/\{index\}/g, i + 1).replace(/\{files\}/g, numFiles)
  1196. .replace(/\{percent\}/g, 100).replace(/\{name\}/g, caption);
  1197. setTimeout(function () {
  1198. $status.html(msg);
  1199. vUrl.revokeObjectURL(previewData);
  1200. }, 500);
  1201. setTimeout(function () {
  1202. readFile(i + 1);
  1203. self.updateFileDetails(numFiles);
  1204. }, 500);
  1205. $el.trigger('fileloaded', [file, previewId, i]);
  1206. };
  1207. reader.onprogress = function (data) {
  1208. if (data.lengthComputable) {
  1209. var progress = parseInt(((data.loaded / data.total) * 100), 10);
  1210. var msg = msgProgress
  1211. .replace(/\{index\}/g, i + 1).replace(/\{files\}/g, numFiles)
  1212. .replace(/\{percent\}/g, progress).replace(/\{name\}/g, caption);
  1213. setTimeout(function () {
  1214. $status.html(msg);
  1215. }, 500);
  1216. }
  1217. };
  1218. if (isText(file.type, caption)) {
  1219. reader.readAsText(file);
  1220. } else {
  1221. reader.readAsArrayBuffer(file);
  1222. }
  1223. } else {
  1224. self.previewDefault(file, previewId, i);
  1225. setTimeout(function() {
  1226. readFile(i + 1);
  1227. self.updateFileDetails(numFiles);
  1228. }, 500);
  1229. $el.trigger('fileloaded', [file, previewId, i]);
  1230. }
  1231. self.filestack.push(file);
  1232. }
  1233. readFile(0);
  1234. self.updateFileDetails(numFiles, false);
  1235. },
  1236. updateFileDetails: function(numFiles) {
  1237. var self = this, msgSelected = self.msgSelected, $el = self.$element, fileStack = self.getFileStack(),
  1238. name = $el.val() || (fileStack.length && fileStack[0].name) || '', label = self.slug(name),
  1239. n = self.isUploadable ? fileStack.length : numFiles;
  1240. numFiles = self.initialPreviewCount + n,
  1241. log = n > 1 ? msgSelected.replace(/\{n\}/g, numFiles) : label;
  1242. if (self.isError) {
  1243. self.$previewContainer.removeClass('loading');
  1244. self.$previewStatus.html('');
  1245. self.$captionContainer.find('.kv-caption-icon').hide();
  1246. log = self.msgValidationError;
  1247. } else {
  1248. self.showFileIcon();
  1249. }
  1250. self.setCaption(log);
  1251. self.$container.removeClass('file-input-new');
  1252. if (arguments.length == 1) {
  1253. $el.trigger('fileselect', [numFiles, label]);
  1254. }
  1255. },
  1256. change: function (e) {
  1257. var self = this, $el = self.$element, label = self.slug($el.val()),
  1258. total = 0, $preview = self.$preview, isDragDrop = arguments.length > 1,
  1259. files = isDragDrop ? e.originalEvent.dataTransfer.files : $el.get(0).files,
  1260. msgSelected = self.msgSelected,
  1261. numFiles = !isEmpty(files) ? (files.length + self.initialPreviewCount) : 1, tfiles,
  1262. ctr = self.filestack.length, isAjaxUpload = (self.isUploadable && ctr != 0);
  1263. if (self.isError && !isAjaxUpload) {
  1264. return;
  1265. }
  1266. self.resetUpload();
  1267. self.hideFileIcon();
  1268. self.$container.find('.file-drop-zone .' + self.dropZoneTitleClass).remove();
  1269. if (isDragDrop) {
  1270. tfiles = files;
  1271. } else {
  1272. if (e.target.files === undefined) {
  1273. tfiles = e.target && e.target.value ? [
  1274. {name: e.target.value.replace(/^.+\\/, '')}
  1275. ] : [];
  1276. } else {
  1277. tfiles = e.target.files;
  1278. }
  1279. }
  1280. if (isEmpty(tfiles) || tfiles.length === 0) {
  1281. if (!isAjaxUpload) {
  1282. self.clear(false);
  1283. }
  1284. $el.trigger('fileselectnone');
  1285. return;
  1286. }
  1287. self.resetErrors();
  1288. if (!isAjaxUpload) {
  1289. if (!self.overwriteInitial) {
  1290. $preview.html(self.initialPreviewContent);
  1291. } else {
  1292. $preview.html('');
  1293. }
  1294. }
  1295. var total = self.isUploadable ? self.getFileStack().length + tfiles.length : tfiles.length;
  1296. if (self.maxFileCount > 0 && total > self.maxFileCount) {
  1297. var msg = self.msgFilesTooMany.replace(/\{m\}/g, self.maxFileCount).replace(/\{n\}/g, total);
  1298. self.isError = self.showError(msg, null, null, null);
  1299. self.$captionContainer.find('.kv-caption-icon').hide();
  1300. self.$caption.html(self.msgValidationError);
  1301. self.$container.removeClass('file-input-new');
  1302. return;
  1303. }
  1304. if (!self.isIE9) {
  1305. self.readFiles(files);
  1306. } else {
  1307. self.updateFileDetails(1);
  1308. }
  1309. self.reader = null;
  1310. },
  1311. autoSizeImage: function(previewId) {
  1312. var self = this, $preview = self.$preview,
  1313. $thumb = $preview.find("#" + previewId),
  1314. $img = $thumb.find('img');
  1315. if (!$img.length) {
  1316. return;
  1317. }
  1318. $img.on('load', function() {
  1319. var w1 = $thumb.width(), w2 = $preview.width();
  1320. if (w1 > w2) {
  1321. $img.css('width', '100%');
  1322. $thumb.css('width', '97%');
  1323. }
  1324. var $cap = $img.closest('.file-preview-frame').find('.file-caption-name');
  1325. if ($cap.length) {
  1326. $cap.width($img.width());
  1327. $cap.attr('title', $cap.text());
  1328. }
  1329. self.$element.trigger('fileimageloaded', previewId);
  1330. });
  1331. },
  1332. autoSizeCaption: function() {
  1333. var self = this;
  1334. if (self.$caption.length == 0 || !self.autoFitCaption) {
  1335. return;
  1336. }
  1337. self.$caption.css('width', 0);
  1338. setTimeout(function() {
  1339. var w = self.$captionContainer.width();
  1340. self.$caption.css('width', 0.98 * w);
  1341. }, 100);
  1342. },
  1343. setCaption: function(content) {
  1344. var self = this, title = $('<div>' + content + '</div>').text(),
  1345. icon = self.layoutTemplates['icon'],
  1346. out = icon + title;
  1347. if (self.$caption.length == 0) {
  1348. return;
  1349. }
  1350. self.$caption.html(out);
  1351. self.$caption.attr('title', title);
  1352. self.autoSizeCaption();
  1353. },
  1354. initBrowse: function ($container) {
  1355. var self = this;
  1356. self.$btnFile = $container.find('.btn-file');
  1357. self.$btnFile.append(self.$element);
  1358. },
  1359. createContainer: function () {
  1360. var self = this;
  1361. var $container = $(document.createElement("span")).attr({"class": 'file-input file-input-new'}).html(self.renderMain());
  1362. self.$element.before($container);
  1363. self.initBrowse($container);
  1364. return $container;
  1365. },
  1366. refreshContainer: function () {
  1367. var self = this, $container = self.$container;
  1368. $container.before(self.$element);
  1369. $container.html(self.renderMain());
  1370. self.initBrowse($container);
  1371. },
  1372. renderMain: function () {
  1373. var self = this, dropCss = (self.isUploadable && self.dropZoneEnabled) ? ' file-drop-zone' : '';;
  1374. var preview = self.showPreview ? self.getLayoutTemplate('preview')
  1375. .replace(/\{class\}/g, self.previewClass)
  1376. .replace(/\{dropClass\}/g, dropCss)
  1377. : '';
  1378. var css = self.isDisabled ? self.captionClass + ' file-caption-disabled' : self.captionClass;
  1379. var caption = self.captionTemplate.replace(/\{class\}/g, css + ' kv-fileinput-caption');
  1380. return self.mainTemplate.replace(/\{class\}/g, self.mainClass).
  1381. replace(/\{preview\}/g, preview).
  1382. replace(/\{caption\}/g, caption).
  1383. replace(/\{upload\}/g, self.renderUpload()).
  1384. replace(/\{remove\}/g, self.renderRemove()).
  1385. replace(/\{cancel\}/g, self.renderCancel()).
  1386. replace(/\{browse\}/g, self.renderBrowse());
  1387. },
  1388. renderBrowse: function () {
  1389. var self = this, css = self.browseClass + ' btn-file', status = '';
  1390. if (self.isDisabled) {
  1391. status = ' disabled ';
  1392. }
  1393. return '<div class="' + css + '"' + status + '> ' + self.browseIcon + self.browseLabel + ' </div>';
  1394. },
  1395. renderRemove: function () {
  1396. var self = this, css = self.removeClass + ' fileinput-remove fileinput-remove-button', status = '';
  1397. if (!self.showRemove) {
  1398. return '';
  1399. }
  1400. if (self.isDisabled) {
  1401. status = ' disabled ';
  1402. }
  1403. return '<button type="button" title="' + self.removeTitle + '" class="' + css + '"' + status + '>' + self.removeIcon + self.removeLabel + '</button>';
  1404. },
  1405. renderCancel: function () {
  1406. var self = this, css = self.cancelClass + ' fileinput-cancel fileinput-cancel-button', status = '';
  1407. if (!self.showCancel) {
  1408. return '';
  1409. }
  1410. return '<button type="button" title="' + self.cancelTitle + '" class="hide ' + css + '">' + self.cancelIcon + self.cancelLabel + '</button>';
  1411. },
  1412. renderUpload: function () {
  1413. var self = this, css = self.uploadClass + ' kv-fileinput-upload', content = '', status = '';
  1414. if (!self.showUpload) {
  1415. return '';
  1416. }
  1417. if (self.isDisabled) {
  1418. status = ' disabled ';
  1419. }
  1420. if (!self.isUploadable || self.isDisabled) {
  1421. content = '<button type="submit" title="' + self.uploadTitle + '"class="' + css + '"' + status + '>' + self.uploadIcon + self.uploadLabel + '</button>';
  1422. } else {
  1423. content = '<a href="' + self.uploadUrl + '" title="' + self.uploadTitle + '" class="' + css + '"' + status + '>' + self.uploadIcon + self.uploadLabel + '</a>';
  1424. }
  1425. return content;
  1426. }
  1427. }
  1428. //FileInput plugin definition
  1429. $.fn.fileinput = function (option) {
  1430. if (!hasFileAPISupport() && !isIE(9)) {
  1431. return;
  1432. }
  1433. var args = Array.apply(null, arguments);
  1434. args.shift();
  1435. return this.each(function () {
  1436. var $this = $(this),
  1437. data = $this.data('fileinput'),
  1438. options = typeof option === 'object' && option;
  1439. if (!data) {
  1440. $this.data('fileinput',
  1441. (data = new FileInput(this, $.extend({}, $.fn.fileinput.defaults, options, $(this).data()))));
  1442. }
  1443. if (typeof option === 'string') {
  1444. data[option].apply(data, args);
  1445. }
  1446. });
  1447. };
  1448. $.fn.fileinput.defaults = {
  1449. showCaption: true,
  1450. showPreview: true,
  1451. showRemove: true,
  1452. showUpload: true,
  1453. showCancel: true,
  1454. autoFitCaption: true,
  1455. mainClass: '',
  1456. previewClass: '',
  1457. captionClass: '',
  1458. mainTemplate: null,
  1459. initialCaption: '',
  1460. initialPreview: '',
  1461. initialPreviewCount: 0,
  1462. initialPreviewDelimiter: '*$$*',
  1463. initialPreviewConfig: [],
  1464. initialPreviewShowDelete: true,
  1465. overwriteInitial: true,
  1466. layoutTemplates: defaultLayoutTemplates,
  1467. previewTemplates: defaultPreviewTemplates,
  1468. allowedPreviewTypes: defaultPreviewTypes,
  1469. allowedPreviewMimeTypes: null,
  1470. allowedFileTypes: null,
  1471. allowedFileExtensions: null,
  1472. previewSettings: defaultPreviewSettings,
  1473. fileTypeSettings: defaultFileTypeSettings,
  1474. browseLabel: 'Browse &hellip;',
  1475. browseIcon: '<i class="glyphicon glyphicon-folder-open"></i> &nbsp;',
  1476. browseClass: 'btn btn-primary',
  1477. removeLabel: 'Remove',
  1478. removeTitle: 'Clear selected files',
  1479. removeIcon: '<i class="glyphicon glyphicon-trash"></i> ',
  1480. removeClass: 'btn btn-default',
  1481. cancelLabel: 'Cancel',
  1482. cancelTitle: 'Abort ongoing upload',
  1483. cancelIcon: '<i class="glyphicon glyphicon-ban-circle"></i> ',
  1484. cancelClass: 'btn btn-default',
  1485. uploadLabel: 'Upload',
  1486. uploadTitle: 'Upload selected files',
  1487. uploadIcon: '<i class="glyphicon glyphicon-upload"></i> ',
  1488. uploadClass: 'btn btn-default',
  1489. uploadUrl: null,
  1490. uploadExtraData: [],
  1491. uploadAsync: true,
  1492. maxFileSize: 0,
  1493. maxFileCount: 0,
  1494. msgSizeTooLarge: 'File "{name}" (<b>{size} KB</b>) exceeds maximum allowed upload size of <b>{maxSize} KB</b>. Please retry your upload!',
  1495. msgFilesTooMany: 'Number of files selected for upload <b>({n})</b> exceeds maximum allowed limit of <b>{m}</b>. Please retry your upload!',
  1496. msgFileNotFound: 'File "{name}" not found!',
  1497. msgFileNotReadable: 'File "{name}" is not readable.',
  1498. msgFilePreviewAborted: 'File preview aborted for "{name}".',
  1499. msgFilePreviewError: 'An error occurred while reading the file "{name}".',
  1500. msgInvalidFileType: 'Invalid type for file "{name}". Only "{types}" files are supported.',
  1501. msgInvalidFileExtension: 'Invalid extension for file "{name}". Only "{extensions}" files are supported.',
  1502. msgValidationError: '<span class="text-danger"><i class="glyphicon glyphicon-exclamation-sign"></i> File Upload Error</span>',
  1503. msgErrorClass: 'file-error-message',
  1504. msgLoading: 'Loading file {index} of {files} &hellip;',
  1505. msgProgress: 'Loading file {index} of {files} - {name} - {percent}% completed.',
  1506. msgSelected: '{n} files selected',
  1507. previewFileType: 'image',
  1508. wrapTextLength: 250,
  1509. wrapIndicator: ' <span class="wrap-indicator" title="{title}" onclick="{dialog}">[&hellip;]</span>',
  1510. elCaptionContainer: null,
  1511. elCaptionText: null,
  1512. elPreviewContainer: null,
  1513. elPreviewImage: null,
  1514. elPreviewStatus: null,
  1515. elErrorContainer: null,
  1516. slugCallback: null,
  1517. dropZoneEnabled: true,
  1518. dropZoneTitle: 'Drag & drop files here &hellip;',
  1519. dropZoneTitleClass: 'file-drop-zone-title',
  1520. fileActionSettings: {}
  1521. };
  1522. /**
  1523. * Convert automatically file inputs with class 'file'
  1524. * into a bootstrap fileinput control.
  1525. */
  1526. $(document).ready(function () {
  1527. var $input = $('input.file[type=file]'), count = $input.attr('type') != null ? $input.length : 0;
  1528. if (count > 0) {
  1529. $input.fileinput();
  1530. }
  1531. });
  1532. $.fn.fileinput.Constructor = FileInput;
  1533. })(window.jQuery);