states.js 25 KB

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