uikit_fileinput.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /*
  20. * uikit version
  21. * author tzd
  22. */
  23. +function ($) { "use strict";
  24. var isIE = window.navigator.appName == 'Microsoft Internet Explorer';
  25. // FILEUPLOAD PUBLIC CLASS DEFINITION
  26. // =================================
  27. var Fileinput = function (element, options) {
  28. this.$element = $(element);
  29. this.$input = this.$element.find(':file');
  30. if (this.$input.length === 0) return;
  31. this.name = this.$input.attr('name') || options.name;
  32. this.$hidden = this.$element.find('input[type=hidden][name="' + this.name + '"]');
  33. if (this.$hidden.length === 0) {
  34. this.$hidden = $('<input type="hidden">').insertBefore(this.$input)
  35. }
  36. this.$preview = this.$element.find('.fileinput-preview');
  37. var height = this.$preview.css('height');
  38. if (this.$preview.css('display') !== 'inline' && height !== '0px' && height !== 'none') {
  39. this.$preview.css('line-height', height)
  40. }
  41. this.original = {
  42. exists: this.$element.hasClass('fileinput-exists'),
  43. preview: this.$preview.html(),
  44. hiddenVal: this.$hidden.val()
  45. };
  46. this.listen()
  47. };
  48. Fileinput.prototype.listen = function() {
  49. this.$input.on('change.uk.fileinput', $.proxy(this.change, this));
  50. $(this.$input[0].form).on('reset.uk.fileinput', $.proxy(this.reset, this));
  51. this.$element.find('[data-trigger="fileinput"]').on('click.uk.fileinput', $.proxy(this.trigger, this));
  52. this.$element.find('[data-dismiss="fileinput"]').on('click.uk.fileinput', $.proxy(this.clear, this))
  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.uk.fileinput');
  60. return
  61. }
  62. this.$hidden.val('');
  63. this.$hidden.attr('name', '');
  64. this.$input.attr('name', this.name);
  65. var file = files[0];
  66. 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") {
  67. var reader = new FileReader();
  68. var preview = this.$preview;
  69. var element = this.$element;
  70. reader.onload = function(re) {
  71. var $img = $('<img>');
  72. $img[0].src = re.target.result;
  73. files[0].result = re.target.result;
  74. element.find('.fileinput-filename').text(file.name);
  75. // if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account
  76. 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));
  77. preview.html($img);
  78. element.addClass('fileinput-exists').removeClass('fileinput-new');
  79. element.trigger('change.uk.fileinput', files)
  80. };
  81. reader.readAsDataURL(file)
  82. } else {
  83. this.$element.find('.fileinput-filename').text(file.name);
  84. this.$preview.text(file.name);
  85. this.$element.addClass('fileinput-exists').removeClass('fileinput-new');
  86. this.$element.trigger('change.uk.fileinput')
  87. }
  88. };
  89. Fileinput.prototype.clear = function(e) {
  90. if (e) e.preventDefault();
  91. this.$hidden.val('');
  92. this.$hidden.attr('name', this.name);
  93. this.$input.attr('name', '');
  94. //ie8+ doesn't support changing the value of input with type=file so clone instead
  95. if (isIE) {
  96. var inputClone = this.$input.clone(true);
  97. this.$input.after(inputClone);
  98. this.$input.remove();
  99. this.$input = inputClone;
  100. } else {
  101. this.$input.val('');
  102. }
  103. this.$preview.html('');
  104. this.$element.find('.fileinput-filename').text('');
  105. this.$element.addClass('fileinput-new').removeClass('fileinput-exists');
  106. if (e !== undefined) {
  107. this.$input.trigger('change');
  108. this.$element.trigger('clear.uk.fileinput');
  109. }
  110. };
  111. Fileinput.prototype.reset = function() {
  112. this.clear();
  113. this.$hidden.val(this.original.hiddenVal);
  114. this.$preview.html(this.original.preview);
  115. this.$element.find('.fileinput-filename').text('');
  116. if (this.original.exists) {
  117. this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
  118. } else {
  119. this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
  120. }
  121. this.$element.trigger('reset.uk.fileinput')
  122. };
  123. Fileinput.prototype.trigger = function(e) {
  124. this.$input.trigger('click');
  125. e.preventDefault()
  126. };
  127. // FILEUPLOAD PLUGIN DEFINITION
  128. // ===========================
  129. var old = $.fn.fileinput;
  130. $.fn.fileinput = function (options) {
  131. return this.each(function () {
  132. var $this = $(this),
  133. data = $this.data('uk.fileinput');
  134. if (!data) $this.data('uk.fileinput', (data = new Fileinput(this, options)));
  135. if (typeof options == 'string') data[options]()
  136. })
  137. };
  138. $.fn.fileinput.Constructor = Fileinput;
  139. // FILEINPUT NO CONFLICT
  140. // ====================
  141. $.fn.fileinput.noConflict = function () {
  142. $.fn.fileinput = old;
  143. return this
  144. };
  145. // FILEUPLOAD DATA-API
  146. // ==================
  147. $(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {
  148. var $this = $(this);
  149. if ($this.data('uk.fileinput')) return;
  150. $this.fileinput($this.data());
  151. var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');
  152. if ($target.length > 0) {
  153. e.preventDefault();
  154. $target.trigger('click.uk.fileinput');
  155. }
  156. })
  157. }(window.jQuery);