Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

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