forked from ExactTarget/fuelux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
134 lines (102 loc) · 2.98 KB
/
search.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
/*
* Fuel UX Search
* https://github.com/ExactTarget/fuelux
*
* Copyright (c) 2012 ExactTarget
* Licensed under the MIT license.
*/
define(['require','jquery'],function(require) {
var $ = require('jquery');
var old = $.fn.search;
// SEARCH CONSTRUCTOR AND PROTOTYPE
var Search = function (element, options) {
this.$element = $(element);
this.options = $.extend({}, $.fn.search.defaults, options);
this.$button = this.$element.find('button')
.on('click', $.proxy(this.buttonclicked, this));
this.$input = this.$element.find('input')
.on('keydown', $.proxy(this.keypress, this))
.on('keyup', $.proxy(this.keypressed, this));
this.$icon = this.$element.find('i');
this.activeSearch = '';
};
Search.prototype = {
constructor: Search,
search: function (searchText) {
this.$icon.attr('class', 'icon-remove');
this.activeSearch = searchText;
this.$element.trigger('searched', searchText);
},
clear: function () {
this.$icon.attr('class', 'icon-search');
this.activeSearch = '';
this.$input.val('');
this.$element.trigger('cleared');
},
action: function () {
var val = this.$input.val();
var inputEmptyOrUnchanged = val === '' || val === this.activeSearch;
if (this.activeSearch && inputEmptyOrUnchanged) {
this.clear();
} else if (val) {
this.search(val);
}
},
buttonclicked: function (e) {
e.preventDefault();
if ($(e.currentTarget).is('.disabled, :disabled')) return;
this.action();
},
keypress: function (e) {
if (e.which === 13) {
e.preventDefault();
}
},
keypressed: function (e) {
var val, inputPresentAndUnchanged;
if (e.which === 13) {
e.preventDefault();
this.action();
} else {
val = this.$input.val();
inputPresentAndUnchanged = val && (val === this.activeSearch);
this.$icon.attr('class', inputPresentAndUnchanged ? 'icon-remove' : 'icon-search');
}
},
disable: function () {
this.$input.attr('disabled', 'disabled');
this.$button.addClass('disabled');
},
enable: function () {
this.$input.removeAttr('disabled');
this.$button.removeClass('disabled');
}
};
// SEARCH PLUGIN DEFINITION
$.fn.search = function (option) {
var args = Array.prototype.slice.call( arguments, 1 );
var methodReturn;
var $set = this.each(function () {
var $this = $( this );
var data = $this.data( 'search' );
var options = typeof option === 'object' && option;
if (!data) $this.data('search', (data = new Search(this, options)));
if (typeof option === 'string') methodReturn = data[ option ].apply( data, args );
});
return ( methodReturn === undefined ) ? $set : methodReturn;
};
$.fn.search.defaults = {};
$.fn.search.Constructor = Search;
$.fn.search.noConflict = function () {
$.fn.search = old;
return this;
};
// SEARCH DATA-API
$(function () {
$('body').on('mousedown.search.data-api', '.search', function () {
var $this = $(this);
if ($this.data('search')) return;
$this.search($this.data());
});
});
});