From 3204822db1ae50aa031446b22e7ba94845a9c7c1 Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Tue, 10 Dec 2013 17:21:10 +0200 Subject: web client: cleaner syntax for widget inheritance --- web/client.js | 1012 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 533 insertions(+), 479 deletions(-) (limited to 'web/client.js') diff --git a/web/client.js b/web/client.js index 89cb341..3497039 100644 --- a/web/client.js +++ b/web/client.js @@ -476,6 +476,26 @@ $(function() { var Widget = { + extend: function(spec) { + var res = Object.create(this); + for (key in spec) res[key] = spec[key]; + res.base = this; + return res; + }, + + super: function() { + var args = _.toArray(arguments); + var cls = args.shift(); + var key = args.shift(); + return cls.base[key].apply(this, args); + }, + + new: function(data, name, meta, level, editable, removable) { + return Object.create(this).init( + data, name, meta, level, editable, removable + ); + }, + init: function(data, name, meta, level, editable, removable) { this.data = data; this.meta = meta; @@ -557,526 +577,563 @@ $(function() { }; - var Field = Object.create(Widget); - - Field.init = function( - data, name, meta, level, editable, removable - ) { - this.name = name; - this.editable = editable && meta.editable; - - var el = _.bind(Widget.init, this)( + var Field = Widget.extend({ + init: function( data, name, meta, level, editable, removable - ); - - if (this.editable) - this.field.change(_.bind(this.validate, this)); - - return el; - }; - - Field.staticRender = function(value) { - return $("").text(value); - }; + ) { + this.name = name; + this.editable = editable && meta.editable; + + var el = this.super( + Field, + "init", + data, + name, + meta, + level, + editable, + removable + ); - Field.makeEl = function() { - _.bind(Widget.makeEl, this)(); - if (!this.field) this.field = this.el; - }; + if (this.editable) + this.field.change(_.bind(this.validate, this)); + + return el; + }, - Field.createEl = function() { return $(""); }; + staticRender: function(value) { + return $("").text(value); + }, - Field.render = function(value, meta) { - this.field.attr({type: "text", value: value}); - }; + makeEl: function() { + this.super(Field, "makeEl"); + if (!this.field) this.field = this.el; + }, - Field.wrap = function() { - var td = $(""); - this.msg = $("
"); - td.append(this.msg); - td.append(this.el); - return td; - }; + createEl: function() { return $(""); }, - Field.validate = function() { - _.bind(Widget.validate, this)(); - if (!this.visible || !this.editable) return; + render: function(value, meta) { + this.field.attr({type: "text", value: value}); + }, - this.msg.text("[checking]"); - statusBar.setError("Validating changes", "validate"); + wrap: function() { + var td = $(""); + this.msg = $("
"); + td.append(this.msg); + td.append(this.el); + return td; + }, - var self = this; - - this.data.set(this.name, this.get()).done(function(txnValid) { - self.msg.empty() - self.setStatus(self.data.status(self.name)); - statusBar.validationReady(txnValid); - self.el.trigger("validated"); - - }).fail(function(xhr) { - if (_.isString(xhr)) self.msg.text(xhr); - - else if (xhr.statusCode().status == 422) - self.msg.html( - _.reduce( - _.map($.parseJSON(xhr.responseText), _.escape), - function(a, b) { return a + "
" + b; } - ) - ); + validate: function() { + this.super(Field, "validate"); + if (!this.visible || !this.editable) return; - else self.msg.text(formatError("Error", xhr)); - - self.setStatus("invalid"); - statusBar.validationReady(false); - }); - }; - - Field.get = function() { - return this.editable ? ( - this.field.val() || null - ) : this.data.get(this.name); - }; - + this.msg.text("[checking]"); + statusBar.setError("Validating changes", "validate"); - var ComboBox = Object.create(Field); - - ComboBox.createEl = function() { return $(""); }, - var CheckBox = Object.create(Field); - CheckBox.staticRender = function(value) { - return $("").text(value ? "Yes" : "No"); - }; + render: function(value, meta) { + var el = this.field; - CheckBox.statusEl = function() { return this.el.parent(); }; + function opt(value, ui_value, selected) { + el.append($("