TreeModel.html 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-selection-TreeModel'>/**
  19. </span> * Adds custom behavior for left/right keyboard navigation for use with a tree.
  20. * Depends on the view having an expand and collapse method which accepts a
  21. * record.
  22. *
  23. * @private
  24. */
  25. Ext.define('Ext.selection.TreeModel', {
  26. extend: 'Ext.selection.RowModel',
  27. alias: 'selection.treemodel',
  28. // typically selection models prune records from the selection
  29. // model when they are removed, because the TreeView constantly
  30. // adds/removes records as they are expanded/collapsed
  31. pruneRemoved: false,
  32. onKeyRight: function(e, t) {
  33. var focused = this.getLastFocused(),
  34. view = this.view;
  35. if (focused) {
  36. // tree node is already expanded, go down instead
  37. // this handles both the case where we navigate to firstChild and if
  38. // there are no children to the nextSibling
  39. if (focused.isExpanded()) {
  40. this.onKeyDown(e, t);
  41. // if its not a leaf node, expand it
  42. } else if (focused.isExpandable()) {
  43. view.expand(focused);
  44. }
  45. }
  46. },
  47. onKeyLeft: function(e, t) {
  48. var focused = this.getLastFocused(),
  49. view = this.view,
  50. viewSm = view.getSelectionModel(),
  51. parentNode, parentRecord;
  52. if (focused) {
  53. parentNode = focused.parentNode;
  54. // if focused node is already expanded, collapse it
  55. if (focused.isExpanded()) {
  56. view.collapse(focused);
  57. // has a parentNode and its not root
  58. // TODO: this needs to cover the case where the root isVisible
  59. } else if (parentNode &amp;&amp; !parentNode.isRoot()) {
  60. // Select a range of records when doing multiple selection.
  61. if (e.shiftKey) {
  62. viewSm.selectRange(parentNode, focused, e.ctrlKey, 'up');
  63. viewSm.setLastFocused(parentNode);
  64. // just move focus, not selection
  65. } else if (e.ctrlKey) {
  66. viewSm.setLastFocused(parentNode);
  67. // select it
  68. } else {
  69. viewSm.select(parentNode);
  70. }
  71. }
  72. }
  73. },
  74. onKeySpace: function(e, t) {
  75. this.toggleCheck(e);
  76. },
  77. onKeyEnter: function(e, t) {
  78. this.toggleCheck(e);
  79. },
  80. toggleCheck: function(e){
  81. e.stopEvent();
  82. var selected = this.getLastSelected();
  83. if (selected) {
  84. this.view.onCheckChange(selected);
  85. }
  86. }
  87. });
  88. </pre>
  89. </body>
  90. </html>