-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtristate-checkbox.js
294 lines (259 loc) · 9.88 KB
/
tristate-checkbox.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
if (typeof(Prototype) == "undefined")
throw "TriStateCheckbox requires Prototype to be loaded.";
if (typeof(Scriptaculous) == "undefined")
throw "TriStateCheckbox requires Scriptaculous to be loaded.";
/**
* Represents a 3-state checkbox, can handle dependancies
* or function as a standalone box.
*
* @author Willie Scholtz
* @since 2010/08/25
*/
var TriStateCheckbox = Class.create({
initialize: function(element, options) {
this.element = $(element);
this.options = Object.extend({
determineSelectorContext: function(el) {return el;},
defaultState: TriStateCheckbox.STATE_NONE,
dependantSelector: '',
selectorContext: null,
displayText: ''
}, options || {});
if (!this.element) {
throw 'Cannot initialize TriStateCheckbox '
+ 'without a valid checkbox element!';
}
this.element.addClassName(TriStateCheckbox.CLASS_TRISTATE_DUMMY);
this.element.checked = false;
this.element.disable();
this.element.hide();
this._initState();
this._init();
},
getState: function() {
return this.state;
},
_hasDependancySelector: function() {
return this.options.dependantSelector
&& !this.options.dependantSelector.blank();
},
_select: function() {
if (!this.selector) {
this.selector = document;
if (this.options.selectorContext) {
var element = $(this.options.selectorContext);
if (Object.isElement(element)) {
element = this.options.determineSelectorContext(element);
if (!Object.isElement(element)) {
element = $(this.options.selectorContext);
}
this.selector = element;
} else {
this.options.dependantSelector = '';
this.options.selectorContext = '';
var str = 'error while determining selectorContext, '
+ 'disabling dependantSelector';
alert(str);
}
}
}
var results = [];
if (this._hasDependancySelector()) {
if (this.selector === document) {
results = $$(this.options.dependantSelector);
} else {
results = this.selector.select(this.options.dependantSelector);
}
}
return results;
},
_determineStateOfElements: function() {
var checked = this._select().findAll(function(el) {
return !el.disabled && (el.type && el.type == 'checkbox');
}).pluck('checked');
/*
* TODO _determineStateOfElements() only handles normal cb's this code needs
* to be added to handle tristate aswell...
var checked = $$(this.options.dependantSelector).inject([], function(arr, el) {
if (el.type && el.type == 'checkbox') {
if (!el.disabled) {
arr.push(el);
} else if (this._isTristateElement(el)) {
var tristate = el.previous('div.tristate');
if (tristate) {
tristate = tristate.down('span.image');
var tri_check = new Object();
tri_check['checked'] = (tristate && tristate
.hasClassName(TriStateCheckbox.CLASS_CHECKED));
arr.push(tri_check);
}
}
}
return arr;
}.bind(this)).pluck('checked');
*/
return TriStateCheckbox[(checked.all() && checked.size() > 0)
? 'STATE_ALL' : (checked.any() ? 'STATE_SOME' : 'STATE_NONE')];
},
_initState: function() {
this.state = this._hasDependancySelector()
? this._determineStateOfElements()
: this.options.defaultState;
},
_changeState: function(element, newState) {
element = element || this.image;
if (element) {
element.removeClassName(this.current);
if (newState != undefined) {
this.state = newState;
}
this.current = this._convertState();
element.addClassName(this.current);
}
},
_convertState: function() {
var highlight = this.over;
var stateClass = highlight
? TriStateCheckbox.CLASS_UNCHECKED_H
: TriStateCheckbox.CLASS_UNCHECKED;
switch (this.state) {
case TriStateCheckbox.STATE_NONE: {
stateClass = highlight
? TriStateCheckbox.CLASS_UNCHECKED_H
: TriStateCheckbox.CLASS_UNCHECKED;
break;
}
case TriStateCheckbox.STATE_SOME: {
stateClass = highlight
? TriStateCheckbox.CLASS_INTERMEDIATE_H
: TriStateCheckbox.CLASS_INTERMEDIATE;
break;
}
case TriStateCheckbox.STATE_ALL: {
stateClass = highlight
? TriStateCheckbox.CLASS_CHECKED_H
: TriStateCheckbox.CLASS_CHECKED;
break;
}
}
return stateClass;
},
_init: function() {
this.current = this._convertState();
this.container = new Element('div', {
id: 'tristate_' + this.element.id,
'class': 'tristate clearfix'
}).update(
'<span class="image ' + this.current + '"></span>' +
'<span class="text">' + this.options.displayText + '</span>'
).hide();
this.element.insert({
'before': this.container
});
this.image = this.container.down('span.image');
this._addEvents();
this.container.show();
},
_addEvents: function() {
this.container.observe(TriStateCheckbox.EVENT_STATE_CHANGE, function(evt) {
var newState = evt.memo.state;
if (newState !== this.state) {
this._changeState(null, newState);
}
}.bind(this));
this.container.observe('mouseover', function(evt) {
var el = evt.element();
if (el && el.hasClassName('image')) {
this.over = true;
this._changeState(el);
}
}.bind(this));
this.container.observe('mouseout', function(evt) {
var el = evt.element();
if (el && el.hasClassName('image')) {
this.over = false;
this._changeState(el);
}
}.bind(this));
this.container.observe('click', function(evt) {
var el = evt.element();
if (el && el.hasClassName('image')) {
this._respondToClick(el);
}
}.bind(this));
if (this._hasDependancySelector()) {
this._select().findAll(function(cb) {
return (cb.type && cb.type == 'checkbox');
}).each(function(el) {
if (this._isTristateElement(el)) {
var tristate = el.previous('div.tristate');
if (tristate && (tristate !== this.container)) {
tristate.observe('click', function(evt) {
var el = evt.element();
if (el && el.hasClassName('image')) {
var newState = this._determineStateOfElements();
this._changeState(null, newState);
}
}.bind(this));
}
} else if (!el.disabled) {
el.observe('click', function() {
this._changeState(null, this._determineStateOfElements());
}.bind(this));
}
}.bind(this));
}
},
_getNextState: function(value) {
if (value == TriStateCheckbox.STATE_SOME) {
return TriStateCheckbox.STATE_ALL
} else if (value == TriStateCheckbox.STATE_ALL) {
return TriStateCheckbox.STATE_NONE;
} else {
if (this._hasDependancySelector()) {
return TriStateCheckbox.STATE_ALL;
} else {
return TriStateCheckbox.STATE_SOME;
}
}
},
_isTristateElement: function(element) {
return element && element.hasClassName(TriStateCheckbox
.CLASS_TRISTATE_DUMMY);
},
_handleClick: function() {
var handleState = this._getNextState(this.state);
if (this._hasDependancySelector()) {
this._select().each(function(cb) {
if (!cb.disabled && (cb.type && cb.type == 'checkbox')) {
cb.checked = (handleState == TriStateCheckbox.STATE_ALL);
} else if (this._isTristateElement(cb)) {
var tristate = cb.previous('div.tristate');
if (tristate && (tristate !== this.container)) {
tristate.fire(TriStateCheckbox.EVENT_STATE_CHANGE, {
state: handleState
});
}
}
}.bind(this));
}
return handleState;
},
_respondToClick: function(el) {
var newState = this._handleClick();
this._changeState(el, newState);
}
});
Object.extend(TriStateCheckbox, {
STATE_NONE: 0,
STATE_SOME: 1,
STATE_ALL: 2,
CLASS_UNCHECKED: 'unchecked',
CLASS_INTERMEDIATE: 'intermediate',
CLASS_CHECKED: 'checked',
CLASS_UNCHECKED_H: 'unchecked-highlight',
CLASS_INTERMEDIATE_H: 'intermediate-highlight',
CLASS_CHECKED_H: 'checked-highlight',
CLASS_TRISTATE_DUMMY: 'tristate-dummy',
EVENT_STATE_CHANGE: 'tristate:statechange'
});