states.js 25 KB

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