not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.5 KiB

  1. /**
  2. _enyo.Selection_ is used to manage row selection state for lists. It
  3. provides selection state management for both single-select and multi-select
  4. lists.
  5. // The following is an excerpt from enyo.FlyweightRepeater.
  6. enyo.kind({
  7. name: "enyo.FlyweightRepeater",
  8. ...
  9. components: [
  10. {kind: "Selection", onSelect: "selectDeselect", onDeselect: "selectDeselect"},
  11. ...
  12. ],
  13. tap: function(inSender, inEvent) {
  14. ...
  15. // mark the tapped row as selected
  16. this.$.selection.select(inEvent.index);
  17. ...
  18. },
  19. selectDeselect: function(inSender, inEvent) {
  20. // this is where a row selection highlight might be applied
  21. this.renderRow(inEvent.key);
  22. }
  23. ...
  24. })
  25. */
  26. enyo.kind({
  27. name: "enyo.Selection",
  28. kind: enyo.Component,
  29. published: {
  30. //* If true, multiple selections are allowed.
  31. multi: false
  32. },
  33. events: {
  34. /**
  35. Fires when an item is selected.
  36. {kind: "Selection", onSelect: "selectRow"...
  37. ...
  38. selectRow: function(inSender, inKey, inPrivateData) {
  39. ...
  40. _inKey_ is whatever key was used to register
  41. the selection (usually a row index).
  42. _inPrivateData_ references data registered
  43. with this key by the code that made the original selection.
  44. */
  45. onSelect: "",
  46. /**
  47. Fires when an item is deselected.
  48. {kind: "Selection", onSelect: "deselectRow"...
  49. ...
  50. deselectRow: function(inSender, inKey, inPrivateData)
  51. ...
  52. _inKey_ is whatever key was used to request
  53. the deselection (usually a row index).
  54. _inPrivateData_ references data registered
  55. with this key by the code that made the selection.
  56. */
  57. onDeselect: "",
  58. //* Sent when selection changes (but not when the selection is cleared).
  59. onChange: ""
  60. },
  61. //* @protected
  62. create: function() {
  63. this.clear();
  64. this.inherited(arguments);
  65. },
  66. multiChanged: function() {
  67. if (!this.multi) {
  68. this.clear();
  69. }
  70. this.doChange();
  71. },
  72. highlander: function(inKey) {
  73. if (!this.multi) {
  74. this.deselect(this.lastSelected);
  75. }
  76. },
  77. //* @public
  78. //* Removes all selections.
  79. clear: function() {
  80. this.selected = {};
  81. },
  82. //* Returns true if the _inKey_ row is selected.
  83. isSelected: function(inKey) {
  84. return this.selected[inKey];
  85. },
  86. //* Manually sets a row's state to selected or unselected.
  87. setByKey: function(inKey, inSelected, inData) {
  88. if (inSelected) {
  89. this.selected[inKey] = (inData || true);
  90. this.lastSelected = inKey;
  91. this.doSelect({key: inKey, data: this.selected[inKey]});
  92. } else {
  93. var was = this.isSelected(inKey);
  94. delete this.selected[inKey];
  95. this.doDeselect({key: inKey, data: was});
  96. }
  97. this.doChange();
  98. },
  99. //* Deselects a row.
  100. deselect: function(inKey) {
  101. if (this.isSelected(inKey)) {
  102. this.setByKey(inKey, false);
  103. }
  104. },
  105. /**
  106. Selects a row. If the _multi_ property is set to false, _select_ will
  107. also deselect the previous selection.
  108. */
  109. select: function(inKey, inData) {
  110. if (this.multi) {
  111. this.setByKey(inKey, !this.isSelected(inKey), inData);
  112. } else if (!this.isSelected(inKey)) {
  113. this.highlander();
  114. this.setByKey(inKey, true, inData);
  115. }
  116. },
  117. /**
  118. Toggles selection state for a row. If the _multi_ property is set to
  119. false, toggling a selection on will deselect the previous selection.
  120. */
  121. toggle: function(inKey, inData) {
  122. if (!this.multi && this.lastSelected != inKey) {
  123. this.deselect(this.lastSelected);
  124. }
  125. this.setByKey(inKey, !this.isSelected(inKey), inData);
  126. },
  127. /**
  128. Returns the selection as a hash in which each selected item has a value;
  129. unselected items are undefined.
  130. */
  131. getSelected: function() {
  132. return this.selected;
  133. }
  134. });