2022-06-18 17:12:38 -04:00
|
|
|
let gSocket;
|
|
|
|
let gCredentials;
|
2022-08-14 14:24:41 -04:00
|
|
|
let gPermissions;
|
2022-06-18 17:12:38 -04:00
|
|
|
|
|
|
|
let gCurrentFile;
|
|
|
|
let gFiles = {};
|
|
|
|
let gApp = {files: {}};
|
|
|
|
let gEditor;
|
|
|
|
let gSplit;
|
|
|
|
let gGraphs = {};
|
|
|
|
let gTimeSeries = {};
|
|
|
|
let gOriginalInput;
|
|
|
|
|
|
|
|
let kErrorColor = "#dc322f";
|
|
|
|
let kStatusColor = "#fff";
|
|
|
|
|
2022-08-14 12:58:26 -04:00
|
|
|
/* Functions that server-side app code can call through the app object. */
|
2022-06-18 17:12:38 -04:00
|
|
|
const k_api = {
|
|
|
|
setDocument: {args: ['content'], func: api_setDocument},
|
|
|
|
postMessage: {args: ['message'], func: api_postMessage},
|
|
|
|
error: {args: ['error'], func: api_error},
|
|
|
|
localStorageSet: {args: ['key', 'value'], func: api_localStorageSet},
|
|
|
|
localStorageGet: {args: ['key'], func: api_localStorageGet},
|
2022-07-26 20:27:10 -04:00
|
|
|
requestPermission: {args: ['permission', 'id'], func: api_requestPermission},
|
2022-08-14 12:58:26 -04:00
|
|
|
print: {args: ['...'], func: api_print},
|
2022-09-14 20:16:37 -04:00
|
|
|
setHash: {args: ['hash'], func: api_setHash},
|
2022-06-18 17:12:38 -04:00
|
|
|
};
|
2017-05-22 15:38:49 -04:00
|
|
|
|
2017-01-16 10:24:44 -05:00
|
|
|
window.addEventListener("keydown", function(event) {
|
2022-02-03 18:57:47 -05:00
|
|
|
if (event.keyCode == 83 && (event.altKey || event.ctrlKey)) {
|
2017-01-16 10:24:44 -05:00
|
|
|
if (editing()) {
|
|
|
|
save();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
} else if (event.keyCode == 66 && event.altKey) {
|
|
|
|
if (editing()) {
|
|
|
|
closeEditor();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function ensureLoaded(nodes, callback) {
|
|
|
|
if (!nodes.length) {
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
let search = nodes.shift();
|
|
|
|
let head = document.head;
|
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < head.childNodes.length; i++) {
|
2017-01-16 10:24:44 -05:00
|
|
|
if (head.childNodes[i].tagName == search.tagName) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let match = true;
|
|
|
|
for (let attribute in search.attributes) {
|
2017-01-16 10:24:44 -05:00
|
|
|
if (head.childNodes[i].attributes[attribute].value != search.attributes[attribute]) {
|
|
|
|
match = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (match) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
ensureLoaded(nodes, callback);
|
|
|
|
} else {
|
2022-06-18 17:12:38 -04:00
|
|
|
let node = document.createElement(search.tagName);
|
2017-01-16 10:24:44 -05:00
|
|
|
node.onreadystatechange = node.onload = function() {
|
|
|
|
ensureLoaded(nodes, callback);
|
|
|
|
};
|
2022-06-18 17:12:38 -04:00
|
|
|
for (let attribute in search.attributes) {
|
2017-01-16 10:24:44 -05:00
|
|
|
node.setAttribute(attribute, search.attributes[attribute]);
|
|
|
|
}
|
|
|
|
head.insertBefore(node, head.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function editing() {
|
|
|
|
return document.getElementById("editPane").style.display != 'none';
|
|
|
|
}
|
|
|
|
|
2022-02-03 18:57:47 -05:00
|
|
|
function toggleEdit() {
|
|
|
|
if (editing()) {
|
|
|
|
closeEditor();
|
|
|
|
} else {
|
|
|
|
edit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 10:24:44 -05:00
|
|
|
function edit() {
|
|
|
|
if (editing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-07 16:06:20 -05:00
|
|
|
window.localStorage.setItem('editing', '1');
|
2022-02-03 18:57:47 -05:00
|
|
|
if (gSplit) {
|
|
|
|
gSplit.destroy();
|
|
|
|
gSplit = undefined;
|
|
|
|
}
|
2022-01-12 21:18:40 -05:00
|
|
|
gSplit = Split(['#editPane', '#viewPane'], {minSize: 0});
|
|
|
|
|
2017-01-16 10:24:44 -05:00
|
|
|
ensureLoaded([
|
2022-06-18 13:07:36 -04:00
|
|
|
{tagName: "script", attributes: {src: "/codemirror/codemirror.min.js"}},
|
|
|
|
{tagName: "link", attributes: {rel: "stylesheet", href: "/codemirror/base16-dark.min.css"}},
|
|
|
|
{tagName: "link", attributes: {rel: "stylesheet", href: "/codemirror/matchesonscrollbar.min.css"}},
|
|
|
|
{tagName: "link", attributes: {rel: "stylesheet", href: "/codemirror/dialog.min.css"}},
|
|
|
|
{tagName: "link", attributes: {rel: "stylesheet", href: "/codemirror/codemirror.min.css"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/trailingspace.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/dialog.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/search.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/searchcursor.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/jump-to-line.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/matchesonscrollbar.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/annotatescrollbar.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/javascript.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/css.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/xml.min.js"}},
|
|
|
|
{tagName: "script", attributes: {src: "/codemirror/htmlmixed.min.js"}},
|
2017-01-16 10:24:44 -05:00
|
|
|
], function() {
|
2022-02-16 21:29:04 -05:00
|
|
|
load().catch(function(error) {
|
|
|
|
alert(error);
|
|
|
|
closeEditor();
|
|
|
|
});
|
2017-01-16 10:24:44 -05:00
|
|
|
});
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2022-02-26 17:51:35 -05:00
|
|
|
function hideFiles() {
|
2022-03-07 16:06:20 -05:00
|
|
|
window.localStorage.setItem('files', '0');
|
2022-02-26 17:51:35 -05:00
|
|
|
document.getElementById('filesPane').classList.add('collapsed');
|
|
|
|
}
|
|
|
|
|
|
|
|
function showFiles() {
|
2022-03-07 16:06:20 -05:00
|
|
|
window.localStorage.setItem('files', '1');
|
2022-02-26 17:51:35 -05:00
|
|
|
document.getElementById('filesPane').classList.remove('collapsed');
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:10:45 -05:00
|
|
|
function trace() {
|
2023-02-17 19:51:22 -05:00
|
|
|
window.open(`/speedscope/#profileURL=${encodeURIComponent('/trace')}`);
|
2022-01-02 14:10:45 -05:00
|
|
|
}
|
|
|
|
|
2022-01-20 21:53:15 -05:00
|
|
|
function stats() {
|
2022-03-07 16:06:20 -05:00
|
|
|
window.localStorage.setItem('stats', '1');
|
2022-01-20 21:53:15 -05:00
|
|
|
document.getElementById("statsPane").style.display = 'flex';
|
2022-02-26 16:17:15 -05:00
|
|
|
send({action: 'enableStats', enabled: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeStats() {
|
2022-03-07 16:06:20 -05:00
|
|
|
window.localStorage.setItem('stats', '0');
|
2022-02-26 16:17:15 -05:00
|
|
|
document.getElementById("statsPane").style.display = 'none';
|
|
|
|
send({action: 'enableStats', enabled: false});
|
2022-01-20 21:53:15 -05:00
|
|
|
}
|
|
|
|
|
2022-02-05 18:04:04 -05:00
|
|
|
function toggleStats() {
|
|
|
|
if (document.getElementById("statsPane").style.display == 'none') {
|
|
|
|
stats();
|
|
|
|
} else {
|
|
|
|
closeStats();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
function guessMode(name) {
|
|
|
|
return name.endsWith(".js") ? "javascript" :
|
|
|
|
name.endsWith(".html") ? "htmlmixed" :
|
|
|
|
null;
|
|
|
|
}
|
2017-01-16 10:24:44 -05:00
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
function loadFile(name, id) {
|
2022-02-16 21:29:04 -05:00
|
|
|
return fetch('/' + id + '/view').then(function(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Request failed: ' + response.status + ' ' + response.statusText);
|
|
|
|
}
|
|
|
|
return response.text();
|
|
|
|
}).then(function(text) {
|
|
|
|
gFiles[name].doc = new CodeMirror.Doc(text, guessMode(name));
|
|
|
|
if (!Object.values(gFiles).some(x => !x.doc)) {
|
|
|
|
document.getElementById("editPane").style.display = 'flex';
|
|
|
|
openFile(Object.keys(gFiles).sort()[0]);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
});
|
2017-01-16 10:24:44 -05:00
|
|
|
}
|
|
|
|
|
2022-01-30 09:51:09 -05:00
|
|
|
function load(path) {
|
2022-02-16 21:29:04 -05:00
|
|
|
return fetch((path || url()) + 'view').then(function(response) {
|
|
|
|
if (!response.ok) {
|
2022-02-26 21:35:51 -05:00
|
|
|
if (response.status == 404) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
throw new Error(response.status + ' ' + response.statusText);
|
|
|
|
}
|
2022-02-16 21:29:04 -05:00
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
}).then(function(json) {
|
|
|
|
if (!gEditor) {
|
|
|
|
gEditor = CodeMirror.fromTextArea(document.getElementById("editor"), {
|
|
|
|
'theme': 'base16-dark',
|
|
|
|
'lineNumbers': true,
|
|
|
|
'tabSize': 4,
|
|
|
|
'indentUnit': 4,
|
|
|
|
'indentWithTabs': true,
|
|
|
|
'showTrailingSpace': true,
|
|
|
|
});
|
|
|
|
gEditor.on('changes', function() {
|
|
|
|
updateFiles();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
gFiles = {};
|
2022-06-18 17:12:38 -04:00
|
|
|
let isApp = false;
|
|
|
|
let promises = [];
|
2022-02-16 21:29:04 -05:00
|
|
|
|
|
|
|
if (json && json['type'] == 'tildefriends-app') {
|
|
|
|
isApp = true;
|
|
|
|
Object.keys(json['files']).forEach(function(name) {
|
|
|
|
gFiles[name] = {};
|
|
|
|
promises.push(loadFile(name, json['files'][name]));
|
|
|
|
});
|
|
|
|
if (Object.keys(json['files']).length == 0) {
|
|
|
|
document.getElementById("editPane").style.display = 'flex';
|
2017-01-18 18:21:42 -05:00
|
|
|
}
|
2022-02-16 21:29:04 -05:00
|
|
|
gApp = json;
|
|
|
|
}
|
|
|
|
if (!isApp) {
|
|
|
|
document.getElementById("editPane").style.display = 'flex';
|
2022-06-18 17:12:38 -04:00
|
|
|
let text = '// New script.\n';
|
2022-02-16 21:29:04 -05:00
|
|
|
gCurrentFile = 'app.js';
|
|
|
|
gFiles[gCurrentFile] = {
|
|
|
|
doc: new CodeMirror.Doc(text, guessMode(gCurrentFile)),
|
|
|
|
};
|
|
|
|
openFile(gCurrentFile);
|
|
|
|
}
|
|
|
|
return Promise.all(promises);
|
2017-01-16 10:24:44 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeEditor() {
|
2022-03-07 16:06:20 -05:00
|
|
|
window.localStorage.setItem('editing', '0');
|
2017-01-16 10:24:44 -05:00
|
|
|
document.getElementById("editPane").style.display = 'none';
|
2022-01-20 21:53:15 -05:00
|
|
|
if (gSplit) {
|
|
|
|
gSplit.destroy();
|
|
|
|
gSplit = undefined;
|
|
|
|
}
|
2017-01-16 10:24:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function explodePath() {
|
|
|
|
return /^\/~([^\/]+)\/([^\/]+)(.*)/.exec(window.location.pathname);
|
|
|
|
}
|
|
|
|
|
2022-01-30 09:51:09 -05:00
|
|
|
function save(save_to) {
|
2017-01-16 10:24:44 -05:00
|
|
|
document.getElementById("save").disabled = true;
|
2021-01-02 13:10:00 -05:00
|
|
|
if (gCurrentFile) {
|
|
|
|
gFiles[gCurrentFile].doc = gEditor.getDoc();
|
|
|
|
}
|
2017-01-16 10:24:44 -05:00
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
let save_path = save_to;
|
2022-02-16 21:29:04 -05:00
|
|
|
if (!save_path) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let name = document.getElementById("name");
|
2022-01-30 09:51:09 -05:00
|
|
|
if (name && name.value) {
|
|
|
|
save_path = name.value;
|
|
|
|
} else {
|
|
|
|
save_path = url();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
let promises = [];
|
2022-02-16 21:29:04 -05:00
|
|
|
for (let name of Object.keys(gFiles)) {
|
|
|
|
let file = gFiles[name];
|
2021-01-12 21:40:46 -05:00
|
|
|
if (file.doc.isClean(file.generation)) {
|
2022-02-16 21:29:04 -05:00
|
|
|
continue;
|
2021-01-12 21:40:46 -05:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
delete file.id;
|
2022-02-16 21:29:04 -05:00
|
|
|
promises.push(fetch('/save', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
},
|
|
|
|
body: file.doc.getValue(),
|
|
|
|
}).then(function(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Saving "' + name + '": ' + response.status + ' ' + response.statusText);
|
|
|
|
}
|
|
|
|
return response.text();
|
|
|
|
}).then(function(text) {
|
|
|
|
file.id = text;
|
|
|
|
if (file.id.charAt(0) == '/') {
|
|
|
|
file.id = file.id.substr(1);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises).then(function() {
|
2022-06-18 17:12:38 -04:00
|
|
|
let app = {
|
2022-02-16 21:29:04 -05:00
|
|
|
type: "tildefriends-app",
|
|
|
|
files: Object.fromEntries(Object.keys(gFiles).map(x => [x, gFiles[x].id || gApp.files[x]])),
|
|
|
|
};
|
|
|
|
Object.values(gFiles).forEach(function(file) { delete file.id; });
|
|
|
|
gApp = JSON.parse(JSON.stringify(app));
|
|
|
|
|
|
|
|
return fetch(save_path + 'save', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(app),
|
|
|
|
}).then(function(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.status + ' ' + response.statusText);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save_path != window.location.pathname) {
|
|
|
|
alert('Saved to ' + save_path + '.');
|
2021-01-02 13:10:00 -05:00
|
|
|
} else {
|
2022-02-16 21:29:04 -05:00
|
|
|
reconnect(save_path);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
});
|
2022-02-16 21:29:04 -05:00
|
|
|
}).catch(function(error) {
|
|
|
|
alert(error);
|
|
|
|
}).finally(function() {
|
|
|
|
document.getElementById("save").disabled = false;
|
|
|
|
Object.values(gFiles).forEach(function(file) {
|
|
|
|
file.generation = file.doc.changeGeneration();
|
2021-01-02 13:10:00 -05:00
|
|
|
});
|
2022-02-16 21:29:04 -05:00
|
|
|
updateFiles();
|
2021-01-02 13:10:00 -05:00
|
|
|
});
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2022-06-20 14:13:19 -04:00
|
|
|
function deleteApp() {
|
|
|
|
let name = document.getElementById("name");
|
|
|
|
let path = name && name.value ? name.value : url();
|
|
|
|
|
|
|
|
if (confirm(`Are you sure you want to delete the app '${path}'?`)) {
|
|
|
|
fetch(path + 'delete').then(function(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.status + ' ' + response.statusText);
|
|
|
|
}
|
|
|
|
alert('Deleted.');
|
|
|
|
}).catch(function(error) {
|
|
|
|
alert(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
function url() {
|
2022-06-18 17:12:38 -04:00
|
|
|
let hash = window.location.href.indexOf('#');
|
|
|
|
let question = window.location.href.indexOf('?');
|
|
|
|
let end = -1;
|
2016-04-06 21:30:07 -04:00
|
|
|
if (hash != -1 && (hash < end || end == -1))
|
|
|
|
{
|
|
|
|
end = hash;
|
|
|
|
}
|
|
|
|
if (question != -1 && (question < end || end == -1))
|
|
|
|
{
|
|
|
|
end = question;
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
return end != -1 ? window.location.href.substring(0, end) : window.location.href;
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:30:07 -04:00
|
|
|
function hash() {
|
|
|
|
return window.location.hash != "#" ? window.location.hash : "";
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
function api_setDocument(content) {
|
|
|
|
let iframe = document.getElementById("document");
|
|
|
|
iframe.srcdoc = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
function api_postMessage(message) {
|
|
|
|
let iframe = document.getElementById("document");
|
|
|
|
iframe.contentWindow.postMessage(message, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
function api_error(error) {
|
|
|
|
if (error) {
|
|
|
|
if (typeof(error) == 'string') {
|
|
|
|
setStatusMessage('⚠️ ' + error, '#f00');
|
|
|
|
} else {
|
2022-06-19 14:01:21 -04:00
|
|
|
setStatusMessage('⚠️ ' + error.message + '\n' + error.stack, '#f00');
|
2022-06-18 17:12:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log('error', error);
|
|
|
|
}
|
|
|
|
|
|
|
|
function api_localStorageSet(key, value) {
|
|
|
|
window.localStorage.setItem('app:' + key, value);
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:16:18 -05:00
|
|
|
function api_localStorageGet(key) {
|
2022-08-13 14:58:06 -04:00
|
|
|
return window.localStorage.getItem('app:' + key);
|
2022-06-18 17:12:38 -04:00
|
|
|
}
|
|
|
|
|
2022-07-26 20:27:10 -04:00
|
|
|
function api_requestPermission(permission, id) {
|
|
|
|
let permissions = document.getElementById('permissions');
|
2022-08-07 18:39:58 -04:00
|
|
|
|
|
|
|
let container = document.createElement('div');
|
|
|
|
container.classList.add('permissions_contents');
|
|
|
|
|
2022-07-26 20:27:10 -04:00
|
|
|
let div = document.createElement('div');
|
2022-08-07 18:39:58 -04:00
|
|
|
div.appendChild(document.createTextNode('This app is requesting the following permission:'));
|
|
|
|
let span = document.createElement('span');
|
|
|
|
span.style = 'font-weight: bold';
|
|
|
|
span.appendChild(document.createTextNode(permission));
|
|
|
|
div.appendChild(span);
|
|
|
|
container.appendChild(div);
|
|
|
|
|
|
|
|
div = document.createElement('div');
|
|
|
|
div.style = 'padding: 1em';
|
|
|
|
let check = document.createElement('input');
|
|
|
|
check.id = 'permissions_remember_check';
|
|
|
|
check.type = 'checkbox';
|
|
|
|
div.appendChild(check);
|
|
|
|
let label = document.createElement('label');
|
|
|
|
label.htmlFor = check.id;
|
|
|
|
label.appendChild(document.createTextNode('Remember this decision.'));
|
|
|
|
div.appendChild(label);
|
|
|
|
container.appendChild(div);
|
|
|
|
|
|
|
|
const k_options = [
|
|
|
|
{
|
|
|
|
text: '✅ Allow',
|
|
|
|
grant: ['allow once', 'allow'],
|
|
|
|
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '❌ Deny',
|
|
|
|
grant: ['deny once', 'deny'],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-08-13 21:46:11 -04:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
div = document.createElement('div');
|
|
|
|
for (let option of k_options) {
|
|
|
|
let button = document.createElement('button');
|
|
|
|
button.innerText = option.text;
|
|
|
|
button.onclick = function() {
|
|
|
|
resolve(option.grant[check.checked ? 1 : 0]);
|
|
|
|
while (permissions.firstChild) {
|
|
|
|
permissions.removeChild(permissions.firstChild);
|
|
|
|
}
|
|
|
|
permissions.style.visibility = 'hidden';
|
2022-08-07 18:39:58 -04:00
|
|
|
}
|
2022-08-13 21:46:11 -04:00
|
|
|
div.appendChild(button);
|
2022-07-26 20:27:10 -04:00
|
|
|
}
|
2022-08-13 21:46:11 -04:00
|
|
|
container.appendChild(div);
|
2022-08-07 18:39:58 -04:00
|
|
|
|
2022-08-13 21:46:11 -04:00
|
|
|
permissions.appendChild(container);
|
|
|
|
permissions.style.visibility = 'visible';
|
|
|
|
});
|
2022-07-26 20:27:10 -04:00
|
|
|
}
|
|
|
|
|
2022-08-14 12:58:26 -04:00
|
|
|
function api_print() {
|
|
|
|
console.log('app>', ...arguments);
|
|
|
|
}
|
|
|
|
|
2022-09-14 20:16:37 -04:00
|
|
|
function api_setHash(hash) {
|
|
|
|
window.location.hash = hash;
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:24:41 -04:00
|
|
|
function hidePermissions() {
|
|
|
|
let permissions = document.getElementById('permissions_settings');
|
|
|
|
while (permissions.firstChild) {
|
|
|
|
permissions.removeChild(permissions.firstChild);
|
|
|
|
}
|
|
|
|
permissions.style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
|
|
|
|
function showPermissions() {
|
|
|
|
let permissions = document.getElementById('permissions_settings');
|
|
|
|
|
|
|
|
let container = document.createElement('div');
|
|
|
|
container.classList.add('permissions_contents');
|
|
|
|
|
|
|
|
let div = document.createElement('div');
|
|
|
|
div.appendChild(document.createTextNode('This app has the following permission:'));
|
|
|
|
for (let key of Object.keys(gPermissions || {})) {
|
|
|
|
let row = document.createElement('div');
|
|
|
|
|
|
|
|
let span = document.createElement('span');
|
|
|
|
span.appendChild(document.createTextNode(key));
|
|
|
|
row.appendChild(span);
|
|
|
|
|
|
|
|
span = document.createElement('span');
|
|
|
|
span.appendChild(document.createTextNode(': '));
|
|
|
|
row.appendChild(span);
|
|
|
|
|
|
|
|
span = document.createElement('span');
|
|
|
|
span.appendChild(document.createTextNode(gPermissions[key] ? '✅ Allowed' : '❌ Denied'));
|
|
|
|
row.appendChild(span);
|
|
|
|
|
|
|
|
span = document.createElement('span');
|
|
|
|
span.appendChild(document.createTextNode(' '));
|
|
|
|
row.appendChild(span);
|
|
|
|
|
|
|
|
let button = document.createElement('button');
|
|
|
|
button.innerText = 'Reset';
|
|
|
|
button.onclick = function() {
|
|
|
|
send({action: "resetPermission", permission: key});
|
|
|
|
};
|
|
|
|
row.appendChild(button);
|
|
|
|
div.appendChild(row);
|
|
|
|
}
|
|
|
|
container.appendChild(div);
|
|
|
|
|
|
|
|
div = document.createElement('div');
|
|
|
|
let button = document.createElement('button');
|
|
|
|
button.innerText = 'Close';
|
|
|
|
button.onclick = function() {
|
|
|
|
hidePermissions();
|
|
|
|
}
|
|
|
|
div.appendChild(button);
|
|
|
|
container.appendChild(div);
|
|
|
|
|
|
|
|
permissions.appendChild(container);
|
|
|
|
permissions.style.visibility = 'visible';
|
|
|
|
}
|
|
|
|
|
2022-08-14 12:58:26 -04:00
|
|
|
function _receive_websocket_message(message) {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (message && message.action == "session") {
|
2022-06-18 16:51:22 -04:00
|
|
|
setStatusMessage("🟢 Executing...", kStatusColor);
|
2021-01-02 13:10:00 -05:00
|
|
|
gCredentials = message.credentials;
|
|
|
|
updateLogin();
|
2022-08-14 14:24:41 -04:00
|
|
|
} else if (message && message.action == 'permissions') {
|
|
|
|
gPermissions = message.permissions;
|
|
|
|
let permissions = document.getElementById('permissions_settings');
|
|
|
|
if (permissions.firstChild) {
|
|
|
|
hidePermissions();
|
|
|
|
showPermissions();
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
} else if (message && message.action == "ready") {
|
|
|
|
setStatusMessage(null);
|
|
|
|
if (window.location.hash) {
|
|
|
|
send({event: "hashChange", hash: window.location.hash});
|
|
|
|
}
|
2022-03-07 16:06:20 -05:00
|
|
|
if (window.localStorage.getItem('stats') == '1') {
|
|
|
|
/* Stats were opened before we connected. */
|
|
|
|
send({action: 'enableStats', enabled: true});
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
} else if (message && message.action == "ping") {
|
2022-01-29 15:43:19 -05:00
|
|
|
send({action: "pong"});
|
2022-01-20 21:53:15 -05:00
|
|
|
} else if (message && message.action == "stats") {
|
2022-06-18 17:12:38 -04:00
|
|
|
let now = new Date().getTime();
|
|
|
|
for (let key of Object.keys(message.stats)) {
|
2022-06-04 12:38:45 -04:00
|
|
|
const k_groups = {
|
|
|
|
rpc_in: {group: 'rpc', name: 'in'},
|
|
|
|
rpc_out: {group: 'rpc', name: 'out'},
|
|
|
|
|
2023-02-28 20:36:26 -05:00
|
|
|
cpu_percent: {group: 'cpu', name: 'main'},
|
|
|
|
thread_percent: {group: 'cpu', name: 'work'},
|
|
|
|
|
2022-06-17 17:18:10 -04:00
|
|
|
arena_percent: {group: 'memory', name: 'm'},
|
2022-06-04 12:38:45 -04:00
|
|
|
js_malloc_percent: {group: 'memory', name: 'js'},
|
2022-06-17 17:18:10 -04:00
|
|
|
memory_percent: {group: 'memory', name: 'tot'},
|
2022-06-04 12:38:45 -04:00
|
|
|
sqlite3_memory_percent: {group: 'memory', name: 'sql'},
|
2022-06-04 13:04:51 -04:00
|
|
|
tf_malloc_percent: {group: 'memory', name: 'tf'},
|
2022-06-04 12:38:45 -04:00
|
|
|
tls_malloc_percent: {group: 'memory', name: 'tls'},
|
|
|
|
uv_malloc_percent: {group: 'memory', name: 'uv'},
|
|
|
|
|
2023-01-18 17:52:54 -05:00
|
|
|
messages_stored: {group: 'stored', name: 'messages'},
|
|
|
|
blobs_stored: {group: 'stored', name: 'blobs'},
|
|
|
|
|
2022-06-04 12:38:45 -04:00
|
|
|
socket_count: {group: 'socket', name: 'total'},
|
|
|
|
socket_open_count: {group: 'socket', name: 'open'},
|
|
|
|
|
|
|
|
import_count: {group: 'functions', name: 'imports'},
|
|
|
|
export_count: {group: 'functions', name: 'exports'},
|
|
|
|
};
|
2022-06-17 17:18:10 -04:00
|
|
|
const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff', '#f00', '#888'];
|
2022-06-04 12:38:45 -04:00
|
|
|
let graph_key = k_groups[key]?.group || key;
|
|
|
|
let graph = gGraphs[graph_key];
|
|
|
|
if (!graph) {
|
|
|
|
graph = {
|
2022-01-20 22:09:23 -05:00
|
|
|
chart: new SmoothieChart({
|
2022-02-05 12:52:37 -05:00
|
|
|
millisPerPixel: 100,
|
|
|
|
minValue: 0,
|
2022-01-20 22:09:23 -05:00
|
|
|
grid: {
|
2022-02-05 12:52:37 -05:00
|
|
|
millisPerLine: 1000,
|
|
|
|
verticalSections: 10,
|
2022-01-20 22:09:23 -05:00
|
|
|
},
|
2022-06-04 12:38:45 -04:00
|
|
|
tooltip: true,
|
2022-01-20 22:09:23 -05:00
|
|
|
}),
|
|
|
|
canvas: document.createElement('canvas'),
|
2022-06-04 12:38:45 -04:00
|
|
|
title: document.createElement('div'),
|
|
|
|
series: [],
|
2022-01-20 22:09:23 -05:00
|
|
|
};
|
2022-06-04 12:38:45 -04:00
|
|
|
gGraphs[graph_key] = graph;
|
2022-03-07 16:06:20 -05:00
|
|
|
graph.canvas.width = 240;
|
|
|
|
graph.canvas.height = 64;
|
2022-06-04 12:38:45 -04:00
|
|
|
graph.title.innerText = graph_key;
|
|
|
|
graph.title.style.flex = '0';
|
|
|
|
document.getElementById('graphs').appendChild(graph.title);
|
2022-01-20 22:09:23 -05:00
|
|
|
document.getElementById('graphs').appendChild(graph.canvas);
|
|
|
|
graph.chart.streamTo(graph.canvas, 1000);
|
2022-01-20 21:53:15 -05:00
|
|
|
}
|
2022-06-04 12:38:45 -04:00
|
|
|
|
|
|
|
let timeseries = gTimeSeries[key];
|
|
|
|
if (!timeseries) {
|
|
|
|
let is_multi = key != graph_key || graph.series.length > 1;
|
|
|
|
timeseries = new TimeSeries();
|
|
|
|
gTimeSeries[key] = timeseries;
|
|
|
|
graph.chart.addTimeSeries(timeseries, {lineWidth: 2, strokeStyle: is_multi ? k_colors[graph.series.length] : '#fff'});
|
|
|
|
graph.series.push(k_groups[key]?.name || key);
|
|
|
|
if (is_multi) {
|
|
|
|
while (graph.title.firstChild) {
|
|
|
|
graph.title.removeChild(graph.title.firstChild);
|
|
|
|
}
|
|
|
|
function makeColoredText(text, color) {
|
|
|
|
let element = document.createElement('span');
|
|
|
|
element.style.color = color;
|
|
|
|
element.innerText = text;
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
graph.title.appendChild(makeColoredText(graph_key + ':', '#fff'));
|
|
|
|
for (let series of graph.series) {
|
|
|
|
graph.title.appendChild(makeColoredText(' ' + series, k_colors[graph.series.indexOf(series)]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timeseries.append(now, message.stats[key]);
|
2022-01-17 16:46:32 -05:00
|
|
|
}
|
2022-08-13 14:58:06 -04:00
|
|
|
} else if (message &&
|
|
|
|
message.message === 'tfrpc' &&
|
|
|
|
message.method) {
|
|
|
|
let api = k_api[message.method];
|
2023-01-20 19:16:18 -05:00
|
|
|
let id = message.id;
|
|
|
|
let params = message.params;
|
2022-06-18 17:12:38 -04:00
|
|
|
if (api) {
|
2023-01-20 19:16:18 -05:00
|
|
|
Promise.resolve(api.func(...params)).then(function(result) {
|
2022-08-13 14:58:06 -04:00
|
|
|
send({
|
|
|
|
message: 'tfrpc',
|
2023-01-20 19:16:18 -05:00
|
|
|
id: id,
|
2022-08-13 14:58:06 -04:00
|
|
|
result: result,
|
|
|
|
});
|
|
|
|
}).catch(function(error) {
|
|
|
|
send({
|
|
|
|
message: 'tfrpc',
|
2023-01-20 19:16:18 -05:00
|
|
|
id: id,
|
2022-08-13 14:58:06 -04:00
|
|
|
error: error,
|
|
|
|
});
|
|
|
|
});
|
2022-06-18 17:12:38 -04:00
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 19:01:55 -04:00
|
|
|
function keyEvent(event) {
|
|
|
|
send({
|
|
|
|
event: "key",
|
|
|
|
type: event.type,
|
|
|
|
which: event.which,
|
|
|
|
keyCode: event.keyCode,
|
|
|
|
charCode: event.charCode,
|
|
|
|
character: String.fromCharCode(event.keyCode || event.which),
|
2016-06-02 19:48:45 -04:00
|
|
|
altKey: event.altKey,
|
2016-06-02 19:01:55 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-18 16:51:22 -04:00
|
|
|
function setStatusMessage(message, color) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let node = document.getElementById("status");
|
2022-06-18 16:51:22 -04:00
|
|
|
while (node.firstChild) {
|
|
|
|
node.removeChild(node.firstChild);
|
2016-04-10 20:28:42 -04:00
|
|
|
}
|
2016-05-07 07:07:54 -04:00
|
|
|
if (message) {
|
|
|
|
node.appendChild(document.createTextNode(message));
|
2021-01-02 13:10:00 -05:00
|
|
|
node.setAttribute("style", "display: inline-block; vertical-align: top; white-space: pre; color: " + (color || kErrorColor));
|
2016-05-07 07:07:54 -04:00
|
|
|
}
|
2016-04-10 20:28:42 -04:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
function send(value) {
|
2016-04-10 20:09:21 -04:00
|
|
|
try {
|
2022-06-18 16:51:22 -04:00
|
|
|
if (gSocket && gSocket.readyState == gSocket.OPEN) {
|
|
|
|
gSocket.send(JSON.stringify(value));
|
|
|
|
}
|
2016-04-10 20:09:21 -04:00
|
|
|
} catch (error) {
|
2022-06-18 16:51:22 -04:00
|
|
|
setStatusMessage('🤷 Send failed: ' + error.toString(), kErrorColor);
|
2016-04-10 20:09:21 -04:00
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateLogin() {
|
2022-06-18 17:12:38 -04:00
|
|
|
let login = document.getElementById("login");
|
2016-03-12 13:50:43 -05:00
|
|
|
while (login.firstChild) {
|
|
|
|
login.removeChild(login.firstChild);
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
let a = document.createElement("a");
|
2016-03-12 13:50:43 -05:00
|
|
|
if (gCredentials && gCredentials.session) {
|
|
|
|
a.appendChild(document.createTextNode("logout " + gCredentials.session.name));
|
2021-01-02 13:10:00 -05:00
|
|
|
a.setAttribute("href", "/login/logout?return=" + encodeURIComponent(url() + hash()));
|
2016-03-12 13:50:43 -05:00
|
|
|
} else {
|
2016-04-14 19:37:23 -04:00
|
|
|
a.appendChild(document.createTextNode("login"));
|
|
|
|
a.setAttribute("href", "/login?return=" + encodeURIComponent(url() + hash()));
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
login.appendChild(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dragHover(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
2022-06-18 17:12:38 -04:00
|
|
|
let input = document.getElementById("input");
|
2016-03-12 13:50:43 -05:00
|
|
|
if (event.type == "dragover") {
|
2016-04-11 11:54:26 -04:00
|
|
|
if (!input.classList.contains("drop")) {
|
|
|
|
input.classList.add("drop");
|
|
|
|
gOriginalInput = input.value;
|
|
|
|
input.value = "drop file to upload";
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-04-11 11:54:26 -04:00
|
|
|
input.classList.remove("drop");
|
|
|
|
input.value = gOriginalInput;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixImage(sourceData, maxWidth, maxHeight, callback) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let result = sourceData;
|
|
|
|
let image = new Image();
|
2016-03-12 13:50:43 -05:00
|
|
|
image.crossOrigin = "anonymous";
|
|
|
|
image.referrerPolicy = "no-referrer";
|
|
|
|
image.onload = function() {
|
|
|
|
if (image.width > maxWidth || image.height > maxHeight) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let downScale = Math.min(maxWidth / image.width, maxHeight / image.height);
|
|
|
|
let canvas = document.createElement("canvas");
|
2016-03-12 13:50:43 -05:00
|
|
|
canvas.width = image.width * downScale;
|
|
|
|
canvas.height = image.height * downScale;
|
2022-06-18 17:12:38 -04:00
|
|
|
let context = canvas.getContext("2d");
|
2016-03-12 13:50:43 -05:00
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
image.width = canvas.width;
|
|
|
|
image.height = canvas.height;
|
|
|
|
context.drawImage(image, 0, 0, image.width, image.height);
|
|
|
|
result = canvas.toDataURL();
|
|
|
|
}
|
|
|
|
callback(result);
|
|
|
|
};
|
|
|
|
image.src = sourceData;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendImage(image) {
|
|
|
|
fixImage(image, 320, 240, function(result) {
|
|
|
|
send({image: result});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileDropRead(event) {
|
|
|
|
sendImage(event.target.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileDrop(event) {
|
|
|
|
dragHover(event);
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
let done = false;
|
2016-03-12 13:50:43 -05:00
|
|
|
if (!done) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let files = event.target.files || event.dataTransfer.files;
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
let file = files[i];
|
2016-03-12 13:50:43 -05:00
|
|
|
if (file.type.substring(0, "image/".length) == "image/") {
|
2022-06-18 17:12:38 -04:00
|
|
|
let reader = new FileReader();
|
2016-03-12 13:50:43 -05:00
|
|
|
reader.onloadend = fileDropRead;
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let html = event.dataTransfer.getData("text/html");
|
|
|
|
let match = /<img.*src="([^"]+)"/.exec(html);
|
2016-03-12 13:50:43 -05:00
|
|
|
if (match) {
|
|
|
|
sendImage(match[1]);
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!done) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let text = event.dataTransfer.getData("text/plain");
|
2016-03-12 13:50:43 -05:00
|
|
|
if (text) {
|
|
|
|
send(text);
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableDragDrop() {
|
2022-06-18 17:12:38 -04:00
|
|
|
let body = document.body;
|
2016-03-12 13:50:43 -05:00
|
|
|
body.addEventListener("dragover", dragHover);
|
|
|
|
body.addEventListener("dragleave", dragHover);
|
|
|
|
body.addEventListener("drop", fileDrop);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hashChange() {
|
|
|
|
send({event: 'hashChange', hash: window.location.hash});
|
|
|
|
}
|
|
|
|
|
|
|
|
function focus() {
|
2016-05-07 07:07:54 -04:00
|
|
|
if (gSocket && gSocket.readyState == gSocket.CLOSED) {
|
|
|
|
connectSocket();
|
|
|
|
} else {
|
|
|
|
send({event: "focus"});
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function blur() {
|
2016-05-07 07:07:54 -04:00
|
|
|
if (gSocket && gSocket.readyState == gSocket.OPEN) {
|
|
|
|
send({event: "blur"});
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
function message(event) {
|
2016-09-17 16:53:03 -04:00
|
|
|
if (event.data && event.data.event == "resizeMe" && event.data.width && event.data.height) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let iframe = document.getElementById("iframe_" + event.data.name);
|
2016-09-17 16:53:03 -04:00
|
|
|
iframe.setAttribute("width", event.data.width);
|
|
|
|
iframe.setAttribute("height", event.data.height);
|
2022-01-06 20:52:47 -05:00
|
|
|
} else if (event.data && event.data.action == "setHash") {
|
|
|
|
window.location.hash = event.data.hash;
|
2022-01-27 22:11:09 -05:00
|
|
|
} else if (event.data && event.data.action == 'storeBlob') {
|
2022-02-16 21:29:04 -05:00
|
|
|
fetch('/save', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/binary',
|
|
|
|
},
|
|
|
|
body: event.data.blob.buffer,
|
|
|
|
}).then(function(response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.status + ' ' + response.statusText);
|
2022-01-27 22:11:09 -05:00
|
|
|
}
|
2022-02-16 21:29:04 -05:00
|
|
|
return response.text();
|
|
|
|
}).then(function(text) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let iframe = document.getElementById("document");
|
2022-04-14 19:47:06 -04:00
|
|
|
iframe.contentWindow.postMessage({'storeBlobComplete': {name: event.data.blob.name, path: text, type: event.data.blob.type, context: event.data.context}}, '*');
|
2022-01-27 22:11:09 -05:00
|
|
|
});
|
2016-09-17 16:53:03 -04:00
|
|
|
} else {
|
2021-01-02 13:10:00 -05:00
|
|
|
send({event: "message", message: event.data});
|
2016-05-01 09:24:37 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-10 20:09:21 -04:00
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
function reconnect(path) {
|
2017-01-16 10:24:44 -05:00
|
|
|
let oldSocket = gSocket;
|
|
|
|
gSocket = null
|
2023-03-12 18:16:18 -04:00
|
|
|
if (oldSocket) {
|
|
|
|
oldSocket.onopen = null;
|
|
|
|
oldSocket.onclose = null;
|
|
|
|
oldSocket.onmessage = null;
|
|
|
|
oldSocket.close();
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
connectSocket(path);
|
2017-01-16 10:24:44 -05:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
function connectSocket(path) {
|
2021-01-19 21:01:14 -05:00
|
|
|
if (!gSocket || gSocket.readyState != gSocket.OPEN) {
|
|
|
|
if (gSocket) {
|
|
|
|
gSocket.onopen = null;
|
|
|
|
gSocket.onclose = null;
|
|
|
|
gSocket.onmessage = null;
|
|
|
|
gSocket.close();
|
|
|
|
}
|
2022-06-18 16:51:22 -04:00
|
|
|
setStatusMessage("⚪ Connecting...", kStatusColor);
|
2016-05-07 07:07:54 -04:00
|
|
|
gSocket = new WebSocket(
|
|
|
|
(window.location.protocol == "https:" ? "wss://" : "ws://")
|
|
|
|
+ window.location.hostname
|
|
|
|
+ (window.location.port.length ? ":" + window.location.port : "")
|
2021-01-02 13:10:00 -05:00
|
|
|
+ "/app/socket");
|
2016-05-07 07:07:54 -04:00
|
|
|
gSocket.onopen = function() {
|
2022-06-18 16:51:22 -04:00
|
|
|
setStatusMessage("🟡 Authenticating...", kStatusColor);
|
2022-08-07 21:48:23 -04:00
|
|
|
let connect_path = path ?? window.location.pathname;
|
2016-05-07 07:07:54 -04:00
|
|
|
gSocket.send(JSON.stringify({
|
|
|
|
action: "hello",
|
2022-08-07 21:48:23 -04:00
|
|
|
path: connect_path,
|
2022-06-18 17:12:38 -04:00
|
|
|
api: Object.entries(k_api).map(([key, value]) => [].concat([key], value.args)),
|
2016-05-07 07:07:54 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
gSocket.onmessage = function(event) {
|
2022-08-14 12:58:26 -04:00
|
|
|
_receive_websocket_message(JSON.parse(event.data));
|
2016-05-07 07:07:54 -04:00
|
|
|
}
|
|
|
|
gSocket.onclose = function(event) {
|
2022-01-29 15:43:19 -05:00
|
|
|
const k_codes = {
|
|
|
|
1000: 'Normal closure',
|
|
|
|
1001: 'Going away',
|
|
|
|
1002: 'Protocol error',
|
|
|
|
1003: 'Unsupported data',
|
|
|
|
1005: 'No status received',
|
|
|
|
1006: 'Abnormal closure',
|
|
|
|
1007: 'Invalid frame payload data',
|
|
|
|
1008: 'Policy violation',
|
|
|
|
1009: 'Message too big',
|
|
|
|
1010: 'Missing extension',
|
|
|
|
1011: 'Internal error',
|
|
|
|
1012: 'Service restart',
|
|
|
|
1013: 'Try again later',
|
|
|
|
1014: 'Bad gateway',
|
|
|
|
1015: 'TLS handshake',
|
|
|
|
};
|
2022-06-18 16:51:22 -04:00
|
|
|
setStatusMessage("🔴 Closed: " + (k_codes[event.code] || event.code), kErrorColor);
|
2016-05-07 07:07:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
function openFile(name) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let newDoc = (name && gFiles[name]) ? gFiles[name].doc : new CodeMirror.Doc("", guessMode(name));
|
|
|
|
let oldDoc = gEditor.swapDoc(newDoc);
|
2021-01-02 13:10:00 -05:00
|
|
|
if (gFiles[gCurrentFile]) {
|
|
|
|
gFiles[gCurrentFile].doc = oldDoc;
|
|
|
|
}
|
|
|
|
gCurrentFile = name;
|
|
|
|
updateFiles();
|
|
|
|
gEditor.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onFileClicked(event) {
|
|
|
|
openFile(event.target.textContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateFiles() {
|
2022-06-18 17:12:38 -04:00
|
|
|
let node = document.getElementById("files");
|
2021-01-02 13:10:00 -05:00
|
|
|
while (node.firstChild) {
|
|
|
|
node.removeChild(node.firstChild);
|
|
|
|
}
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
for (let file of Object.keys(gFiles).sort()) {
|
|
|
|
let li = document.createElement("li");
|
2021-01-02 13:10:00 -05:00
|
|
|
li.onclick = onFileClicked;
|
|
|
|
li.appendChild(document.createTextNode(file));
|
|
|
|
if (file == gCurrentFile) {
|
|
|
|
li.classList.add("current");
|
|
|
|
}
|
2021-01-12 21:40:46 -05:00
|
|
|
if (!gFiles[file].doc.isClean(gFiles[file].generation)) {
|
2021-01-12 21:15:09 -05:00
|
|
|
li.classList.add("dirty");
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
node.appendChild(li);
|
|
|
|
}
|
|
|
|
|
|
|
|
gEditor.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeNewFile(name) {
|
|
|
|
gFiles[name] = {
|
2021-01-12 21:40:46 -05:00
|
|
|
doc: new CodeMirror.Doc("", guessMode(name)),
|
|
|
|
generation: -1,
|
2021-01-02 13:10:00 -05:00
|
|
|
};
|
|
|
|
openFile(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function newFile() {
|
2022-06-18 17:12:38 -04:00
|
|
|
let name = prompt("Name of new file:", "file.js");
|
2021-01-02 13:10:00 -05:00
|
|
|
if (name && !gFiles[name]) {
|
|
|
|
makeNewFile(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFile() {
|
|
|
|
if (confirm("Remove " + gCurrentFile + "?")) {
|
|
|
|
delete gFiles[gCurrentFile];
|
|
|
|
openFile(Object.keys(gFiles)[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 11:54:26 -04:00
|
|
|
window.addEventListener("load", function() {
|
2016-03-12 13:50:43 -05:00
|
|
|
window.addEventListener("hashchange", hashChange);
|
|
|
|
window.addEventListener("focus", focus);
|
|
|
|
window.addEventListener("blur", blur);
|
2021-01-02 13:10:00 -05:00
|
|
|
window.addEventListener("message", message, false);
|
2016-05-07 07:07:54 -04:00
|
|
|
window.addEventListener("online", connectSocket);
|
2021-01-02 13:10:00 -05:00
|
|
|
document.getElementById("name").value = window.location.pathname;
|
2023-01-28 17:44:45 -05:00
|
|
|
document.getElementById('edit_link').addEventListener('click', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
toggleEdit();
|
|
|
|
});
|
|
|
|
document.getElementById('show_permissions_link').addEventListener('click', () => showPermissions());
|
|
|
|
document.getElementById('files_hide').addEventListener('click', () => hideFiles());
|
|
|
|
document.getElementById('files_show').addEventListener('click', () => showFiles());
|
|
|
|
document.getElementById('closeStats').addEventListener('click', () => closeStats());
|
|
|
|
document.getElementById('closeEditor').addEventListener('click', () => closeEditor());
|
|
|
|
document.getElementById('save').addEventListener('click', () => save());
|
|
|
|
document.getElementById('delete').addEventListener('click', () => deleteApp());
|
|
|
|
document.getElementById('trace_button').addEventListener('click', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
trace();
|
|
|
|
});
|
|
|
|
document.getElementById('stats_button').addEventListener('click', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
toggleStats();
|
|
|
|
});
|
|
|
|
document.getElementById('new_file_button').addEventListener('click', () => newFile());
|
|
|
|
document.getElementById('remove_file_button').addEventListener('click', () => removeFile());
|
2022-02-03 18:57:47 -05:00
|
|
|
for (let tag of document.getElementsByTagName('a')) {
|
|
|
|
if (tag.accessKey) {
|
|
|
|
tag.classList.add('tooltip_parent');
|
2022-06-18 17:12:38 -04:00
|
|
|
let tooltip = document.createElement('div');
|
2022-02-03 18:57:47 -05:00
|
|
|
tooltip.classList.add('tooltip');
|
|
|
|
if (tag.dataset.tip) {
|
2022-06-18 17:12:38 -04:00
|
|
|
let description = document.createElement('div');
|
2022-02-03 18:57:47 -05:00
|
|
|
description.innerText = tag.dataset.tip;
|
|
|
|
tooltip.appendChild(description);
|
|
|
|
}
|
2022-06-18 17:12:38 -04:00
|
|
|
let parts = tag.accessKeyLabel ? tag.accessKeyLabel.split('+') : [];
|
|
|
|
for (let i = 0; i < parts.length; i++)
|
2022-02-03 18:57:47 -05:00
|
|
|
{
|
2022-06-18 17:12:38 -04:00
|
|
|
let key = parts[i];
|
|
|
|
let kbd = document.createElement('kbd');
|
2022-02-03 18:57:47 -05:00
|
|
|
kbd.innerText = key;
|
|
|
|
tooltip.appendChild(kbd);
|
|
|
|
if (i < parts.length - 1) {
|
|
|
|
tooltip.appendChild(document.createTextNode('+'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tag.appendChild(tooltip);
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
enableDragDrop();
|
2021-01-02 13:10:00 -05:00
|
|
|
connectSocket(window.location.pathname);
|
2022-03-07 16:06:20 -05:00
|
|
|
|
|
|
|
if (window.localStorage.getItem('editing') == '1') {
|
|
|
|
edit();
|
|
|
|
} else {
|
|
|
|
closeEditor();
|
|
|
|
}
|
|
|
|
if (window.localStorage.getItem('files') == '1') {
|
|
|
|
showFiles();
|
|
|
|
} else {
|
|
|
|
hideFiles();
|
|
|
|
}
|
|
|
|
if (window.localStorage.getItem('stats') == '1') {
|
|
|
|
stats();
|
|
|
|
} else {
|
|
|
|
closeStats();
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
});
|