_ListBase.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. define("dijit/form/_ListBase", [
  2. "dojo/_base/declare", // declare
  3. "dojo/window" // winUtils.scrollIntoView
  4. ], function(declare, winUtils){
  5. // module:
  6. // dijit/form/_ListBase
  7. // summary:
  8. // Focus-less menu to handle UI events consistently
  9. return declare( "dijit.form._ListBase", null, {
  10. // summary:
  11. // Focus-less menu to handle UI events consistently
  12. // Abstract methods that must be defined externally:
  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. _getTarget: function(/*Event*/ evt){
  21. var tgt = evt.target;
  22. var container = this.containerNode;
  23. if(tgt == container || tgt == this.domNode){ return null; }
  24. while(tgt && tgt.parentNode != container){
  25. // recurse to the top
  26. tgt = tgt.parentNode;
  27. }
  28. return tgt;
  29. },
  30. selectFirstNode: function(){
  31. // summary:
  32. // Select the first displayed item in the list.
  33. var first = this.containerNode.firstChild;
  34. while(first && first.style.display == "none"){
  35. first = first.nextSibling;
  36. }
  37. this._setSelectedAttr(first, true);
  38. },
  39. selectLastNode: function(){
  40. // summary:
  41. // Select the last displayed item in the list
  42. var last = this.containerNode.lastChild;
  43. while(last && last.style.display == "none"){
  44. last = last.previousSibling;
  45. }
  46. this._setSelectedAttr(last, true);
  47. },
  48. selectNextNode: function(){
  49. // summary:
  50. // Select the item just below the current selection.
  51. // If nothing selected, select first node.
  52. var selectedNode = this._getSelectedAttr();
  53. if(!selectedNode){
  54. this.selectFirstNode();
  55. }else{
  56. var next = selectedNode.nextSibling;
  57. while(next && next.style.display == "none"){
  58. next = next.nextSibling;
  59. }
  60. if(!next){
  61. this.selectFirstNode();
  62. }else{
  63. this._setSelectedAttr(next, true);
  64. }
  65. }
  66. },
  67. selectPreviousNode: function(){
  68. // summary:
  69. // Select the item just above the current selection.
  70. // If nothing selected, select last node (if
  71. // you select Previous and try to keep scrolling up the list).
  72. var selectedNode = this._getSelectedAttr();
  73. if(!selectedNode){
  74. this.selectLastNode();
  75. }else{
  76. var prev = selectedNode.previousSibling;
  77. while(prev && prev.style.display == "none"){
  78. prev = prev.previousSibling;
  79. }
  80. if(!prev){
  81. this.selectLastNode();
  82. }else{
  83. this._setSelectedAttr(prev, true);
  84. }
  85. }
  86. },
  87. _setSelectedAttr: function(/*DomNode*/ node, /*Boolean*/ scroll){
  88. // summary:
  89. // Does the actual select.
  90. // node:
  91. // The option to select
  92. // scroll:
  93. // If necessary, scroll node into view. Set to false for mouse/touch to
  94. // avoid jumping problems on mobile/RTL, see https://bugs.dojotoolkit.org/ticket/17739.
  95. if(this.selected != node){
  96. var selectedNode = this._getSelectedAttr();
  97. if(selectedNode){
  98. this.onDeselect(selectedNode);
  99. this.selected = null;
  100. }
  101. if(node && node.parentNode == this.containerNode){
  102. this.selected = node;
  103. if(scroll){
  104. winUtils.scrollIntoView(node);
  105. }
  106. this.onSelect(node);
  107. }
  108. }else if(node){
  109. this.onSelect(node);
  110. }
  111. },
  112. _getSelectedAttr: function(){
  113. // summary:
  114. // Returns the selected node.
  115. var v = this.selected;
  116. return (v && v.parentNode == this.containerNode) ? v : (this.selected = null);
  117. }
  118. });
  119. });