ThumbnailView.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 ComponentView from '../../view/Component.js';
  42. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  43. import * as matrix from 'zrender/lib/core/matrix.js';
  44. import RoamController from '../helper/RoamController.js';
  45. import tokens from '../../visual/tokens.js';
  46. import { createBoxLayoutReference, getBoxLayoutParams, getLayoutRect } from '../../util/layout.js';
  47. import { expandOrShrinkRect, Rect, Group, traverseUpdateZ, retrieveZInfo } from '../../util/graphic.js';
  48. import { applyTransform } from 'zrender/lib/core/vector.js';
  49. import View, { VIEW_COORD_SYS_TRANS_RAW, viewCoordSysCopyTrans, viewCoordSysSetBoundingRect, viewCoordSysSetViewRect, viewCoordSysCopyOverallMatrix } from '../../coord/View.js';
  50. import { bind, defaults, extend } from 'zrender/lib/core/util.js';
  51. var ThumbnailView = /** @class */function (_super) {
  52. __extends(ThumbnailView, _super);
  53. function ThumbnailView() {
  54. var _this = _super !== null && _super.apply(this, arguments) || this;
  55. _this.type = ThumbnailView.type;
  56. return _this;
  57. }
  58. ThumbnailView.prototype.render = function (thumbnailModel, ecModel, api) {
  59. this._api = api;
  60. this._model = thumbnailModel;
  61. if (!this._coordSys) {
  62. this._coordSys = new View();
  63. }
  64. if (!this._isEnabled()) {
  65. this._clear();
  66. return;
  67. }
  68. this._renderVersion = api.getECUpdateCycleVersion();
  69. var group = this.group;
  70. group.removeAll();
  71. var itemStyleModel = thumbnailModel.getModel('itemStyle');
  72. var itemStyle = itemStyleModel.getItemStyle();
  73. if (itemStyle.fill == null) {
  74. itemStyle.fill = ecModel.get('backgroundColor') || tokens.color.neutral00;
  75. }
  76. var refContainer = createBoxLayoutReference(thumbnailModel, api).refContainer;
  77. var boxRect = getLayoutRect(getBoxLayoutParams(thumbnailModel, true), refContainer);
  78. var boxBorderWidth = itemStyle.lineWidth || 0;
  79. var contentRect = this._contentRect = expandOrShrinkRect(boxRect.clone(), boxBorderWidth / 2, true, true);
  80. var contentGroup = new Group();
  81. group.add(contentGroup);
  82. contentGroup.setClipPath(new Rect({
  83. shape: contentRect.plain()
  84. }));
  85. var targetGroup = this._targetGroup = new Group();
  86. contentGroup.add(targetGroup);
  87. // Draw border and background and shadow of thumbnail box.
  88. var borderShape = boxRect.plain();
  89. borderShape.r = itemStyleModel.getShallow('borderRadius', true);
  90. group.add(this._bgRect = new Rect({
  91. style: itemStyle,
  92. shape: borderShape,
  93. silent: false,
  94. cursor: 'grab'
  95. }));
  96. var windowStyleModel = thumbnailModel.getModel('windowStyle');
  97. var windowR = windowStyleModel.getShallow('borderRadius', true);
  98. contentGroup.add(this._windowRect = new Rect({
  99. shape: {
  100. x: 0,
  101. y: 0,
  102. width: 0,
  103. height: 0,
  104. r: windowR
  105. },
  106. style: windowStyleModel.getItemStyle(),
  107. silent: false,
  108. cursor: 'grab'
  109. }));
  110. this._dealRenderContent();
  111. this._dealUpdateWindow();
  112. updateZ(thumbnailModel, this);
  113. };
  114. /**
  115. * Can be called asynchronously directly.
  116. * This method should be idempotent.
  117. */
  118. ThumbnailView.prototype.renderContent = function (bridgeRendered) {
  119. this._bridgeRendered = bridgeRendered;
  120. if (this._isEnabled()) {
  121. this._dealRenderContent();
  122. this._dealUpdateWindow();
  123. updateZ(this._model, this);
  124. }
  125. };
  126. ThumbnailView.prototype._dealRenderContent = function () {
  127. var bridgeRendered = this._bridgeRendered;
  128. if (!bridgeRendered || bridgeRendered.renderVersion !== this._renderVersion) {
  129. return;
  130. }
  131. var targetGroup = this._targetGroup;
  132. var coordSys = this._coordSys;
  133. var contentRect = this._contentRect;
  134. targetGroup.removeAll();
  135. if (!bridgeRendered) {
  136. return;
  137. }
  138. var bridgeGroup = bridgeRendered.group;
  139. var bridgeRect = bridgeGroup.getBoundingRect();
  140. targetGroup.add(bridgeGroup);
  141. this._bgRect.z2 = bridgeRendered.z2Range.min - 10;
  142. viewCoordSysSetBoundingRect(coordSys, bridgeRect.x, bridgeRect.y, bridgeRect.width, bridgeRect.height);
  143. // Use `getLayoutRect` is just to find an appropriate rect in thumbnail.
  144. var viewRect = getLayoutRect({
  145. left: 'center',
  146. top: 'center',
  147. aspect: bridgeRect.width / bridgeRect.height
  148. }, contentRect);
  149. viewCoordSysSetViewRect(coordSys, viewRect.x, viewRect.y, viewRect.width, viewRect.height);
  150. viewCoordSysCopyTrans(bridgeGroup, coordSys, VIEW_COORD_SYS_TRANS_RAW);
  151. bridgeGroup.dirty();
  152. this._windowRect.z2 = bridgeRendered.z2Range.max + 10;
  153. this._resetRoamController(bridgeRendered.roamType);
  154. };
  155. /**
  156. * Can be called from action handler directly.
  157. * This method should be idempotent.
  158. */
  159. ThumbnailView.prototype.updateWindow = function (param) {
  160. var bridgeRendered = this._bridgeRendered;
  161. if (bridgeRendered && bridgeRendered.renderVersion === param.renderVersion) {
  162. bridgeRendered.targetTrans = param.targetTrans;
  163. }
  164. if (this._isEnabled()) {
  165. this._dealUpdateWindow();
  166. }
  167. };
  168. ThumbnailView.prototype._dealUpdateWindow = function () {
  169. var bridgeRendered = this._bridgeRendered;
  170. if (!bridgeRendered || bridgeRendered.renderVersion !== this._renderVersion) {
  171. return;
  172. }
  173. var invTargetTrans = matrix.invert([], bridgeRendered.targetTrans);
  174. var transTargetToThis = matrix.mul([], viewCoordSysCopyOverallMatrix(null, this._coordSys), invTargetTrans);
  175. this._transThisToTarget = matrix.invert([], transTargetToThis);
  176. var viewportRect = bridgeRendered.viewportRect;
  177. if (!viewportRect) {
  178. viewportRect = new BoundingRect(0, 0, this._api.getWidth(), this._api.getHeight());
  179. } else {
  180. viewportRect = viewportRect.clone();
  181. }
  182. viewportRect.applyTransform(transTargetToThis);
  183. var windowRect = this._windowRect;
  184. var r = windowRect.shape.r;
  185. windowRect.setShape(defaults({
  186. r: r
  187. }, viewportRect));
  188. };
  189. ThumbnailView.prototype._resetRoamController = function (roamType) {
  190. var _this = this;
  191. var api = this._api;
  192. var roamController = this._roamController;
  193. if (!roamController) {
  194. roamController = this._roamController = new RoamController(api.getZr());
  195. }
  196. if (!roamType || !this._isEnabled()) {
  197. roamController.disable();
  198. return;
  199. }
  200. roamController.enable(roamType, {
  201. api: api,
  202. zInfo: {
  203. component: this._model
  204. },
  205. triggerInfo: {
  206. roamTrigger: null,
  207. isInSelf: function (e, x, y) {
  208. return _this._contentRect.contain(x, y);
  209. }
  210. }
  211. });
  212. roamController.off('pan').off('zoom').on('pan', bind(this._onPan, this)).on('zoom', bind(this._onZoom, this));
  213. // PENDING: Currently roaming animation (when roaming args are changed;
  214. // e.g., `center` and `zoom` are changed) is not supported in thumbnail.
  215. // Animation should be able to be interrupted on pan and zoom, and synchronize
  216. // back to the target. See VIEW_COORD_SYS_ANIMATION.
  217. };
  218. ThumbnailView.prototype._onPan = function (event) {
  219. var trans = this._transThisToTarget;
  220. if (!this._isEnabled() || !trans) {
  221. return;
  222. }
  223. var oldOffset = applyTransform([], [event.oldX, event.oldY], trans);
  224. var newOffset = applyTransform([], [event.oldX - event.dx, event.oldY - event.dy], trans);
  225. this._api.dispatchAction(makeRoamPayload(this._model.getTarget().baseMapProvider, {
  226. dx: newOffset[0] - oldOffset[0],
  227. dy: newOffset[1] - oldOffset[1]
  228. }));
  229. };
  230. ThumbnailView.prototype._onZoom = function (event) {
  231. var trans = this._transThisToTarget;
  232. if (!this._isEnabled() || !trans) {
  233. return;
  234. }
  235. var offset = applyTransform([], [event.originX, event.originY], trans);
  236. this._api.dispatchAction(makeRoamPayload(this._model.getTarget().baseMapProvider, {
  237. zoom: 1 / event.scale,
  238. originX: offset[0],
  239. originY: offset[1]
  240. }));
  241. };
  242. /**
  243. * This method is also responsible for check enable in asynchronous situation,
  244. * e.g., in event listeners that is supposed to be outdated but not be removed.
  245. */
  246. ThumbnailView.prototype._isEnabled = function () {
  247. var thumbnailModel = this._model;
  248. if (!thumbnailModel || !thumbnailModel.shouldShow()) {
  249. return false;
  250. }
  251. var baseMapProvider = thumbnailModel.getTarget().baseMapProvider;
  252. if (!baseMapProvider) {
  253. return false;
  254. }
  255. return true;
  256. };
  257. ThumbnailView.prototype._clear = function () {
  258. this.group.removeAll();
  259. this._bridgeRendered = null;
  260. if (this._roamController) {
  261. this._roamController.disable();
  262. }
  263. };
  264. ThumbnailView.prototype.remove = function () {
  265. this._clear();
  266. };
  267. ThumbnailView.prototype.dispose = function () {
  268. this._clear();
  269. };
  270. ThumbnailView.type = 'thumbnail';
  271. return ThumbnailView;
  272. }(ComponentView);
  273. export { ThumbnailView };
  274. function makeRoamPayload(baseMapProvider, params) {
  275. var type = baseMapProvider.mainType === 'series' ? baseMapProvider.subType + "Roam" // e.g. 'graphRoam'
  276. : baseMapProvider.mainType + "Roam"; // e.g., 'geoRoam'
  277. var payload = {
  278. type: type
  279. };
  280. payload[baseMapProvider.mainType + "Id"] = baseMapProvider.id;
  281. extend(payload, params);
  282. return payload;
  283. }
  284. function updateZ(thumbnailModel, thumbnailView) {
  285. var zInfo = retrieveZInfo(thumbnailModel);
  286. traverseUpdateZ(thumbnailView.group, zInfo.z, zInfo.zlevel);
  287. }