dojo/parser.js

  • Provides:

    • dojo.parser
  • dojo.parser

    • type
      Function
    • ?? initialized = 1 (debug: boolean) ??
    • source: [view]
       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":
          // 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 === "" || 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);
        }
       }


       var dummyClass = {}, instanceClasses = {
        // 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).
       // TODO: remove this in 2.0, when we stop caching parameters.
       d.connect(d, "extend", function(){
        instanceClasses = {};
       });


       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
        // className:
        //  fully qualified name (like "dijit.form.Button")
        // returns:
        //  structure like
        //   {
        //    cls: dijit.Button,
        //    params: { label: "string", disabled: "boolean"}
        //   }


        var c = instanceClasses[className];
        if(!c){
         // get pointer to widget class
         var cls = d.getObject(className), params = null;
         if(!cls){ return null; }  // class not defined [yet]
         if(!skipParamsLookup){ // from fastpath, we don't need to lookup the attrs on the proto because they are explicit
          params = getProtoInfo(cls.prototype, {})
         }
         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, {});
        }

        
        return c;
       }


       this._functionFromScript = function(script, attrData){
        // summary:
        //  Convert a
        //  into a function
        // script: DOMNode
        //  The
        //  into a function
        // script: DOMNode
        //  The