parser.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.mobile.parser"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.mobile.parser"] = true;
  8. dojo.provide("dojox.mobile.parser");
  9. dojo.provide("dojo.parser"); // not to load dojo.parser unexpectedly
  10. dojox.mobile.parser = new function(){
  11. this.instantiate = function(list, defaultParams){
  12. // summary:
  13. // Function for instantiating a list of widget nodes.
  14. // list:
  15. // The list of DOMNodes to walk and instantiate widgets on.
  16. var ws = [];
  17. if(list){
  18. var i, len;
  19. len = list.length;
  20. for(i = 0; i < len; i++){
  21. var node = list[i];
  22. var cls = dojo.getObject(dojo.attr(node, "dojoType"));
  23. var proto = cls.prototype;
  24. var params = {};
  25. if(defaultParams){
  26. for(var name in defaultParams){
  27. params[name] = defaultParams[name];
  28. }
  29. }
  30. for(var prop in proto){
  31. var val = dojo.attr(node, prop);
  32. if(!val){ continue; }
  33. if(typeof proto[prop] == "string"){
  34. params[prop] = val;
  35. }else if(typeof proto[prop] == "number"){
  36. params[prop] = val - 0;
  37. }else if(typeof proto[prop] == "boolean"){
  38. params[prop] = (val != "false");
  39. }else if(typeof proto[prop] == "object"){
  40. params[prop] = eval("(" + val + ")");
  41. }
  42. }
  43. params["class"] = node.className;
  44. params["style"] = node.style && node.style.cssText;
  45. var instance = new cls(params, node);
  46. ws.push(instance);
  47. var jsId = node.getAttribute("jsId");
  48. if(jsId){
  49. dojo.setObject(jsId, instance);
  50. }
  51. }
  52. len = ws.length;
  53. for(i = 0; i < len; i++){
  54. var w = ws[i];
  55. w.startup && !w._started && (!w.getParent || !w.getParent()) && w.startup();
  56. }
  57. }
  58. return ws;
  59. };
  60. this.parse = function(rootNode, defaultParams){
  61. // summary:
  62. // Function to handle parsing for widgets in the current document.
  63. // It is not as powerful as the full dojo parser, but it will handle basic
  64. // use cases fine.
  65. // rootNode:
  66. // The root node in the document to parse from
  67. if(!rootNode){
  68. rootNode = dojo.body();
  69. }else if(!defaultParams && rootNode.rootNode){
  70. // Case where 'rootNode' is really a params object.
  71. rootNode = rootNode.rootNode;
  72. }
  73. var nodes = rootNode.getElementsByTagName("*");
  74. var list = [];
  75. for(var i = 0, len = nodes.length; i < len; i++){
  76. if(nodes[i].getAttribute("dojoType")){
  77. list.push(nodes[i]);
  78. }
  79. }
  80. return this.instantiate(list, defaultParams);
  81. };
  82. }();
  83. dojo._loaders.unshift(function(){
  84. if(dojo.config.parseOnLoad){
  85. dojox.mobile.parser.parse();
  86. }
  87. });
  88. }