Created
March 30, 2013 23:58
-
-
Save kissarat/5278893 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doNothing = function () {}; | |
if (!Object.prototype.__defineGetter__) { | |
Object.prototype.__defineGetter__ = function(name, getter) { | |
Object.defineProperty(this, name, { get: getter }); | |
} | |
} | |
function definePrototypeProperty (name, getter) { | |
Object.defineProperty(this.prototype, name, { get: getter }); | |
} | |
if (!Element.firstElementChild) { | |
with(Element) { | |
function nextElement(node) { | |
do | |
if (node instanceof Element) | |
return node; | |
while (node = child.nextSibling); | |
} | |
definePrototypeProperty('firstElementChild', function() { | |
return nextElement(this.firstChild); | |
}); | |
definePrototypeProperty('nextElementSibling', function() { | |
return nextElement(this); | |
}); | |
} | |
} | |
//------------------- Resource -------------------------------- | |
Resource = function(url, type) { | |
this.url = url; | |
this.type = type; | |
} | |
Resource.prototype = { | |
load : function(callback) { | |
var self = this; | |
var ajax = new XMLHttpRequest(); | |
var handler = function () { | |
if (4 == ajax.readyState) | |
if(200 == ajax.status) { | |
self.value = self.handles[self.type](ajax); | |
if (callback) | |
callback(self.value); | |
} | |
} | |
ajax.onreadystatechange = handler; | |
ajax.open('GET', this.url, true); | |
ajax.send(null); | |
}, | |
handles : { | |
'application/xml' : function(ajax) { | |
return ajax.responseXML; | |
}, | |
'application/json' : function(ajax) { | |
return JSON.parse(ajax.responseText); | |
} | |
} | |
} | |
//------------------- Library -------------------------------- | |
Library = function(libraryUrl) { | |
this.resource = new Resource(libraryUrl, 'application/xml'); | |
this.widgets = Object.create(null); | |
Object.defineProperty(this, 'xml', { | |
get : function() { | |
return this.resource.value | |
} | |
}); | |
} | |
Library.prototype = { | |
getWidget: function(name) { | |
return this.widgets[name]; | |
}, | |
load : function() { | |
var self = this; | |
this.resource.load(function (dom) { | |
var nodeList = dom.getElementsByTagName('widget'); | |
for(var i=0; i<nodeList.length; i++) { | |
var widget = new Widget(nodeList.item(i)); | |
self.widgets[widget.name] = widget; | |
} | |
}); | |
} | |
/*query: function(xpath) { | |
return document.evaluate(xpath, this.resource.value); | |
}*/ | |
} | |
//------------------- Widget -------------------------------- | |
Widget = function (dom) { | |
this.name = dom.getAttribute('name'); | |
this.root = dom.getElementsByTagName('body').item(0).cloneNode(true); | |
this.bindings = this.createBindings(dom.getElementsByTagName('head').item(0)); | |
} | |
Widget.prototype = { | |
query: function(xpath) { | |
return document.evaluate(xpath, this.root); | |
}, | |
createRoot : function(source) { | |
var destination = document.createElement('div'); | |
for(var child = source.firstChild; child; child = child.nextSibling) | |
if (child instanceof Element) | |
destination.appendChild(child.clone(true)); | |
}, | |
createBindings : function(head) { | |
var bindings = new Object(); | |
for(var binding = head.firstChild; binding; binding = binding.nextSibling) { | |
if ("bind" != binding.nodeName) | |
continue; | |
var name = binding.getAttribute('name'); | |
var target = binding.getAttribute('target'); | |
target = this.query(target).iterateNext(); | |
Object.defineProperty(bindings, name, { | |
get : function() { | |
return target.nodeValue; | |
}, | |
set : function(value) { | |
target.nodeValue = value; | |
} | |
}); | |
} | |
return bindings; | |
} | |
} | |
//------------------- Observable -------------------------------- | |
Observable = { | |
addObserver : function(observer) { | |
this.observers.push(observer); | |
}, | |
removeObserver : function(observer) { | |
delete this.observers[observer]; | |
}, | |
updateObservers : function(propertyName) { | |
for(i in this.observers) | |
this.observers[i](this, propertyName); | |
} | |
} | |
//------------------- Descriptor -------------------------------- | |
Descriptor = function() { | |
this.descriptions = []; | |
} | |
Descriptor.prototype = { | |
createModel : function() { | |
var model = Object.create(Observable); | |
model.observers = []; | |
model.data = []; | |
for(var name in this.descriptions) { | |
var description = this.descriptions[name]; | |
description.get = function() { | |
return this.data[name]; | |
} | |
description.set = function(value) { | |
this.data[name] = value; | |
this.updateObservers(name); | |
} | |
Object.defineProperty(model, name, description); | |
} | |
model.descriptor = this; | |
return model; | |
} | |
} | |
//WidgetBase = function() {} | |
/*WidgetBase.prototype = { | |
}*/ | |
Text = function() { | |
this.dom = document.createElement('input'); | |
this.dom.setAttribute('type', 'text'); | |
Object.__defineSetter__('name', function(value) { | |
this.dom.setAttribute('name', 'value'); | |
}); | |
} | |
function getParameters(str) { | |
var params = Object.create(null); | |
var strings = str.split('&'); | |
if ([''] != strings) { | |
strings[0] = strings[0].slice(1); | |
for (var i in strings) { | |
var param = strings[i].split('='); | |
params[param[0]] = param[1]; | |
} | |
location.params = params; | |
} | |
return params; | |
} | |
addEventListener('load', function() { | |
//cozy = new Library('tools/gui.xml'); | |
//cozy.load(); | |
location.params = getParameters(location.search); | |
}); | |
function addField(doc) { | |
var field = doc.createElement('div'); | |
doc.body.appendChild(field); | |
return field; | |
} | |
w = window.open(); | |
doc = w.document; | |
var properties = ['inputEncoding', 'title']; | |
for(var i=0; i<properties.length; i++) { | |
var field = addField(doc); | |
field.innerText = properties[i] + " " + document[properties[i]]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment