summaryrefslogtreecommitdiffstats
path: root/web
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-02-06 12:01:31 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-02-12 12:38:31 +0200
commit8991a77656de2e904087832df646e5dfd20b05a4 (patch)
tree0d4cb6e609f1517e902d426d80fd52b380977880 /web
parent1b328cbc26315577c0e27af6e63514ea762a8d7f (diff)
downloadaconf-8991a77656de2e904087832df646e5dfd20b05a4.tar.bz2
aconf-8991a77656de2e904087832df646e5dfd20b05a4.tar.xz
web client: path module
Diffstat (limited to 'web')
-rw-r--r--web/client.js83
-rw-r--r--web/path.js48
2 files changed, 71 insertions, 60 deletions
diff --git a/web/client.js b/web/client.js
index 73983a4..7aacb1b 100644
--- a/web/client.js
+++ b/web/client.js
@@ -19,6 +19,7 @@ requirejs.config({
require(
[
+ "acf2/path",
"acf2/statusbar",
"jquery",
"underscore",
@@ -26,7 +27,7 @@ require(
"jquery-blockui",
"jquery-ui/sortable"
],
- function(statusBar, $, _) {
+ function(pth, statusBar, $, _) {
$("#login").submit(function() {
$.ajax("/login", {
@@ -36,44 +37,6 @@ require(
})
}).done(function(data, status, xhr) {
- function split(path) {
- var res = [];
- while (path && path != "/") {
- var comp = path.match(/^\/([^\\\/]|\\.)+/)[0];
- res.push(comp.substring(1));
- path = path.substring(comp.length);
- }
- return res;
- }
-
- function escape(name) {
- if (!_.isString(name)) return String(name);
- name = name.replace(/([\\\/])/g, "\\$1");
- if (isNaN(Number(name))) return name;
- return "\\" + name;
- }
-
- function join() {
- var arg = _.toArray(arguments);
- if (arg.length == 1) return arg[0];
-
- var path = arg.shift();
- var name = arg.shift();
-
- arg.unshift((path == "/" ? "" : path) + "/" + escape(name));
-
- return join.apply(undefined, arg);
- }
-
- function isSubordinate(p1, p2, real) {
- p1 = split(p1);
- p2 = split(p2);
- if (real && p1.length <= p2.length) return false;
- for (var i = 0; i < p2.length; i++)
- if (p1[i] != p2[i]) return false;
- return true;
- }
-
function isList(meta) {
return _.contains(["list", "set"], meta.type);
}
@@ -151,7 +114,7 @@ require(
}
data.get = function(name, valid) {
- var p = join(path, name);
+ var p = pth.join(path, name);
if (!valid && p in invalid) return invalid[p][0];
if (data.meta.type == "set") {
@@ -164,7 +127,7 @@ require(
};
data.metaRequest = function(name) {
- return request("/meta" + join(path, name));
+ return request("/meta" + pth.join(path, name));
};
data.match = function(filter) {
@@ -175,11 +138,11 @@ require(
};
data.status = function(name) {
- var p = name ? join(path, name) : path;
+ var p = name ? pth.join(path, name) : path;
function scan(objs) {
return _.size(_.filter(
_.keys(objs), function(obj) {
- return isSubordinate(obj, p);
+ return pth.isSubordinate(obj, p);
}
));
}
@@ -203,7 +166,7 @@ require(
var def = $.Deferred();
function reject(xhr) { def.reject(xhr); }
- var mpath = join(path, name);
+ var mpath = pth.join(path, name);
var value = data.get(name, true);
if (value == undefined) value = null;
@@ -213,7 +176,7 @@ require(
function ignore(path) {
_.each(_.keys(invalid), function(p) {
- if (isSubordinate(p, path))
+ if (pth.isSubordinate(p, path))
delete invalid[p];
});
}
@@ -266,7 +229,7 @@ require(
_.each(
_.keys(changed),
function(p) {
- if (isSubordinate(p, mpath, true))
+ if (pth.isSubordinate(p, mpath, true))
delete changed[p];
}
);
@@ -292,7 +255,7 @@ require(
newValue
))
ignore(
- join(path, field.name)
+ pth.join(path, field.name)
);
}
);
@@ -305,7 +268,7 @@ require(
_.each(
data.meta.fields,
function(field) {
- var mmpath = join(
+ var mmpath = pth.join(
mpath,
field.name
);
@@ -356,15 +319,15 @@ require(
};
function adjustListIndex(oldIndex, newIndex) {
- var opath = join(path, oldIndex);
- var npath = join(path, newIndex);
+ var opath = pth.join(path, oldIndex);
+ var npath = pth.join(path, newIndex);
_.each(
[changed, invalid],
function(map) {
_.each(
_.keys(map),
function(p) {
- if (isSubordinate(
+ if (pth.isSubordinate(
p, opath
)) {
map[npath +
@@ -392,7 +355,7 @@ require(
if (isTreeNode(data.meta) &&
data.meta.type != "set") {
- delete changed[join(path, name)];
+ delete changed[pth.join(path, name)];
changed[path] = path;
if (data.meta.type == "list")
@@ -428,7 +391,7 @@ require(
objRequest(path, {type: "POST", data: {
index: newIndex,
data: isTreeNode(data.meta.members) ?
- join(path, oldIndex) : value
+ pth.join(path, oldIndex) : value
}}).done(function() {
data.data.splice(newIndex - 1, 0, value);
@@ -449,7 +412,7 @@ require(
};
data.invoke = function(name) {
- return objRequest(join(path, name), {type: "POST"});
+ return objRequest(pth.join(path, name), {type: "POST"});
};
def.resolve(data);
@@ -1330,7 +1293,7 @@ require(
data ? $.Deferred().resolve(data) : txnMgr.query(path)
).done(function(data) {
var layout = data.meta.widget;
- var name = split(path).pop();
+ var name = pth.split(path).pop();
(layout ? layouts[layout] : Vertical).extend({
createEl: function() { return $("#content").empty(); },
wrap: function() { return this.el; },
@@ -1350,10 +1313,10 @@ require(
false
);
}).fail(function() {
- var comps = split(path);
+ var comps = pth.split(path);
comps.pop();
comps.unshift("/");
- redirect(join.apply(undefined, comps));
+ redirect(pth.join.apply(undefined, comps));
});
};
@@ -1426,21 +1389,21 @@ require(
if (path > "/") $("#content").text("Loading...");
txnMgr.start().done(function() {
- var comps = split(path);
+ var comps = pth.split(path);
renderMenu($("#modules").empty(), "/", comps[0], false);
var tabs = $("#tabs").empty();
if (path == "/") return;
function renderTabs(p) {
- p = join(p, comps.shift());
+ p = pth.join(p, comps.shift());
renderMenu(tabs, p, comps[0], true)
.done(function(data) {
var tabLevel = !comps.length;
renderObject(
(
tabLevel && _.isString(data)
- ) ? join(p, data) : path,
+ ) ? pth.join(p, data) : path,
tabLevel && _.isObject(data) ? data : null
);
})
diff --git a/web/path.js b/web/path.js
new file mode 100644
index 0000000..bb60c76
--- /dev/null
+++ b/web/path.js
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2012-2014 Kaarle Ritvanen
+ * See LICENSE file for license details
+ */
+
+define(["underscore"], function(_) {
+ function split(path) {
+ var res = [];
+ while (path && path != "/") {
+ var comp = path.match(/^\/([^\\\/]|\\.)+/)[0];
+ res.push(comp.substring(1));
+ path = path.substring(comp.length);
+ }
+ return res;
+ }
+
+ function escape(name) {
+ if (!_.isString(name)) return String(name);
+ name = name.replace(/([\\\/])/g, "\\$1");
+ if (isNaN(Number(name))) return name;
+ return "\\" + name;
+ }
+
+ function join() {
+ var arg = _.toArray(arguments);
+ if (arg.length == 1) return arg[0];
+
+ var path = arg.shift();
+ var name = arg.shift();
+
+ arg.unshift((path == "/" ? "" : path) + "/" + escape(name));
+
+ return join.apply(undefined, arg);
+ }
+
+ return {
+ split: split,
+ join: join,
+ isSubordinate: function(p1, p2, real) {
+ p1 = split(p1);
+ p2 = split(p2);
+ if (real && p1.length <= p2.length) return false;
+ for (var i = 0; i < p2.length; i++)
+ if (p1[i] != p2[i]) return false;
+ return true;
+ }
+ };
+});