Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6056 efrain 1
(function ($) {
2
    "use strict";
3
 
4
    // This set of validators requires the File API, so if we'ere in a browser
5
    // that isn't sufficiently "HTML5"-y, don't even bother creating them.  It'll
6
    // do no good, so we just automatically pass those tests.
7
    var is_supported_browser = !!window.File,
8
        fileSizeToBytes,
9
        formatter = $.validator.format;
10
 
11
    /**
12
     * Converts a measure of data size from a given unit to bytes.
13
     *
14
     * @param number size
15
     *   A measure of data size, in the give unit
16
     * @param string unit
17
     *   A unit of data.  Valid inputs are "B", "KB", "MB", "GB", "TB"
18
     *
19
     * @return number|bool
20
     *   The number of bytes in the above size/unit combo.  If an
21
     *   invalid unit is specified, false is returned
22
     */
23
    fileSizeToBytes = (function () {
24
 
25
        var units = ["B", "KB", "MB", "GB", "TB"];
26
 
27
        return function (size, unit) {
28
 
29
            var index_of_unit = units.indexOf(unit),
30
                coverted_size;
31
 
32
            if (index_of_unit === -1) {
33
 
34
                coverted_size = false;
35
 
36
            } else {
37
 
38
                while (index_of_unit > 0) {
39
                    size *= 1024;
40
                    index_of_unit -= 1;
41
                }
42
 
43
                coverted_size = size;
44
            }
45
 
46
            return coverted_size;
47
        };
48
    }());
49
 
50
    /**
51
     * Validates that an uploaded file is of a given file type, tested
52
     * by it's reported mime string.
53
     *
54
     * @param obj params
55
     *   An optional set of configuration parmeters.  Supported options are:
56
     *    "types" : array (default ["text"])
57
     *      An array of file types.  This types are loosely checked, so including
58
     *      "text" in this array of types will cause "text/plain" and "text/css"
59
     *      to both be excepted.  If the selected file matches any of the strings
60
     *      in this array, validation passes.
61
     */
62
    $.validator.addMethod(
63
        "fileType",
64
        function (value, element, params) {
65
 
66
            var files,
67
                types = params.types || ["text"],
68
                is_valid = false;
69
 
70
            if (!is_supported_browser || this.optional(element)) {
71
 
72
                is_valid = true;
73
 
74
            } else {
75
 
76
                files = element.files;
77
 
78
                if (files.length < 1) {
79
 
80
                    is_valid = false;
81
 
82
                } else {
83
 
84
                    $.each(types, function (key, value) {
85
                        is_valid = is_valid || files[0].type.indexOf(value) !== -1;
86
                    });
87
 
88
                }
89
            }
90
 
91
            return is_valid;
92
        },
93
        function (params, element) {
94
            return formatter(
95
                "File must be one of the following types: {0}.",
96
                params.types.join(",")
97
            );
98
        }
99
    );
100
 
101
    /**
102
     * Validates that a file selected for upload is at least a given
103
     * file size.
104
     *
105
     * @param obj params
106
     *   An optional set of configuration parameters.  Supported options are:
107
     *     "unit" : string (default "KB")
108
     *       The unit of measure of the file size limit is in.  Valid inputs
109
     *       are "B", "KB", "MB" and "GB"
110
     *     "size" : number (default 100)
111
     *        The minimum size of the file, in the above units, that the file
112
     *        must be, to be accepted as "valid"
113
     */
114
    $.validator.addMethod(
115
        "minFileSize",
116
        function (value, element, params) {
117
 
118
            var files,
119
                unit = params.unit || "KB",
120
                size = params.size || 100,
121
                min_file_size = fileSizeToBytes(size, unit),
122
                is_valid = false;
123
 
124
            if (!is_supported_browser || this.optional(element)) {
125
 
126
                is_valid = true;
127
 
128
            } else {
129
 
130
                files = element.files;
131
 
132
                if (files.length < 1) {
133
 
134
                    is_valid = false;
135
 
136
                } else {
137
 
138
                    is_valid = files[0].size >= min_file_size;
139
 
140
                }
141
            }
142
 
143
            return is_valid;
144
        },
145
        function (params, element) {
146
            return formatter(
147
                "File must be at least {0}{1} large.",
148
                [params.size || 100, params.unit || "KB"]
149
            );
150
        }
151
    );
152
 
153
    /**
154
     * Validates that a file selected for upload is no loarger than a given
155
     * file size.
156
     *
157
     * @param obj params
158
     *   An optional set of configuration parameters.  Supported options are:
159
     *     "unit" : string (default "KB")
160
     *       The unit of measure of the file size limit is in.  Valid inputs
161
     *       are "B", "KB", "MB" and "GB"
162
     *     "size" : number (default 100)
163
     *        The maximum size of the file, in the above units, that the file
164
     *        can be to be accepted as "valid"
165
     */
166
    $.validator.addMethod(
167
        "maxFileSize",
168
        function (value, element, params) {
169
 
170
            var files,
171
                unit = params.unit || "KB",
172
                size = params.size || 100,
173
                max_file_size = fileSizeToBytes(size, unit),
174
                is_valid = false;
175
 
176
            if (!is_supported_browser || this.optional(element)) {
177
 
178
                is_valid = true;
179
 
180
            } else {
181
 
182
                files = element.files;
183
 
184
                if (files.length < 1) {
185
 
186
                    is_valid = false;
187
 
188
                } else {
189
 
190
                    is_valid = files[0].size <= max_file_size;
191
 
192
                }
193
            }
194
 
195
            return is_valid;
196
        },
197
        function (params, element) {
198
            return formatter(
199
                "File cannot be larger than {0}{1}.",
200
                [params.size || 100, params.unit || "KB"]
201
            );
202
        }
203
    );
204
 
205
}(jQuery));