_ListBase.js.uncompressed.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. define("dijit/form/_ListBase", [
  2. "dojo/_base/declare", // declare
  3. "dojo/on",
  4. "dojo/window" // winUtils.scrollIntoView
  5. ], function(declare, on, winUtils){
  6. // module:
  7. // dijit/form/_ListBase
  8. return declare( "dijit.form._ListBase", null, {
  9. // summary:
  10. // Focus-less menu to handle UI events consistently
  11. // Abstract methods that must be defined externally:
  12. //
  13. // - onSelect: item is active (mousedown but not yet mouseup, or keyboard arrow selected but no Enter)
  14. // - onDeselect: cancels onSelect
  15. // tags:
  16. // private
  17. // selected: DOMNode
  18. // currently selected node
  19. selected: null,
  20. _listConnect: function(/*String|Function*/ eventType, /*String*/ callbackFuncName){
  21. // summary:
  22. // Connects 'containerNode' to specified method of this object
  23. // and automatically registers for 'disconnect' on widget destroy.
  24. // description:
  25. // Provide widget-specific analog to 'connect'.
  26. // The callback function is called with the normal event object,
  27. // but also a second parameter is passed that indicates which list item
  28. // actually received the event.
  29. // returns:
  30. // A handle that can be passed to `disconnect` in order to disconnect
  31. // before the widget is destroyed.
  32. // tags:
  33. // private
  34. var self = this;
  35. return self.own(on(self.containerNode,
  36. on.selector(
  37. function(eventTarget, selector, target){
  38. return eventTarget.parentNode == target;
  39. },
  40. eventType
  41. ),
  42. function(evt){
  43. evt.preventDefault();
  44. self[callbackFuncName](evt, this);
  45. }
  46. ));
  47. },
  48. selectFirstNode: function(){
  49. // summary:
  50. // Select the first displayed item in the list.
  51. var first = this.containerNode.firstChild;
  52. while(first && first.style.display == "none"){
  53. first = first.nextSibling;
  54. }
  55. this._setSelectedAttr(first);
  56. },
  57. selectLastNode: function(){
  58. // summary:
  59. // Select the last displayed item in the list
  60. var last = this.containerNode.lastChild;
  61. while(last && last.style.display == "none"){
  62. last = last.previousSibling;
  63. }
  64. this._setSelectedAttr(last);
  65. },
  66. selectNextNode: function(){
  67. // summary:
  68. // Select the item just below the current selection.
  69. // If nothing selected, select first node.
  70. var selectedNode = this.selected;
  71. if(!selectedNode){
  72. this.selectFirstNode();
  73. }else{
  74. var next = selectedNode.nextSibling;
  75. while(next && next.style.display == "none"){
  76. next = next.nextSibling;
  77. }
  78. if(!next){
  79. this.selectFirstNode();
  80. }else{
  81. this._setSelectedAttr(next);
  82. }
  83. }
  84. },
  85. selectPreviousNode: function(){
  86. // summary:
  87. // Select the item just above the current selection.
  88. // If nothing selected, select last node (if
  89. // you select Previous and try to keep scrolling up the list).
  90. var selectedNode = this.selected;
  91. if(!selectedNode){
  92. this.selectLastNode();
  93. }else{
  94. var prev = selectedNode.previousSibling;
  95. while(prev && prev.style.display == "none"){
  96. prev = prev.previousSibling;
  97. }
  98. if(!prev){
  99. this.selectLastNode();
  100. }else{
  101. this._setSelectedAttr(prev);
  102. }
  103. }
  104. },
  105. _setSelectedAttr: function(/*DomNode*/ node){
  106. // summary:
  107. // Does the actual select.
  108. if(this.selected != node){
  109. var selectedNode = this.selected;
  110. if(selectedNode){
  111. this.onDeselect(selectedNode);
  112. this.selected = null;
  113. }
  114. if(node){
  115. this.selected = node;
  116. winUtils.scrollIntoView(node);
  117. this.onSelect(node);
  118. }
  119. }else if(node){
  120. this.onSelect(node);
  121. }
  122. }
  123. });
  124. });