This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ui.reduceoptions.js
228 lines (206 loc) · 5.36 KB
/
jquery.ui.reduceoptions.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Reduce-Options jQuery UI Plugin
*
* Acts on grouped elements and allows to hide unselected ones initially if a
* certain amount of elements is already selected. Provides a toggle element.
*
* by berliner
* Git: https://github.com/berliner/jQuery-UI-reduceOptions-Plugin
*/
(function( $, undefined ) {
$.widget("ui.reduceOptions", {
options: {
wrapper: '',
element_inner: '',
element_outer: '',
attribute: 'checked',
value: 'checked',
threshold: 1,
label_more: 'more',
label_less: 'less',
observe: '',
show: function (el) {
$(el).fadeIn('slow');
},
hide: function (el) {
$(el).hide();
},
after_update: null
},
/**
* Constructor for this widget.
*/
_create: function() {
var self = this;
var wrapper = this.element;
var o = self.options;
self.options.wrapper = wrapper;
if (o.observe) {
$(document).on(o.observe, function() {
self.update();
});
}
self.update();
self._on($(wrapper).find(o.element_inner), {
change: 'verifyLinkVisibility'
});
},
/**
* Count all relevant elements in this set.
*/
countElements: function() {
var self = this;
var wrapper = self.element;
var o = self.options;
return $(wrapper).find(o.element_inner).length;
},
/**
* Count the selected elements in the current set.
*/
countSelected: function() {
var self = this;
var wrapper = self.element;
var o = self.options;
var checked = 0;
$(wrapper).find(o.element_inner).each(function() {
if ($(this).attr(o.attribute) == o.value) {
checked++;
}
});
return checked;
},
/**
* See if and which toggle links should be visible.
*/
verifyLinkVisibility: function() {
var self = this;
var o = self.options;
var count = self.countElements();
var selected = self.countSelected();
if ((selected == 0 && !self.collapsed) || count == selected) {
$(self.element).find('.ui-reduce-options-button').hide();
}
else {
$(self.element).find('.ui-reduce-options-button').show();
}
},
/**
* Show all elements in the set.
*/
showAll: function() {
var self = this;
// make sure all are visible
$(self.options.wrapper).find(self.options.element_inner).each(function() {
self.options.show($(this).parent(self.options.element_outer));
});
},
/**
* Hide all elements in the set.
*/
hideAll: function() {
var self = this;
// make sure all are visible
$(self.options.wrapper).find(self.options.element_inner).each(function() {
self.options.hide($(this).parent(self.options.element_outer));
});
},
/**
* Hide all unselected elements in the current set.
*/
hideUnselected: function() {
var self = this;
var o = self.options;
$(self.element).find(o.element_inner + '[' + o.attribute + '!="' + o.value + '"]').each(function() {
o.hide($(this).parent(o.element_outer));
});
},
/**
* Reduce elements in given set.
* The argument e is an event object.
*/
reduceOptions: function(e) {
this.hideUnselected();
this.addMoreLink();
this.collapsed = true;
e.preventDefault();
return false;
},
/**
* Expand elements in given set.
* The argument e is an event object.
*/
expandOptions: function(e) {
this.showAll();
this.removeLink();
this.addLessLink();
this.collapsed = false;
this.verifyLinkVisibility();
e.preventDefault();
return false;
},
/**
* Link builder helper function.
*/
addLink: function(props) {
this.removeLink();
var link = $('<a>').attr({
'class': 'ui-reduce-options-button ui-reduce-options-show-' + props.type,
'href': '#',
'title': props.label
}).text(props.label);
this._on(link, {
click: props.click
});
$(this.element).append(link);
},
/**
* Remove the mode/less link.
*/
removeLink: function() {
$(this.element).find('.ui-reduce-options-button').remove();
},
/**
* Add a "more" link.
*/
addMoreLink: function() {
this.addLink({
type: 'more',
label: this.options.label_more,
click: 'expandOptions'
});
},
/**
* Add a "less" link.
*/
addLessLink: function() {
this.addLink({
type: 'less',
label: this.options.label_less,
click: 'reduceOptions'
});
},
/**
* Update the current element set.
*/
update: function() {
var self = this;
var wrapper = this.element;
var o = self.options;
if (self.countSelected() == 0) {
self.showAll();
self.addLessLink();
self.collapsed = false;
}
else if (self.countElements() > self.countSelected()) {
// hide the unselected options
self.hideUnselected();
self.collapsed = true;
self.addMoreLink();
}
self.verifyLinkVisibility();
if (o.after_update) {
o.after_update(self);
}
}
});
}( jQuery ));