From 96f7ffd1186aedcd606a330047b9072cb1fd0ede Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Thu, 6 Feb 2014 22:47:49 +0200 Subject: web client: dom module --- web/client.js | 55 +++++++++++++++++-------------------------------------- web/dom.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 web/dom.js diff --git a/web/client.js b/web/client.js index 310c869..24d335f 100644 --- a/web/client.js +++ b/web/client.js @@ -19,6 +19,7 @@ requirejs.config({ require( [ + "acf2/dom", "acf2/error", "acf2/path", "acf2/statusbar", @@ -30,7 +31,7 @@ require( "jquery-blockui", "jquery-ui/sortable" ], - function(formatError, pth, statusBar, txnMgr, type, $, _) { + function(dom, formatError, pth, statusBar, txnMgr, type, $, _) { $("#login").submit(function() { $.ajax("/login", { @@ -46,28 +47,6 @@ require( ); - function href() { - return $("").attr({href: "javascript:void(0);"}); - } - - function objectRef(value, el) { - el = el || href(); - if (value) { - el.click(function() { - $.bbq.pushState("#" + value); - }).text("Show"); - } - return el; - }; - - function makeRow(el) { - if (el.is("td")) return $("").html(el); - return el; - } - - function setStatus(el, status) { el.prop("class", status); } - - var Widget = { extend: function(spec) { var res = Object.create(this); @@ -101,7 +80,7 @@ require( if (!editable || !meta.editable) { var el = this.staticRender(value, meta); if (el) { - setStatus(el, status); + dom.setStatus(el, status); return el; } } @@ -125,12 +104,12 @@ require( this.visible = true; if (removable) { - var link = href().click(function() { + var link = dom.href().click(function() { data.delete(name).done(function(txnValid) { $("#content").trigger("reload", [txnValid]); }) }).text("Delete"); - this.wrapped = makeRow(this.wrapped); + this.wrapped = dom.makeRow(this.wrapped); if (this.wrapped.is("tr")) link = $("").html(link); this.wrapped.append(link); } @@ -182,7 +161,7 @@ require( setStatus: function(status) { if (this.el && this.showStatus) - setStatus(this.statusEl(), status); + dom.setStatus(this.statusEl(), status); }, statusEl: function() { return this.el; }, @@ -345,12 +324,12 @@ require( var Link = Widget.extend({ staticRender: function(value, meta) { - return $("").html(objectRef(value)); + return $("").html(dom.objectRef(value)); }, - createEl: href, + createEl: dom.href, - render: function(value, meta) { objectRef(value, this.el) }, + render: function(value, meta) { dom.objectRef(value, this.el) }, wrap: function() { return $("").html(this.el); }, @@ -648,8 +627,8 @@ require( appendWidget: function(el, label) { var self = this; - el = makeRow(el); - + el = dom.makeRow(el); + if (el.is("tr")) { el.prepend($("").text(label)); this.appendRow(el); @@ -740,13 +719,13 @@ require( var item = $(""); if (choice.ref) - item.html(objectRef(choice.ref) + item.html(dom.objectRef(choice.ref) .text(choice["ui-value"])); else item.text(choice["ui-value"]); row.append(item); function setRowStatus() { - setStatus(row, data.status(choice.value)); + dom.setStatus(row, data.status(choice.value)); } setRowStatus(); @@ -786,7 +765,7 @@ require( }, staticRender: function(value, meta) { - return $("").html(objectRef(value).text(value)); + return $("").html(dom.objectRef(value).text(value)); }, statusEl: function() { return this.el.find("select"); }, @@ -801,8 +780,8 @@ require( var link = $("
"); var update = _.bind(function() { - link.html(objectRef(this.get())); - }, this); + link.html(dom.objectRef(this.get())); + }, this); this.el.append(link); this.field.change(update); @@ -921,7 +900,7 @@ require( } function addItem(path, ui_name, status, current) { - var el = $("
  • ").html(objectRef(path).text(ui_name)); + var el = $("
  • ").html(dom.objectRef(path).text(ui_name)); el.prop("class", status); if (current) el.addClass("current"); target.append(el); diff --git a/web/dom.js b/web/dom.js new file mode 100644 index 0000000..44f8491 --- /dev/null +++ b/web/dom.js @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2012-2014 Kaarle Ritvanen + * See LICENSE file for license details + */ + +define(["jquery", "jquery-bbq"], function($) { + function href() { + return $("").attr({href: "javascript:void(0);"}); + } + + return { + href: href, + + objectRef: function(value, el) { + el = el || href(); + if (value) { + el.click(function() { + $.bbq.pushState("#" + value); + }).text("Show"); + } + return el; + }, + + makeRow: function(el) { + if (el.is("td")) return $("").html(el); + return el; + }, + + setStatus: function(el, status) { el.prop("class", status); } + }; +}); -- cgit v1.2.3