From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/dojo/parser.js | 277 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 includes/js/dojo/parser.js (limited to 'includes/js/dojo/parser.js') diff --git a/includes/js/dojo/parser.js b/includes/js/dojo/parser.js new file mode 100644 index 0000000..5394338 --- /dev/null +++ b/includes/js/dojo/parser.js @@ -0,0 +1,277 @@ +if(!dojo._hasResource["dojo.parser"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojo.parser"] = true; +dojo.provide("dojo.parser"); +dojo.require("dojo.date.stamp"); + +dojo.parser = new function(){ + // summary: The Dom/Widget parsing package + + var d = dojo; + var dtName = d._scopeName + "Type"; + var qry = "[" + dtName + "]"; + + function val2type(/*Object*/ value){ + // summary: + // Returns name of type of given value. + + if(d.isString(value)){ return "string"; } + if(typeof value == "number"){ return "number"; } + if(typeof value == "boolean"){ return "boolean"; } + if(d.isFunction(value)){ return "function"; } + if(d.isArray(value)){ return "array"; } // typeof [] == "object" + if(value instanceof Date) { return "date"; } // assume timestamp + if(value instanceof d._Url){ return "url"; } + return "object"; + } + + function str2obj(/*String*/ value, /*String*/ type){ + // summary: + // Convert given string value to given type + switch(type){ + case "string": + return value; + case "number": + return value.length ? Number(value) : NaN; + case "boolean": + // for checked/disabled value might be "" or "checked". interpret as true. + return typeof value == "boolean" ? value : !(value.toLowerCase()=="false"); + case "function": + if(d.isFunction(value)){ + // IE gives us a function, even when we say something like onClick="foo" + // (in which case it gives us an invalid function "function(){ foo }"). + // Therefore, convert to string + value=value.toString(); + value=d.trim(value.substring(value.indexOf('{')+1, value.length-1)); + } + try{ + if(value.search(/[^\w\.]+/i) != -1){ + // TODO: "this" here won't work + value = d.parser._nameAnonFunc(new Function(value), this); + } + return d.getObject(value, false); + }catch(e){ return new Function(); } + case "array": + return value.split(/\s*,\s*/); + case "date": + switch(value){ + case "": return new Date(""); // the NaN of dates + case "now": return new Date(); // current date + default: return d.date.stamp.fromISOString(value); + } + case "url": + return d.baseUrl + value; + default: + return d.fromJson(value); + } + } + + var instanceClasses = { + // map from fully qualified name (like "dijit.Button") to structure like + // { cls: dijit.Button, params: {label: "string", disabled: "boolean"} } + }; + + function getClassInfo(/*String*/ className){ + // className: + // fully qualified name (like "dijit.Button") + // returns: + // structure like + // { + // cls: dijit.Button, + // params: { label: "string", disabled: "boolean"} + // } + + if(!instanceClasses[className]){ + // get pointer to widget class + var cls = d.getObject(className); + if(!d.isFunction(cls)){ + throw new Error("Could not load class '" + className + + "'. Did you spell the name correctly and use a full path, like 'dijit.form.Button'?"); + } + var proto = cls.prototype; + + // get table of parameter names & types + var params={}; + for(var name in proto){ + if(name.charAt(0)=="_"){ continue; } // skip internal properties + var defVal = proto[name]; + params[name]=val2type(defVal); + } + + instanceClasses[className] = { cls: cls, params: params }; + } + return instanceClasses[className]; + } + + this._functionFromScript = function(script){ + var preamble = ""; + var suffix = ""; + var argsStr = script.getAttribute("args"); + if(argsStr){ + d.forEach(argsStr.split(/\s*,\s*/), function(part, idx){ + preamble += "var "+part+" = arguments["+idx+"]; "; + }); + } + var withStr = script.getAttribute("with"); + if(withStr && withStr.length){ + d.forEach(withStr.split(/\s*,\s*/), function(part){ + preamble += "with("+part+"){"; + suffix += "}"; + }); + } + return new Function(preamble+script.innerHTML+suffix); + } + + this.instantiate = function(/* Array */nodes){ + // summary: + // Takes array of nodes, and turns them into class instances and + // potentially calls a layout method to allow them to connect with + // any children + var thelist = []; + d.forEach(nodes, function(node){ + if(!node){ return; } + var type = node.getAttribute(dtName); + if((!type)||(!type.length)){ return; } + var clsInfo = getClassInfo(type); + var clazz = clsInfo.cls; + var ps = clazz._noScript||clazz.prototype._noScript; + + // read parameters (ie, attributes). + // clsInfo.params lists expected params like {"checked": "boolean", "n": "number"} + var params = {}; + var attributes = node.attributes; + for(var name in clsInfo.params){ + var item = attributes.getNamedItem(name); + if(!item || (!item.specified && (!dojo.isIE || name.toLowerCase()!="value"))){ continue; } + var value = item.value; + // Deal with IE quirks for 'class' and 'style' + switch(name){ + case "class": + value = node.className; + break; + case "style": + value = node.style && node.style.cssText; // FIXME: Opera? + } + var _type = clsInfo.params[name]; + params[name] = str2obj(value, _type); + } + + // Process