diff options
-rw-r--r-- | web/client.js | 213 |
1 files changed, 119 insertions, 94 deletions
diff --git a/web/client.js b/web/client.js index 94a11eb..ae2546b 100644 --- a/web/client.js +++ b/web/client.js @@ -56,6 +56,117 @@ $(function() { function objRequest(path, options) { return request("/config" + path, options); } + + function query(path) { + var def = $.Deferred(); + + objRequest(path).done(function(data) { + data.get = function(name) { + var p = join(path, name); + if (_.isArray(data.data)) name--; + return p in invalid ? invalid[p][0] : data.data[name]; + }; + + data.status = function(name) { + var p = join(path, name); + function scan(objs) { + return _.size(_.filter( + _.keys(objs), function(obj) { + return obj == p || !obj.indexOf(p + "/"); + } + )); + } + + if (scan(invalid)) return "invalid"; + if (scan(changed)) return "changed"; + return null; + } + + data.set = function(name, newValue) { + var def = $.Deferred(); + + var mpath = join(path, name); + var value = data.data[name]; + + var prevTask; + if (mpath in invalid) + prevTask = invalid[mpath][1]; + if (!prevTask) { + prevTask = $.Deferred(); + prevTask.resolve(); + } + + prevTask.always(function() { + var options; + if (newValue != null) + options = { + type: "PUT", + data: JSON.stringify(newValue) + }; + else { + var savedValue = (mpath in changed) ? + changed[mpath][1] : value; + if (savedValue != null) + options = {type: "DELETE"}; + } + + if (!options) { + if (data.meta.type == "model" && _.findWhere( + data.meta.fields, {name: name} + ).required) + def.reject("Required value not set"); + else def.resolve(); + return; + } + + var task = objRequest(mpath, options); + + var tn = _.isObject(newValue); + if (tn) newValue = mpath; + invalid[mpath] = [newValue, task]; + + task.done(function() { + if (!(mpath in changed)) + changed[mpath] = [value]; + if (newValue == changed[mpath][0]) + delete changed[mpath]; + else changed[mpath][1] = newValue; + + delete invalid[mpath]; + + function resolve() { def.resolve(isValid()); } + + if (tn) query(mpath).done(function(data) { + if (data.meta.type == "model") + _.each( + data.meta.fields, + function(field) { + var mmpath = join( + mpath, field.name + ); + if (field.required && + !(mmpath in invalid) && + data.get( + field.name + ) == null) + invalid[mmpath] = [null]; + }); + resolve(); + }).fail(function(xhr) { def.reject(xhr); }); + + else resolve(); + + }).fail(function(xhr) { def.reject(xhr); }); + }); + + return def; + }; + + def.resolve(data); + }).fail(function() { def.reject(); }); + + return def; + } return { start: function() { @@ -86,95 +197,7 @@ $(function() { abort: abort, - query: function(path) { - var def = $.Deferred(); - - objRequest(path).done(function(data) { - data.get = function(name) { - var p = join(path, name); - if (_.isArray(data.data)) name--; - return p in invalid ? - invalid[p][0] : data.data[name]; - }; - - data.status = function(name) { - var p = join(path, name); - function scan(objs) { - return _.size(_.filter( - _.keys(objs), function(obj) { - return obj == p || - !obj.indexOf(p + "/"); - } - )); - } - - if (scan(invalid)) return "invalid"; - if (scan(changed)) return "changed"; - return null; - } - - data.set = function(name, newValue) { - var def = $.Deferred(); - - function ensureTask(task) { - if (task) return task; - task = $.Deferred(); - task.resolve(); - return task; - } - - var mpath = join(path, name); - var value = data.data[name]; - - var prevTask; - if (mpath in invalid) - prevTask = invalid[mpath][1]; - prevTask = ensureTask(prevTask); - - prevTask.always(function() { - var options; - if (newValue != null) - options = { - type: "PUT", - data: JSON.stringify(newValue) - }; - else { - var savedValue = (mpath in changed) ? - changed[mpath][1] : value; - if (savedValue != null) - options = {type: "DELETE"}; - } - - var task; - if (options) - task = objRequest(mpath, options); - task = ensureTask(task); - - if (_.isObject(newValue)) newValue = mpath; - invalid[mpath] = [newValue, task]; - - task.done(function() { - if (!(mpath in changed)) - changed[mpath] = [value]; - if (newValue == changed[mpath][0]) - delete changed[mpath]; - else changed[mpath][1] = newValue; - - delete invalid[mpath]; - - def.resolve(isValid()); - - }).fail(function(xhr) { def.reject(xhr); }); - }); - - return def; - }; - - def.resolve(data); - }).fail(function() { def.reject(); }); - - return def; - } + query: query }; })(xhr.getResponseHeader("X-ACF-Auth-Token")); @@ -361,6 +384,7 @@ $(function() { "You have uncommitted changes", true ); + else setErrorStatus("Some values need checking"); } @@ -400,22 +424,23 @@ $(function() { validated(txnValid); }).fail(function(xhr) { - if (xhr.statusCode().status == 422) + if (_.isString(xhr)) msg.text(xhr); + + else if (xhr.statusCode().status == 422) msg.html(_.reduce( _.map( - $.parseJSON( - xhr.responseText - ), + $.parseJSON(xhr.responseText), _.escape ), function(a, b) { return a + "<br/>" + b; } )); + else msg.text(formatError("Error", xhr)); widget.setStatus("invalid"); - setErrorStatus("Some values need checking"); + validated(false); }); }); |