-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
111 lines (91 loc) · 2.14 KB
/
index.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
/**
* Switch
*
* A switch UI for checkbox.
*
* Copyright (c) 2014 by Hsiaoming Yang.
*/
var classes = require('classes');
var events = require('event');
var emitter = require('emitter');
/**
* Switch Object
*
* @param {Boolean} value
*/
function Switch(value) {
var me = this;
me._value = value || false;
var labelOn = createElement('label', 'switch-on-label');
me._labelOn = labelOn;
var labelOff = createElement('label', 'switch-off-label');
me._labelOff = labelOff;
var mask = createElement('div', 'switch-mask');
me._mask = mask;
var el = createElement('div', 'switch');
el._class = classes(el);
me.el = el;
el.appendChild(labelOn);
el.appendChild(mask);
el.appendChild(labelOff);
events.bind(el, 'click', function() {
me.toggle();
});
me.label('ON', 'OFF');
me.change(me._value);
}
emitter(Switch.prototype);
Switch.prototype.label = function(on, off) {
if (on) this._labelOn.innerHTML = on;
if (off) this._labelOff.innerHTML = off;
};
Switch.prototype.change = function(value) {
value = value ? true: false;
this._value = value;
if (value) {
this.el._class.add('on');
this.el._class.remove('off');
} else {
this.el._class.add('off');
this.el._class.remove('on');
}
this.emit('change', value);
};
Switch.prototype.toggle = function() {
if (this._value) {
this.change(false);
} else {
this.change(true);
}
};
/**
* Getter and setter for Switch.
*/
Switch.prototype.value = function(val) {
if (val === undefined) {
return this._value;
} else {
this.change(value);
}
};
Switch.prototype.takeover = function(input) {
if (input.checked) {
this.change(true);
}
// render element to the dom
input.parentNode.insertBefore(this.el, input);
// hide original input
input.style.display = 'none';
var on = input.getAttribute('data-on-label');
var off = input.getAttribute('data-off-label');
this.label(on, off);
this.on('change', function(value) {
input.checked = value;
});
};
function createElement(tagName, className) {
var node = document.createElement(tagName);
node.className = className;
return node;
}
module.exports = Switch;