PageRenderTime 44ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/public/assets/global/plugins/bootstrap-fileinput/bootstrap-fileinput.js

https://gitlab.com/lara_intern/BarcodeTechSolution
JavaScript | 198 lines | 121 code | 49 blank | 28 comment | 26 complexity | 381d041a01675fc3d8907794fe65bee6 MD5 | raw file
  1. /* ===========================================================
  2. * Bootstrap: fileinput.js v3.1.3
  3. * http://jasny.github.com/bootstrap/javascript/#fileinput
  4. * ===========================================================
  5. * Copyright 2012-2014 Arnold Daniels
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License")
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ========================================================== */
  19. +function ($) { "use strict";
  20. var isIE = window.navigator.appName == 'Microsoft Internet Explorer'
  21. // FILEUPLOAD PUBLIC CLASS DEFINITION
  22. // =================================
  23. var Fileinput = function (element, options) {
  24. this.$element = $(element)
  25. this.$input = this.$element.find(':file')
  26. if (this.$input.length === 0) return
  27. this.name = this.$input.attr('name') || options.name
  28. this.$hidden = this.$element.find('input[type=hidden][name="' + this.name + '"]')
  29. if (this.$hidden.length === 0) {
  30. this.$hidden = $('<input type="hidden">').insertBefore(this.$input)
  31. }
  32. this.$preview = this.$element.find('.fileinput-preview')
  33. var height = this.$preview.css('height')
  34. if (this.$preview.css('display') !== 'inline' && height !== '0px' && height !== 'none') {
  35. this.$preview.css('line-height', height)
  36. }
  37. this.original = {
  38. exists: this.$element.hasClass('fileinput-exists'),
  39. preview: this.$preview.html(),
  40. hiddenVal: this.$hidden.val()
  41. }
  42. this.listen()
  43. }
  44. Fileinput.prototype.listen = function() {
  45. this.$input.on('change.bs.fileinput', $.proxy(this.change, this))
  46. $(this.$input[0].form).on('reset.bs.fileinput', $.proxy(this.reset, this))
  47. this.$element.find('[data-trigger="fileinput"]').on('click.bs.fileinput', $.proxy(this.trigger, this))
  48. this.$element.find('[data-dismiss="fileinput"]').on('click.bs.fileinput', $.proxy(this.clear, this))
  49. },
  50. Fileinput.prototype.change = function(e) {
  51. var files = e.target.files === undefined ? (e.target && e.target.value ? [{ name: e.target.value.replace(/^.+\\/, '')}] : []) : e.target.files
  52. e.stopPropagation()
  53. if (files.length === 0) {
  54. this.clear()
  55. return
  56. }
  57. this.$hidden.val('')
  58. this.$hidden.attr('name', '')
  59. this.$input.attr('name', this.name)
  60. var file = files[0]
  61. if (this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match(/^image\/(gif|png|jpeg)$/) : file.name.match(/\.(gif|png|jpe?g)$/i)) && typeof FileReader !== "undefined") {
  62. var reader = new FileReader()
  63. var preview = this.$preview
  64. var element = this.$element
  65. reader.onload = function(re) {
  66. var $img = $('<img>')
  67. $img[0].src = re.target.result
  68. files[0].result = re.target.result
  69. element.find('.fileinput-filename').text(file.name)
  70. // if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account
  71. if (preview.css('max-height') != 'none') $img.css('max-height', parseInt(preview.css('max-height'), 10) - parseInt(preview.css('padding-top'), 10) - parseInt(preview.css('padding-bottom'), 10) - parseInt(preview.css('border-top'), 10) - parseInt(preview.css('border-bottom'), 10))
  72. preview.html($img)
  73. element.addClass('fileinput-exists').removeClass('fileinput-new')
  74. element.trigger('change.bs.fileinput', files)
  75. }
  76. reader.readAsDataURL(file)
  77. } else {
  78. this.$element.find('.fileinput-filename').text(file.name)
  79. this.$preview.text(file.name)
  80. this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
  81. this.$element.trigger('change.bs.fileinput')
  82. }
  83. },
  84. Fileinput.prototype.clear = function(e) {
  85. if (e) e.preventDefault()
  86. this.$hidden.val('')
  87. this.$hidden.attr('name', this.name)
  88. this.$input.attr('name', '')
  89. //ie8+ doesn't support changing the value of input with type=file so clone instead
  90. if (isIE) {
  91. var inputClone = this.$input.clone(true);
  92. this.$input.after(inputClone);
  93. this.$input.remove();
  94. this.$input = inputClone;
  95. } else {
  96. this.$input.val('')
  97. }
  98. this.$preview.html('')
  99. this.$element.find('.fileinput-filename').text('')
  100. this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
  101. if (e !== undefined) {
  102. this.$input.trigger('change')
  103. this.$element.trigger('clear.bs.fileinput')
  104. }
  105. },
  106. Fileinput.prototype.reset = function() {
  107. this.clear()
  108. this.$hidden.val(this.original.hiddenVal)
  109. this.$preview.html(this.original.preview)
  110. this.$element.find('.fileinput-filename').text('')
  111. if (this.original.exists) this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
  112. else this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
  113. this.$element.trigger('reset.bs.fileinput')
  114. },
  115. Fileinput.prototype.trigger = function(e) {
  116. this.$input.trigger('click')
  117. e.preventDefault()
  118. }
  119. // FILEUPLOAD PLUGIN DEFINITION
  120. // ===========================
  121. var old = $.fn.fileinput
  122. $.fn.fileinput = function (options) {
  123. return this.each(function () {
  124. var $this = $(this),
  125. data = $this.data('bs.fileinput')
  126. if (!data) $this.data('bs.fileinput', (data = new Fileinput(this, options)))
  127. if (typeof options == 'string') data[options]()
  128. })
  129. }
  130. $.fn.fileinput.Constructor = Fileinput
  131. // FILEINPUT NO CONFLICT
  132. // ====================
  133. $.fn.fileinput.noConflict = function () {
  134. $.fn.fileinput = old
  135. return this
  136. }
  137. // FILEUPLOAD DATA-API
  138. // ==================
  139. $(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {
  140. var $this = $(this)
  141. if ($this.data('bs.fileinput')) return
  142. $this.fileinput($this.data())
  143. var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');
  144. if ($target.length > 0) {
  145. e.preventDefault()
  146. $target.trigger('click.bs.fileinput')
  147. }
  148. })
  149. }(window.jQuery);