-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathswitch.js
187 lines (155 loc) · 7.48 KB
/
switch.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
// ┌────────────────────────────────────────────────────────────────────┐ \\
// │ freeboard-switch-plugin │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ http://blog.onlinux.fr/?tag=freeboard │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Licensed under the MIT license. │ \\
// ├────────────────────────────────────────────────────────────────────┤ \\
// │ Freeboard widget plugin for Highcharts. │ \\
// └────────────────────────────────────────────────────────────────────┘ \\
(function()
{
//
// DECLARATIONS
//
var LOADING_INDICATOR_DELAY = 1000;
var SWITCH_ID = 0;
//
freeboard.loadWidgetPlugin({
type_name: "switch_plugin",
display_name: "Switch",
description : "Interactive on-off switch",
settings: [
{
name: "title",
display_name: "Title",
type: "text"
},
{
name: "value",
display_name: "Value",
type: "calculated"
},
{
name: "urlOn",
display_name: "url On ",
type: "calculated"
},
{
name: "urlOff",
display_name: "url Off ",
type: "calculated"
},
{
name: "on_text",
display_name: "On Text",
type: "text",
default_value: 'On'
},
{
name: "off_text",
display_name: "Off Text",
type: "text",
default_value: 'Off'
},
],
newInstance: function (settings, newInstanceCallback) {
newInstanceCallback(new wswitch(settings));
}
});
freeboard.addStyle ('.floating-box',"display: inline-block; vertical-align: top; width: 78px; background-color: #222;margin-top: 10px; margin-right: 5px;");
freeboard.addStyle ('.onoffswitch-title',"font-size: 17px; line-height: 29px; width: 65%; height: 29px; padding-left: 10px;border: 1px solid #3d3d3d;");
freeboard.addStyle ('.round' ,"border-radius: 50%;");
var wswitch = function (settings) {
var self = this;
var thisWidgetId = "onoffswitch-" + SWITCH_ID++;
var currentSettings = settings;
var box1 = $('<div class="floating-box"></div>');
var box2 = $('<div class="floating-box onoffswitch-title">' + settings.title + '</div>');
var onOffSwitch = $('<div class="onoffswitch"><label class="onoffswitch-label" for="'+ thisWidgetId +'"><div class="onoffswitch-inner"><span class="on"></span><span class="off"></span></div><div class="onoffswitch-switch round"></div></label></div>');
//onOffSwitch.find("span.on").text("True");
onOffSwitch.prependTo(box1);
var isOn = false;
var onText;
var offText;
var url;
function updateState() {
console.log("isOn: " + isOn);
$('#'+thisWidgetId).prop('checked', isOn);
console.log(onOffSwitch.find("span.on"));
onOffSwitch.find("span.on").text(onText);
onOffSwitch.find("span.off").text(offText);
}
var alertContents = function () {
if (request.readyState === XMLHttpRequest.DONE) {
if (request.status === 200) {
console.log(request.responseText);
setTimeout(function(){
freeboard.showLoadingIndicator(false);
//freeboard.showDialog($("<div align='center'>Request response 200</div>"),"Success!","OK",null,function(){});
}, LOADING_INDICATOR_DELAY);
} else {
console.log('There was a problem with the request.');
setTimeout(function(){
freeboard.showLoadingIndicator(false);
freeboard.showDialog($("<div align='center'>There was a problem with the request. Code " + request.status + request.responseText + " </div>"),"Error!","OK",null,function(){});
}, LOADING_INDICATOR_DELAY);
}
}
}
var request;
var sendValue = function (url, options) {
console.log(url, options);
request = new XMLHttpRequest();
if (!request) {
console.log('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
request.onreadystatechange = alertContents;
request.open('GET', url, true);
freeboard.showLoadingIndicator(true);
request.send();
}
this.render = function (element) {
$(element).append(box1).append(box2);
var input = $('<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="'+ thisWidgetId +'">').prependTo(onOffSwitch).change(function()
{
isOn =!isOn;
console.log( thisWidgetId + ": toogled " + isOn);
url = (isOn) ? currentSettings.urlOn: currentSettings.urlOff;
if ( _.isUndefined(url) )
freeboard.showDialog($("<div align='center'>url undefined</div>"),"Error!","OK",null,function(){});
else {
sendValue(url, isOn);
}
});
}
this.onSettingsChanged = function (newSettings) {
currentSettings = newSettings;
box2.html((_.isUndefined(newSettings.title) ? "" : newSettings.title));
console.log( "isUndefined on_text: " + _.isUndefined(newSettings.on_text) );
onText = newSettings.on_text;
offText = newSettings.off_text;
updateState();
}
this.onCalculatedValueChanged = function (settingName, newValue) {
console.log(settingName, newValue);
if (settingName == "value") {
isOn = Boolean(newValue);
}
if (settingName == "urlOn") {
urlOn = newValue;
}
if (settingName == "urlOff") {
urlOff = newValue;
}
updateState();
}
this.onDispose = function () {
}
this.getHeight = function () {
return 1;
}
this.onSettingsChanged(settings);
};
}());