diff options
| author | Private Island Networks Inc <opensource@privateisland.tech> | 2026-03-03 15:56:53 -0500 |
|---|---|---|
| committer | Private Island Networks Inc <opensource@privateisland.tech> | 2026-03-03 15:56:53 -0500 |
| commit | ab6ca080771b706a310ebfd8a4192841cdfef05c (patch) | |
| tree | f9da21650402f17330d68bb7d6f86b031191ddb9 /static/js/base.js | |
Diffstat (limited to 'static/js/base.js')
| -rw-r--r-- | static/js/base.js | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/static/js/base.js b/static/js/base.js new file mode 100644 index 0000000..2920c25 --- /dev/null +++ b/static/js/base.js @@ -0,0 +1,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; +} + + |



