/*
This module contains a generic data browsing interface.
*/

CT.Browser = CT.Class({
	CLASSNAME: "CT.Browser",
	_: {
		edit: function(data, cb, action, pname) {
			var _ = this._, params = {
				action: action || "edit",
				pw: core.config.keys.storage
			}, view = this.view;
			params[pname || "data"] = data;
			CT.net.post({
				path: "/_db",
				params: params,
				cb: cb || function(dfull) {
					if (data.key)
						return view(dfull);
					_.tlist.postAdd(dfull, true);
				}
			});
		},
		load: function(d) {
			var _ = this._, oz = this.opts, view = this.view;
			if (d.name)
				return view(d);
			CT.dom.setContent(_.content, CT.dom.div([
				CT.dom.div("what's this " + oz.modelName + " called?", "bigger centered"),
				CT.dom.smartField({
					classname: "w1",
					blurs: oz.blurs,
					cb: function(name) {
						d.name = name;
						view(d);
					}
				})
			], "margined padded bordered round"));
		},
		build: function(docs) {
			var _ = this._, oz = this.opts,
				defz = this.defaults, ntxt = "new " + oz.modelName;
			_.tlist = CT.panel.triggerList([ntxt].concat(docs), function(d) {
				_.load((d == ntxt) ? CT.merge({
					modelName: oz.modelName,
					owner: user.core.get("key")
				}, defz()) : d);
			});
			CT.dom.setContent(_.list, _.tlist);
			CT.dom.id("tlnew" + oz.modelName).trigger();
		},
		setup: function() {
			var _ = this._, oz = this.opts;
			_.list = CT.dom.div(null, "ctlist");
			_.content = CT.dom.div(null, "ctcontent");
			CT.dom.setContent(oz.parent, [
				_.list,
				_.content
			]);
			CT.db.get(oz.modelName, _.build, null, null, null, {
				owner: user.core.get("key") // requires user module
			});
		}
	},
	view: function(d) {
		// override!
		CT.dom.setContent(_.content, JSON.stringify(d));
	},
	defaults: function() {
		// override
	},
	init: function(opts) {
		this.opts = CT.merge(opts, {
			parent: "ctmain",
			modelName: null, // REQUIRED
			nopts: {},
			blurs: ["name please", "title", "what's it called?"]
		});
		setTimeout(this._.setup);
	}
});
@@ -112,16 +112,29 @@
 ,%0A%09_: %7B%0A
+%09%09nodes: %7B%7D,%0A
 %09%09edit: 
@@ -165,24 +165,24 @@
 n, pname) %7B%0A
-
 %09%09%09var _ = t
@@ -631,32 +631,38 @@
 om.setContent(_.
+nodes.
 content, CT.dom.
@@ -1273,16 +1273,22 @@
 ntent(_.
+nodes.
 list, _.
@@ -1380,32 +1380,46 @@
 %09var _ = this._,
+ nz = _.nodes,
  oz = this.opts;
@@ -1422,17 +1422,18 @@
 pts;%0A%09%09%09
-_
+nz
 .list = 
@@ -1463,17 +1463,18 @@
 t%22);%0A%09%09%09
-_
+nz
 .content
@@ -1545,17 +1545,18 @@
 , %5B%0A%09%09%09%09
-_
+nz
 .list,%0A%09
@@ -1558,17 +1558,18 @@
 st,%0A%09%09%09%09
-_
+nz
 .content
@@ -1739,16 +1739,16 @@
 erride!%0A
-
 %09%09CT.dom
@@ -1761,16 +1761,22 @@
 ntent(_.
+nodes.
 content,

store content/list nodes on _.nodes{}