-
Notifications
You must be signed in to change notification settings - Fork 9
/
bootstrap-add-clear.js
145 lines (124 loc) · 4.25 KB
/
bootstrap-add-clear.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*!
* bootstrap-add-clear v1.0.7 (http://github.com/gesquive/bootstrap-add-clear)
* Licensed under MIT (http://github.com/gesquive/bootstrap-add-clear/blob/master/LICENSE)
*/
;(function($, window, document, undefined) {
var pluginName = "addClear";
// The actual plugin constructor
function Plugin(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
var $this = $(this.element),
me = this,
options = this.options;
$this.wrap("<div class='add-clear-span form-group has-feedback " + options.wrapperClass + "'></div>");
$this.after($("<span class='add-clear-x form-control-feedback " + options.symbolClass + "' style='display: none;'>" + options.closeSymbol + "</span>"));
$this.next().css({
'color': options.color,
'cursor': 'pointer',
'text-decoration': 'none',
'display': 'none',
'overflow': 'hidden',
'position': 'absolute',
'pointer-events': 'auto',
'right': options.right,
'top': options.top,
'z-index': options.zindex
}, this);
if ($this.val().length >= 1 && options.showOnLoad === true) {
$this.siblings(".add-clear-x").show();
}
$this.on('focus.addclear', function() {
if ($(this).val().length >= 1) {
$(this).siblings(".add-clear-x").show();
}
});
$this.on('blur.addclear', function() {
var self = this;
if (options.hideOnBlur) {
setTimeout(function() {
$(self).siblings(".add-clear-x").hide();
}, 50);
}
});
$this.on('keyup.addclear', function(e) {
if (options.clearOnEscape === true && e.keyCode == 27) {
$(this).val('').focus();
if (options.onClear) {
options.onClear($(this).siblings("input"));
}
}
if ($(this).val().length >= 1) {
$(this).siblings(".add-clear-x").show();
} else {
$(this).siblings(".add-clear-x").hide();
}
});
$this.on('input.addclear change.addclear paste.addclear', function() {
if ($(this).val().length >= 1) {
$(this).siblings(".add-clear-x").show();
} else {
$(this).siblings(".add-clear-x").hide();
}
});
$this.siblings(".add-clear-x").on('click.addclear', function(e) {
$(this).siblings(me.element).val("");
$(this).hide();
if (options.returnFocus === true) {
$(this).siblings(me.element).focus();
}
if (options.onClear) {
options.onClear($(this).siblings("input"));
}
e.preventDefault();
});
}
};
$.fn[pluginName] = function (options, optionName, optionValue) {
return this.each(function () {
if (options === "option") {
var $this = $(this);
if (optionName === "show") {
$this.siblings(".add-clear-x").show();
} else if (optionName === "hide") {
$this.siblings(".add-clear-x").hide();
}
}
var isSetOption = optionName && optionName !== "show" && optionName !== "hide";
if (isSetOption) {
var oldInstance = $.data(this, "plugin_" + pluginName);
if (!oldInstance || !oldInstance.options) {
throw "Cannot set option, plugin was not instantiated";
}
oldInstance.options[optionName] = optionValue;
} else {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this,
"plugin_" + pluginName,
new Plugin(this, options));
}
}
});
};
$.fn[pluginName].Constructor = Plugin;
var defaults = $.fn[pluginName].defaults = {
closeSymbol: "",
symbolClass: 'glyphicon glyphicon-remove-circle',
color: "#CCC",
top: 0,
right: 0,
returnFocus: true,
showOnLoad: false,
onClear: null,
hideOnBlur: false,
clearOnEscape: true,
wrapperClass: '',
zindex: 100
};
})(jQuery, window, document);