2010-11-15 07:39:52 +00:00
|
|
|
/*
|
2011-11-08 16:40:44 +00:00
|
|
|
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
|
2010-11-15 07:39:52 +00:00
|
|
|
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
|
|
|
see: http://dojotoolkit.org/license for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
if(!dojo._hasResource["dojo.parser"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
|
dojo._hasResource["dojo.parser"] = true;
|
2010-11-15 07:39:52 +00:00
|
|
|
dojo.provide("dojo.parser");
|
|
|
|
dojo.require("dojo.date.stamp");
|
2011-03-04 16:02:28 +00:00
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
new Date("X"); // workaround for #11279, new Date("") == NaN
|
|
|
|
|
|
|
|
dojo.parser = new function(){
|
2011-11-08 16:40:44 +00:00
|
|
|
// summary:
|
|
|
|
// The Dom/Widget parsing package
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
var d = dojo;
|
|
|
|
|
|
|
|
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":
|
2011-11-08 16:40:44 +00:00
|
|
|
// for checked/disabled value might be "" or "checked". interpret as true.
|
2011-03-04 16:02:28 +00:00
|
|
|
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"
|
2011-11-08 16:40:44 +00:00
|
|
|
// (in which case it gives us an invalid function "function(){ foo }").
|
|
|
|
// Therefore, convert to string
|
2011-03-04 16:02:28 +00:00
|
|
|
value=value.toString();
|
|
|
|
value=d.trim(value.substring(value.indexOf('{')+1, value.length-1));
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
if(value === "" || value.search(/[^\w\.]+/i) != -1){
|
|
|
|
// The user has specified some text for a function like "return x+5"
|
|
|
|
return new Function(value);
|
|
|
|
}else{
|
|
|
|
// The user has specified the name of a function like "myOnClick"
|
|
|
|
// or a single word function "return"
|
|
|
|
return d.getObject(value, false) || new Function(value);
|
|
|
|
}
|
|
|
|
}catch(e){ return new Function(); }
|
|
|
|
case "array":
|
|
|
|
return value ? 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
var dummyClass = {}, instanceClasses = {
|
2011-03-04 16:02:28 +00:00
|
|
|
// map from fully qualified name (like "dijit.Button") to structure like
|
|
|
|
// { cls: dijit.Button, params: {label: "string", disabled: "boolean"} }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Widgets like BorderContainer add properties to _Widget via dojo.extend().
|
|
|
|
// If BorderContainer is loaded after _Widget's parameter list has been cached,
|
|
|
|
// we need to refresh that parameter list (for _Widget and all widgets that extend _Widget).
|
2011-11-08 16:40:44 +00:00
|
|
|
// TODO: remove this in 2.0, when we stop caching parameters.
|
|
|
|
d.connect(d, "extend", function(){
|
2011-03-04 16:02:28 +00:00
|
|
|
instanceClasses = {};
|
|
|
|
});
|
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
function getProtoInfo(cls, params){
|
|
|
|
// cls: A prototype
|
|
|
|
// The prototype of the class to check props on
|
|
|
|
// params: Object
|
|
|
|
// The parameters object to mix found parameters onto.
|
|
|
|
for(var name in cls){
|
|
|
|
if(name.charAt(0)=="_"){ continue; } // skip internal properties
|
|
|
|
if(name in dummyClass){ continue; } // skip "constructor" and "toString"
|
|
|
|
params[name] = val2type(cls[name]);
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClassInfo(/*String*/ className, /*Boolean*/ skipParamsLookup){
|
|
|
|
// summary:
|
|
|
|
// Maps a widget name string like "dijit.form.Button" to the widget constructor itself,
|
|
|
|
// and a list of that widget's parameters and their types
|
2011-03-04 16:02:28 +00:00
|
|
|
// className:
|
|
|
|
// fully qualified name (like "dijit.form.Button")
|
|
|
|
// returns:
|
|
|
|
// structure like
|
2011-11-08 16:40:44 +00:00
|
|
|
// {
|
|
|
|
// cls: dijit.Button,
|
2011-03-04 16:02:28 +00:00
|
|
|
// params: { label: "string", disabled: "boolean"}
|
|
|
|
// }
|
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
var c = instanceClasses[className];
|
|
|
|
if(!c){
|
2011-03-04 16:02:28 +00:00
|
|
|
// get pointer to widget class
|
2011-11-08 16:40:44 +00:00
|
|
|
var cls = d.getObject(className), params = null;
|
2011-03-04 16:02:28 +00:00
|
|
|
if(!cls){ return null; } // class not defined [yet]
|
2011-11-08 16:40:44 +00:00
|
|
|
if(!skipParamsLookup){ // from fastpath, we don't need to lookup the attrs on the proto because they are explicit
|
|
|
|
params = getProtoInfo(cls.prototype, {})
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
2011-11-08 16:40:44 +00:00
|
|
|
c = { cls: cls, params: params };
|
|
|
|
|
|
|
|
}else if(!skipParamsLookup && !c.params){
|
|
|
|
// if we're calling getClassInfo and have a cls proto, but no params info, scan that cls for params now
|
|
|
|
// and update the pointer in instanceClasses[className]. This happens when a widget appears in another
|
|
|
|
// widget's template which still uses dojoType, but an instance of the widget appears prior with a data-dojo-type,
|
|
|
|
// skipping this lookup the first time.
|
|
|
|
c.params = getProtoInfo(c.cls.prototype, {});
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
2011-11-08 16:40:44 +00:00
|
|
|
|
|
|
|
return c;
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
this._functionFromScript = function(script, attrData){
|
|
|
|
// summary:
|
|
|
|
// Convert a <script type="dojo/method" args="a, b, c"> ... </script>
|
|
|
|
// into a function
|
|
|
|
// script: DOMNode
|
|
|
|
// The <script> DOMNode
|
|
|
|
// attrData: String
|
|
|
|
// For HTML5 compliance, searches for attrData + "args" (typically
|
|
|
|
// "data-dojo-args") instead of "args"
|
2011-03-04 16:02:28 +00:00
|
|
|
var preamble = "";
|
|
|
|
var suffix = "";
|
2011-11-08 16:40:44 +00:00
|
|
|
var argsStr = (script.getAttribute(attrData + "args") || script.getAttribute("args"));
|
2011-03-04 16:02:28 +00:00
|
|
|
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);
|
2011-11-08 16:40:44 +00:00
|
|
|
};
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
this.instantiate = function(/* Array */nodes, /* Object? */mixin, /* Object? */args){
|
|
|
|
// summary:
|
|
|
|
// Takes array of nodes, and turns them into class instances and
|
|
|
|
// potentially calls a startup method to allow them to connect with
|
|
|
|
// any children.
|
|
|
|
// nodes: Array
|
|
|
|
// Array of nodes or objects like
|
|
|
|
// | {
|
|
|
|
// | type: "dijit.form.Button",
|
|
|
|
// | node: DOMNode,
|
|
|
|
// | scripts: [ ... ], // array of <script type="dojo/..."> children of node
|
|
|
|
// | inherited: { ... } // settings inherited from ancestors like dir, theme, etc.
|
|
|
|
// | }
|
|
|
|
// mixin: Object?
|
|
|
|
// An object that will be mixed in with each node in the array.
|
|
|
|
// Values in the mixin will override values in the node, if they
|
|
|
|
// exist.
|
|
|
|
// args: Object?
|
|
|
|
// An object used to hold kwArgs for instantiation.
|
2011-11-08 16:40:44 +00:00
|
|
|
// See parse.args argument for details.
|
|
|
|
|
|
|
|
var thelist = [],
|
2011-03-04 16:02:28 +00:00
|
|
|
mixin = mixin||{};
|
|
|
|
args = args||{};
|
2011-11-08 16:40:44 +00:00
|
|
|
|
|
|
|
// TODO: for 2.0 default to data-dojo- regardless of scopeName (or maybe scopeName won't exist in 2.0)
|
|
|
|
var attrName = (args.scope || d._scopeName) + "Type", // typically "dojoType"
|
|
|
|
attrData = "data-" + (args.scope || d._scopeName) + "-"; // typically "data-dojo-"
|
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
d.forEach(nodes, function(obj){
|
|
|
|
if(!obj){ return; }
|
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
// Get pointers to DOMNode, dojoType string, and clsInfo (metadata about the dojoType), etc.
|
|
|
|
var node, type, clsInfo, clazz, scripts, fastpath;
|
2011-03-04 16:02:28 +00:00
|
|
|
if(obj.node){
|
|
|
|
// new format of nodes[] array, object w/lots of properties pre-computed for me
|
|
|
|
node = obj.node;
|
|
|
|
type = obj.type;
|
2011-11-08 16:40:44 +00:00
|
|
|
fastpath = obj.fastpath;
|
|
|
|
clsInfo = obj.clsInfo || (type && getClassInfo(type, fastpath));
|
2011-03-04 16:02:28 +00:00
|
|
|
clazz = clsInfo && clsInfo.cls;
|
|
|
|
scripts = obj.scripts;
|
|
|
|
}else{
|
2011-11-08 16:40:44 +00:00
|
|
|
// old (backwards compatible) format of nodes[] array, simple array of DOMNodes. no fastpath/data-dojo-type support here.
|
2011-03-04 16:02:28 +00:00
|
|
|
node = obj;
|
2011-11-08 16:40:44 +00:00
|
|
|
type = attrName in mixin ? mixin[attrName] : node.getAttribute(attrName);
|
2011-03-04 16:02:28 +00:00
|
|
|
clsInfo = type && getClassInfo(type);
|
|
|
|
clazz = clsInfo && clsInfo.cls;
|
2011-11-08 16:40:44 +00:00
|
|
|
scripts = (clazz && (clazz._noScript || clazz.prototype._noScript) ? [] :
|
2011-03-04 16:02:28 +00:00
|
|
|
d.query("> script[type^='dojo/']", node));
|
|
|
|
}
|
|
|
|
if(!clsInfo){
|
|
|
|
throw new Error("Could not load class '" + type);
|
|
|
|
}
|
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
// Setup hash to hold parameter settings for this widget. Start with the parameter
|
2011-03-04 16:02:28 +00:00
|
|
|
// settings inherited from ancestors ("dir" and "lang").
|
|
|
|
// Inherited setting may later be overridden by explicit settings on node itself.
|
2011-11-08 16:40:44 +00:00
|
|
|
var params = {};
|
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
if(args.defaults){
|
|
|
|
// settings for the document itself (or whatever subtree is being parsed)
|
2011-11-08 16:40:44 +00:00
|
|
|
d._mixin(params, args.defaults);
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
|
|
|
if(obj.inherited){
|
|
|
|
// settings from dir=rtl or lang=... on a node above this node
|
2011-11-08 16:40:44 +00:00
|
|
|
d._mixin(params, obj.inherited);
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
2011-11-08 16:40:44 +00:00
|
|
|
|
|
|
|
// mix things found in data-dojo-props into the params
|
|
|
|
if(fastpath){
|
|
|
|
var extra = node.getAttribute(attrData + "props");
|
|
|
|
if(extra && extra.length){
|
|
|
|
try{
|
|
|
|
extra = d.fromJson.call(args.propsThis, "{" + extra + "}");
|
|
|
|
d._mixin(params, extra);
|
|
|
|
}catch(e){
|
|
|
|
// give the user a pointer to their invalid parameters. FIXME: can we kill this in production?
|
|
|
|
throw new Error(e.toString() + " in data-dojo-props='" + extra + "'");
|
|
|
|
}
|
|
|
|
}
|
2011-03-04 16:02:28 +00:00
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
// For the benefit of _Templated, check if node has data-dojo-attach-point/data-dojo-attach-event
|
|
|
|
// and mix those in as though they were parameters
|
|
|
|
var attachPoint = node.getAttribute(attrData + "attach-point");
|
|
|
|
if(attachPoint){
|
|
|
|
params.dojoAttachPoint = attachPoint;
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
2011-11-08 16:40:44 +00:00
|
|
|
var attachEvent = node.getAttribute(attrData + "attach-event");
|
|
|
|
if(attachEvent){
|
|
|
|
params.dojoAttachEvent = attachEvent;
|
|
|
|
}
|
|
|
|
dojo.mixin(params, mixin);
|
|
|
|
}else{
|
|
|
|
// FIXME: we need something like "deprecateOnce()" to throw dojo.deprecation for something.
|
|
|
|
// remove this logic in 2.0
|
|
|
|
// read parameters (ie, attributes) specified on DOMNode
|
|
|
|
|
|
|
|
var attributes = node.attributes;
|
|
|
|
|
|
|
|
// clsInfo.params lists expected params like {"checked": "boolean", "n": "number"}
|
|
|
|
for(var name in clsInfo.params){
|
|
|
|
var item = name in mixin ? { value:mixin[name], specified:true } : 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 = "className" in mixin ? mixin.className : node.className;
|
|
|
|
break;
|
|
|
|
case "style":
|
|
|
|
value = "style" in mixin ? mixin.style : (node.style && node.style.cssText); // FIXME: Opera?
|
|
|
|
}
|
|
|
|
var _type = clsInfo.params[name];
|
|
|
|
if(typeof value == "string"){
|
|
|
|
params[name] = str2obj(value, _type);
|
|
|
|
}else{
|
|
|
|
params[name] = value;
|
|
|
|
}
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process <script type="dojo/*"> script tags
|
|
|
|
// <script type="dojo/method" event="foo"> tags are added to params, and passed to
|
|
|
|
// the widget on instantiation.
|
|
|
|
// <script type="dojo/method"> tags (with no event) are executed after instantiation
|
|
|
|
// <script type="dojo/connect" event="foo"> tags are dojo.connected after instantiation
|
|
|
|
// note: dojo/* script tags cannot exist in self closing widgets, like <input />
|
|
|
|
var connects = [], // functions to connect after instantiation
|
|
|
|
calls = []; // functions to call after instantiation
|
|
|
|
|
|
|
|
d.forEach(scripts, function(script){
|
|
|
|
node.removeChild(script);
|
2011-11-08 16:40:44 +00:00
|
|
|
// FIXME: drop event="" support in 2.0. use data-dojo-event="" instead
|
|
|
|
var event = (script.getAttribute(attrData + "event") || script.getAttribute("event")),
|
2011-03-04 16:02:28 +00:00
|
|
|
type = script.getAttribute("type"),
|
2011-11-08 16:40:44 +00:00
|
|
|
nf = d.parser._functionFromScript(script, attrData);
|
2011-03-04 16:02:28 +00:00
|
|
|
if(event){
|
|
|
|
if(type == "dojo/connect"){
|
|
|
|
connects.push({event: event, func: nf});
|
|
|
|
}else{
|
|
|
|
params[event] = nf;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
calls.push(nf);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var markupFactory = clazz.markupFactory || clazz.prototype && clazz.prototype.markupFactory;
|
|
|
|
// create the instance
|
|
|
|
var instance = markupFactory ? markupFactory(params, node, clazz) : new clazz(params, node);
|
|
|
|
thelist.push(instance);
|
|
|
|
|
|
|
|
// map it to the JS namespace if that makes sense
|
2011-11-08 16:40:44 +00:00
|
|
|
// FIXME: in 2.0, drop jsId support. use data-dojo-id instead
|
|
|
|
var jsname = (node.getAttribute(attrData + "id") || node.getAttribute("jsId"));
|
2011-03-04 16:02:28 +00:00
|
|
|
if(jsname){
|
|
|
|
d.setObject(jsname, instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
// process connections and startup functions
|
|
|
|
d.forEach(connects, function(connect){
|
|
|
|
d.connect(instance, connect.event, null, connect.func);
|
|
|
|
});
|
|
|
|
d.forEach(calls, function(func){
|
|
|
|
func.call(instance);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Call startup on each top level instance if it makes sense (as for
|
|
|
|
// widgets). Parent widgets will recursively call startup on their
|
|
|
|
// (non-top level) children
|
|
|
|
if(!mixin._started){
|
|
|
|
// TODO: for 2.0, when old instantiate() API is desupported, store parent-child
|
|
|
|
// relationships in the nodes[] array so that no getParent() call is needed.
|
|
|
|
// Note that will require a parse() call from ContentPane setting a param that the
|
|
|
|
// ContentPane is the parent widget (so that the parse doesn't call startup() on the
|
|
|
|
// ContentPane's children)
|
|
|
|
d.forEach(thelist, function(instance){
|
2011-11-08 16:40:44 +00:00
|
|
|
if( !args.noStart && instance &&
|
|
|
|
dojo.isFunction(instance.startup) &&
|
|
|
|
!instance._started &&
|
2011-03-04 16:02:28 +00:00
|
|
|
(!instance.getParent || !instance.getParent())
|
|
|
|
){
|
|
|
|
instance.startup();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return thelist;
|
|
|
|
};
|
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
this.parse = function(rootNode, args){
|
2011-03-04 16:02:28 +00:00
|
|
|
// summary:
|
|
|
|
// Scan the DOM for class instances, and instantiate them.
|
|
|
|
//
|
|
|
|
// description:
|
|
|
|
// Search specified node (or root node) recursively for class instances,
|
2011-11-08 16:40:44 +00:00
|
|
|
// and instantiate them. Searches for either data-dojo-type="Class" or
|
|
|
|
// dojoType="Class" where "Class" is a a fully qualified class name,
|
|
|
|
// like `dijit.form.Button`
|
|
|
|
//
|
|
|
|
// Using `data-dojo-type`:
|
|
|
|
// Attributes using can be mixed into the parameters used to instantitate the
|
|
|
|
// Class by using a `data-dojo-props` attribute on the node being converted.
|
|
|
|
// `data-dojo-props` should be a string attribute to be converted from JSON.
|
|
|
|
//
|
|
|
|
// Using `dojoType`:
|
|
|
|
// Attributes are read from the original domNode and converted to appropriate
|
|
|
|
// types by looking up the Class prototype values. This is the default behavior
|
|
|
|
// from Dojo 1.0 to Dojo 1.5. `dojoType` support is deprecated, and will
|
|
|
|
// go away in Dojo 2.0.
|
2011-03-04 16:02:28 +00:00
|
|
|
//
|
|
|
|
// rootNode: DomNode?
|
|
|
|
// A default starting root node from which to start the parsing. Can be
|
|
|
|
// omitted, defaulting to the entire document. If omitted, the `args`
|
2011-11-08 16:40:44 +00:00
|
|
|
// object can be passed in this place. If the `args` object has a
|
2011-03-04 16:02:28 +00:00
|
|
|
// `rootNode` member, that is used.
|
|
|
|
//
|
2011-11-08 16:40:44 +00:00
|
|
|
// args: Object
|
2011-03-04 16:02:28 +00:00
|
|
|
// a kwArgs object passed along to instantiate()
|
2011-11-08 16:40:44 +00:00
|
|
|
//
|
2011-03-04 16:02:28 +00:00
|
|
|
// * noStart: Boolean?
|
|
|
|
// when set will prevent the parser from calling .startup()
|
2011-11-08 16:40:44 +00:00
|
|
|
// when locating the nodes.
|
2011-03-04 16:02:28 +00:00
|
|
|
// * rootNode: DomNode?
|
|
|
|
// identical to the function's `rootNode` argument, though
|
2011-11-08 16:40:44 +00:00
|
|
|
// allowed to be passed in via this `args object.
|
|
|
|
// * template: Boolean
|
|
|
|
// If true, ignores ContentPane's stopParser flag and parses contents inside of
|
|
|
|
// a ContentPane inside of a template. This allows dojoAttachPoint on widgets/nodes
|
|
|
|
// nested inside the ContentPane to work.
|
2011-03-04 16:02:28 +00:00
|
|
|
// * inherited: Object
|
|
|
|
// Hash possibly containing dir and lang settings to be applied to
|
|
|
|
// parsed widgets, unless there's another setting on a sub-node that overrides
|
2011-11-08 16:40:44 +00:00
|
|
|
// * scope: String
|
|
|
|
// Root for attribute names to search for. If scopeName is dojo,
|
|
|
|
// will search for data-dojo-type (or dojoType). For backwards compatibility
|
|
|
|
// reasons defaults to dojo._scopeName (which is "dojo" except when
|
|
|
|
// multi-version support is used, when it will be something like dojo16, dojo20, etc.)
|
|
|
|
// * propsThis: Object
|
|
|
|
// If specified, "this" referenced from data-dojo-props will refer to propsThis.
|
|
|
|
// Intended for use from the widgets-in-template feature of `dijit._Templated`
|
2011-03-04 16:02:28 +00:00
|
|
|
//
|
|
|
|
// example:
|
|
|
|
// Parse all widgets on a page:
|
|
|
|
// | dojo.parser.parse();
|
|
|
|
//
|
|
|
|
// example:
|
|
|
|
// Parse all classes within the node with id="foo"
|
2011-11-08 16:40:44 +00:00
|
|
|
// | dojo.parser.parse(dojo.byId('foo'));
|
2011-03-04 16:02:28 +00:00
|
|
|
//
|
|
|
|
// example:
|
2011-11-08 16:40:44 +00:00
|
|
|
// Parse all classes in a page, but do not call .startup() on any
|
2011-03-04 16:02:28 +00:00
|
|
|
// child
|
|
|
|
// | dojo.parser.parse({ noStart: true })
|
|
|
|
//
|
|
|
|
// example:
|
|
|
|
// Parse all classes in a node, but do not call .startup()
|
|
|
|
// | dojo.parser.parse(someNode, { noStart:true });
|
|
|
|
// | // or
|
2011-11-08 16:40:44 +00:00
|
|
|
// | dojo.parser.parse({ noStart:true, rootNode: someNode });
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
// determine the root node based on the passed arguments.
|
|
|
|
var root;
|
|
|
|
if(!args && rootNode && rootNode.rootNode){
|
|
|
|
args = rootNode;
|
|
|
|
root = args.rootNode;
|
|
|
|
}else{
|
|
|
|
root = rootNode;
|
|
|
|
}
|
2011-11-08 16:40:44 +00:00
|
|
|
root = root ? dojo.byId(root) : dojo.body();
|
|
|
|
args = args || {};
|
|
|
|
|
|
|
|
var attrName = (args.scope || d._scopeName) + "Type", // typically "dojoType"
|
|
|
|
attrData = "data-" + (args.scope || d._scopeName) + "-"; // typically "data-dojo-"
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
function scan(parent, list){
|
|
|
|
// summary:
|
|
|
|
// Parent is an Object representing a DOMNode, with or without a dojoType specified.
|
|
|
|
// Scan parent's children looking for nodes with dojoType specified, storing in list[].
|
|
|
|
// If parent has a dojoType, also collects <script type=dojo/*> children and stores in parent.scripts[].
|
|
|
|
// parent: Object
|
|
|
|
// Object representing the parent node, like
|
|
|
|
// | {
|
2011-11-08 16:40:44 +00:00
|
|
|
// | node: DomNode, // scan children of this node
|
2011-03-04 16:02:28 +00:00
|
|
|
// | inherited: {dir: "rtl"}, // dir/lang setting inherited from above node
|
|
|
|
// |
|
|
|
|
// | // attributes only set if node has dojoType specified
|
|
|
|
// | scripts: [], // empty array, put <script type=dojo/*> in here
|
|
|
|
// | clsInfo: { cls: dijit.form.Button, ...}
|
|
|
|
// | }
|
|
|
|
// list: DomNode[]
|
|
|
|
// Output array of objects (same format as parent) representing nodes to be turned into widgets
|
|
|
|
|
|
|
|
// Effective dir and lang settings on parent node, either set directly or inherited from grandparent
|
|
|
|
var inherited = dojo.clone(parent.inherited);
|
|
|
|
dojo.forEach(["dir", "lang"], function(name){
|
2011-11-08 16:40:44 +00:00
|
|
|
// TODO: what if this is a widget and dir/lang are declared in data-dojo-props?
|
2011-03-04 16:02:28 +00:00
|
|
|
var val = parent.node.getAttribute(name);
|
|
|
|
if(val){
|
|
|
|
inherited[name] = val;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// if parent is a widget, then search for <script type=dojo/*> tags and put them in scripts[].
|
2011-11-08 16:40:44 +00:00
|
|
|
var scripts = parent.clsInfo && !parent.clsInfo.cls.prototype._noScript ? parent.scripts : null;
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
// unless parent is a widget with the stopParser flag set, continue search for dojoType, recursively
|
2011-11-08 16:40:44 +00:00
|
|
|
var recurse = (!parent.clsInfo || !parent.clsInfo.cls.prototype.stopParser) || (args && args.template);
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
// scan parent's children looking for dojoType and <script type=dojo/*>
|
|
|
|
for(var child = parent.node.firstChild; child; child = child.nextSibling){
|
|
|
|
if(child.nodeType == 1){
|
2011-11-08 16:40:44 +00:00
|
|
|
// FIXME: desupport dojoType in 2.0. use data-dojo-type instead
|
|
|
|
var type, html5 = recurse && child.getAttribute(attrData + "type");
|
|
|
|
if(html5){
|
|
|
|
type = html5;
|
|
|
|
}else{
|
|
|
|
// fallback to backward compatible mode, using dojoType. remove in 2.0
|
|
|
|
type = recurse && child.getAttribute(attrName);
|
|
|
|
}
|
|
|
|
|
|
|
|
var fastpath = html5 == type;
|
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
if(type){
|
2011-11-08 16:40:44 +00:00
|
|
|
// if dojoType/data-dojo-type specified, add to output array of nodes to instantiate
|
2011-03-04 16:02:28 +00:00
|
|
|
var params = {
|
|
|
|
"type": type,
|
2011-11-08 16:40:44 +00:00
|
|
|
fastpath: fastpath,
|
|
|
|
clsInfo: getClassInfo(type, fastpath), // note: won't find classes declared via dojo.Declaration
|
2011-03-04 16:02:28 +00:00
|
|
|
node: child,
|
|
|
|
scripts: [], // <script> nodes that are parent's children
|
|
|
|
inherited: inherited // dir & lang attributes inherited from parent
|
|
|
|
};
|
|
|
|
list.push(params);
|
|
|
|
|
|
|
|
// Recurse, collecting <script type="dojo/..."> children, and also looking for
|
|
|
|
// descendant nodes with dojoType specified (unless the widget has the stopParser flag),
|
|
|
|
scan(params, list);
|
|
|
|
}else if(scripts && child.nodeName.toLowerCase() == "script"){
|
|
|
|
// if <script type="dojo/...">, save in scripts[]
|
|
|
|
type = child.getAttribute("type");
|
2011-11-08 16:40:44 +00:00
|
|
|
if (type && /^dojo\/\w/i.test(type)) {
|
2011-03-04 16:02:28 +00:00
|
|
|
scripts.push(child);
|
|
|
|
}
|
|
|
|
}else if(recurse){
|
|
|
|
// Recurse, looking for grandchild nodes with dojoType specified
|
|
|
|
scan({
|
|
|
|
node: child,
|
|
|
|
inherited: inherited
|
|
|
|
}, list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
// Ignore bogus entries in inherited hash like {dir: ""}
|
|
|
|
var inherited = {};
|
|
|
|
if(args && args.inherited){
|
|
|
|
for(var key in args.inherited){
|
|
|
|
if(args.inherited[key]){ inherited[key] = args.inherited[key]; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
// Make list of all nodes on page w/dojoType specified
|
|
|
|
var list = [];
|
|
|
|
scan({
|
2011-11-08 16:40:44 +00:00
|
|
|
node: root,
|
|
|
|
inherited: inherited
|
2011-03-04 16:02:28 +00:00
|
|
|
}, list);
|
|
|
|
|
|
|
|
// go build the object instances
|
2011-11-08 16:40:44 +00:00
|
|
|
var mixin = args && args.template ? {template: true} : null;
|
|
|
|
return this.instantiate(list, mixin, args); // Array
|
2011-03-04 16:02:28 +00:00
|
|
|
};
|
2010-11-15 07:39:52 +00:00
|
|
|
}();
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
//Register the parser callback. It should be the first callback
|
|
|
|
//after the a11y test.
|
|
|
|
|
2010-11-15 07:39:52 +00:00
|
|
|
(function(){
|
2011-11-08 16:40:44 +00:00
|
|
|
var parseRunner = function(){
|
2011-03-04 16:02:28 +00:00
|
|
|
if(dojo.config.parseOnLoad){
|
2011-11-08 16:40:44 +00:00
|
|
|
dojo.parser.parse();
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: need to clobber cross-dependency!!
|
2011-11-08 16:40:44 +00:00
|
|
|
if(dojo.getObject("dijit.wai.onload") === dojo._loaders[0]){
|
2011-03-04 16:02:28 +00:00
|
|
|
dojo._loaders.splice(1, 0, parseRunner);
|
|
|
|
}else{
|
|
|
|
dojo._loaders.unshift(parseRunner);
|
|
|
|
}
|
2010-11-15 07:39:52 +00:00
|
|
|
})();
|
2011-03-04 16:02:28 +00:00
|
|
|
|
2010-11-15 07:39:52 +00:00
|
|
|
}
|