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
|
/*
*
* Copyright (C) 2026 Private Island Networks Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* file: base.js
*
*/
"use strict";
const STATUS = {
OK: 10,
FORM: 20,
DUPLICATE: 30,
REDIRECT: 100,
UNBOUND_FORM: 0,
ERROR_CSRF: -1,
ERROR_PARAMS: -2,
ERROR_SERVER: -3,
ERROR_COOKIES: -4,
ERROR_TAMPER: -5,
};
const PORT_PHY0 = 0;
const PORT_PHY1 = 1;
const PORT_PHY2 = 2;
const PORT_PC = 10;
const MSG_FORMAT_BASIC = 0;
const MSG_FORMAT_CONTROLLER = 1;
const MSG_FORMAT_MLE = 2;
const MODE_IDLE = 0;
const MODE_LIVE = 1;
const MODE_FILE = 2;
const MSG_TYPE_NULL = 0
const MSG_TYPE_WRITE = 1;
const MSG_TYPE_READ = 2;
const MSG_TYPE_REPLY_SUCCESS = 3;
const MSG_TYPE_REPLY_ERROR = 4;
const MSG_TYPE_NOTIY = 5;
const MSG_TYPE_MLE = 0X10;
const MSG_MAP = { MSG_TYPE_NULL : "NULL", MSG_TYPE_WRITE : "Write", MSG_TYPE_READ : "Read",
MSG_TYPE_REPLY_SUCCESS : "Success", MSG_TYPE_REPLY_ERROR : "Error", MSG_TYPE_NOTIY : "Notify",
MSG_TYPE_MLE : "MLE"};
/**
* Convert the server's response from JSON (string) to JS object form and
* gracefully handle an exception
*/
function getResponse(resp) {
try {
resp = JSON.parse(resp);
} catch (err) {
resp = {};
resp.d = "";
resp.r = MC_CONSTANTS.SERVER;
}
return resp;
}
function getString(obj) {
var result = "";
for (var key in obj) {
if (result) {
result += "&";
}
result += key + "=" + encodeURIComponent(obj[key]);
}
return result;
}
function getPrefix() {
return window.location.pathname.split('/')[1]; // eg., /todos/<function>
}
/**
* Alert the user depending on error returned by the server. Todo: consider
* using jqm popups instead of alert
*/
function processError(resp) {
$.loading('none');
if (resp.r === MC_CONSTANTS.COOKIES || resp.r === MC_CONSTANTS.CSRF) {
alert("Error saving your submission. Cookies may be blocked. Please modify your browser's settings and try again including refreshing this page");
} else if (resp.r === MC_CONSTANTS.TAMPER) {
alert("Our web server is in an inconsistent state with your browser. Please refresh your page and try again.");
} else {
alert("Server error. Sorry about the trouble. It's been logged, and our team will look into it");
}
}
function $(selector, el) {
if (!el) {
el = document;
}
return el.querySelector(selector);
}
$.loading = function(display) {
$('#mc-loading').style.display = display;
};
$.get = function(url, data, callback) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("progress", updateProgress);
xhr.addEventListener("load", transferComplete);
xhr.addEventListener("error", transferFailed);
xhr.addEventListener("abort", transferCanceled);
if (data) {
url += "?" + getString(data);
}
xhr.open('GET', url, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(null);
function updateProgress(event) {
// was:event.loaded / event.total
console.log('updateProgress');
}
function transferComplete(event) {
callback(event.target.response);
}
function transferFailed(event) {
console.log('transferFailed');
event.preventDefault();
event.stopImmediatePropagation();
}
function transferCanceled(event) {
console.log('transferCanceled');
}
};
$.post = function(url, data, callback) {
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
if (!(data instanceof FormData)) {
xhr.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
data = getString(data);
}
xhr.onreadystatechange = function(event) {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
callback(event.target.response);
}
};
xhr.send(data);
};
function $$(selector, el) {
if (!el) {
el = document;
}
return el.querySelectorAll(selector);
// Note: the returned object is a NodeList.
// If you'd like to convert it to a Array for convenience, use this instead:
// return Array.prototype.slice.call(el.querySelectorAll(selector));
}
// MDN: Assigning to an undeclared variable throws a ReferenceError
function isStrictMode() {
try {
mc_doesNotExist = true;
}
catch(E) {
console.log(E.message);
return true;
}
return false;
}
|