GraphicView.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import Displayable from 'zrender/lib/graphic/Displayable.js';
  43. import * as modelUtil from '../../util/model.js';
  44. import * as graphicUtil from '../../util/graphic.js';
  45. import * as layoutUtil from '../../util/layout.js';
  46. import { parsePercent } from '../../util/number.js';
  47. import ComponentView from '../../view/Component.js';
  48. import { getECData } from '../../util/innerStore.js';
  49. import { isEC4CompatibleStyle, convertFromEC4CompatibleStyle } from '../../util/styleCompat.js';
  50. import { applyLeaveTransition, applyUpdateTransition, isTransitionAll, updateLeaveTo } from '../../animation/customGraphicTransition.js';
  51. import { updateProps } from '../../animation/basicTransition.js';
  52. import { applyKeyframeAnimation, stopPreviousKeyframeAnimationAndRestore } from '../../animation/customGraphicKeyframeAnimation.js';
  53. var nonShapeGraphicElements = {
  54. // Reserved but not supported in graphic component.
  55. path: null,
  56. compoundPath: null,
  57. // Supported in graphic component.
  58. group: graphicUtil.Group,
  59. image: graphicUtil.Image,
  60. text: graphicUtil.Text
  61. };
  62. export var inner = modelUtil.makeInner();
  63. // ------------------------
  64. // View
  65. // ------------------------
  66. var GraphicComponentView = /** @class */function (_super) {
  67. __extends(GraphicComponentView, _super);
  68. function GraphicComponentView() {
  69. var _this = _super !== null && _super.apply(this, arguments) || this;
  70. _this.type = GraphicComponentView.type;
  71. return _this;
  72. }
  73. GraphicComponentView.prototype.init = function () {
  74. this._elMap = zrUtil.createHashMap();
  75. };
  76. GraphicComponentView.prototype.render = function (graphicModel, ecModel, api) {
  77. // Having leveraged between use cases and algorithm complexity, a very
  78. // simple layout mechanism is used:
  79. // The size(width/height) can be determined by itself or its parent (not
  80. // implemented yet), but can not by its children. (Top-down travel)
  81. // The location(x/y) can be determined by the bounding rect of itself
  82. // (can including its descendants or not) and the size of its parent.
  83. // (Bottom-up travel)
  84. // When `chart.clear()` or `chart.setOption({...}, true)` with the same id,
  85. // view will be reused.
  86. if (graphicModel !== this._lastGraphicModel) {
  87. this._clear();
  88. }
  89. this._lastGraphicModel = graphicModel;
  90. this._updateElements(graphicModel);
  91. this._relocate(graphicModel, api);
  92. };
  93. /**
  94. * Update graphic elements.
  95. */
  96. GraphicComponentView.prototype._updateElements = function (graphicModel) {
  97. var elOptionsToUpdate = graphicModel.useElOptionsToUpdate();
  98. if (!elOptionsToUpdate) {
  99. return;
  100. }
  101. var elMap = this._elMap;
  102. var rootGroup = this.group;
  103. var globalZ = graphicModel.get('z');
  104. var globalZLevel = graphicModel.get('zlevel');
  105. // Top-down tranverse to assign graphic settings to each elements.
  106. zrUtil.each(elOptionsToUpdate, function (elOption) {
  107. var id = modelUtil.convertOptionIdName(elOption.id, null);
  108. var elExisting = id != null ? elMap.get(id) : null;
  109. var parentId = modelUtil.convertOptionIdName(elOption.parentId, null);
  110. var targetElParent = parentId != null ? elMap.get(parentId) : rootGroup;
  111. var elType = elOption.type;
  112. var elOptionStyle = elOption.style;
  113. if (elType === 'text' && elOptionStyle) {
  114. // In top/bottom mode, textVerticalAlign should not be used, which cause
  115. // inaccurately locating.
  116. if (elOption.hv && elOption.hv[1]) {
  117. elOptionStyle.textVerticalAlign = elOptionStyle.textBaseline = elOptionStyle.verticalAlign = elOptionStyle.align = null;
  118. }
  119. }
  120. var textContentOption = elOption.textContent;
  121. var textConfig = elOption.textConfig;
  122. if (elOptionStyle && isEC4CompatibleStyle(elOptionStyle, elType, !!textConfig, !!textContentOption)) {
  123. var convertResult = convertFromEC4CompatibleStyle(elOptionStyle, elType, true);
  124. if (!textConfig && convertResult.textConfig) {
  125. textConfig = elOption.textConfig = convertResult.textConfig;
  126. }
  127. if (!textContentOption && convertResult.textContent) {
  128. textContentOption = convertResult.textContent;
  129. }
  130. }
  131. // Remove unnecessary props to avoid potential problems.
  132. var elOptionCleaned = getCleanedElOption(elOption);
  133. // For simple, do not support parent change, otherwise reorder is needed.
  134. if (process.env.NODE_ENV !== 'production') {
  135. elExisting && zrUtil.assert(targetElParent === elExisting.parent, 'Changing parent is not supported.');
  136. }
  137. var $action = elOption.$action || 'merge';
  138. var isMerge = $action === 'merge';
  139. var isReplace = $action === 'replace';
  140. if (isMerge) {
  141. var isInit = !elExisting;
  142. var el_1 = elExisting;
  143. if (isInit) {
  144. el_1 = createEl(id, targetElParent, elOption.type, elMap);
  145. } else {
  146. el_1 && (inner(el_1).isNew = false);
  147. // Stop and restore before update any other attributes.
  148. stopPreviousKeyframeAnimationAndRestore(el_1);
  149. }
  150. if (el_1) {
  151. applyUpdateTransition(el_1, elOptionCleaned, graphicModel, {
  152. isInit: isInit
  153. });
  154. updateCommonAttrs(el_1, elOption, globalZ, globalZLevel);
  155. }
  156. } else if (isReplace) {
  157. removeEl(elExisting, elOption, elMap, graphicModel);
  158. var el_2 = createEl(id, targetElParent, elOption.type, elMap);
  159. if (el_2) {
  160. applyUpdateTransition(el_2, elOptionCleaned, graphicModel, {
  161. isInit: true
  162. });
  163. updateCommonAttrs(el_2, elOption, globalZ, globalZLevel);
  164. }
  165. } else if ($action === 'remove') {
  166. updateLeaveTo(elExisting, elOption);
  167. removeEl(elExisting, elOption, elMap, graphicModel);
  168. }
  169. var el = elMap.get(id);
  170. if (el && textContentOption) {
  171. if (isMerge) {
  172. var textContentExisting = el.getTextContent();
  173. textContentExisting ? textContentExisting.attr(textContentOption) : el.setTextContent(new graphicUtil.Text(textContentOption));
  174. } else if (isReplace) {
  175. el.setTextContent(new graphicUtil.Text(textContentOption));
  176. }
  177. }
  178. if (el) {
  179. var clipPathOption = elOption.clipPath;
  180. if (clipPathOption) {
  181. var clipPathType = clipPathOption.type;
  182. var clipPath = void 0;
  183. var isInit = false;
  184. if (isMerge) {
  185. var oldClipPath = el.getClipPath();
  186. isInit = !oldClipPath || inner(oldClipPath).type !== clipPathType;
  187. clipPath = isInit ? newEl(clipPathType) : oldClipPath;
  188. } else if (isReplace) {
  189. isInit = true;
  190. clipPath = newEl(clipPathType);
  191. }
  192. el.setClipPath(clipPath);
  193. applyUpdateTransition(clipPath, clipPathOption, graphicModel, {
  194. isInit: isInit
  195. });
  196. applyKeyframeAnimation(clipPath, clipPathOption.keyframeAnimation, graphicModel);
  197. }
  198. var elInner = inner(el);
  199. el.setTextConfig(textConfig);
  200. elInner.option = elOption;
  201. setEventData(el, graphicModel, elOption);
  202. graphicUtil.setTooltipConfig({
  203. el: el,
  204. componentModel: graphicModel,
  205. itemName: el.name,
  206. itemTooltipOption: elOption.tooltip
  207. });
  208. applyKeyframeAnimation(el, elOption.keyframeAnimation, graphicModel);
  209. }
  210. });
  211. };
  212. /**
  213. * Locate graphic elements.
  214. */
  215. GraphicComponentView.prototype._relocate = function (graphicModel, api) {
  216. var elOptions = graphicModel.option.elements;
  217. var rootGroup = this.group;
  218. var elMap = this._elMap;
  219. var apiWidth = api.getWidth();
  220. var apiHeight = api.getHeight();
  221. var xy = ['x', 'y'];
  222. // Top-down to calculate percentage width/height of group
  223. for (var i = 0; i < elOptions.length; i++) {
  224. var elOption = elOptions[i];
  225. var id = modelUtil.convertOptionIdName(elOption.id, null);
  226. var el = id != null ? elMap.get(id) : null;
  227. if (!el || !el.isGroup) {
  228. continue;
  229. }
  230. var parentEl = el.parent;
  231. var isParentRoot = parentEl === rootGroup;
  232. // Like 'position:absolut' in css, default 0.
  233. var elInner = inner(el);
  234. var parentElInner = inner(parentEl);
  235. elInner.width = parsePercent(elInner.option.width, isParentRoot ? apiWidth : parentElInner.width) || 0;
  236. elInner.height = parsePercent(elInner.option.height, isParentRoot ? apiHeight : parentElInner.height) || 0;
  237. }
  238. // Bottom-up tranvese all elements (consider ec resize) to locate elements.
  239. for (var i = elOptions.length - 1; i >= 0; i--) {
  240. var elOption = elOptions[i];
  241. var id = modelUtil.convertOptionIdName(elOption.id, null);
  242. var el = id != null ? elMap.get(id) : null;
  243. if (!el) {
  244. continue;
  245. }
  246. var parentEl = el.parent;
  247. var parentElInner = inner(parentEl);
  248. var containerInfo = parentEl === rootGroup ? {
  249. width: apiWidth,
  250. height: apiHeight
  251. } : {
  252. width: parentElInner.width,
  253. height: parentElInner.height
  254. };
  255. // PENDING
  256. // Currently, when `bounding: 'all'`, the union bounding rect of the group
  257. // does not include the rect of [0, 0, group.width, group.height], which
  258. // is probably weird for users. Should we make a break change for it?
  259. var layoutPos = {};
  260. var layouted = layoutUtil.positionElement(el, elOption, containerInfo, null, {
  261. hv: elOption.hv,
  262. boundingMode: elOption.bounding
  263. }, layoutPos);
  264. if (!inner(el).isNew && layouted) {
  265. var transition = elOption.transition;
  266. var animatePos = {};
  267. for (var k = 0; k < xy.length; k++) {
  268. var key = xy[k];
  269. var val = layoutPos[key];
  270. if (transition && (isTransitionAll(transition) || zrUtil.indexOf(transition, key) >= 0)) {
  271. animatePos[key] = val;
  272. } else {
  273. el[key] = val;
  274. }
  275. }
  276. updateProps(el, animatePos, graphicModel, 0);
  277. } else {
  278. el.attr(layoutPos);
  279. }
  280. }
  281. };
  282. /**
  283. * Clear all elements.
  284. */
  285. GraphicComponentView.prototype._clear = function () {
  286. var _this = this;
  287. var elMap = this._elMap;
  288. elMap.each(function (el) {
  289. removeEl(el, inner(el).option, elMap, _this._lastGraphicModel);
  290. });
  291. this._elMap = zrUtil.createHashMap();
  292. };
  293. GraphicComponentView.prototype.dispose = function () {
  294. this._clear();
  295. };
  296. GraphicComponentView.type = 'graphic';
  297. return GraphicComponentView;
  298. }(ComponentView);
  299. export { GraphicComponentView };
  300. function newEl(graphicType) {
  301. if (process.env.NODE_ENV !== 'production') {
  302. zrUtil.assert(graphicType, 'graphic type MUST be set');
  303. }
  304. var Clz = zrUtil.hasOwn(nonShapeGraphicElements, graphicType)
  305. // Those graphic elements are not shapes. They should not be
  306. // overwritten by users, so do them first.
  307. ? nonShapeGraphicElements[graphicType] : graphicUtil.getShapeClass(graphicType);
  308. if (process.env.NODE_ENV !== 'production') {
  309. zrUtil.assert(Clz, "graphic type " + graphicType + " can not be found");
  310. }
  311. var el = new Clz({});
  312. inner(el).type = graphicType;
  313. return el;
  314. }
  315. function createEl(id, targetElParent, graphicType, elMap) {
  316. var el = newEl(graphicType);
  317. targetElParent.add(el);
  318. elMap.set(id, el);
  319. inner(el).id = id;
  320. inner(el).isNew = true;
  321. return el;
  322. }
  323. function removeEl(elExisting, elOption, elMap, graphicModel) {
  324. var existElParent = elExisting && elExisting.parent;
  325. if (existElParent) {
  326. elExisting.type === 'group' && elExisting.traverse(function (el) {
  327. removeEl(el, elOption, elMap, graphicModel);
  328. });
  329. applyLeaveTransition(elExisting, elOption, graphicModel);
  330. elMap.removeKey(inner(elExisting).id);
  331. }
  332. }
  333. function updateCommonAttrs(el, elOption, defaultZ, defaultZlevel) {
  334. if (!el.isGroup) {
  335. zrUtil.each([['cursor', Displayable.prototype.cursor],
  336. // We should not support configure z and zlevel in the element level.
  337. // But seems we didn't limit it previously. So here still use it to avoid breaking.
  338. ['zlevel', defaultZlevel || 0], ['z', defaultZ || 0],
  339. // z2 must not be null/undefined, otherwise sort error may occur.
  340. ['z2', 0]], function (item) {
  341. var prop = item[0];
  342. if (zrUtil.hasOwn(elOption, prop)) {
  343. el[prop] = zrUtil.retrieve2(elOption[prop], item[1]);
  344. } else if (el[prop] == null) {
  345. el[prop] = item[1];
  346. }
  347. });
  348. }
  349. zrUtil.each(zrUtil.keys(elOption), function (key) {
  350. // Assign event handlers.
  351. // PENDING: should enumerate all event names or use pattern matching?
  352. if (key.indexOf('on') === 0) {
  353. var val = elOption[key];
  354. el[key] = zrUtil.isFunction(val) ? val : null;
  355. }
  356. });
  357. if (zrUtil.hasOwn(elOption, 'draggable')) {
  358. el.draggable = elOption.draggable;
  359. }
  360. // Other attributes
  361. elOption.name != null && (el.name = elOption.name);
  362. elOption.id != null && (el.id = elOption.id);
  363. }
  364. // Remove unnecessary props to avoid potential problems.
  365. function getCleanedElOption(elOption) {
  366. elOption = zrUtil.extend({}, elOption);
  367. zrUtil.each(['id', 'parentId', '$action', 'hv', 'bounding', 'textContent', 'clipPath'].concat(layoutUtil.LOCATION_PARAMS), function (name) {
  368. delete elOption[name];
  369. });
  370. return elOption;
  371. }
  372. function setEventData(el, graphicModel, elOption) {
  373. var eventData = getECData(el).eventData;
  374. // Simple optimize for large amount of elements that no need event.
  375. if (!el.silent && !el.ignore && !eventData) {
  376. eventData = getECData(el).eventData = {
  377. componentType: 'graphic',
  378. componentIndex: graphicModel.componentIndex,
  379. name: el.name
  380. };
  381. }
  382. // `elOption.info` enables user to mount some info on
  383. // elements and use them in event handlers.
  384. if (eventData) {
  385. eventData.info = elOption.info;
  386. }
  387. }