states.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. // For the elements that have been triggered by other components,
  368. // and are still required to be highlighted,
  369. // because the current is directly forced to blur the element,
  370. // it will cause the focus self to be unable to highlight, so skip the blur of this element.
  371. if (child.__highByOuter && sameSeries && focus === 'self') {
  372. return;
  373. }
  374. singleEnterBlur(child);
  375. });
  376. if (isArrayLike(focus)) {
  377. leaveBlurOfIndices(seriesModel.getData(), focus);
  378. } else if (isObject(focus)) {
  379. var dataTypes = keys(focus);
  380. for (var d = 0; d < dataTypes.length; d++) {
  381. leaveBlurOfIndices(seriesModel.getData(dataTypes[d]), focus[dataTypes[d]]);
  382. }
  383. }
  384. blurredSeries.push(seriesModel);
  385. getComponentStates(seriesModel).isBlured = true;
  386. }
  387. });
  388. ecModel.eachComponent(function (componentType, componentModel) {
  389. if (componentType === 'series') {
  390. return;
  391. }
  392. var view = api.getViewOfComponentModel(componentModel);
  393. if (view && view.toggleBlurSeries) {
  394. view.toggleBlurSeries(blurredSeries, true, ecModel);
  395. }
  396. });
  397. }
  398. export function blurComponent(componentMainType, componentIndex, api) {
  399. if (componentMainType == null || componentIndex == null) {
  400. return;
  401. }
  402. var componentModel = api.getModel().getComponent(componentMainType, componentIndex);
  403. if (!componentModel) {
  404. return;
  405. }
  406. getComponentStates(componentModel).isBlured = true;
  407. var view = api.getViewOfComponentModel(componentModel);
  408. if (!view || !view.focusBlurEnabled) {
  409. return;
  410. }
  411. view.group.traverse(function (child) {
  412. singleEnterBlur(child);
  413. });
  414. }
  415. export function blurSeriesFromHighlightPayload(seriesModel, payload, api) {
  416. var seriesIndex = seriesModel.seriesIndex;
  417. var data = seriesModel.getData(payload.dataType);
  418. if (!data) {
  419. if (process.env.NODE_ENV !== 'production') {
  420. error("Unknown dataType " + payload.dataType);
  421. }
  422. return;
  423. }
  424. var dataIndex = queryDataIndex(data, payload); // Pick the first one if there is multiple/none exists.
  425. dataIndex = (isArray(dataIndex) ? dataIndex[0] : dataIndex) || 0;
  426. var el = data.getItemGraphicEl(dataIndex);
  427. if (!el) {
  428. var count = data.count();
  429. var current = 0; // If data on dataIndex is NaN.
  430. while (!el && current < count) {
  431. el = data.getItemGraphicEl(current++);
  432. }
  433. }
  434. if (el) {
  435. var ecData = getECData(el);
  436. blurSeries(seriesIndex, ecData.focus, ecData.blurScope, api);
  437. } else {
  438. // If there is no element put on the data. Try getting it from raw option
  439. // TODO Should put it on seriesModel?
  440. var focus_1 = seriesModel.get(['emphasis', 'focus']);
  441. var blurScope = seriesModel.get(['emphasis', 'blurScope']);
  442. if (focus_1 != null) {
  443. blurSeries(seriesIndex, focus_1, blurScope, api);
  444. }
  445. }
  446. }
  447. export function findComponentHighDownDispatchers(componentMainType, componentIndex, name, api) {
  448. var ret = {
  449. focusSelf: false,
  450. dispatchers: null
  451. };
  452. if (componentMainType == null || componentMainType === 'series' || componentIndex == null || name == null) {
  453. return ret;
  454. }
  455. var componentModel = api.getModel().getComponent(componentMainType, componentIndex);
  456. if (!componentModel) {
  457. return ret;
  458. }
  459. var view = api.getViewOfComponentModel(componentModel);
  460. if (!view || !view.findHighDownDispatchers) {
  461. return ret;
  462. }
  463. var dispatchers = view.findHighDownDispatchers(name); // At presnet, the component (like Geo) only blur inside itself.
  464. // So we do not use `blurScope` in component.
  465. var focusSelf;
  466. for (var i = 0; i < dispatchers.length; i++) {
  467. if (process.env.NODE_ENV !== 'production' && !isHighDownDispatcher(dispatchers[i])) {
  468. error('param should be highDownDispatcher');
  469. }
  470. if (getECData(dispatchers[i]).focus === 'self') {
  471. focusSelf = true;
  472. break;
  473. }
  474. }
  475. return {
  476. focusSelf: focusSelf,
  477. dispatchers: dispatchers
  478. };
  479. }
  480. export function handleGlobalMouseOverForHighDown(dispatcher, e, api) {
  481. if (process.env.NODE_ENV !== 'production' && !isHighDownDispatcher(dispatcher)) {
  482. error('param should be highDownDispatcher');
  483. }
  484. var ecData = getECData(dispatcher);
  485. var _a = findComponentHighDownDispatchers(ecData.componentMainType, ecData.componentIndex, ecData.componentHighDownName, api),
  486. dispatchers = _a.dispatchers,
  487. focusSelf = _a.focusSelf; // If `findHighDownDispatchers` is supported on the component,
  488. // highlight/downplay elements with the same name.
  489. if (dispatchers) {
  490. if (focusSelf) {
  491. blurComponent(ecData.componentMainType, ecData.componentIndex, api);
  492. }
  493. each(dispatchers, function (dispatcher) {
  494. return enterEmphasisWhenMouseOver(dispatcher, e);
  495. });
  496. } else {
  497. // Try blur all in the related series. Then emphasis the hoverred.
  498. // TODO. progressive mode.
  499. blurSeries(ecData.seriesIndex, ecData.focus, ecData.blurScope, api);
  500. if (ecData.focus === 'self') {
  501. blurComponent(ecData.componentMainType, ecData.componentIndex, api);
  502. } // Other than series, component that not support `findHighDownDispatcher` will
  503. // also use it. But in this case, highlight/downplay are only supported in
  504. // mouse hover but not in dispatchAction.
  505. enterEmphasisWhenMouseOver(dispatcher, e);
  506. }
  507. }
  508. export function handleGlobalMouseOutForHighDown(dispatcher, e, api) {
  509. if (process.env.NODE_ENV !== 'production' && !isHighDownDispatcher(dispatcher)) {
  510. error('param should be highDownDispatcher');
  511. }
  512. allLeaveBlur(api);
  513. var ecData = getECData(dispatcher);
  514. var dispatchers = findComponentHighDownDispatchers(ecData.componentMainType, ecData.componentIndex, ecData.componentHighDownName, api).dispatchers;
  515. if (dispatchers) {
  516. each(dispatchers, function (dispatcher) {
  517. return leaveEmphasisWhenMouseOut(dispatcher, e);
  518. });
  519. } else {
  520. leaveEmphasisWhenMouseOut(dispatcher, e);
  521. }
  522. }
  523. export function toggleSelectionFromPayload(seriesModel, payload, api) {
  524. if (!isSelectChangePayload(payload)) {
  525. return;
  526. }
  527. var dataType = payload.dataType;
  528. var data = seriesModel.getData(dataType);
  529. var dataIndex = queryDataIndex(data, payload);
  530. if (!isArray(dataIndex)) {
  531. dataIndex = [dataIndex];
  532. }
  533. seriesModel[payload.type === TOGGLE_SELECT_ACTION_TYPE ? 'toggleSelect' : payload.type === SELECT_ACTION_TYPE ? 'select' : 'unselect'](dataIndex, dataType);
  534. }
  535. export function updateSeriesElementSelection(seriesModel) {
  536. var allData = seriesModel.getAllData();
  537. each(allData, function (_a) {
  538. var data = _a.data,
  539. type = _a.type;
  540. data.eachItemGraphicEl(function (el, idx) {
  541. seriesModel.isSelected(idx, type) ? enterSelect(el) : leaveSelect(el);
  542. });
  543. });
  544. }
  545. export function getAllSelectedIndices(ecModel) {
  546. var ret = [];
  547. ecModel.eachSeries(function (seriesModel) {
  548. var allData = seriesModel.getAllData();
  549. each(allData, function (_a) {
  550. var data = _a.data,
  551. type = _a.type;
  552. var dataIndices = seriesModel.getSelectedDataIndices();
  553. if (dataIndices.length > 0) {
  554. var item = {
  555. dataIndex: dataIndices,
  556. seriesIndex: seriesModel.seriesIndex
  557. };
  558. if (type != null) {
  559. item.dataType = type;
  560. }
  561. ret.push(item);
  562. }
  563. });
  564. });
  565. return ret;
  566. }
  567. /**
  568. * Enable the function that mouseover will trigger the emphasis state.
  569. *
  570. * NOTE:
  571. * This function should be used on the element with dataIndex, seriesIndex.
  572. *
  573. */
  574. export function enableHoverEmphasis(el, focus, blurScope) {
  575. setAsHighDownDispatcher(el, true);
  576. traverseUpdateState(el, setDefaultStateProxy);
  577. enableHoverFocus(el, focus, blurScope);
  578. }
  579. export function disableHoverEmphasis(el) {
  580. setAsHighDownDispatcher(el, false);
  581. }
  582. export function toggleHoverEmphasis(el, focus, blurScope, isDisabled) {
  583. isDisabled ? disableHoverEmphasis(el) : enableHoverEmphasis(el, focus, blurScope);
  584. }
  585. export function enableHoverFocus(el, focus, blurScope) {
  586. var ecData = getECData(el);
  587. if (focus != null) {
  588. // TODO dataIndex may be set after this function. This check is not useful.
  589. // if (ecData.dataIndex == null) {
  590. // if (__DEV__) {
  591. // console.warn('focus can only been set on element with dataIndex');
  592. // }
  593. // }
  594. // else {
  595. ecData.focus = focus;
  596. ecData.blurScope = blurScope; // }
  597. } else if (ecData.focus) {
  598. ecData.focus = null;
  599. }
  600. }
  601. var OTHER_STATES = ['emphasis', 'blur', 'select'];
  602. var defaultStyleGetterMap = {
  603. itemStyle: 'getItemStyle',
  604. lineStyle: 'getLineStyle',
  605. areaStyle: 'getAreaStyle'
  606. };
  607. /**
  608. * Set emphasis/blur/selected states of element.
  609. */
  610. export function setStatesStylesFromModel(el, itemModel, styleType, // default itemStyle
  611. getter) {
  612. styleType = styleType || 'itemStyle';
  613. for (var i = 0; i < OTHER_STATES.length; i++) {
  614. var stateName = OTHER_STATES[i];
  615. var model = itemModel.getModel([stateName, styleType]);
  616. var state = el.ensureState(stateName); // Let it throw error if getterType is not found.
  617. state.style = getter ? getter(model) : model[defaultStyleGetterMap[styleType]]();
  618. }
  619. }
  620. /**
  621. *
  622. * Set element as highlight / downplay dispatcher.
  623. * It will be checked when element received mouseover event or from highlight action.
  624. * It's in change of all highlight/downplay behavior of it's children.
  625. *
  626. * @param el
  627. * @param el.highDownSilentOnTouch
  628. * In touch device, mouseover event will be trigger on touchstart event
  629. * (see module:zrender/dom/HandlerProxy). By this mechanism, we can
  630. * conveniently use hoverStyle when tap on touch screen without additional
  631. * code for compatibility.
  632. * But if the chart/component has select feature, which usually also use
  633. * hoverStyle, there might be conflict between 'select-highlight' and
  634. * 'hover-highlight' especially when roam is enabled (see geo for example).
  635. * In this case, `highDownSilentOnTouch` should be used to disable
  636. * hover-highlight on touch device.
  637. * @param asDispatcher If `false`, do not set as "highDownDispatcher".
  638. */
  639. export function setAsHighDownDispatcher(el, asDispatcher) {
  640. var disable = asDispatcher === false;
  641. var extendedEl = el; // Make `highDownSilentOnTouch` and `onStateChange` only work after
  642. // `setAsHighDownDispatcher` called. Avoid it is modified by user unexpectedly.
  643. if (el.highDownSilentOnTouch) {
  644. extendedEl.__highDownSilentOnTouch = el.highDownSilentOnTouch;
  645. } // Simple optimize, since this method might be
  646. // called for each elements of a group in some cases.
  647. if (!disable || extendedEl.__highDownDispatcher) {
  648. // Emphasis, normal can be triggered manually by API or other components like hover link.
  649. // el[method]('emphasis', onElementEmphasisEvent)[method]('normal', onElementNormalEvent);
  650. // Also keep previous record.
  651. extendedEl.__highByOuter = extendedEl.__highByOuter || 0;
  652. extendedEl.__highDownDispatcher = !disable;
  653. }
  654. }
  655. export function isHighDownDispatcher(el) {
  656. return !!(el && el.__highDownDispatcher);
  657. }
  658. /**
  659. * Enable component highlight/downplay features:
  660. * + hover link (within the same name)
  661. * + focus blur in component
  662. */
  663. export function enableComponentHighDownFeatures(el, componentModel, componentHighDownName) {
  664. var ecData = getECData(el);
  665. ecData.componentMainType = componentModel.mainType;
  666. ecData.componentIndex = componentModel.componentIndex;
  667. ecData.componentHighDownName = componentHighDownName;
  668. }
  669. /**
  670. * Support highlight/downplay record on each elements.
  671. * For the case: hover highlight/downplay (legend, visualMap, ...) and
  672. * user triggered highlight/downplay should not conflict.
  673. * Only all of the highlightDigit cleared, return to normal.
  674. * @param {string} highlightKey
  675. * @return {number} highlightDigit
  676. */
  677. export function getHighlightDigit(highlightKey) {
  678. var highlightDigit = _highlightKeyMap[highlightKey];
  679. if (highlightDigit == null && _highlightNextDigit <= 32) {
  680. highlightDigit = _highlightKeyMap[highlightKey] = _highlightNextDigit++;
  681. }
  682. return highlightDigit;
  683. }
  684. export function isSelectChangePayload(payload) {
  685. var payloadType = payload.type;
  686. return payloadType === SELECT_ACTION_TYPE || payloadType === UNSELECT_ACTION_TYPE || payloadType === TOGGLE_SELECT_ACTION_TYPE;
  687. }
  688. export function isHighDownPayload(payload) {
  689. var payloadType = payload.type;
  690. return payloadType === HIGHLIGHT_ACTION_TYPE || payloadType === DOWNPLAY_ACTION_TYPE;
  691. }
  692. export function savePathStates(el) {
  693. var store = getSavedStates(el);
  694. store.normalFill = el.style.fill;
  695. store.normalStroke = el.style.stroke;
  696. var selectState = el.states.select || {};
  697. store.selectFill = selectState.style && selectState.style.fill || null;
  698. store.selectStroke = selectState.style && selectState.style.stroke || null;
  699. }