NodeList.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. define(["./kernel", "../query", "./array", "./html", "../NodeList-dom"], function(dojo, query, array){
  2. // module:
  3. // dojo/_base/NodeList
  4. /*=====
  5. return {
  6. // summary:
  7. // This module extends dojo/NodeList with the legacy connect(), coords(),
  8. // blur(), focus(), change(), click(), error(), keydown(), keypress(),
  9. // keyup(), load(), mousedown(), mouseenter(), mouseleave(), mousemove(),
  10. // mouseout(), mouseover(), mouseup(), and submit() methods.
  11. };
  12. =====*/
  13. var NodeList = query.NodeList,
  14. nlp = NodeList.prototype;
  15. nlp.connect = NodeList._adaptAsForEach(function(){
  16. // don't bind early to dojo.connect since we no longer explicitly depend on it
  17. return dojo.connect.apply(this, arguments);
  18. });
  19. /*=====
  20. nlp.connect = function(methodName, objOrFunc, funcName){
  21. // summary:
  22. // Attach event handlers to every item of the NodeList. Uses dojo.connect()
  23. // so event properties are normalized.
  24. //
  25. // Application must manually require() "dojo/_base/connect" before using this method.
  26. // methodName: String
  27. // the name of the method to attach to. For DOM events, this should be
  28. // the lower-case name of the event
  29. // objOrFunc: Object|Function|String
  30. // if 2 arguments are passed (methodName, objOrFunc), objOrFunc should
  31. // reference a function or be the name of the function in the global
  32. // namespace to attach. If 3 arguments are provided
  33. // (methodName, objOrFunc, funcName), objOrFunc must be the scope to
  34. // locate the bound function in
  35. // funcName: String?
  36. // optional. A string naming the function in objOrFunc to bind to the
  37. // event. May also be a function reference.
  38. // example:
  39. // add an onclick handler to every button on the page
  40. // | query("div:nth-child(odd)").connect("onclick", function(e){
  41. // | console.log("clicked!");
  42. // | });
  43. // example:
  44. // attach foo.bar() to every odd div's onmouseover
  45. // | query("div:nth-child(odd)").connect("onmouseover", foo, "bar");
  46. return null; // NodeList
  47. };
  48. =====*/
  49. nlp.coords = NodeList._adaptAsMap(dojo.coords);
  50. /*=====
  51. nlp.coords = function(){
  52. // summary:
  53. // Deprecated: Use position() for border-box x/y/w/h
  54. // or marginBox() for margin-box w/h/l/t.
  55. // Returns the box objects of all elements in a node list as
  56. // an Array (*not* a NodeList). Acts like `domGeom.coords`, though assumes
  57. // the node passed is each node in this list.
  58. return []; // Array
  59. };
  60. =====*/
  61. NodeList.events = [
  62. // summary:
  63. // list of all DOM events used in NodeList
  64. "blur", "focus", "change", "click", "error", "keydown", "keypress",
  65. "keyup", "load", "mousedown", "mouseenter", "mouseleave", "mousemove",
  66. "mouseout", "mouseover", "mouseup", "submit"
  67. ];
  68. // FIXME: pseudo-doc the above automatically generated on-event functions
  69. // syntactic sugar for DOM events
  70. array.forEach(NodeList.events, function(evt){
  71. var _oe = "on" + evt;
  72. nlp[_oe] = function(a, b){
  73. return this.connect(_oe, a, b);
  74. };
  75. // FIXME: should these events trigger publishes?
  76. /*
  77. return (a ? this.connect(_oe, a, b) :
  78. this.forEach(function(n){
  79. // FIXME:
  80. // listeners get buried by
  81. // addEventListener and can't be dug back
  82. // out to be triggered externally.
  83. // see:
  84. // http://developer.mozilla.org/en/docs/DOM:element
  85. console.log(n, evt, _oe);
  86. // FIXME: need synthetic event support!
  87. var _e = { target: n, faux: true, type: evt };
  88. // dojo._event_listener._synthesizeEvent({}, { target: n, faux: true, type: evt });
  89. try{ n[evt](_e); }catch(e){ console.log(e); }
  90. try{ n[_oe](_e); }catch(e){ console.log(e); }
  91. })
  92. );
  93. */
  94. }
  95. );
  96. dojo.NodeList = NodeList;
  97. return NodeList;
  98. });