jasny-bootstrap.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*!
  2. * Jasny Bootstrap v4.0.0 (http://jasny.github.io/bootstrap)
  3. * Copyright 2012-2014 Arnold Daniels
  4. * Licensed under Apache-2.0 (https://github.com/jasny/bootstrap/blob/master/LICENSE)
  5. */
  6. +function ($) { "use strict";
  7. var isIE = window.navigator.appName == 'Microsoft Internet Explorer'
  8. // FILEUPLOAD PUBLIC CLASS DEFINITION
  9. // =================================
  10. var Fileinput = function (element, options) {
  11. this.$element = $(element)
  12. this.options = $.extend({}, Fileinput.DEFAULTS, options)
  13. this.$input = this.$element.find(':file')
  14. if (this.$input.length === 0) return
  15. this.name = this.$input.attr('name') || options.name
  16. this.$hidden = this.$element.find('input[type=hidden][name="' + this.name + '"]')
  17. if (this.$hidden.length === 0) {
  18. this.$hidden = $('<input type="hidden">').insertBefore(this.$input)
  19. }
  20. this.$preview = this.$element.find('.fileinput-preview')
  21. var height = this.$preview.css('height')
  22. if (this.$preview.css('display') !== 'inline' && height !== '0px' && height !== 'none') {
  23. this.$preview.css('line-height', height)
  24. }
  25. this.original = {
  26. exists: this.$element.hasClass('fileinput-exists'),
  27. preview: this.$preview.html(),
  28. hiddenVal: this.$hidden.val()
  29. }
  30. this.listen()
  31. this.reset()
  32. }
  33. Fileinput.DEFAULTS = {
  34. clearName: true
  35. }
  36. Fileinput.prototype.listen = function() {
  37. this.$input.on('change.bs.fileinput', $.proxy(this.change, this))
  38. $(this.$input[0].form).on('reset.bs.fileinput', $.proxy(this.reset, this))
  39. this.$element.find('[data-trigger="fileinput"]').on('click.bs.fileinput', $.proxy(this.trigger, this))
  40. this.$element.find('[data-dismiss="fileinput"]').on('click.bs.fileinput', $.proxy(this.clear, this))
  41. },
  42. Fileinput.prototype.verifySizes = function(files) {
  43. if (typeof this.options.maxSize === 'undefined') return true
  44. var max = parseFloat(this.options.maxSize)
  45. if (max !== this.options.maxSize) return true
  46. for (var i = 0; i < files.length; i++) {
  47. var size = typeof files[i].size !== 'undefined' ? files[i].size : null
  48. if (size === null) continue
  49. size = size / 1000 / 1000 /* convert from bytes to MB */
  50. if (size > max) return false
  51. }
  52. return true
  53. }
  54. Fileinput.prototype.change = function(e) {
  55. var files = e.target.files === undefined ? (e.target && e.target.value ? [{ name: e.target.value.replace(/^.+\\/, '')}] : []) : e.target.files
  56. e.stopPropagation()
  57. if (files.length === 0) {
  58. this.clear()
  59. this.$element.trigger('clear.bs.fileinput')
  60. return
  61. }
  62. if (!this.verifySizes(files)) {
  63. this.$element.trigger('max_size.bs.fileinput')
  64. this.clear()
  65. this.$element.trigger('clear.bs.fileinput')
  66. return
  67. }
  68. this.$hidden.val('')
  69. this.$hidden.attr('name', '')
  70. this.$input.attr('name', this.name)
  71. var file = files[0]
  72. if (this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match(/^image\/(gif|png|jpeg|svg\+xml)$/) : file.name.match(/\.(gif|png|jpe?g|svg)$/i)) && typeof FileReader !== "undefined") {
  73. var Fileinput = this
  74. var reader = new FileReader()
  75. var preview = this.$preview
  76. var element = this.$element
  77. reader.onload = function(re) {
  78. var $img = $('<img>')
  79. $img[0].src = re.target.result
  80. files[0].result = re.target.result
  81. element.find('.fileinput-filename').text(file.name)
  82. // if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account
  83. if (preview.css('max-height') != 'none') {
  84. var mh = parseInt(preview.css('max-height'), 10) || 0
  85. var pt = parseInt(preview.css('padding-top'), 10) || 0
  86. var pb = parseInt(preview.css('padding-bottom'), 10) || 0
  87. var bt = parseInt(preview.css('border-top'), 10) || 0
  88. var bb = parseInt(preview.css('border-bottom'), 10) || 0
  89. $img.css('max-height', mh - pt - pb - bt - bb)
  90. }
  91. preview.html($img)
  92. if (Fileinput.options.exif) {
  93. //Fix image tranformation if this is possible
  94. Fileinput.setImageTransform($img, file);
  95. }
  96. element.addClass('fileinput-exists').removeClass('fileinput-new')
  97. element.trigger('change.bs.fileinput', files)
  98. }
  99. reader.readAsDataURL(file)
  100. } else {
  101. var text = file.name
  102. var $nameView = this.$element.find('.fileinput-filename')
  103. if (files.length > 1) {
  104. text = $.map(files, function(file) {
  105. return file.name;
  106. }).join(', ')
  107. }
  108. $nameView.text(text)
  109. this.$preview.text(file.name)
  110. this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
  111. this.$element.trigger('change.bs.fileinput')
  112. }
  113. },
  114. Fileinput.prototype.setImageTransform = function($img, file) {
  115. var Fileinput = this;
  116. var reader = new FileReader();
  117. reader.onload = function(me) {
  118. var transform = false;
  119. var view = new DataView(reader.result);
  120. var exif = Fileinput.getImageExif(view);
  121. if (exif) {
  122. Fileinput.resetOrientation($img, exif);
  123. }
  124. }
  125. reader.readAsArrayBuffer(file);
  126. }
  127. Fileinput.prototype.getImageExif = function(view) {
  128. if (view.getUint16(0, false) != 0xFFD8) {
  129. return -2;
  130. }
  131. var length = view.byteLength, offset = 2;
  132. while (offset < length) {
  133. var marker = view.getUint16(offset, false);
  134. offset += 2;
  135. if (marker == 0xFFE1) {
  136. if (view.getUint32(offset += 2, false) != 0x45786966) {
  137. return -1;
  138. }
  139. var little = view.getUint16(offset += 6, false) == 0x4949;
  140. offset += view.getUint32(offset + 4, little);
  141. var tags = view.getUint16(offset, little);
  142. offset += 2;
  143. for (var i = 0; i < tags; i++) {
  144. if (view.getUint16(offset + (i * 12), little) == 0x0112) {
  145. return view.getUint16(offset + (i * 12) + 8, little);
  146. }
  147. }
  148. }
  149. else if ((marker & 0xFF00) != 0xFF00){
  150. break;
  151. } else {
  152. offset += view.getUint16(offset, false);
  153. }
  154. }
  155. return -1;
  156. }
  157. Fileinput.prototype.resetOrientation = function($img, transform) {
  158. var img = new Image();
  159. img.onload = function() {
  160. var width = img.width,
  161. height = img.height,
  162. canvas = document.createElement('canvas'),
  163. ctx = canvas.getContext("2d");
  164. // set proper canvas dimensions before transform & export
  165. if ([5,6,7,8].indexOf(transform) > -1) {
  166. canvas.width = height;
  167. canvas.height = width;
  168. } else {
  169. canvas.width = width;
  170. canvas.height = height;
  171. }
  172. // transform context before drawing image
  173. switch (transform) {
  174. case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;
  175. case 3: ctx.transform(-1, 0, 0, -1, width, height ); break;
  176. case 4: ctx.transform(1, 0, 0, -1, 0, height ); break;
  177. case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;
  178. case 6: ctx.transform(0, 1, -1, 0, height , 0); break;
  179. case 7: ctx.transform(0, -1, -1, 0, height , width); break;
  180. case 8: ctx.transform(0, -1, 1, 0, 0, width); break;
  181. default: ctx.transform(1, 0, 0, 1, 0, 0);
  182. }
  183. // draw image
  184. ctx.drawImage(img, 0, 0);
  185. // export base64
  186. $img.attr('src', canvas.toDataURL());
  187. };
  188. img.src = $img.attr('src');
  189. };
  190. Fileinput.prototype.clear = function(e) {
  191. if (e) e.preventDefault()
  192. this.$hidden.val('')
  193. this.$hidden.attr('name', this.name)
  194. if (this.options.clearName) this.$input.attr('name', '')
  195. //ie8+ doesn't support changing the value of input with type=file so clone instead
  196. if (isIE) {
  197. var inputClone = this.$input.clone(true);
  198. this.$input.after(inputClone);
  199. this.$input.remove();
  200. this.$input = inputClone;
  201. } else {
  202. this.$input.val('')
  203. }
  204. this.$preview.html('')
  205. this.$element.find('.fileinput-filename').text('')
  206. this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
  207. if (e !== undefined) {
  208. this.$input.trigger('change')
  209. this.$element.trigger('clear.bs.fileinput')
  210. }
  211. },
  212. Fileinput.prototype.reset = function() {
  213. this.clear()
  214. this.$hidden.val(this.original.hiddenVal)
  215. this.$preview.html(this.original.preview)
  216. this.$element.find('.fileinput-filename').text('')
  217. if (this.original.exists) this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
  218. else this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
  219. this.$element.trigger('reseted.bs.fileinput')
  220. },
  221. Fileinput.prototype.trigger = function(e) {
  222. this.$input.trigger('click')
  223. e.preventDefault()
  224. }
  225. // FILEUPLOAD PLUGIN DEFINITION
  226. // ===========================
  227. var old = $.fn.fileinput
  228. $.fn.fileinput = function (options) {
  229. return this.each(function () {
  230. var $this = $(this),
  231. data = $this.data('bs.fileinput')
  232. if (!data) $this.data('bs.fileinput', (data = new Fileinput(this, options)))
  233. if (typeof options == 'string') data[options]()
  234. })
  235. }
  236. $.fn.fileinput.Constructor = Fileinput
  237. // FILEINPUT NO CONFLICT
  238. // ====================
  239. $.fn.fileinput.noConflict = function () {
  240. $.fn.fileinput = old
  241. return this
  242. }
  243. // FILEUPLOAD DATA-API
  244. // ==================
  245. $(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {
  246. var $this = $(this)
  247. if ($this.data('bs.fileinput')) return
  248. $this.fileinput($this.data())
  249. var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');
  250. if ($target.length > 0) {
  251. e.preventDefault()
  252. $target.trigger('click.bs.fileinput')
  253. }
  254. })
  255. }(window.jQuery);