-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.rest.button.js
executable file
·232 lines (179 loc) · 6.34 KB
/
widget.rest.button.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
//Freeboard.io Widget to create a simple button which fires a REST POST Event to control a sensor
//
//
function isa_guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
//return "isa";
return s4() + s4() + s4();
//return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
//alert(isa_guid());
(function()
{
var buttonWidget = function (settings) {
var self = this;
var currentSettings = settings;
var uuid = isa_guid()
responseDivID="response_" + uuid;
if (typeof currentSettings.button_color === 'undefined') currentSettings["button_color"] = "green";
if (typeof currentSettings.post_value === 'undefined') currentSettings["post_value"] = "";
// Automatically set httpbin.org path for testing
//
if (currentSettings.rest_path=="http://httpbin.org/_auto")
{
currentSettings.rest_path = "http://httpbin.org/" + currentSettings.http_verb.toLowerCase();
}
set_headers=""
if (typeof currentSettings.headers !== 'undefined' && currentSettings.headers.length !=0 )
{
//headers = JSON.parse(currentSettings.headers)
isa_headers = currentSettings.headers
for (index in isa_headers)
{
var name = isa_headers[index].name;
var value = isa_headers[index].value;
set_headers += " xhttp.setRequestHeader('" + name + "', '" + value + "'); "
}
console.log(set_headers);
}
var myButton = $("<button class='isa_rest_button' type='button' id='button-" + uuid + "' onclick='buttonFunction" + uuid + "()'>"+ currentSettings.button_name +"</button>");
status_text = Date().substring(15,25) + ": status"
var isaResponseDiv = $("<p class='isa_rest_response' style='background:black;' id='" + responseDivID + "'>" + status_text + "</p>")
sending_status = `$('#${responseDivID}').html("Sending...");`
if(currentSettings.include_timestamp)
{
function post_value_with_timestamp()
{
if (currentSettings.post_value.length===0)
{
currentSettings["post_value"]='{}';
}
var jtemp=JSON.parse(currentSettings.post_value);
jtemp["timestamp"] = new Date().toJSON();
currentSettings["post_value"] = JSON.stringify(jtemp);
console.log(currentSettings.post_value);
return currentSettings.post_value;
}
}
else
{
function post_value_with_timestamp()
{
return currentSettings.post_value;
}
}
eval("post_value_with_timestamp_" + uuid + " = post_value_with_timestamp"); // create a copy of the function
//var myScript = $("<script>function buttonFunction" + uuid + "() {" + sending_status + "var xhttp = new XMLHttpRequest(); xhttp.open('" + currentSettings.http_verb + "', '"+currentSettings.rest_path+"', false); " + set_headers + " xhttp.send('" + currentSettings.post_value + "'); $('#" + responseDivID + "').html(Date() + ' </br> HTTP_STATUS: ' + xhttp.status ); console.log(xhttp.response) }</script>");
var myScript = $("<script>function buttonFunction" + uuid + "() {" + sending_status + "var xhttp = new XMLHttpRequest(); xhttp.open('" + currentSettings.http_verb + "', '"+currentSettings.rest_path+"', false); " + set_headers + " xhttp.send(" + "post_value_with_timestamp_" + uuid + "()" + "); $('#" + responseDivID + "').html(Date() + ' </br> HTTP_STATUS: ' + xhttp.status ); console.log(xhttp.response) }</script>");
console.log(myScript.html());
var isaCSS = $("<style> #button-" + uuid + " { background-color: " + currentSettings.button_color + "; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; }</style>")
//TODO: implement payload
function updateState() {
//TODO: implement changes and re-render button
//
//
}
this.render = function (containerElement) {
$(containerElement).append(myButton);
$(containerElement).append(isaResponseDiv);
$(containerElement).append(myScript);
$(containerElement).append(isaCSS);
freeboard.addStyle('.isa_rest_button','width: 100%; height:50%; float:left')
freeboard.addStyle('.isa_rest_response','width: 100%; height:50%; float:right;')
}
this.onSettingsChanged = function (newSettings) {
currentSettings = newSettings;
updateState();
}
this.onCalculatedValueChanged = function (settingName, newValue) {
// no input so no change ever :-)
}
this.onDispose = function () {
}
// ISA --> for proper height - http://freeboard.github.io/freeboard/docs/plugin_example.html
this.getHeight = function () {
return 2;
}
this.onSettingsChanged(settings);
};
freeboard.loadWidgetPlugin({
type_name: "button_widget",
display_name: "REST Button Widget",
fill_size: false,
settings: [
{
name: "button_name",
display_name: "Button Name",
type: "text",
default_value: "Test httpbin.org"
},
{
name: "http_verb",
display_name: "HTTP Method",
type: "option",
options : [
{name:"GET",value:"GET"},
{name:"POST",value:"POST"},
{name:"PUT",value:"PUT"},
{name:"DELETE",value:"DELETE"}
]
},
{
name: "rest_path",
display_name: "REST URL Path",
type: "text",
default_value: "http://httpbin.org/_auto"
},
//{
// name: "headers",
// display_name: "Headers (optional) in JSON key value pairs",
// type: "text",
// default_value: "{}"
// description: 'Example - {"Content-Type":"application/json","X-My-Header":"Whatever"}'
//},
{
name: "headers",
display_name: "Headers",
type: "array",
description: "Note: single quotes are not allowed in headers",
settings: [
{
name: "name",
display_name: "Name",
type: "text"
},
{
name: "value",
display_name: "Value",
type: "text"
}
]
},
{
name: "post_value",
display_name: "PUT/POST Value (optional)",
type: "text",
description: "Note: single quotes are not allowed in body",
},
{
name: "include_timestamp",
display_name: "Include Timestamp (optional)",
type: "boolean"
//default_value: "green"
},
{
name: "button_color",
display_name: "Button Color (optional)",
type: "text",
default_value: "green"
}
],
newInstance: function (settings, newInstanceCallback) {
newInstanceCallback(new buttonWidget(settings));
}
});
}());