plainheart commented on code in PR #20129:
URL: https://github.com/apache/echarts/pull/20129#discussion_r1674177489


##########
src/component/legend/legendAction.ts:
##########
@@ -17,59 +17,81 @@
 * under the License.
 */
 
-// @ts-nocheck
+import {curry, each, hasOwn} from 'zrender/src/core/util';
+import { EChartsExtensionInstallRegisters } from '../../extension';
+import { Payload } from '../../util/types';
+import type GlobalModel from '../../model/Global';
+import type LegendModel from './LegendModel';
 
-import {curry, each} from 'zrender/src/core/util';
+type LegendSelectMethodNames = 'select' | 'unSelect' | 'toggleSelected' | 
'allSelect' | 'inverseSelect';
 
-function legendSelectActionHandler(methodName, payload, ecModel) {
-    const selectedMap = {};
-    const isToggleSelect = methodName === 'toggleSelected';
-    let isSelected;
-    // Update all legend components
-    ecModel.eachComponent('legend', function (legendModel) {
-        if (isToggleSelect && isSelected != null) {
-            // Force other legend has same selected status
-            // Or the first is toggled to true and other are toggled to false
-            // In the case one legend has some item unSelected in option. And 
if other legend
-            // doesn't has the item, they will assume it is selected.
-            legendModel[isSelected ? 'select' : 'unSelect'](payload.name);
-        }
-        else if (methodName === 'allSelect' || methodName === 'inverseSelect') 
{
+function legendSelectActionHandler(methodName: LegendSelectMethodNames, 
payload: Payload, ecModel: GlobalModel) {
+    const isAllSelect = methodName === 'allSelect' || methodName === 
'inverseSelect';
+    const selectedMap: Record<string, boolean> = {};
+
+    const actionLegendIndices: number[] = [];
+    ecModel.eachComponent({ mainType: 'legend', query: payload }, function 
(legendModel: LegendModel) {
+        if (isAllSelect) {
             legendModel[methodName]();
         }
         else {
             legendModel[methodName](payload.name);
-            isSelected = legendModel.isSelected(payload.name);
         }
-        const legendData = legendModel.getData();
-        each(legendData, function (model) {
-            const name = model.get('name');
-            // Wrap element
-            if (name === '\n' || name === '') {
-                return;
-            }
-            const isItemSelected = legendModel.isSelected(name);
-            if (selectedMap.hasOwnProperty(name)) {
-                // Unselected if any legend is unselected
-                selectedMap[name] = selectedMap[name] && isItemSelected;
-            }
-            else {
-                selectedMap[name] = isItemSelected;
-            }
+
+        makeSelectedMap(legendModel, selectedMap);
+
+        actionLegendIndices.push(legendModel.componentIndex);
+    });
+
+    const allSelectedMap: Record<string, boolean> = {};
+
+    // make selectedMap from all legend components
+    ecModel.eachComponent('legend', function (legendModel: LegendModel) {
+        each(selectedMap, function (isSelected, name) {
+            // Force other legend has same selected status
+            // Or the first is toggled to true and other are toggled to false
+            // In the case one legend has some item unSelected in option. And 
if other legend
+            // doesn't has the item, they will assume it is selected.
+            legendModel[isSelected ? 'select' : 'unSelect'](name);
         });
+
+        makeSelectedMap(legendModel, allSelectedMap);
     });
+
     // Return the event explicitly
-    return (methodName === 'allSelect' || methodName === 'inverseSelect')
+    return isAllSelect
         ? {
-            selected: selectedMap
+            selected: selectedMap,
+            // return legendIndex array to tell the developers which legends 
are allSelect / inverseSelect
+            legendIndex: actionLegendIndices

Review Comment:
   哦对了,这里其实如果是非全选 action 返回的这个 index array 其实也是错的,因为
   
https://github.com/apache/echarts/blob/13fe1d903715f1edf1ec13a2f556ec44028d8794/src/component/legend/LegendView.ts#L686-L689
   这里派发 toggleSelected 的时候并未指定是哪个 legend,导致这里始终会获取到全部的 legend,因此考虑了下先不动那里。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to