states.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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 LRU from 'zrender/lib/core/LRU.js';
  41. import { extend, indexOf, isArrayLike, isObject, keys, isArray, each, isString, isGradientObject, map } from 'zrender/lib/core/util.js';
  42. import { getECData } from './innerStore.js';
  43. import * as colorTool from 'zrender/lib/tool/color.js';
  44. import { queryDataIndex, makeInner } from './model.js';
  45. import Path from 'zrender/lib/graphic/Path.js';
  46. import { error } from './log.js'; // Reserve 0 as default.
  47. var _highlightNextDigit = 1;
  48. var _highlightKeyMap = {};
  49. var getSavedStates = makeInner();
  50. var getComponentStates = makeInner();
  51. export var HOVER_STATE_NORMAL = 0;
  52. export var HOVER_STATE_BLUR = 1;
  53. export var HOVER_STATE_EMPHASIS = 2;
  54. export var SPECIAL_STATES = ['emphasis', 'blur', 'select'];
  55. export var DISPLAY_STATES = ['normal', 'emphasis', 'blur', 'select'];
  56. export var Z2_EMPHASIS_LIFT = 10;
  57. export var Z2_SELECT_LIFT = 9;
  58. export var HIGHLIGHT_ACTION_TYPE = 'highlight';
  59. export var DOWNPLAY_ACTION_TYPE = 'downplay';
  60. export var SELECT_ACTION_TYPE = 'select';
  61. export var UNSELECT_ACTION_TYPE = 'unselect';
  62. export var TOGGLE_SELECT_ACTION_TYPE = 'toggleSelect';
  63. function hasFillOrStroke(fillOrStroke) {
  64. return fillOrStroke != null && fillOrStroke !== 'none';
  65. } // Most lifted color are duplicated.
  66. var liftedColorCache = new LRU(100);
  67. function liftColor(color) {
  68. if (isString(color)) {
  69. var liftedColor = liftedColorCache.get(color);
  70. if (!liftedColor) {
  71. liftedColor = colorTool.lift(color, -0.1);
  72. liftedColorCache.put(color, liftedColor);
  73. }
  74. return liftedColor;
  75. } else if (isGradientObject(color)) {
  76. var ret = extend({}, color);
  77. ret.colorStops = map(color.colorStops, function (stop) {
  78. return {
  79. offset: stop.offset,
  80. color: colorTool.lift(stop.color, -0.1)
  81. };
  82. });
  83. return ret;
  84. } // Change nothing.
  85. return color;
  86. }
  87. function doChangeHoverState(el, stateName, hoverStateEnum) {
  88. if (el.onHoverStateChange && (el.hoverState || 0) !== hoverStateEnum) {
  89. el.onHoverStateChange(stateName);
  90. }
  91. el.hoverState = hoverStateEnum;
  92. }
  93. function singleEnterEmphasis(el) {
  94. // Only mark the flag.
  95. // States will be applied in the echarts.ts in next frame.
  96. doChangeHoverState(el, 'emphasis', HOVER_STATE_EMPHASIS);
  97. }
  98. function singleLeaveEmphasis(el) {
  99. // Only mark the flag.
  100. // States will be applied in the echarts.ts in next frame.
  101. if (el.hoverState === HOVER_STATE_EMPHASIS) {
  102. doChangeHoverState(el, 'normal', HOVER_STATE_NORMAL);
  103. }
  104. }
  105. function singleEnterBlur(el) {
  106. doChangeHoverState(el, 'blur', HOVER_STATE_BLUR);
  107. }
  108. function singleLeaveBlur(el) {
  109. if (el.hoverState === HOVER_STATE_BLUR) {
  110. doChangeHoverState(el, 'normal', HOVER_STATE_NORMAL);
  111. }
  112. }
  113. function singleEnterSelect(el) {
  114. el.selected = true;
  115. }
  116. function singleLeaveSelect(el) {
  117. el.selected = false;
  118. }
  119. function updateElementState(el, updater, commonParam) {
  120. updater(el, commonParam);
  121. }
  122. function traverseUpdateState(el, updater, commonParam) {
  123. updateElementState(el, updater, commonParam);
  124. el.isGroup && el.traverse(function (child) {
  125. updateElementState(child, updater, commonParam);
  126. });
  127. }
  128. export function setStatesFlag(el, stateName) {
  129. switch (stateName) {
  130. case 'emphasis':
  131. el.hoverState = HOVER_STATE_EMPHASIS;
  132. break;
  133. case 'normal':
  134. el.hoverState = HOVER_STATE_NORMAL;
  135. break;
  136. case 'blur':
  137. el.hoverState = HOVER_STATE_BLUR;
  138. break;
  139. case 'select':
  140. el.selected = true;
  141. }
  142. }
  143. /**
  144. * If we reuse elements when rerender.
  145. * DON'T forget to clearStates before we update the style and shape.
  146. * Or we may update on the wrong state instead of normal state.
  147. */
  148. export function clearStates(el) {
  149. if (el.isGroup) {
  150. el.traverse(function (child) {
  151. child.clearStates();
  152. });
  153. } else {
  154. el.clearStates();
  155. }
  156. }
  157. function getFromStateStyle(el, props, toStateName, defaultValue) {
  158. var style = el.style;
  159. var fromState = {};
  160. for (var i = 0; i < props.length; i++) {
  161. var propName = props[i];
  162. var val = style[propName];
  163. fromState[propName] = val == null ? defaultValue && defaultValue[propName] : val;
  164. }
  165. for (var i = 0; i < el.animators.length; i++) {
  166. var animator = el.animators[i];
  167. if (animator.__fromStateTransition // Don't consider the animation to emphasis state.
  168. && animator.__fromStateTransition.indexOf(toStateName) < 0 && animator.targetName === 'style') {
  169. animator.saveTo(fromState, props);
  170. }
  171. }
  172. return fromState;
  173. }
  174. function createEmphasisDefaultState(el, stateName, targetStates, state) {
  175. var hasSelect = targetStates && indexOf(targetStates, 'select') >= 0;
  176. var cloned = false;
  177. if (el instanceof Path) {
  178. var store = getSavedStates(el);
  179. var fromFill = hasSelect ? store.selectFill || store.normalFill : store.normalFill;
  180. var fromStroke = hasSelect ? store.selectStroke || store.normalStroke : store.normalStroke;
  181. if (hasFillOrStroke(fromFill) || hasFillOrStroke(fromStroke)) {
  182. state = state || {};
  183. var emphasisStyle = state.style || {}; // inherit case
  184. if (emphasisStyle.fill === 'inherit') {
  185. cloned = true;
  186. state = extend({}, state);
  187. emphasisStyle = extend({}, emphasisStyle);
  188. emphasisStyle.fill = fromFill;
  189. } // Apply default color lift
  190. else if (!hasFillOrStroke(emphasisStyle.fill) && hasFillOrStroke(fromFill)) {
  191. cloned = true; // Not modify the original value.
  192. state = extend({}, state);
  193. emphasisStyle = extend({}, emphasisStyle); // Already being applied 'emphasis'. DON'T lift color multiple times.
  194. emphasisStyle.fill = liftColor(fromFill);
  195. } // Not highlight stroke if fill has been highlighted.
  196. else if (!hasFillOrStroke(emphasisStyle.stroke) && hasFillOrStroke(fromStroke)) {
  197. if (!cloned) {
  198. state = extend({}, state);
  199. emphasisStyle = extend({}, emphasisStyle);
  200. }
  201. emphasisStyle.stroke = liftColor(fromStroke);
  202. }
  203. state.style = emphasisStyle;
  204. }
  205. }
  206. if (state) {
  207. // TODO Share with textContent?
  208. if (state.z2 == null) {
  209. if (!cloned) {
  210. state = extend({}, state);
  211. }
  212. var z2EmphasisLift = el.z2EmphasisLift;
  213. state.z2 = el.z2 + (z2EmphasisLift != null ? z2EmphasisLift : Z2_EMPHASIS_LIFT);
  214. }
  215. }
  216. return state;
  217. }
  218. function createSelectDefaultState(el, stateName, state) {
  219. // const hasSelect = indexOf(el.currentStates, stateName) >= 0;
  220. if (state) {
  221. // TODO Share with textContent?
  222. if (state.z2 == null) {
  223. state = extend({}, state);
  224. var z2SelectLift = el.z2SelectLift;
  225. state.z2 = el.z2 + (z2SelectLift != null ? z2SelectLift : Z2_SELECT_LIFT);
  226. }
  227. }
  228. return state;
  229. }
  230. function createBlurDefaultState(el, stateName, state) {
  231. var hasBlur = indexOf(el.currentStates, stateName) >= 0;
  232. var currentOpacity = el.style.opacity;
  233. var fromState = !hasBlur ? getFromStateStyle(el, ['opacity'], stateName, {
  234. opacity: 1
  235. }) : null;
  236. state = state || {};
  237. var blurStyle = state.style || {};
  238. if (blurStyle.opacity == null) {
  239. // clone state
  240. state = extend({}, state);
  241. blurStyle = extend({
  242. // Already being applied 'emphasis'. DON'T mul opacity multiple times.
  243. opacity: hasBlur ? currentOpacity : fromState.opacity * 0.1
  244. }, blurStyle);
  245. state.style = blurStyle;
  246. }
  247. return state;
  248. }
  249. function elementStateProxy(stateName, targetStates) {
  250. var state = this.states[stateName];
  251. if (this.style) {
  252. if (stateName === 'emphasis') {
  253. return createEmphasisDefaultState(this, stateName, targetStates, state);
  254. } else if (stateName === 'blur') {
  255. return createBlurDefaultState(this, stateName, state);
  256. } else if (stateName === 'select') {
  257. return createSelectDefaultState(this, stateName, state);
  258. }
  259. }
  260. return state;
  261. }
  262. /**
  263. * Set hover style (namely "emphasis style") of element.
  264. * @param el Should not be `zrender/graphic/Group`.
  265. * @param focus 'self' | 'selfInSeries' | 'series'
  266. */
  267. export function setDefaultStateProxy(el) {
  268. el.stateProxy = elementStateProxy;
  269. var textContent = el.getTextContent();
  270. var textGuide = el.getTextGuideLine();
  271. if (textContent) {
  272. textContent.stateProxy = elementStateProxy;
  273. }
  274. if (textGuide) {
  275. textGuide.stateProxy = elementStateProxy;
  276. }
  277. }
  278. export function enterEmphasisWhenMouseOver(el, e) {
  279. !shouldSilent(el, e) // "emphasis" event highlight has higher priority than mouse highlight.
  280. && !el.__highByOuter && traverseUpdateState(el, singleEnterEmphasis);
  281. }
  282. export function leaveEmphasisWhenMouseOut(el, e) {
  283. !shouldSilent(el, e) // "emphasis" event highlight has higher priority than mouse highlight.
  284. && !el.__highByOuter && traverseUpdateState(el, singleLeaveEmphasis);
  285. }
  286. export function enterEmphasis(el, highlightDigit) {
  287. el.__highByOuter |= 1 << (highlightDigit || 0);
  288. traverseUpdateState(el, singleEnterEmphasis);
  289. }
  290. export function leaveEmphasis(el, highlightDigit) {
  291. !(el.__highByOuter &= ~(1 << (highlightDigit || 0))) && traverseUpdateState(el, singleLeaveEmphasis);
  292. }
  293. export function enterBlur(el) {
  294. traverseUpdateState(el, singleEnterBlur);
  295. }
  296. export function leaveBlur(el) {
  297. traverseUpdateState(el, singleLeaveBlur);
  298. }
  299. export function enterSelect(el) {
  300. traverseUpdateState(el, singleEnterSelect);
  301. }
  302. export function leaveSelect(el) {
  303. traverseUpdateState(el, singleLeaveSelect);
  304. }
  305. function shouldSilent(el, e) {
  306. return el.__highDownSilentOnTouch && e.zrByTouch;
  307. }
  308. export function allLeaveBlur(api) {
  309. var model = api.getModel();
  310. var leaveBlurredSeries = [];
  311. var allComponentViews = [];
  312. model.eachComponent(function (componentType, componentModel) {
  313. var componentStates = getComponentStates(componentModel);
  314. var isSeries = componentType === 'series';
  315. var view = isSeries ? api.getViewOfSeriesModel(componentModel) : api.getViewOfComponentModel(componentModel);
  316. !isSeries && allComponentViews.push(view);
  317. if (componentStates.isBlured) {
  318. // Leave blur anyway
  319. view.group.traverse(function (child) {
  320. singleLeaveBlur(child);
  321. });
  322. isSeries && leaveBlurredSeries.push(componentModel);
  323. }
  324. componentStates.isBlured = false;
  325. });
  326. each(allComponentViews, function (view) {
  327. if (view && view.toggleBlurSeries) {
  328. view.toggleBlurSeries(leaveBlurredSeries, false, model);
  329. }
  330. });
  331. }
  332. export function blurSeries(targetSeriesIndex, focus, blurScope, api) {
  333. var ecModel = api.getModel();
  334. blurScope = blurScope || 'coordinateSystem';
  335. function leaveBlurOfIndices(data, dataIndices) {
  336. for (var i = 0; i < dataIndices.length; i++) {
  337. var itemEl = data.getItemGraphicEl(dataIndices[i]);
  338. itemEl && leaveBlur(itemEl);
  339. }
  340. }
  341. if (targetSeriesIndex == null) {
  342. return;
  343. }
  344. if (!focus || focus === 'none') {
  345. return;
  346. }
  347. var targetSeriesModel = ecModel.getSeriesByIndex(targetSeriesIndex);
  348. var targetCoordSys = targetSeriesModel.coordinateSystem;
  349. if (targetCoordSys && targetCoordSys.master) {
  350. targetCoordSys = targetCoordSys.master;
  351. }
  352. var blurredSeries = [];
  353. ecModel.eachSeries(function (seriesModel) {
  354. var sameSeries = targetSeriesModel === seriesModel;
  355. var coordSys = seriesModel.coordinateSystem;
  356. if (coordSys && coordSys.master) {
  357. coordSys = coordSys.master;
  358. }
  359. var sameCoordSys = coordSys && targetCoordSys ? coordSys === targetCoordSys : sameSeries; // If there is no coordinate system. use sameSeries instead.
  360. if (!( // Not blur other series if blurScope series
  361. blurScope === 'series' && !sameSeries // Not blur other coordinate system if blurScope is coordinateSystem
  362. || blurScope === 'coordinateSystem' && !sameCoordSys // Not blur self series if focus is series.
  363. || focus === 'series' && sameSeries // TODO blurScope: coordinate system
  364. )) {
  365. var view = api.getViewOfSeriesModel(seriesModel);
  366. view.group.traverse(function (child) {
  367. singleEnterBlur(child);
  368. });
  369. if (isArrayLike(focus)) {
  370. leaveBlurOfIndices(seriesModel.getData(), focus);
  371. } else if (isObject(focus)) {
  372. var dataTypes = keys(focus);
  373. for (var d = 0; d < dataTypes.length; d++) {
  374. leaveBlurOfIndices(seriesModel.getData(dataTypes[d]), focus[dataTypes[d]]);
  375. }
  376. }
  377. blurredSeries.push(seriesModel);
  378. getComponentStates(seriesModel).isBlured = true;
  379. }
  380. });
  381. ecModel.eachComponent(function (componentType, componentModel) {
  382. if (componentType === 'series') {
  383. return;
  384. }
  385. var view = api.getViewOfComponentModel(componentModel);
  386. if (view && view.toggleBlurSeries) {
  387. view.toggleBlurSeries(blurredSeries, true, ecModel);
  388. }
  389. });
  390. }
  391. export function blurComponent(componentMainType, componentIndex, api) {
  392. if (componentMainType == null || componentIndex == null) {
  393. return;
  394. }
  395. var componentModel = api.getModel().getComponent(componentMainType, componentIndex);
  396. if (!componentModel) {
  397. return;
  398. }
  399. getComponentStates(componentModel).isBlured = true;
  400. var view = api.getViewOfComponentModel(componentModel);
  401. if (!view || !view.focusBlurEnabled) {
  402. return;
  403. }
  404. view.group.traverse(function (child) {
  405. singleEnterBlur(child);
  406. });
  407. }
  408. export function blurSeriesFromHighlightPayload(seriesModel, payload, api) {
  409. var seriesIndex = seriesModel.seriesIndex;
  410. var data = seriesModel.getData(payload.dataType);
  411. if (!data) {
  412. if (process.env.NODE_ENV !== 'production') {
  413. error("Unknown dataType " + payload.dataType);
  414. }
  415. return;
  416. }
  417. var dataIndex = queryDataIndex(data, payload); // Pick the first one if there is multiple/none exists.
  418. dataIndex = (isArray(dataIndex) ? dataIndex[0] : dataIndex) || 0;
  419. var el = data.getItemGraphicEl(dataIndex);
  420. if (!el) {
  421. var count = data.count();
  422. var current = 0; // If data on dataIndex is NaN.
  423. while (!el && current < count) {
  424. el = data.getItemGraphicEl(current++);
  425. }
  426. }
  427. if (el) {
  428. var ecData = getECData(el);
  429. blurSeries(seriesIndex, ecData.focus, ecData.blurScope, api);
  430. } else {
  431. // If there is no element put on the data. Try getting it from raw option
  432. // TODO Should put it on seriesModel?
  433. var focus_1 = seriesModel.get(['emphasis', 'focus']);
  434. var blurScope = seriesModel.get(['emphasis', 'blurScope']);
  435. if (focus_1 != null) {
  436. blurSeries(seriesIndex, focus_1, blurScope, api);
  437. }
  438. }
  439. }
  440. export function findComponentHighDownDispatchers(componentMainType, componentIndex, name, api) {
  441. var ret = {
  442. focusSelf: false,
  443. dispatchers: null
  444. };
  445. if (componentMainType == null || componentMainType === 'series' || componentIndex == null || name == null) {
  446. return ret;
  447. }
  448. var componentModel = api.getModel().getComponent(componentMainType, componentIndex);
  449. if (!componentModel) {
  450. return ret;
  451. }
  452. var view = api.getViewOfComponentModel(componentModel);
  453. if (!view || !view.findHighDownDispatchers) {
  454. return ret;
  455. }
  456. var dispatchers = view.findHighDownDispatchers(name); // At presnet, the component (like Geo) only blur inside itself.
  457. // So we do not use `blurScope` in component.
  458. var focusSelf;
  459. for (var i = 0; i < dispatchers.length; i++) {
  460. if (process.env.NODE_ENV !== 'production' && !isHighDownDispatcher(dispatchers[i])) {
  461. error('param should be highDownDispatcher');
  462. }
  463. if (getECData(dispatchers[i]).focus === 'self') {
  464. focusSelf = true;
  465. break;
  466. }
  467. }
  468. return {
  469. focusSelf: focusSelf,
  470. dispatchers: dispatchers
  471. };
  472. }
  473. export function handleGlobalMouseOverForHighDown(dispatcher, e, api) {
  474. if (process.env.NODE_ENV !== 'production' && !isHighDownDispatcher(dispatcher)) {
  475. error('param should be highDownDispatcher');
  476. }
  477. var ecData = getECData(dispatcher);
  478. var _a = findComponentHighDownDispatchers(ecData.componentMainType, ecData.componentIndex, ecData.componentHighDownName, api),
  479. dispatchers = _a.dispatchers,
  480. focusSelf = _a.focusSelf; // If `findHighDownDispatchers` is supported on the component,
  481. // highlight/downplay elements with the same name.
  482. if (dispatchers) {
  483. if (focusSelf) {
  484. blurComponent(ecData.componentMainType, ecData.componentIndex, api);
  485. }
  486. each(dispatchers, function (dispatcher) {
  487. return enterEmphasisWhenMouseOver(dispatcher, e);
  488. });
  489. } else {
  490. // Try blur all in the related series. Then emphasis the hoverred.
  491. // TODO. progressive mode.
  492. blurSeries(ecData.seriesIndex, ecData.focus, ecData.blurScope, api);
  493. if (ecData.focus === 'self') {
  494. blurComponent(ecData.componentMainType, ecData.componentIndex, api);
  495. } // Other than series, component that not support `findHighDownDispatcher` will
  496. // also use it. But in this case, highlight/downplay are only supported in
  497. // mouse hover but not in dispatchAction.
  498. enterEmphasisWhenMouseOver(dispatcher, e);
  499. }
  500. }
  501. export function handleGlobalMouseOutForHighDown(dispatcher, e, api) {
  502. if (process.env.NODE_ENV !== 'production' && !isHighDownDispatcher(dispatcher)) {
  503. error('param should be highDownDispatcher');
  504. }
  505. allLeaveBlur(api);
  506. var ecData = getECData(dispatcher);
  507. var dispatchers = findComponentHighDownDispatchers(ecData.componentMainType, ecData.componentIndex, ecData.componentHighDownName, api).dispatchers;
  508. if (dispatchers) {
  509. each(dispatchers, function (dispatcher) {
  510. return leaveEmphasisWhenMouseOut(dispatcher, e);
  511. });
  512. } else {
  513. leaveEmphasisWhenMouseOut(dispatcher, e);
  514. }
  515. }
  516. export function toggleSelectionFromPayload(seriesModel, payload, api) {
  517. if (!isSelectChangePayload(payload)) {
  518. return;
  519. }
  520. var dataType = payload.dataType;
  521. var data = seriesModel.getData(dataType);
  522. var dataIndex = queryDataIndex(data, payload);
  523. if (!isArray(dataIndex)) {
  524. dataIndex = [dataIndex];
  525. }
  526. seriesModel[payload.type === TOGGLE_SELECT_ACTION_TYPE ? 'toggleSelect' : payload.type === SELECT_ACTION_TYPE ? 'select' : 'unselect'](dataIndex, dataType);
  527. }
  528. export function updateSeriesElementSelection(seriesModel) {
  529. var allData = seriesModel.getAllData();
  530. each(allData, function (_a) {
  531. var data = _a.data,
  532. type = _a.type;
  533. data.eachItemGraphicEl(function (el, idx) {
  534. seriesModel.isSelected(idx, type) ? enterSelect(el) : leaveSelect(el);
  535. });
  536. });
  537. }
  538. export function getAllSelectedIndices(ecModel) {
  539. var ret = [];
  540. ecModel.eachSeries(function (seriesModel) {
  541. var allData = seriesModel.getAllData();
  542. each(allData, function (_a) {
  543. var data = _a.data,
  544. type = _a.type;
  545. var dataIndices = seriesModel.getSelectedDataIndices();
  546. if (dataIndices.length > 0) {
  547. var item = {
  548. dataIndex: dataIndices,
  549. seriesIndex: seriesModel.seriesIndex
  550. };
  551. if (type != null) {
  552. item.dataType = type;
  553. }
  554. ret.push(item);
  555. }
  556. });
  557. });
  558. return ret;
  559. }
  560. /**
  561. * Enable the function that mouseover will trigger the emphasis state.
  562. *
  563. * NOTE:
  564. * This function should be used on the element with dataIndex, seriesIndex.
  565. *
  566. */
  567. export function enableHoverEmphasis(el, focus, blurScope) {
  568. setAsHighDownDispatcher(el, true);
  569. traverseUpdateState(el, setDefaultStateProxy);
  570. enableHoverFocus(el, focus, blurScope);
  571. }
  572. export function disableHoverEmphasis(el) {
  573. setAsHighDownDispatcher(el, false);
  574. }
  575. export function toggleHoverEmphasis(el, focus, blurScope, isDisabled) {
  576. isDisabled ? disableHoverEmphasis(el) : enableHoverEmphasis(el, focus, blurScope);
  577. }
  578. export function enableHoverFocus(el, focus, blurScope) {
  579. var ecData = getECData(el);
  580. if (focus != null) {
  581. // TODO dataIndex may be set after this function. This check is not useful.
  582. // if (ecData.dataIndex == null) {
  583. // if (__DEV__) {
  584. // console.warn('focus can only been set on element with dataIndex');
  585. // }
  586. // }
  587. // else {
  588. ecData.focus = focus;
  589. ecData.blurScope = blurScope; // }
  590. } else if (ecData.focus) {
  591. ecData.focus = null;
  592. }
  593. }
  594. var OTHER_STATES = ['emphasis', 'blur', 'select'];
  595. var defaultStyleGetterMap = {
  596. itemStyle: 'getItemStyle',
  597. lineStyle: 'getLineStyle',
  598. areaStyle: 'getAreaStyle'
  599. };
  600. /**
  601. * Set emphasis/blur/selected states of element.
  602. */
  603. export function setStatesStylesFromModel(el, itemModel, styleType, // default itemStyle
  604. getter) {
  605. styleType = styleType || 'itemStyle';
  606. for (var i = 0; i < OTHER_STATES.length; i++) {
  607. var stateName = OTHER_STATES[i];
  608. var model = itemModel.getModel([stateName, styleType]);
  609. var state = el.ensureState(stateName); // Let it throw error if getterType is not found.
  610. state.style = getter ? getter(model) : model[defaultStyleGetterMap[styleType]]();
  611. }
  612. }
  613. /**
  614. *
  615. * Set element as highlight / downplay dispatcher.
  616. * It will be checked when element received mouseover event or from highlight action.
  617. * It's in change of all highlight/downplay behavior of it's children.
  618. *
  619. * @param el
  620. * @param el.highDownSilentOnTouch
  621. * In touch device, mouseover event will be trigger on touchstart event
  622. * (see module:zrender/dom/HandlerProxy). By this mechanism, we can
  623. * conveniently use hoverStyle when tap on touch screen without additional
  624. * code for compatibility.
  625. * But if the chart/component has select feature, which usually also use
  626. * hoverStyle, there might be conflict between 'select-highlight' and
  627. * 'hover-highlight' especially when roam is enabled (see geo for example).
  628. * In this case, `highDownSilentOnTouch` should be used to disable
  629. * hover-highlight on touch device.
  630. * @param asDispatcher If `false`, do not set as "highDownDispatcher".
  631. */
  632. export function setAsHighDownDispatcher(el, asDispatcher) {
  633. var disable = asDispatcher === false;
  634. var extendedEl = el; // Make `highDownSilentOnTouch` and `onStateChange` only work after
  635. // `setAsHighDownDispatcher` called. Avoid it is modified by user unexpectedly.
  636. if (el.highDownSilentOnTouch) {
  637. extendedEl.__highDownSilentOnTouch = el.highDownSilentOnTouch;
  638. } // Simple optimize, since this method might be
  639. // called for each elements of a group in some cases.
  640. if (!disable || extendedEl.__highDownDispatcher) {
  641. // Emphasis, normal can be triggered manually by API or other components like hover link.
  642. // el[method]('emphasis', onElementEmphasisEvent)[method]('normal', onElementNormalEvent);
  643. // Also keep previous record.
  644. extendedEl.__highByOuter = extendedEl.__highByOuter || 0;
  645. extendedEl.__highDownDispatcher = !disable;
  646. }
  647. }
  648. export function isHighDownDispatcher(el) {
  649. return !!(el && el.__highDownDispatcher);
  650. }
  651. /**
  652. * Enable component highlight/downplay features:
  653. * + hover link (within the same name)
  654. * + focus blur in component
  655. */
  656. export function enableComponentHighDownFeatures(el, componentModel, componentHighDownName) {
  657. var ecData = getECData(el);
  658. ecData.componentMainType = componentModel.mainType;
  659. ecData.componentIndex = componentModel.componentIndex;
  660. ecData.componentHighDownName = componentHighDownName;
  661. }
  662. /**
  663. * Support highlight/downplay record on each elements.
  664. * For the case: hover highlight/downplay (legend, visualMap, ...) and
  665. * user triggered highlight/downplay should not conflict.
  666. * Only all of the highlightDigit cleared, return to normal.
  667. * @param {string} highlightKey
  668. * @return {number} highlightDigit
  669. */
  670. export function getHighlightDigit(highlightKey) {
  671. var highlightDigit = _highlightKeyMap[highlightKey];
  672. if (highlightDigit == null && _highlightNextDigit <= 32) {
  673. highlightDigit = _highlightKeyMap[highlightKey] = _highlightNextDigit++;
  674. }
  675. return highlightDigit;
  676. }
  677. export function isSelectChangePayload(payload) {
  678. var payloadType = payload.type;
  679. return payloadType === SELECT_ACTION_TYPE || payloadType === UNSELECT_ACTION_TYPE || payloadType === TOGGLE_SELECT_ACTION_TYPE;
  680. }
  681. export function isHighDownPayload(payload) {
  682. var payloadType = payload.type;
  683. return payloadType === HIGHLIGHT_ACTION_TYPE || payloadType === DOWNPLAY_ACTION_TYPE;
  684. }
  685. export function savePathStates(el) {
  686. var store = getSavedStates(el);
  687. store.normalFill = el.style.fill;
  688. store.normalStroke = el.style.stroke;
  689. var selectState = el.states.select || {};
  690. store.selectFill = selectState.style && selectState.style.fill || null;
  691. store.selectStroke = selectState.style && selectState.style.stroke || null;
  692. }