Fiddled with saving and loading so that admin users can push and pull to parent apps.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3806 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
parent
ec5d7c1a01
commit
0278aceb62
15
core/app.js
15
core/app.js
@ -64,6 +64,7 @@ function socket(request, response, client) {
|
||||
var packageName;
|
||||
var blobId;
|
||||
var match;
|
||||
var parentApp;
|
||||
if (match = /^\/([&%][^\.]{44}(?:\.\w+)?)(\/?.*)/.exec(message.path)) {
|
||||
blobId = match[1];
|
||||
} else if (match = /^\/\~([^\/]+)\/([^\/]+)\/$/.exec(message.path)) {
|
||||
@ -74,8 +75,20 @@ function socket(request, response, client) {
|
||||
response.send(JSON.stringify({action: "error", error: message.path + ' not found'}), 0x1);
|
||||
return;
|
||||
}
|
||||
if (user != 'core') {
|
||||
var coreId = await new Database('core').get('path:' + path);
|
||||
parentApp = {
|
||||
path: '/~core/' + path + '/',
|
||||
id: coreId,
|
||||
};
|
||||
}
|
||||
response.send(JSON.stringify({action: "session", credentials: credentials}), 0x1);
|
||||
}
|
||||
response.send(JSON.stringify({
|
||||
action: "session",
|
||||
credentials: credentials,
|
||||
parentApp: parentApp,
|
||||
id: blobId,
|
||||
}), 0x1);
|
||||
|
||||
options.api = message.api || [];
|
||||
options.credentials = credentials;
|
||||
|
@ -9,6 +9,7 @@ var gApp = {files: {}};
|
||||
var gEditor;
|
||||
var gSplit;
|
||||
var gGraphs = {};
|
||||
var gParentApp;
|
||||
|
||||
var kErrorColor = "#dc322f";
|
||||
var kStatusColor = "#fff";
|
||||
@ -164,6 +165,7 @@ function guessMode(name) {
|
||||
}
|
||||
|
||||
function loadFile(name, id) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.addEventListener("loadend", function() {
|
||||
if (request.status == 200) {
|
||||
@ -171,26 +173,34 @@ function loadFile(name, id) {
|
||||
if (!Object.values(gFiles).some(x => !x.doc)) {
|
||||
document.getElementById("editPane").style.display = 'flex';
|
||||
openFile(Object.keys(gFiles).sort()[0]);
|
||||
resolve();
|
||||
}
|
||||
} else {
|
||||
reject('Error loading source.');
|
||||
}
|
||||
});
|
||||
request.addEventListener("error", function() {
|
||||
alert("Error loading source.");
|
||||
closeEditor();
|
||||
reject('Error loading source.');
|
||||
});
|
||||
request.addEventListener("timeout", function() {
|
||||
alert("Timed out loading source.");
|
||||
closeEditor();
|
||||
reject('Timed out loading source.');
|
||||
});
|
||||
request.addEventListener("abort", function() {
|
||||
alert("Loading source aborted.");
|
||||
closeEditor();
|
||||
reject('Loading source aborted.');
|
||||
});
|
||||
request.open("GET", "/" + id + "/view");
|
||||
request.send();
|
||||
});
|
||||
}
|
||||
|
||||
function load() {
|
||||
function load(path) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.addEventListener("loadend", function() {
|
||||
if (request.status == 200 || request.status == 404) {
|
||||
@ -210,6 +220,7 @@ function load() {
|
||||
gFiles = {};
|
||||
var text;
|
||||
var isApp = false;
|
||||
var promises = [];
|
||||
if (request.status == 200) {
|
||||
text = request.responseText;
|
||||
try {
|
||||
@ -218,7 +229,7 @@ function load() {
|
||||
isApp = true;
|
||||
Object.keys(json['files']).forEach(function(name) {
|
||||
gFiles[name] = {};
|
||||
loadFile(name, json['files'][name]);
|
||||
promises.push(loadFile(name, json['files'][name]));
|
||||
});
|
||||
if (Object.keys(json['files']).length == 0) {
|
||||
document.getElementById("editPane").style.display = 'flex';
|
||||
@ -227,6 +238,8 @@ function load() {
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
} else {
|
||||
reject('Load failed.');
|
||||
}
|
||||
if (!isApp) {
|
||||
document.getElementById("editPane").style.display = 'flex';
|
||||
@ -239,22 +252,27 @@ function load() {
|
||||
};
|
||||
openFile(gCurrentFile);
|
||||
}
|
||||
Promise.all(promises).then(resolve).catch(reject);
|
||||
}
|
||||
});
|
||||
request.addEventListener("error", function() {
|
||||
alert("Error loading source.");
|
||||
closeEditor();
|
||||
reject('Error loading source.');
|
||||
});
|
||||
request.addEventListener("timeout", function() {
|
||||
alert("Timed out loading source.");
|
||||
closeEditor();
|
||||
reject('Timed out loading source.');
|
||||
});
|
||||
request.addEventListener("abort", function() {
|
||||
alert("Loading source aborted.");
|
||||
closeEditor();
|
||||
reject('Loading source aborted.');
|
||||
});
|
||||
request.open("GET", url() + "view");
|
||||
request.open("GET", (path || url()) + "view");
|
||||
request.send();
|
||||
});
|
||||
}
|
||||
|
||||
function closeStats() {
|
||||
@ -277,22 +295,34 @@ function explodePath() {
|
||||
return /^\/~([^\/]+)\/([^\/]+)(.*)/.exec(window.location.pathname);
|
||||
}
|
||||
|
||||
function save() {
|
||||
function save(save_to) {
|
||||
document.getElementById("save").disabled = true;
|
||||
document.getElementById("push_to_parent").disabled = true;
|
||||
document.getElementById("pull_from_parent").disabled = true;
|
||||
if (gCurrentFile) {
|
||||
gFiles[gCurrentFile].doc = gEditor.getDoc();
|
||||
}
|
||||
|
||||
var run = document.getElementById("run").checked;
|
||||
|
||||
var appFinished = function(success) {
|
||||
document.getElementById("save").disabled = false;
|
||||
document.getElementById("push_to_parent").disabled = false;
|
||||
document.getElementById("pull_from_parent").disabled = false;
|
||||
Object.values(gFiles).forEach(function(file) {
|
||||
file.generation = file.doc.changeGeneration();
|
||||
});
|
||||
updateFiles();
|
||||
}
|
||||
|
||||
var save_path = save_to;
|
||||
if (!save_to) {
|
||||
var name = document.getElementById("name");
|
||||
if (name && name.value) {
|
||||
save_path = name.value;
|
||||
} else {
|
||||
save_path = url();
|
||||
}
|
||||
}
|
||||
|
||||
var always = function() {
|
||||
var anyUnfinished = Object.values(gFiles).some(x => x.request);
|
||||
var anyUnsaved = Object.values(gFiles).some(x => !x.doc.isClean(x.generation) && !x.id);
|
||||
@ -312,15 +342,10 @@ function save() {
|
||||
});
|
||||
request.addEventListener("loadend", function() {
|
||||
if (request.status == 200) {
|
||||
var latest = document.getElementById('latest');
|
||||
latest.href = request.responseText;
|
||||
latest.style.visibility = request.responseText != window.location.path ? 'visible' : 'hidden';
|
||||
if (run) {
|
||||
if (request.responseText) {
|
||||
reconnect(request.responseText);
|
||||
if (save_path != window.location.pathname) {
|
||||
alert('Saved to ' + save_path + '.');
|
||||
} else {
|
||||
reconnect(window.location.path);
|
||||
}
|
||||
reconnect(save_path);
|
||||
}
|
||||
appFinished(true);
|
||||
} else {
|
||||
@ -337,14 +362,7 @@ function save() {
|
||||
appFinished(false);
|
||||
});
|
||||
|
||||
var saveTo = null;
|
||||
var name = document.getElementById("name");
|
||||
if (name && name.value) {
|
||||
saveTo = name.value + "save";
|
||||
} else {
|
||||
saveTo = url() + "save";
|
||||
}
|
||||
request.open("POST", saveTo, true);
|
||||
request.open("POST", save_path + 'save', true);
|
||||
request.setRequestHeader("Content-Type", "text/json");
|
||||
request.send(JSON.stringify(app));
|
||||
} else if (!anyUnfinished) {
|
||||
@ -399,6 +417,14 @@ function save() {
|
||||
}
|
||||
}
|
||||
|
||||
function pullFromParent() {
|
||||
load(gParentApp ? gParentApp.path : null).then(x => save());
|
||||
}
|
||||
|
||||
function pushToParent() {
|
||||
save(gParentApp ? gParentApp.path : null);
|
||||
}
|
||||
|
||||
function url() {
|
||||
var hash = window.location.href.indexOf('#');
|
||||
var question = window.location.href.indexOf('?');
|
||||
@ -422,7 +448,11 @@ function receive(message) {
|
||||
if (message && message.action == "session") {
|
||||
setStatusMessage("...Executing...", kStatusColor, true);
|
||||
gCredentials = message.credentials;
|
||||
gParentApp = message.parentApp;
|
||||
updateLogin();
|
||||
var parent_enabled = message.parentApp;
|
||||
document.getElementById('push_to_parent').style.display = parent_enabled ? 'inline-block' : 'none';
|
||||
document.getElementById('pull_from_parent').style.display = parent_enabled ? 'inline-block' : 'none';
|
||||
} else if (message && message.action == "ready") {
|
||||
setStatusMessage(null);
|
||||
if (window.location.hash) {
|
||||
|
15
core/core.js
15
core/core.js
@ -479,11 +479,9 @@ async function blobHandler(request, response, blobId, uri) {
|
||||
var user = match[1];
|
||||
var app = match[2];
|
||||
var credentials = auth.query(request.headers);
|
||||
if (!credentials || !credentials.session || credentials.session.name != user) {
|
||||
response.writeHead(401, {"Content-Type": "text/plain; charset=utf-8"});
|
||||
response.end("401 Unauthorized");
|
||||
return;
|
||||
}
|
||||
if (credentials && credentials.session &&
|
||||
(credentials.session.name == user ||
|
||||
(credentials.permissions.administration && user == 'core'))) {
|
||||
var database = new Database(user);
|
||||
var apps = new Set();
|
||||
try {
|
||||
@ -495,6 +493,11 @@ async function blobHandler(request, response, blobId, uri) {
|
||||
database.set('apps', JSON.stringify([...apps]));
|
||||
}
|
||||
database.set('path:' + app, newBlobId);
|
||||
} else {
|
||||
response.writeHead(401, {"Content-Type": "text/plain; charset=utf-8"});
|
||||
response.end("401 Unauthorized");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"});
|
||||
@ -594,7 +597,7 @@ loadSettings().then(function() {
|
||||
return staticFileHandler(request, response, null, match[1]);
|
||||
} else if (match = /^\/perfetto\/([\.\w-/]*)$/.exec(request.uri)) {
|
||||
return perfettoHandler(request, response, match[1]);
|
||||
} else if (match = /^(.*)(\/save)$/.exec(request.uri)) {
|
||||
} else if (match = /^(.*)(\/save?)$/.exec(request.uri)) {
|
||||
return blobHandler(request, response, match[1], match[2]);
|
||||
} else if (match = /^\/trace$/.exec(request.uri)) {
|
||||
var data = trace();
|
||||
|
@ -31,9 +31,9 @@
|
||||
<input type="button" id="closeEditor" name="closeEditor" value="Close" onclick="closeEditor()">
|
||||
<input type="button" id="save" name="save" value="Save" onclick="save()">
|
||||
<input type="text" id="name" name="name"></input>
|
||||
<input type="checkbox" id="run" name="run" checked><label for="run">Restart after save</label>
|
||||
<input type="button" id="push_to_parent" value="Push to Parent" onclick="pushToParent()">
|
||||
<input type="button" id="pull_from_parent" value="Pull from Parent" onclick="pullFromParent()">
|
||||
<input type="button" id="revert" name="revert" value="Revert to Saved" onclick="revert()">
|
||||
<a id="latest" href="">Latest</a>
|
||||
</div>
|
||||
<div class="hbox" style="height: 100%">
|
||||
<div id="filesPane">
|
||||
|
Loading…
Reference in New Issue
Block a user