Declaration.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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["dijit.Declaration"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.Declaration"] = true;
  8. dojo.provide("dijit.Declaration");
  9. dojo.require("dijit._Widget");
  10. dojo.require("dijit._Templated");
  11. dojo.declare(
  12. "dijit.Declaration",
  13. dijit._Widget,
  14. {
  15. // summary:
  16. // The Declaration widget allows a developer to declare new widget
  17. // classes directly from a snippet of markup.
  18. // _noScript: [private] Boolean
  19. // Flag to parser to leave alone the script tags contained inside of me
  20. _noScript: true,
  21. // stopParser: [private] Boolean
  22. // Flag to parser to not try and parse widgets declared inside of me
  23. stopParser: true,
  24. // widgetClass: [const] String
  25. // Name of class being declared, ex: "acme.myWidget"
  26. widgetClass: "",
  27. // propList: [const] Object
  28. // Set of attributes for this widget along with default values, ex:
  29. // {delay: 100, title: "hello world"}
  30. defaults: null,
  31. // mixins: [const] String[]
  32. // List containing the prototype for this widget, and also any mixins,
  33. // ex: ["dijit._Widget", "dijit._Container"]
  34. mixins: [],
  35. buildRendering: function(){
  36. var src = this.srcNodeRef.parentNode.removeChild(this.srcNodeRef),
  37. methods = dojo.query("> script[type^='dojo/method']", src).orphan(),
  38. connects = dojo.query("> script[type^='dojo/connect']", src).orphan(),
  39. srcType = src.nodeName;
  40. var propList = this.defaults || {};
  41. // For all methods defined like <script type="dojo/method" data-dojo-event="foo">,
  42. // add that method to prototype.
  43. // If there's no "event" specified then it's code to run on instantiation,
  44. // so it becomes a connection to "postscript" (handled below).
  45. dojo.forEach(methods, function(s){
  46. var evt = s.getAttribute("event") || s.getAttribute("data-dojo-event"),
  47. func = dojo.parser._functionFromScript(s);
  48. if(evt){
  49. propList[evt] = func;
  50. }else{
  51. connects.push(s);
  52. }
  53. });
  54. // map array of strings like [ "dijit.form.Button" ] to array of mixin objects
  55. // (note that dojo.map(this.mixins, dojo.getObject) doesn't work because it passes
  56. // a bogus third argument to getObject(), confusing it)
  57. this.mixins = this.mixins.length ?
  58. dojo.map(this.mixins, function(name){ return dojo.getObject(name); } ) :
  59. [ dijit._Widget, dijit._Templated ];
  60. propList.widgetsInTemplate = true;
  61. propList._skipNodeCache = true;
  62. propList.templateString = "<"+srcType+" class='"+src.className+"' dojoAttachPoint='"+(src.getAttribute("dojoAttachPoint") || '')+"' dojoAttachEvent='"+(src.getAttribute("dojoAttachEvent") || '')+"' >"+src.innerHTML.replace(/\%7B/g,"{").replace(/\%7D/g,"}")+"</"+srcType+">";
  63. // strip things so we don't create stuff under us in the initial setup phase
  64. dojo.query("[dojoType]", src).forEach(function(node){
  65. node.removeAttribute("dojoType");
  66. });
  67. // create the new widget class
  68. var wc = dojo.declare(
  69. this.widgetClass,
  70. this.mixins,
  71. propList
  72. );
  73. // Handle <script> blocks of form:
  74. // <script type="dojo/connect" data-dojo-event="foo">
  75. // and
  76. // <script type="dojo/method">
  77. // (Note that the second one is just shorthand for a dojo/connect to postscript)
  78. // Since this is a connect in the declaration, we are actually connection to the method
  79. // in the _prototype_.
  80. dojo.forEach(connects, function(s){
  81. var evt = s.getAttribute("event") || s.getAttribute("data-dojo-event") || "postscript",
  82. func = dojo.parser._functionFromScript(s);
  83. dojo.connect(wc.prototype, evt, func);
  84. });
  85. }
  86. }
  87. );
  88. }