bootstrap-fileupload.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* ===========================================================
  2. * bootstrap-fileupload.js j2
  3. * http://jasny.github.com/bootstrap/javascript.html#fileupload
  4. * ===========================================================
  5. * Copyright 2012 Jasny BV, Netherlands.
  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 ($) {
  20. "use strict"; // jshint ;_
  21. /* FILEUPLOAD PUBLIC CLASS DEFINITION
  22. * ================================= */
  23. var Fileupload = function (element, options) {
  24. this.$element = $(element)
  25. this.type = this.$element.data('uploadtype') || (this.$element.find('.thumbnail').length > 0 ? "image" : "file")
  26. this.$input = this.$element.find(':file')
  27. if (this.$input.length === 0) return
  28. this.name = this.$input.attr('name') || options.name
  29. this.$hidden = this.$element.find('input[type=hidden][name="'+this.name+'"]')
  30. if (this.$hidden.length === 0) {
  31. this.$hidden = $('<input type="hidden" />')
  32. this.$element.prepend(this.$hidden)
  33. }
  34. this.$preview = this.$element.find('.fileupload-preview')
  35. var height = this.$preview.css('height')
  36. if (this.$preview.css('display') != 'inline' && height != '0px' && height != 'none') this.$preview.css('line-height', height)
  37. this.original = {
  38. 'exists': this.$element.hasClass('fileupload-exists'),
  39. 'preview': this.$preview.html(),
  40. 'hiddenVal': this.$hidden.val()
  41. }
  42. this.$remove = this.$element.find('[data-dismiss="fileupload"]')
  43. this.$element.find('[data-trigger="fileupload"]').on('click.fileupload', $.proxy(this.trigger, this))
  44. this.listen()
  45. }
  46. Fileupload.prototype = {
  47. listen: function() {
  48. this.$input.on('change.fileupload', $.proxy(this.change, this))
  49. $(this.$input[0].form).on('reset.fileupload', $.proxy(this.reset, this))
  50. if (this.$remove) this.$remove.on('click.fileupload', $.proxy(this.clear, this))
  51. },
  52. change: function(e, invoked) {
  53. var file = e.target.files !== undefined ? e.target.files[0] : (e.target.value ? { name: e.target.value.replace(/^.+\\/, '') } : null)
  54. if (invoked === 'clear') return
  55. if (!file) {
  56. this.clear()
  57. return
  58. }
  59. this.$hidden.val('')
  60. this.$hidden.attr('name', '')
  61. this.$input.attr('name', this.name)
  62. if (this.type === "image" && this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match('image.*') : file.name.match('\\.(gif|png|jpe?g)$')) && typeof FileReader !== "undefined") {
  63. var reader = new FileReader()
  64. var preview = this.$preview
  65. var element = this.$element
  66. reader.onload = function(e) {
  67. preview.html('<img src="' + e.target.result + '" ' + (preview.css('max-height') != 'none' ? 'style="max-height: ' + preview.css('max-height') + ';"' : '') + ' />')
  68. element.addClass('fileupload-exists').removeClass('fileupload-new')
  69. }
  70. reader.readAsDataURL(file)
  71. } else {
  72. this.$preview.text(file.name)
  73. this.$element.addClass('fileupload-exists').removeClass('fileupload-new')
  74. }
  75. },
  76. clear: function(e) {
  77. this.$hidden.val('')
  78. this.$hidden.attr('name', this.name)
  79. this.$input.attr('name', '')
  80. //ie8+ doesn't support changing the value of input with type=file so clone instead
  81. if($.browser.msie){
  82. var inputClone = this.$input.clone(true);
  83. this.$input.after(inputClone);
  84. this.$input.remove();
  85. this.$input = inputClone;
  86. }else{
  87. this.$input.val('')
  88. }
  89. this.$preview.html('')
  90. this.$element.addClass('fileupload-new').removeClass('fileupload-exists')
  91. if (e) {
  92. this.$input.trigger('change', [ 'clear' ])
  93. e.preventDefault()
  94. }
  95. },
  96. reset: function(e) {
  97. this.clear()
  98. this.$hidden.val(this.original.hiddenVal)
  99. this.$preview.html(this.original.preview)
  100. if (this.original.exists) this.$element.addClass('fileupload-exists').removeClass('fileupload-new')
  101. else this.$element.addClass('fileupload-new').removeClass('fileupload-exists')
  102. },
  103. trigger: function(e) {
  104. this.$input.trigger('click')
  105. e.preventDefault()
  106. }
  107. }
  108. /* FILEUPLOAD PLUGIN DEFINITION
  109. * =========================== */
  110. $.fn.fileupload = function (options) {
  111. return this.each(function () {
  112. var $this = $(this)
  113. , data = $this.data('fileupload')
  114. if (!data) $this.data('fileupload', (data = new Fileupload(this, options)))
  115. if (typeof options == 'string') data[options]()
  116. })
  117. }
  118. $.fn.fileupload.Constructor = Fileupload
  119. /* FILEUPLOAD DATA-API
  120. * ================== */
  121. $(function () {
  122. $('body').on('click.fileupload.data-api', '[data-provides="fileupload"]', function (e) {
  123. var $this = $(this)
  124. if ($this.data('fileupload')) return
  125. $this.fileupload($this.data())
  126. var $target = $(e.target).is('[data-dismiss=fileupload],[data-trigger=fileupload]') ?
  127. $(e.target) : $(e.target).parents('[data-dismiss=fileupload],[data-trigger=fileupload]').first()
  128. if ($target.length > 0) {
  129. $target.trigger('click.fileupload')
  130. e.preventDefault()
  131. }
  132. })
  133. })
  134. }(window.jQuery);