ThumbnailView.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 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.getMainProcessVersion();
  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. coordSys.setBoundingRect(bridgeRect.x, bridgeRect.y, bridgeRect.width, bridgeRect.height);
  143. // Use `getLayoutRect` is just to find an approperiate rect in thumbnail.
  144. var viewRect = getLayoutRect({
  145. left: 'center',
  146. top: 'center',
  147. aspect: bridgeRect.width / bridgeRect.height
  148. }, contentRect);
  149. coordSys.setViewRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
  150. bridgeGroup.attr(coordSys.getTransformInfo().raw);
  151. this._windowRect.z2 = bridgeRendered.z2Range.max + 10;
  152. this._resetRoamController(bridgeRendered.roamType);
  153. };
  154. /**
  155. * Can be called from action handler directly.
  156. * This method should be idempotent.
  157. */
  158. ThumbnailView.prototype.updateWindow = function (param) {
  159. var bridgeRendered = this._bridgeRendered;
  160. if (bridgeRendered && bridgeRendered.renderVersion === param.renderVersion) {
  161. bridgeRendered.targetTrans = param.targetTrans;
  162. }
  163. if (this._isEnabled()) {
  164. this._dealUpdateWindow();
  165. }
  166. };
  167. ThumbnailView.prototype._dealUpdateWindow = function () {
  168. var bridgeRendered = this._bridgeRendered;
  169. if (!bridgeRendered || bridgeRendered.renderVersion !== this._renderVersion) {
  170. return;
  171. }
  172. var invTargetTrans = matrix.invert([], bridgeRendered.targetTrans);
  173. var transTargetToThis = matrix.mul([], this._coordSys.transform, invTargetTrans);
  174. this._transThisToTarget = matrix.invert([], transTargetToThis);
  175. var viewportRect = bridgeRendered.viewportRect;
  176. if (!viewportRect) {
  177. viewportRect = new BoundingRect(0, 0, this._api.getWidth(), this._api.getHeight());
  178. } else {
  179. viewportRect = viewportRect.clone();
  180. }
  181. viewportRect.applyTransform(transTargetToThis);
  182. var windowRect = this._windowRect;
  183. var r = windowRect.shape.r;
  184. windowRect.setShape(defaults({
  185. r: r
  186. }, viewportRect));
  187. };
  188. ThumbnailView.prototype._resetRoamController = function (roamType) {
  189. var _this = this;
  190. var api = this._api;
  191. var roamController = this._roamController;
  192. if (!roamController) {
  193. roamController = this._roamController = new RoamController(api.getZr());
  194. }
  195. if (!roamType || !this._isEnabled()) {
  196. roamController.disable();
  197. return;
  198. }
  199. roamController.enable(roamType, {
  200. api: api,
  201. zInfo: {
  202. component: this._model
  203. },
  204. triggerInfo: {
  205. roamTrigger: null,
  206. isInSelf: function (e, x, y) {
  207. return _this._contentRect.contain(x, y);
  208. }
  209. }
  210. });
  211. roamController.off('pan').off('zoom').on('pan', bind(this._onPan, this)).on('zoom', bind(this._onZoom, this));
  212. };
  213. ThumbnailView.prototype._onPan = function (event) {
  214. var trans = this._transThisToTarget;
  215. if (!this._isEnabled() || !trans) {
  216. return;
  217. }
  218. var oldOffset = applyTransform([], [event.oldX, event.oldY], trans);
  219. var newOffset = applyTransform([], [event.oldX - event.dx, event.oldY - event.dy], trans);
  220. this._api.dispatchAction(makeRoamPayload(this._model.getTarget().baseMapProvider, {
  221. dx: newOffset[0] - oldOffset[0],
  222. dy: newOffset[1] - oldOffset[1]
  223. }));
  224. };
  225. ThumbnailView.prototype._onZoom = function (event) {
  226. var trans = this._transThisToTarget;
  227. if (!this._isEnabled() || !trans) {
  228. return;
  229. }
  230. var offset = applyTransform([], [event.originX, event.originY], trans);
  231. this._api.dispatchAction(makeRoamPayload(this._model.getTarget().baseMapProvider, {
  232. zoom: 1 / event.scale,
  233. originX: offset[0],
  234. originY: offset[1]
  235. }));
  236. };
  237. /**
  238. * This method is also responsible for check enable in asynchronous situation,
  239. * e.g., in event listeners that is supposed to be outdated but not be removed.
  240. */
  241. ThumbnailView.prototype._isEnabled = function () {
  242. var thumbnailModel = this._model;
  243. if (!thumbnailModel || !thumbnailModel.shouldShow()) {
  244. return false;
  245. }
  246. var baseMapProvider = thumbnailModel.getTarget().baseMapProvider;
  247. if (!baseMapProvider) {
  248. return false;
  249. }
  250. return true;
  251. };
  252. ThumbnailView.prototype._clear = function () {
  253. this.group.removeAll();
  254. this._bridgeRendered = null;
  255. if (this._roamController) {
  256. this._roamController.disable();
  257. }
  258. };
  259. ThumbnailView.prototype.remove = function () {
  260. this._clear();
  261. };
  262. ThumbnailView.prototype.dispose = function () {
  263. this._clear();
  264. };
  265. ThumbnailView.type = 'thumbnail';
  266. return ThumbnailView;
  267. }(ComponentView);
  268. export { ThumbnailView };
  269. function makeRoamPayload(baseMapProvider, params) {
  270. var type = baseMapProvider.mainType === 'series' ? baseMapProvider.subType + "Roam" // e.g. 'graphRoam'
  271. : baseMapProvider.mainType + "Roam"; // e.g., 'geoRoam'
  272. var payload = {
  273. type: type
  274. };
  275. payload[baseMapProvider.mainType + "Id"] = baseMapProvider.id;
  276. extend(payload, params);
  277. return payload;
  278. }
  279. function updateZ(thumbnailModel, thumbnailView) {
  280. var zInfo = retrieveZInfo(thumbnailModel);
  281. traverseUpdateZ(thumbnailView.group, zInfo.z, zInfo.zlevel);
  282. }