selectableMixin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var zrUtil = require("zrender/lib/core/util");
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. /**
  39. * Data selectable mixin for chart series.
  40. * To eanble data select, option of series must have `selectedMode`.
  41. * And each data item will use `selected` to toggle itself selected status
  42. */
  43. var _default = {
  44. /**
  45. * @param {Array.<Object>} targetList [{name, value, selected}, ...]
  46. * If targetList is an array, it should like [{name: ..., value: ...}, ...].
  47. * If targetList is a "List", it must have coordDim: 'value' dimension and name.
  48. */
  49. updateSelectedMap: function (targetList) {
  50. this._targetList = zrUtil.isArray(targetList) ? targetList.slice() : [];
  51. this._selectTargetMap = zrUtil.reduce(targetList || [], function (targetMap, target) {
  52. targetMap.set(target.name, target);
  53. return targetMap;
  54. }, zrUtil.createHashMap());
  55. },
  56. /**
  57. * Either name or id should be passed as input here.
  58. * If both of them are defined, id is used.
  59. *
  60. * @param {string|undefined} name name of data
  61. * @param {number|undefined} id dataIndex of data
  62. */
  63. // PENGING If selectedMode is null ?
  64. select: function (name, id) {
  65. var target = id != null ? this._targetList[id] : this._selectTargetMap.get(name);
  66. var selectedMode = this.get('selectedMode');
  67. if (selectedMode === 'single') {
  68. this._selectTargetMap.each(function (target) {
  69. target.selected = false;
  70. });
  71. }
  72. target && (target.selected = true);
  73. },
  74. /**
  75. * Either name or id should be passed as input here.
  76. * If both of them are defined, id is used.
  77. *
  78. * @param {string|undefined} name name of data
  79. * @param {number|undefined} id dataIndex of data
  80. */
  81. unSelect: function (name, id) {
  82. var target = id != null ? this._targetList[id] : this._selectTargetMap.get(name); // var selectedMode = this.get('selectedMode');
  83. // selectedMode !== 'single' && target && (target.selected = false);
  84. target && (target.selected = false);
  85. },
  86. /**
  87. * Either name or id should be passed as input here.
  88. * If both of them are defined, id is used.
  89. *
  90. * @param {string|undefined} name name of data
  91. * @param {number|undefined} id dataIndex of data
  92. */
  93. toggleSelected: function (name, id) {
  94. var target = id != null ? this._targetList[id] : this._selectTargetMap.get(name);
  95. if (target != null) {
  96. this[target.selected ? 'unSelect' : 'select'](name, id);
  97. return target.selected;
  98. }
  99. },
  100. /**
  101. * Either name or id should be passed as input here.
  102. * If both of them are defined, id is used.
  103. *
  104. * @param {string|undefined} name name of data
  105. * @param {number|undefined} id dataIndex of data
  106. */
  107. isSelected: function (name, id) {
  108. var target = id != null ? this._targetList[id] : this._selectTargetMap.get(name);
  109. return target && target.selected;
  110. }
  111. };
  112. module.exports = _default;