All pastes #740157 Raw Edit

Stuff

public text v1 · immutable
·published 2007-10-17 18:57 UTC
rendered paste body
	/*
	Property: load
		Asynchronously load an XML structure into a node of this tree.
	
	Parameters:
		url - string, required, specifies the URL from which to load the XML document.
		vars - query string, optional.
	*/
	
	load: function(url, vars) {
	var urlScript = 'cat-xml.php?cPath=' + (this.id);
	//window.alert('attempting to load: ' + urlScript);
						new Ajax(urlScript, {
							method: 'get',
							evalScripts: true,
							update: $('loadXML')
						}).request();
		if (this.loading) return; // if this node is already loading, return
		this.loading = true;      // flag this node as loading
		
		this.toggle(false, true); // expand the node to make the loader visible
		
		this.clear();

		this.insert(this.control.loader);
		
		var f = function() {
			new XHR({
				method: 'GET',
				onSuccess: this._loaded.bind(this),
				onFailure: this._load_err.bind(this)
			}).send(url, vars || '');
		};
		
		window.setTimeout(f.bind(this), 20); // allowing a small delay for the browser to draw the loader-icon.
		
	},
	
	_loaded: function(text, xml) {
		// called on success - import nodes from the root element:
		this.control.disable();
		this.clear();
		this._import(xml.documentElement);
		this.control.enable();
		this.loading = false;
	},
	
	_import: function(e) {
		// import childnodes from an xml element:
		var n = e.childNodes;
		for (var i=0; i<n.length; i++) if (n[i].tagName == 'node') {
			var opt = {data:{}};
			var a = n[i].attributes;
			for (var t=0; t<a.length; t++) {
				switch (a[t].name) {
					case 'text':
					case 'id':
					case 'icon':
					case 'openicon':
					case 'color':
					case 'open':
						opt[a[t].name] = a[t].value;
						break;
					default:
						opt.data[a[t].name] = a[t].value;
				}
			}
			var node = this.insert(opt);
			if (node.data.load) {
				node.open = false; // can't have a dynamically loading node that's already open!
				node.insert(this.control.loader);
				node.onExpand = function(state) {
					this.load(this.data.load);
					this.onExpand = new Function();
				}
			}
			// recursively import subnodes of this node:
			if (n[i].childNodes.length) node._import(n[i]);
		}
	},
	
	_load_err: function(req) {
		window.alert('Error loading: ' + this.text);
	},
	
	/*
	Property: empty
		Utility function, used to clear the contents of an Element.
	*/
	
	empty: function(e) {
		while (e.lastChild) e.removeChild(e.lastChild);
	}
	
})