Declaration.js.uncompressed.js 3.7 KB

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