summaryrefslogtreecommitdiffstats
path: root/web/client.js
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-01-31 00:35:36 +0200
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-02-01 01:53:55 +0200
commitbab83426a96667120390d25f6f57f34aa6d85b07 (patch)
treecb2e429e4bb58a1f0943fd015a7c90da0c5e29c8 /web/client.js
parent6a93c44c9a9130626aba9a581ca333065e0dedfa (diff)
downloadaconf-bab83426a96667120390d25f6f57f34aa6d85b07.tar.bz2
aconf-bab83426a96667120390d25f6f57f34aa6d85b07.tar.xz
web client: list sorting
Diffstat (limited to 'web/client.js')
-rw-r--r--web/client.js71
1 files changed, 67 insertions, 4 deletions
diff --git a/web/client.js b/web/client.js
index 63680a6..f615f0f 100644
--- a/web/client.js
+++ b/web/client.js
@@ -102,6 +102,8 @@ $(function() {
options = options || {};
options.headers = {"X-ACF-Auth-Token": token};
if (txn) options.headers["X-ACF-Transaction-ID"] = txn;
+ if (options.data != undefined)
+ options.data = JSON.stringify(options.data);
return $.ajax(url, options);
}
@@ -236,7 +238,7 @@ $(function() {
if (!del)
options = {
type: set ? "POST" : "PUT",
- data: JSON.stringify(newValue)
+ data: newValue
};
else if (data.get(name, true) != null)
options = {type: "DELETE"};
@@ -376,8 +378,9 @@ $(function() {
}
function adjustListIndices(start, end) {
- for (var i = start; i < end; i++)
- adjustListIndex(i + 1, i);
+ var offset = start < end ? 1 : -1;
+ for (var i = start; i != end; i += offset)
+ adjustListIndex(i + offset, i);
}
function _delete(name) {
@@ -407,6 +410,41 @@ $(function() {
});
};
+ data.move = function(oldIndex, newIndex) {
+ if (oldIndex == newIndex)
+ return $.Deferred().resolve(isValid());
+
+ var value = data.get(oldIndex);
+ var length = data.data.length;
+
+ return exclusive(function() {
+ var def = $.Deferred();
+ function reject() { def.reject(); }
+
+ if (oldIndex > newIndex) oldIndex++;
+ else newIndex++;
+
+ objRequest(path, {type: "POST", data: {
+ index: newIndex, data: join(path, oldIndex)
+ }}).done(function() {
+
+ data.data.splice(newIndex - 1, 0, value);
+
+ adjustListIndices(length + 1, newIndex);
+ adjustListIndex(oldIndex, newIndex);
+
+ data.delete(oldIndex)
+ .done(function(txnValid) {
+ def.resolve(txnValid);
+ })
+ .fail(reject);
+
+ }).fail(reject);
+
+ return def;
+ });
+ };
+
data.invoke = function(name) {
return objRequest(join(path, name), {type: "POST"});
};
@@ -1014,6 +1052,8 @@ $(function() {
if (meta.editable &&
_.contains(["collection", "list"], meta.type)) {
+ if (!this.table) this.makeSortable(this.div);
+
var keys = _.clone(_.keys(data.data));
var button = $("<input>").attr(
@@ -1079,6 +1119,7 @@ $(function() {
}
else {
if (el.is("table")) {
+ this.makeSortable(el.find("tbody"));
var td;
el.find("tr").each(function(index, row) {
td = $("<td>");
@@ -1096,10 +1137,32 @@ $(function() {
appendRow: function(row) {
if (!this.table) {
- this.table = $("<tbody>");
+ this.table = this.makeSortable($("<tbody>"));
this.div.append($("<table>").html(this.table));
}
this.table.append(row);
+ },
+
+ makeSortable: function(el) {
+ var data = this.reqData;
+
+ if (data.meta.type == "list")
+ el.sortable({
+ start: function(event, ui) {
+ ui.item.data("index", ui.item.index());
+ },
+ stop: function(event, ui) {
+ var oldIndex = ui.item.data("index") + 1;
+ var newIndex = ui.item.index() + 1;
+ if (newIndex != oldIndex)
+ data.move(oldIndex, newIndex)
+ .done(function(txnValid) {
+ statusBar.validationReady(txnValid);
+ renderObject();
+ });
+ }
+ });
+ return el;
}
});