PageSourceSearch

https://shitucompany.cn/vendors/bower_components/bootstrap-filestyle/src/bootstrap-filestyle.js

js shitucompany.cn collected 2026-10-02 16:09:38 UTC 9,314 bytes, 338 lines download raw bytes

1/*
2 * bootstrap-filestyle
3 * doc: http://markusslima.github.io/bootstrap-filestyle/
4 * github: https://github.com/markusslima/bootstrap-filestyle
5 *
6 * Copyright (c) 2014 Markus Vinicius da Silva Lima
7 * Version 1.1.2
8 * Licensed under the MIT license.
9 */
10(function($) {"use strict";
11
12	var Filestyle = function(element, options) {
13		this.options = options;
14		this.$elementFilestyle = [];
15		this.$element = $(element);
16	};
17
18	Filestyle.prototype = {
19		clear : function() {
20			this.$element.val('');
21			this.$elementFilestyle.find(':text').val('');
22			this.$elementFilestyle.find('.badge').remove();
23		},
24
25		destroy : function() {
26			this.$element.removeAttr('style').removeData('filestyle').val('');
27			this.$elementFilestyle.remove();
28		},
29
30		disabled : function(value) {
31			if (value === true) {
32				if (!this.options.disabled) {
33					this.$element.attr('disabled', 'true');
34					this.$elementFilestyle.find('label').attr('disabled', 'true');
35					this.options.disabled = true;
36				}
37			} else if (value === false) {
38				if (this.options.disabled) {
39					this.$element.removeAttr('disabled');
40					this.$elementFilestyle.find('label').removeAttr('disabled');
41					this.options.disabled = false;
42				}
43			} else {
44				return this.options.disabled;
45			}
46		},
47
48		buttonBefore : function(value) {
49			if (value === true) {
50				if (!this.options.buttonBefore) {
51					this.options.buttonBefore = true;
52					if (this.options.input) {
53						this.$elementFilestyle.remove();
54						this.constructor();
55						this.pushNameFiles();
56					}
57				}
58			} else if (value === false) {
59				if (this.options.buttonBefore) {
60					this.options.buttonBefore = false;
61					if (this.options.input) {
62						this.$elementFilestyle.remove();
63						this.constructor();
64						this.pushNameFiles();
65					}
66				}
67			} else {
68				return this.options.buttonBefore;
69			}
70		},
71
72		icon : function(value) {
73			if (value === true) {
74				if (!this.options.icon) {
75					this.options.icon = true;
76					this.$elementFilestyle.find('label').prepend(this.htmlIcon());
77				}
78			} else if (value === false) {
79				if (this.options.icon) {
80					this.options.icon = false;
81					this.$elementFilestyle.find('.glyphicon').remove();
82				}
83			} else {
84				return this.options.icon;
85			}
86		},
87
88		input : function(value) {
89			if (value === true) {
90				if (!this.options.input) {
91					this.options.input = true;
92
93					if (this.options.buttonBefore) {
94						this.$elementFilestyle.append(this.htmlInput());
95					} else {
96						this.$elementFilestyle.prepend(this.htmlInput());
97					}
98
99					this.$elementFilestyle.find('.badge').remove();
100
101					this.pushNameFiles();
102
103					this.$elementFilestyle.find('.group-span-filestyle').addClass('input-group-btn');
104				}
105			} else if (value === false) {
106				if (this.options.input) {
107					this.options.input = false;
108					this.$elementFilestyle.find(':text').remove();
109					var files = this.pushNameFiles();
110					if (files.length > 0 && this.options.badge) {
111						this.$elementFilestyle.find('label').append(' <span class="badge">' + files.length + '</span>');
112					}
113					this.$elementFilestyle.find('.group-span-filestyle').removeClass('input-group-btn');
114				}
115			} else {
116				return this.options.input;
117			}
118		},
119
120		size : function(value) {
121			if (value !== undefined) {
122				var btn = this.$elementFilestyle.find('label'), input = this.$elementFilestyle.find('input');
123
124				btn.removeClass('btn-lg btn-sm');
125				input.removeClass('input-lg input-sm');
126				if (value != 'nr') {
127					btn.addClass('btn-' + value);
128					input.addClass('input-' + value);
129				}
130			} else {
131				return this.options.size;
132			}
133		},
134
135		buttonText : function(value) {
136			if (value !== undefined) {
137				this.options.buttonText = value;
138				this.$elementFilestyle.find('label span').html(this.options.buttonText);
139			} else {
140				return this.options.buttonText;
141			}
142		},
143
144		buttonName : function(value) {
145			if (value !== undefined) {
146				this.options.buttonName = value;
147				this.$elementFilestyle.find('label').attr({
148					'class' : 'btn ' + this.options.buttonName
149				});
150			} else {
151				return this.options.buttonName;
152			}
153		},
154
155		iconName : function(value) {
156			if (value !== undefined) {
157				this.$elementFilestyle.find('.glyphicon').attr({
158					'class' : '.glyphicon ' + this.options.iconName
159				});
160			} else {
161				return this.options.iconName;
162			}
163		},
164
165		htmlIcon : function() {
166			if (this.options.icon) {
167				return '<span class="glyphicon ' + this.options.iconName + '"></span> ';
168			} else {
169				return '';
170			}
171		},
172
173		htmlInput : function() {
174			if (this.options.input) {
175				return '<input type="text" class="form-control ' + (this.options.size == 'nr' ? '' : 'input-' + this.options.size) + '" disabled> ';
176			} else {
177				return '';
178			}
179		},
180
181		// puts the name of the input files
182		// return files
183		pushNameFiles : function() {
184			var content = '', files = [];
185			if (this.$element[0].files === undefined) {
186				files[0] = {
187					'name' : this.$element[0] && this.$element[0].value
188				};
189			} else {
190				files = this.$element[0].files;
191			}
192
193			for (var i = 0; i < files.length; i++) {
194				content += files[i].name.split("\\").pop() + ', ';
195			}
196
197			if (content !== '') {
198				this.$elementFilestyle.find(':text').val(content.replace(/\, $/g, ''));
199			} else {
200				this.$elementFilestyle.find(':text').val('');
201			}
202			
203			return files;
204		},
205
206		constructor : function() {
207			var _self = this, 
208				html = '', 
209				id = _self.$element.attr('id'), 
210				files = [], 
211				btn = '', 
212				$label;
213
214			if (id === '' || !id) {
215				id = 'filestyle-' + $('.bootstrap-filestyle').length;
216				_self.$element.attr({
217					'id' : id
218				});
219			}
220
221			btn = '<span class="group-span-filestyle ' + (_self.options.input ? 'input-group-btn' : '') + '">' + 
222				  '<label for="' + id + '" class="btn ' + _self.options.buttonName + ' ' + 
223				  	(_self.options.size == 'nr' ? '' : 'btn-' + _self.options.size) + '" ' + 
224				  	(_self.options.disabled ? 'disabled="true"' : '') + '>
224' + 
225				  		_self.htmlIcon() + _self.options.buttonText + 
226				  '</label>' + 
227				  '</span>';
228
229			html = _self.options.buttonBefore ? btn + _self.htmlInput() : _self.htmlInput() + btn;
230
231			_self.$elementFilestyle = $('<div class="bootstrap-filestyle input-group">' + html + '</div>');
232			_self.$elementFilestyle.find('.group-span-filestyle').attr('tabindex', "0").keypress(function(e) {
233				if (e.keyCode === 13 || e.charCode === 32) {
234					_self.$elementFilestyle.find('label').click();
235					return false;
236				}
237			});
238
239			// hidding input file and add filestyle
240			_self.$element.css({
241				'position' : 'absolute',
242				'clip' : 'rect(0px 0px 0px 0px)' // using 0px for work in IE8
243			}).attr('tabindex', "-1").after(_self.$elementFilestyle);
244
245			if (_self.options.disabled) {
246				_self.$element.attr('disabled', 'true');
247			}
248
249			// Getting input file value
250			_self.$element.change(function() {
251				var files = _self.pushNameFiles();
252
253				if (_self.options.input == false && _self.options.badge) {
254					if (_self.$elementFilestyle.find('.badge').length == 0) {
255						_self.$elementFilestyle.find('label').append(' <span class="badge">' + files.length + '</span>');
256					} else if (files.length == 0) {
257						_self.$elementFilestyle.find('.badge').remove();
258					} else {
259						_self.$elementFilestyle.find('.badge').html(files.length);
260					}
261				} else {
262					_self.$elementFilestyle.find('.badge').remove();
263				}
264			});
265
266			// Check if browser is Firefox
267			if (window.navigator.userAgent.search(/firefox/i) > -1) {
268				// Simulating choose file for firefox
269				_self.$elementFilestyle.find('label').click(function() {
270					_self.$element.click();
271					return false;
272				});
273			}
274		}
275	};
276
277	var old = $.fn.filestyle;
278
279	$.fn.filestyle = function(option, value) {
280		var get = '', element = this.each(function() {
281			if ($(this).attr('type') === 'file') {
282				var $this = $(this), data = $this.data('filestyle'), options = $.extend({}, $.fn.filestyle.defaults, option, typeof option === 'object' && option);
283
284				if (!data) {
285					$this.data('filestyle', ( data = new Filestyle(this, options)));
286					data.constructor();
287				}
288
289				if ( typeof option === 'string') {
290					get = data[option](value);
291				}
292			}
293		});
294
295		if ( typeof get !== undefined) {
296			return get;
297		} else {
298			return element;
299		}
300	};
301
302	$.fn.filestyle.defaults = {
303		'buttonText' : 'Choose file',
304		'iconName' : 'glyphicon-folder-open',
305		'buttonName' : 'btn-default',
306		'size' : 'nr',
307		'input' : true,
308		'badge' : true,
309		'icon' : true,
310		'buttonBefore' : false,
311		'disabled' : false
312	};
313
314	$.fn.filestyle.noConflict = function() {
315		$.fn.filestyle = old;
316		return this;
317	};
318
319	// Data attributes register
320	$(function() {
321		$('.filestyle').each(function() {
322			var $this = $(this), options = {
323
324				'input' : $this.attr('data-input') === 'false' ? false : true,
325				'icon' : $this.attr('data-icon') === 'false' ? false : true,
326				'buttonBefore' : $this.attr('data-buttonBefore') === 'true' ? true : false,
327				'disabled' : $this.attr('data-disabled') === 'true' ? true : false,
328				'size' : $this.attr('data-size'),
329				'buttonText' : $this.attr('data-buttonText'),
330				'buttonName' : $this.attr('data-buttonName'),
331				'iconName' : $this.attr('data-iconName'),
332				'badge' : $this.attr('data-badge') === 'false' ? false : true
333			};
334
335			$this.filestyle(options);
336		});
337	});
338})(window.jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.