selectableMixin.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. var zrUtil = require("zrender/lib/core/util");
  2. /**
  3. * Data selectable mixin for chart series.
  4. * To eanble data select, option of series must have `selectedMode`.
  5. * And each data item will use `selected` to toggle itself selected status
  6. */
  7. var _default = {
  8. updateSelectedMap: function (targetList) {
  9. this._targetList = targetList.slice();
  10. this._selectTargetMap = zrUtil.reduce(targetList || [], function (targetMap, target) {
  11. targetMap.set(target.name, target);
  12. return targetMap;
  13. }, zrUtil.createHashMap());
  14. },
  15. /**
  16. * Either name or id should be passed as input here.
  17. * If both of them are defined, id is used.
  18. *
  19. * @param {string|undefined} name name of data
  20. * @param {number|undefined} id dataIndex of data
  21. */
  22. // PENGING If selectedMode is null ?
  23. select: function (name, id) {
  24. var target = id != null ? this._targetList[id] : this._selectTargetMap.get(name);
  25. var selectedMode = this.get('selectedMode');
  26. if (selectedMode === 'single') {
  27. this._selectTargetMap.each(function (target) {
  28. target.selected = false;
  29. });
  30. }
  31. target && (target.selected = true);
  32. },
  33. /**
  34. * Either name or id should be passed as input here.
  35. * If both of them are defined, id is used.
  36. *
  37. * @param {string|undefined} name name of data
  38. * @param {number|undefined} id dataIndex of data
  39. */
  40. unSelect: function (name, id) {
  41. var target = id != null ? this._targetList[id] : this._selectTargetMap.get(name); // var selectedMode = this.get('selectedMode');
  42. // selectedMode !== 'single' && target && (target.selected = false);
  43. target && (target.selected = false);
  44. },
  45. /**
  46. * Either name or id should be passed as input here.
  47. * If both of them are defined, id is used.
  48. *
  49. * @param {string|undefined} name name of data
  50. * @param {number|undefined} id dataIndex of data
  51. */
  52. toggleSelected: function (name, id) {
  53. var target = id != null ? this._targetList[id] : this._selectTargetMap.get(name);
  54. if (target != null) {
  55. this[target.selected ? 'unSelect' : 'select'](name, id);
  56. return target.selected;
  57. }
  58. },
  59. /**
  60. * Either name or id should be passed as input here.
  61. * If both of them are defined, id is used.
  62. *
  63. * @param {string|undefined} name name of data
  64. * @param {number|undefined} id dataIndex of data
  65. */
  66. isSelected: function (name, id) {
  67. var target = id != null ? this._targetList[id] : this._selectTargetMap.get(name);
  68. return target && target.selected;
  69. }
  70. };
  71. module.exports = _default;