parser.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. define("dojox/mobile/parser", [
  2. "dojo/_base/kernel",
  3. "dojo/_base/config",
  4. "dojo/_base/lang",
  5. "dojo/_base/window",
  6. "dojo/ready"
  7. ], function(dojo, config, lang, win, ready){
  8. // module:
  9. // dojox/mobile/parser
  10. // summary:
  11. // A lightweight parser.
  12. var dm = lang.getObject("dojox.mobile", true);
  13. var parser = new function(){
  14. // summary:
  15. // A lightweight parser.
  16. // description:
  17. // dojox.mobile.parser is an extremely small subset of
  18. // dojo.parser. It has no extended features over dojo.parser, so
  19. // there is no reason you have to use dojox.mobile.parser instead
  20. // of dojo.parser. However, if dojox.mobile.parser's capability is
  21. // enough for your application, use of it could reduce the total
  22. // code size.
  23. this.instantiate = function(/* Array */nodes, /* Object? */mixin, /* Object? */args){
  24. // summary:
  25. // Function for instantiating a list of widget nodes.
  26. // nodes:
  27. // The list of DOMNodes to walk and instantiate widgets on.
  28. mixin = mixin || {};
  29. args = args || {};
  30. var i, ws = [];
  31. if(nodes){
  32. for(i = 0; i < nodes.length; i++){
  33. var n = nodes[i];
  34. var cls = lang.getObject(n.getAttribute("dojoType") || n.getAttribute("data-dojo-type"));
  35. var proto = cls.prototype;
  36. var params = {}, prop, v, t;
  37. lang.mixin(params, eval('({'+(n.getAttribute("data-dojo-props")||"")+'})'));
  38. lang.mixin(params, args.defaults);
  39. lang.mixin(params, mixin);
  40. for(prop in proto){
  41. v = n.getAttributeNode(prop);
  42. v = v && v.nodeValue;
  43. t = typeof proto[prop];
  44. if(!v && (t !== "boolean" || v !== "")){ continue; }
  45. if(t === "string"){
  46. params[prop] = v;
  47. }else if(t === "number"){
  48. params[prop] = v - 0;
  49. }else if(t === "boolean"){
  50. params[prop] = (v !== "false");
  51. }else if(t === "object"){
  52. params[prop] = eval("(" + v + ")");
  53. }
  54. }
  55. params["class"] = n.className;
  56. params.style = n.style && n.style.cssText;
  57. v = n.getAttribute("data-dojo-attach-point");
  58. if(v){ params.dojoAttachPoint = v; }
  59. v = n.getAttribute("data-dojo-attach-event");
  60. if(v){ params.dojoAttachEvent = v; }
  61. var instance = new cls(params, n);
  62. ws.push(instance);
  63. var jsId = n.getAttribute("jsId") || n.getAttribute("data-dojo-id");
  64. if(jsId){
  65. lang.setObject(jsId, instance);
  66. }
  67. }
  68. for(i = 0; i < ws.length; i++){
  69. var w = ws[i];
  70. !args.noStart && w.startup && !w._started && w.startup();
  71. }
  72. }
  73. return ws;
  74. };
  75. this.parse = function(rootNode, args){
  76. // summary:
  77. // Function to handle parsing for widgets in the current document.
  78. // It is not as powerful as the full parser, but it will handle basic
  79. // use cases fine.
  80. // rootNode:
  81. // The root node in the document to parse from
  82. if(!rootNode){
  83. rootNode = win.body();
  84. }else if(!args && rootNode.rootNode){
  85. // Case where 'rootNode' is really a params object.
  86. args = rootNode;
  87. rootNode = rootNode.rootNode;
  88. }
  89. var nodes = rootNode.getElementsByTagName("*");
  90. var i, list = [];
  91. for(i = 0; i < nodes.length; i++){
  92. var n = nodes[i];
  93. if(n.getAttribute("dojoType") || n.getAttribute("data-dojo-type")){
  94. list.push(n);
  95. }
  96. }
  97. var mixin = args && args.template ? {template: true} : null;
  98. return this.instantiate(list, mixin, args);
  99. };
  100. }();
  101. if(config.parseOnLoad){
  102. ready(100, parser, "parse");
  103. }
  104. dm.parser = parser; // for backward compatibility
  105. dojo.parser = parser; // in case user application calls dojo.parser
  106. return parser;
  107. });