| 20 |
- |
1 |
/*
|
|
|
2 |
* bootstrap4-input-clearer v1.2.0
|
|
|
3 |
* Add clear icons to Bootstrap 4 input fields.
|
|
|
4 |
* https://github.com/mheigl/bootstrap4-input-clearer
|
|
|
5 |
*
|
|
|
6 |
* Made by Michael Heigl
|
|
|
7 |
* Under GNU General Public License v3.0
|
|
|
8 |
*/
|
|
|
9 |
;(function ($) {
|
|
|
10 |
"use strict";
|
|
|
11 |
|
|
|
12 |
var pluginName = "clearer",
|
|
|
13 |
defaults = {
|
|
|
14 |
clearHtml: '<i class="fas fa-window-close"></i>',
|
|
|
15 |
cssClass: '',
|
|
|
16 |
focusable: true
|
|
|
17 |
};
|
|
|
18 |
|
|
|
19 |
function Plugin (element, options) {
|
|
|
20 |
this.element = element;
|
|
|
21 |
this.settings = $.extend({}, defaults, options);
|
|
|
22 |
this.init();
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
$.extend(Plugin.prototype, {
|
|
|
26 |
init: function () {
|
|
|
27 |
var self = this;
|
|
|
28 |
|
|
|
29 |
this.$element = $(this.element);
|
|
|
30 |
this.$clearer = $('<div class="input-group-append ' + this.settings.cssClass + '">'
|
|
|
31 |
+ '<button class="btn input-group-text form-control" type="button">' + this.settings.clearHtml + '</button></div>');
|
|
|
32 |
|
|
|
33 |
if (this.settings.focusable === false) {
|
|
|
34 |
this.$clearer.attr({ 'tabindex': -1 });
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
if (this.$element.closest('.input-group').length === 0) {
|
|
|
38 |
this.$element.wrap("<div class='input-group'></div>");
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
this.$element.after(this.$clearer);
|
|
|
42 |
|
|
|
43 |
this.update();
|
|
|
44 |
|
|
|
45 |
this.$clearer.on('click.clearer', function (e) {
|
|
|
46 |
self.$element.val('');
|
|
|
47 |
self.$element.trigger('change');
|
|
|
48 |
self.$element.focus();
|
|
|
49 |
self.update();
|
|
|
50 |
e.preventDefault();
|
|
|
51 |
});
|
|
|
52 |
|
|
|
53 |
this.$element.on('focus.clearer blur.clearer', function () {
|
|
|
54 |
self.update();
|
|
|
55 |
});
|
|
|
56 |
|
|
|
57 |
this.$element.on('keyup.clearer', function (e) {
|
|
|
58 |
if (e.keyCode === 27) {
|
|
|
59 |
$(this).val('').focus();
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
self.update();
|
|
|
63 |
});
|
|
|
64 |
|
|
|
65 |
this.$element.on('input.clearer change.clearer paste.clearer', function () {
|
|
|
66 |
self.update();
|
|
|
67 |
});
|
|
|
68 |
},
|
|
|
69 |
update: function() {
|
|
|
70 |
if (this.$element.val().length >= 1) {
|
|
|
71 |
this.$clearer.show();
|
|
|
72 |
} else {
|
|
|
73 |
this.$clearer.hide();
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
});
|
|
|
77 |
|
|
|
78 |
$.fn[pluginName] = function (options) {
|
|
|
79 |
return this.each(function () {
|
|
|
80 |
if (!$.data(this, "plugin_" + pluginName)) {
|
|
|
81 |
$.data(this, "plugin_" +
|
|
|
82 |
pluginName, new Plugin(this, options));
|
|
|
83 |
}
|
|
|
84 |
});
|
|
|
85 |
};
|
|
|
86 |
})(jQuery, window, document);
|