Project

General

Profile

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
/*
21
 * uikit version
22
 * author tzd
23
*/
24

    
25
+function ($) { "use strict";
26

    
27
    var isIE = window.navigator.appName == 'Microsoft Internet Explorer';
28

    
29
    // FILEUPLOAD PUBLIC CLASS DEFINITION
30
    // =================================
31

    
32
    var Fileinput = function (element, options) {
33
        this.$element = $(element);
34

    
35
        this.$input = this.$element.find(':file');
36
        if (this.$input.length === 0) return;
37

    
38
        this.name = this.$input.attr('name') || options.name;
39

    
40
        this.$hidden = this.$element.find('input[type=hidden][name="' + this.name + '"]');
41
        if (this.$hidden.length === 0) {
42
            this.$hidden = $('<input type="hidden">').insertBefore(this.$input)
43
        }
44

    
45
        this.$preview = this.$element.find('.fileinput-preview');
46
        var height = this.$preview.css('height');
47
        if (this.$preview.css('display') !== 'inline' && height !== '0px' && height !== 'none') {
48
            this.$preview.css('line-height', height)
49
        }
50

    
51
        this.original = {
52
            exists: this.$element.hasClass('fileinput-exists'),
53
            preview: this.$preview.html(),
54
            hiddenVal: this.$hidden.val()
55
        };
56

    
57
        this.listen()
58
    };
59

    
60
    Fileinput.prototype.listen = function() {
61
        this.$input.on('change.uk.fileinput', $.proxy(this.change, this));
62
        $(this.$input[0].form).on('reset.uk.fileinput', $.proxy(this.reset, this));
63

    
64
        this.$element.find('[data-trigger="fileinput"]').on('click.uk.fileinput', $.proxy(this.trigger, this));
65
        this.$element.find('[data-dismiss="fileinput"]').on('click.uk.fileinput', $.proxy(this.clear, this))
66
    };
67

    
68
    Fileinput.prototype.change = function(e) {
69
        var files = e.target.files === undefined ? (e.target && e.target.value ? [{ name: e.target.value.replace(/^.+\\/, '')}] : []) : e.target.files;
70

    
71
        e.stopPropagation();
72

    
73
        if (files.length === 0) {
74
            this.clear();
75
            this.$element.trigger('clear.uk.fileinput');
76
            return
77
        }
78

    
79
        this.$hidden.val('');
80
        this.$hidden.attr('name', '');
81
        this.$input.attr('name', this.name);
82

    
83
        var file = files[0];
84

    
85
        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") {
86
            var reader = new FileReader();
87
            var preview = this.$preview;
88
            var element = this.$element;
89

    
90
            reader.onload = function(re) {
91
                var $img = $('<img>');
92
                $img[0].src = re.target.result;
93
                files[0].result = re.target.result;
94

    
95
                element.find('.fileinput-filename').text(file.name);
96

    
97
                // if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account
98
                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));
99

    
100
                preview.html($img);
101
                element.addClass('fileinput-exists').removeClass('fileinput-new');
102

    
103
                element.trigger('change.uk.fileinput', files)
104
            };
105

    
106
            reader.readAsDataURL(file)
107
        } else {
108
            this.$element.find('.fileinput-filename').text(file.name);
109
            this.$preview.text(file.name);
110

    
111
            this.$element.addClass('fileinput-exists').removeClass('fileinput-new');
112

    
113
            this.$element.trigger('change.uk.fileinput')
114
        }
115
    };
116

    
117
    Fileinput.prototype.clear = function(e) {
118
        if (e) e.preventDefault();
119

    
120
        this.$hidden.val('');
121
        this.$hidden.attr('name', this.name);
122
        this.$input.attr('name', '');
123

    
124
        //ie8+ doesn't support changing the value of input with type=file so clone instead
125
        if (isIE) {
126
            var inputClone = this.$input.clone(true);
127
            this.$input.after(inputClone);
128
            this.$input.remove();
129
            this.$input = inputClone;
130
        } else {
131
            this.$input.val('');
132
        }
133

    
134
        this.$preview.html('');
135
        this.$element.find('.fileinput-filename').text('');
136
        this.$element.addClass('fileinput-new').removeClass('fileinput-exists');
137

    
138
        if (e !== undefined) {
139
            this.$input.trigger('change');
140
            this.$element.trigger('clear.uk.fileinput');
141
        }
142
    };
143

    
144
    Fileinput.prototype.reset = function() {
145
        this.clear();
146

    
147
        this.$hidden.val(this.original.hiddenVal);
148
        this.$preview.html(this.original.preview);
149
        this.$element.find('.fileinput-filename').text('');
150

    
151
        if (this.original.exists) {
152
            this.$element.addClass('fileinput-exists').removeClass('fileinput-new')    
153
        } else {
154
            this.$element.addClass('fileinput-new').removeClass('fileinput-exists')  
155
        } 
156

    
157
        this.$element.trigger('reset.uk.fileinput')
158
    };
159

    
160
    Fileinput.prototype.trigger = function(e) {
161
        this.$input.trigger('click');
162
        e.preventDefault()
163
    };
164

    
165
    // FILEUPLOAD PLUGIN DEFINITION
166
    // ===========================
167

    
168
    var old = $.fn.fileinput;
169

    
170
    $.fn.fileinput = function (options) {
171
        return this.each(function () {
172
            var $this = $(this),
173
                data = $this.data('uk.fileinput');
174
            if (!data) $this.data('uk.fileinput', (data = new Fileinput(this, options)));
175
            if (typeof options == 'string') data[options]()
176
        })
177
    };
178

    
179
    $.fn.fileinput.Constructor = Fileinput;
180

    
181
    // FILEINPUT NO CONFLICT
182
    // ====================
183

    
184
    $.fn.fileinput.noConflict = function () {
185
        $.fn.fileinput = old;
186
        return this
187
    };
188

    
189
    // FILEUPLOAD DATA-API
190
    // ==================
191

    
192
    $(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {
193
        var $this = $(this);
194
        if ($this.data('uk.fileinput')) return;
195
        $this.fileinput($this.data());
196

    
197
        var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');
198
        if ($target.length > 0) {
199
            e.preventDefault();
200
            $target.trigger('click.uk.fileinput');
201
        }
202
    })
203

    
204
}(window.jQuery);
(20-20/27)