Declaration.js 3.9 KB

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