-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitch.js
More file actions
161 lines (135 loc) · 5.03 KB
/
Switch.js
File metadata and controls
161 lines (135 loc) · 5.03 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
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** Switch
//*****************************************************************************/
/**
* Basic on/off toggle switch
*
******************************************************************************/
javaxt.dhtml.Switch = function(parent, config) {
var me = this;
var defaultConfig = {
/** If true, the slider will be set to the on position.
*/
value: true,
/** Style for individual elements within the component. Note that you can
* provide CSS class names instead of individual style definitions.
*/
style: {
groove: {
backgroundColor: "#dcdcdc",
width: "40px",
height: "24px",
borderRadius: "12px",
position: "relative"
},
handle: {
width: "20px",
height: "20px",
backgroundColor: "#fff",
borderRadius: "10px",
position: "absolute",
margin: "2px",
boxShadow: "0px 3px 4px rgba(0, 0, 0, 0.2)"
},
grooveActive: {
backgroundColor: "#4bd763",
width: "40px",
height: "24px",
borderRadius: "12px",
position: "relative"
},
handleActive: {
width: "20px",
height: "20px",
backgroundColor: "#fff",
borderRadius: "10px",
position: "absolute",
margin: "2px",
boxShadow: "0px 3px 4px rgba(0, 0, 0, 0.2)"
}
}
};
var groove, handle, value;
//**************************************************************************
//** 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
var mainDiv = createElement("span", parent);
mainDiv.className = "javaxt-switch";
me.el = mainDiv;
addShowHide(me);
//Create slider
groove = createElement("div", mainDiv, config.style.groove);
groove.onclick = function(){
me.setValue(!me.getValue());
};
handle = createElement("div", groove, config.style.handle);
me.setValue(config.value, true);
};
//**************************************************************************
//** getValue
//**************************************************************************
/** Returns true on the slider is set to the on/active position. Otherwise
* returns false.
*/
this.getValue = function(){
return value;
};
//**************************************************************************
//** setValue
//**************************************************************************
/** Used to set the slider to the on or off position.
* @param b If true, sets the slider to the on position. Otherwise, the
* slider will be set to the off position.
* @param silent By default, this slider will fire the onChange event
* whenever the value is changed. When silent is set to true, the slider
* will NOT fire the onChange event. This parameter is optional.
*/
this.setValue = function(b, silent){
if (b===true || b===false){
if (b===value) return;
if (b===true){
setStyle(groove, config.style.grooveActive);
setStyle(handle, config.style.handleActive);
handle.style.left = "";
handle.style.right = 0;
}
else{
setStyle(groove, config.style.groove);
setStyle(handle, config.style.handle);
handle.style.right = "";
handle.style.left = 0;
}
value = b;
if (silent===true){}
else me.onChange(value);
}
};
//**************************************************************************
//** onChange
//**************************************************************************
/** Called when the input value changes
*/
this.onChange = function(value){};
//**************************************************************************
//** Utils
//**************************************************************************
var merge = javaxt.dhtml.utils.merge;
var setStyle = javaxt.dhtml.utils.setStyle;
var addShowHide = javaxt.dhtml.utils.addShowHide;
var createElement = javaxt.dhtml.utils.createElement;
init();
};