-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckbox.js
More file actions
442 lines (348 loc) · 12.5 KB
/
Checkbox.js
File metadata and controls
442 lines (348 loc) · 12.5 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** Checkbox Class
//******************************************************************************
/**
* Form input that can be either checked or unchecked. The input consists of
* a square box with a checkmark when selected.
* <br/>
* Here's a simple example of how to instantiate a checkbox using an existing
* div (DOM element) and a minimal config. See config settings for a full
* range of options.
<pre>
var checkbox = new javaxt.dhtml.Checkbox(div, {
label: "I Agree",
checked: false
});
</pre>
* Once the checkbox is instantiated you can call any of the public methods.
* You can also add event listeners by overriding any of the public "on" or
* "before" methods like this:
<pre>
checkbox.onClick = function(checked){
console.log("I Agree", checked);
};
</pre>
*
******************************************************************************/
javaxt.dhtml.Checkbox = function(parent, config) {
this.className = "javaxt.dhtml.Checkbox";
var me = this;
var box, check, mask, label;
var defaultConfig = {
/** Label for the checkbox (optional). Accepts either a string or a DOM
* element.
*/
label: null,
/** Value associated with the checkbox (optional). Note that this is
* different than the checkbox state. Use the isChecked() method to
* determine whether the checkbox is checked.
*/
value: null,
/** If true, the checkbox will initially render with a check. Default is
* false.
*/
checked: false,
/** If true, the component will be disabled when rendered. Default is
* false.
*/
disabled: false,
/** Style for individual elements within the component. Note that you can
* provide CSS class names instead of individual style definitions.
*/
style:{
panel: {
display: "inline-block"
},
/** Style for the checkbox
*/
box: {
width: "13px",
height: "13px",
border: "1px solid #ccc",
borderRadius: "3px",
backgroundColor: "#F6F6F6",
cursor: "pointer",
margin: "0px",
color: "#2b2b2b"
},
/** Style for the label next to the checkbox
*/
label: {
fontFamily: "helvetica,arial,verdana,sans-serif",
fontSize: "14px",
whiteSpace: "nowrap",
cursor: "pointer",
padding: "1px 0 0 5px"
},
/** Style for the checkmark inside the checkbox
*/
check: {
content: "",
display: "block",
width: "3px",
height: "6px",
border: "solid #fff",
borderWidth: "0 2px 2px 0",
transform: "rotate(45deg)",
margin: "1px 0 0 4px"
},
/** Style for the checkbox when it is selected/checked
*/
select: {
backgroundColor: "#007FFF",
border: "1px solid #003EFF",
color: "#fff"
},
/** Style for the mask used to disable the checkbox
*/
disable: {
cursor: "default"
},
/** Style for the checkbox when mouse hovers over
*/
hover: {
backgroundColor: "#ededed"
}
}
};
//**************************************************************************
//** Constructor
//**************************************************************************
var init = function(){
if (typeof parent === "string"){
parent = document.getElementById(parent);
}
if (!parent) return;
//Clone the config so we don't modify the original config object
var clone = {};
merge(clone, config);
//Merge clone with default config
merge(clone, defaultConfig);
config = clone;
//Create container
me.el = createElement("span", parent, "javaxt-checkbox");
addShowHide(me);
var innerDiv = createElement('div', me.el, config.style.panel);
innerDiv.style.position = "relative";
if (config.display){
console.warn(
"The 'display' config in the javaxt.dhtml.Checkbox " +
"class has been deprecated. Use panel style instead");
innerDiv.style.display = config.display;
}
//Create checkbox
if (config.label){
//Create table with 2 columns - one for the checkbox
//and a column for the checkbox label.
var table = createTable(innerDiv);
table.style.fontFamily = "inherit";
table.style.textAlign = "inherit";
table.style.color = "inherit";
table.style.width = "";
table.style.height = "";
var tr = table.addRow();
box = createElement('div', tr.addColumn(), config.style.box);
label = createElement("div", tr.addColumn(), config.style.label);
if (isElement(config.label)){
label.appendChild(config.label);
}
else{
label.innerHTML = config.label;
}
addEventHandlers(table);
}
else{
//Create checkbox with no label
box = createElement('div', innerDiv, config.style.box);
addEventHandlers(box);
}
//Set button state
if (config.disabled===true) me.disable();
if (config.checked===true) me.select();
};
//**************************************************************************
//** addEventHandlers
//**************************************************************************
var addEventHandlers = function(div){
//Disable text selection
div.unselectable="on";
div.onselectstart=function(){return false;};
div.onmousedown=function(){
addStyle(box, "select");
return false;
};
//Create onclick function
var onclick = function(){
if (config.sound!=null) config.sound.play();
me.toggle();
me.onClick(box.checked);
};
//Create logic to process touch events
var touchStartTime;
var touchEndTime;
var x1, x2, y1, y2;
var isTouch = false;
div.ontouchstart = function(e) {
isTouch = true;
e.preventDefault();
x1 = e.changedTouches[0].pageX;
y1 = e.changedTouches[0].pageY;
touchStartTime = new Date().getTime();
touchEndTime = null;
if (box.checked!==true){
addStyle(box, "hover");
}
};
div.ontouchend = function(e) {
touchEndTime= new Date().getTime();
x2 = e.changedTouches[0].pageX;
y2 = e.changedTouches[0].pageY;
var distance = Math.sqrt( (x2-=x1)*x2 + (y2-=y1)*y2 );
if (distance<0) distance = -distance;
var duration = touchEndTime - touchStartTime;
if ((duration <= 500 && distance <= 10) || //Quick tap
(duration > 500 && distance <= 10)) { //Long press
onclick();
}
};
//Logic to process mouse events
div.onclick = function(){
if (!isTouch) onclick();
};
div.onmouseover = function(){
if (box.checked!==true){
addStyle(box, "hover");
}
};
div.onmouseout = function(){
if (box.checked!==true){
setStyle(box, "box");
}
};
};
//**************************************************************************
//** onClick
//**************************************************************************
/** Called whenever the checkbox is clicked.
*/
this.onClick = function(checked){};
//**************************************************************************
//** onChange
//**************************************************************************
/** Called whenever the checkbox value is changed.
*/
this.onChange = function(checked){};
//**************************************************************************
//** enable
//**************************************************************************
/** Used to enable the checkbox.
*/
this.enable = function(){
mask.style.visibility = "hidden";
box.style.opacity = "";
label.style.opacity = "";
};
//**************************************************************************
//** disable
//**************************************************************************
/** Used to disable the checkbox.
*/
this.disable = function(){
if (mask){
mask.style.visibility = "visible";
}
else{
mask = createElement('div', config.style.disable);
mask.style.position = "absolute";
mask.style.zIndex = "1";
mask.style.width = "100%";
mask.style.height = "100%";
var innerDiv = me.el.firstChild;
innerDiv.insertBefore(mask, innerDiv.firstChild);
}
box.style.opacity = "0.5";
label.style.opacity = "0.5";
};
//**************************************************************************
//** select
//**************************************************************************
/** Used to add a check to the checkbox.
*/
this.select = function(silent){
if (box.checked===true) return;
box.checked = true;
addStyle(box,"select");
if (check){
check.style.visibility = "visible";
}
else{
check = createElement('div', box, config.style.check);
}
if (silent===true) return;
me.onChange(true);
};
//**************************************************************************
//** deselect
//**************************************************************************
/** Used to remove the check from the checkbox.
*/
this.deselect = function(silent){
if (box.checked===true){
box.checked = false;
setStyle(box,"box");
check.style.visibility = "hidden";
if (silent===true) return;
me.onChange(false);
}
};
//**************************************************************************
//** toggle
//**************************************************************************
/** Used to toggle the checkbox state.
*/
this.toggle = function(){
if (box.checked===true){
me.deselect();
}
else{
me.select();
}
};
//**************************************************************************
//** getValue
//**************************************************************************
/** Returns the value associated with the checkbox. Note that this is
* different than the checkbox state. Use the isChecked() method to
* determine whether the checkbox is checked. The value is defined in
* the config used to instantiate this class.
*/
this.getValue = function(){
if (config.value==null) return me.isChecked();
return config.value;
};
//**************************************************************************
//** isChecked
//**************************************************************************
/** Returns true if the checkbox is checked.
*/
this.isChecked = function(){
return box.checked===true;
};
//**************************************************************************
//** Utils
//**************************************************************************
var createElement = javaxt.dhtml.utils.createElement;
var createTable = javaxt.dhtml.utils.createTable;
var addShowHide = javaxt.dhtml.utils.addShowHide;
var isElement = javaxt.dhtml.utils.isElement;
var merge = javaxt.dhtml.utils.merge;
var setStyle = function(el, style){
javaxt.dhtml.utils.setStyle(el, config.style[style]);
};
var addStyle = function(el, style){
javaxt.dhtml.utils.addStyle(el, config.style[style]);
};
init();
};