legendAction.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { curry, each, hasOwn } from 'zrender/lib/core/util.js';
  41. function legendSelectActionHandler(methodName, payload, ecModel) {
  42. var isAllSelect = methodName === 'allSelect' || methodName === 'inverseSelect';
  43. var selectedMap = {};
  44. var actionLegendIndices = [];
  45. ecModel.eachComponent({
  46. mainType: 'legend',
  47. query: payload
  48. }, function (legendModel) {
  49. if (isAllSelect) {
  50. legendModel[methodName]();
  51. } else {
  52. legendModel[methodName](payload.name);
  53. }
  54. makeSelectedMap(legendModel, selectedMap);
  55. actionLegendIndices.push(legendModel.componentIndex);
  56. });
  57. var allSelectedMap = {};
  58. // make selectedMap from all legend components
  59. ecModel.eachComponent('legend', function (legendModel) {
  60. each(selectedMap, function (isSelected, name) {
  61. // Force other legend has same selected status
  62. // Or the first is toggled to true and other are toggled to false
  63. // In the case one legend has some item unSelected in option. And if other legend
  64. // doesn't has the item, they will assume it is selected.
  65. legendModel[isSelected ? 'select' : 'unSelect'](name);
  66. });
  67. makeSelectedMap(legendModel, allSelectedMap);
  68. });
  69. // Return the event explicitly
  70. return isAllSelect ? {
  71. selected: allSelectedMap,
  72. // return legendIndex array to tell the developers which legends are allSelect / inverseSelect
  73. legendIndex: actionLegendIndices
  74. } : {
  75. name: payload.name,
  76. selected: allSelectedMap
  77. };
  78. }
  79. function makeSelectedMap(legendModel, out) {
  80. var selectedMap = out || {};
  81. each(legendModel.getData(), function (model) {
  82. var name = model.get('name');
  83. // Wrap element
  84. if (name === '\n' || name === '') {
  85. return;
  86. }
  87. var isItemSelected = legendModel.isSelected(name);
  88. if (hasOwn(selectedMap, name)) {
  89. // Unselected if any legend is unselected
  90. selectedMap[name] = selectedMap[name] && isItemSelected;
  91. } else {
  92. selectedMap[name] = isItemSelected;
  93. }
  94. });
  95. return selectedMap;
  96. }
  97. export function installLegendAction(registers) {
  98. /**
  99. * @event legendToggleSelect
  100. * @type {Object}
  101. * @property {string} type 'legendToggleSelect'
  102. * @property {string} [from]
  103. * @property {string} name Series name or data item name
  104. */
  105. registers.registerAction('legendToggleSelect', 'legendselectchanged', curry(legendSelectActionHandler, 'toggleSelected'));
  106. registers.registerAction('legendAllSelect', 'legendselectall', curry(legendSelectActionHandler, 'allSelect'));
  107. registers.registerAction('legendInverseSelect', 'legendinverseselect', curry(legendSelectActionHandler, 'inverseSelect'));
  108. /**
  109. * @event legendSelect
  110. * @type {Object}
  111. * @property {string} type 'legendSelect'
  112. * @property {string} name Series name or data item name
  113. */
  114. registers.registerAction('legendSelect', 'legendselected', curry(legendSelectActionHandler, 'select'));
  115. /**
  116. * @event legendUnSelect
  117. * @type {Object}
  118. * @property {string} type 'legendUnSelect'
  119. * @property {string} name Series name or data item name
  120. */
  121. registers.registerAction('legendUnSelect', 'legendunselected', curry(legendSelectActionHandler, 'unSelect'));
  122. }