scheduler.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. exports.Scheduler = void 0;
  10. const x6_common_1 = require("@antv/x6-common");
  11. const view_1 = require("../view");
  12. const queueJob_1 = require("./queueJob");
  13. class Scheduler extends x6_common_1.Disposable {
  14. get model() {
  15. return this.graph.model;
  16. }
  17. get container() {
  18. return this.graph.view.stage;
  19. }
  20. constructor(graph) {
  21. super();
  22. this.views = {};
  23. this.willRemoveViews = {};
  24. this.queue = new queueJob_1.JobQueue();
  25. this.graph = graph;
  26. this.init();
  27. }
  28. init() {
  29. this.startListening();
  30. this.renderViews(this.model.getCells());
  31. }
  32. startListening() {
  33. this.model.on('reseted', this.onModelReseted, this);
  34. this.model.on('cell:added', this.onCellAdded, this);
  35. this.model.on('cell:removed', this.onCellRemoved, this);
  36. this.model.on('cell:change:zIndex', this.onCellZIndexChanged, this);
  37. this.model.on('cell:change:visible', this.onCellVisibleChanged, this);
  38. }
  39. stopListening() {
  40. this.model.off('reseted', this.onModelReseted, this);
  41. this.model.off('cell:added', this.onCellAdded, this);
  42. this.model.off('cell:removed', this.onCellRemoved, this);
  43. this.model.off('cell:change:zIndex', this.onCellZIndexChanged, this);
  44. this.model.off('cell:change:visible', this.onCellVisibleChanged, this);
  45. }
  46. onModelReseted({ options }) {
  47. this.queue.clearJobs();
  48. this.removeZPivots();
  49. this.resetViews();
  50. const cells = this.model.getCells();
  51. this.renderViews(cells, Object.assign(Object.assign({}, options), { queue: cells.map((cell) => cell.id) }));
  52. }
  53. onCellAdded({ cell, options }) {
  54. this.renderViews([cell], options);
  55. }
  56. onCellRemoved({ cell }) {
  57. this.removeViews([cell]);
  58. }
  59. onCellZIndexChanged({ cell, options, }) {
  60. const viewItem = this.views[cell.id];
  61. if (viewItem) {
  62. this.requestViewUpdate(viewItem.view, Scheduler.FLAG_INSERT, options, queueJob_1.JOB_PRIORITY.Update, true);
  63. }
  64. }
  65. onCellVisibleChanged({ cell, current, }) {
  66. this.toggleVisible(cell, !!current);
  67. }
  68. requestViewUpdate(view, flag, options = {}, priority = queueJob_1.JOB_PRIORITY.Update, flush = true) {
  69. const id = view.cell.id;
  70. const viewItem = this.views[id];
  71. if (!viewItem) {
  72. return;
  73. }
  74. viewItem.flag = flag;
  75. viewItem.options = options;
  76. const priorAction = view.hasAction(flag, ['translate', 'resize', 'rotate']);
  77. if (priorAction || options.async === false) {
  78. priority = queueJob_1.JOB_PRIORITY.PRIOR; // eslint-disable-line
  79. flush = false; // eslint-disable-line
  80. }
  81. this.queue.queueJob({
  82. id,
  83. priority,
  84. cb: () => {
  85. this.renderViewInArea(view, flag, options);
  86. const queue = options.queue;
  87. if (queue) {
  88. const index = queue.indexOf(view.cell.id);
  89. if (index >= 0) {
  90. queue.splice(index, 1);
  91. }
  92. if (queue.length === 0) {
  93. this.graph.trigger('render:done');
  94. }
  95. }
  96. },
  97. });
  98. const effectedEdges = this.getEffectedEdges(view);
  99. effectedEdges.forEach((edge) => {
  100. this.requestViewUpdate(edge.view, edge.flag, options, priority, false);
  101. });
  102. if (flush) {
  103. this.flush();
  104. }
  105. }
  106. setRenderArea(area) {
  107. this.renderArea = area;
  108. this.flushWaitingViews();
  109. }
  110. isViewMounted(view) {
  111. if (view == null) {
  112. return false;
  113. }
  114. const viewItem = this.views[view.cell.id];
  115. if (!viewItem) {
  116. return false;
  117. }
  118. return viewItem.state === Scheduler.ViewState.MOUNTED;
  119. }
  120. renderViews(cells, options = {}) {
  121. cells.sort((c1, c2) => {
  122. if (c1.isNode() && c2.isEdge()) {
  123. return -1;
  124. }
  125. return 0;
  126. });
  127. cells.forEach((cell) => {
  128. const id = cell.id;
  129. const views = this.views;
  130. let flag = 0;
  131. let viewItem = views[id];
  132. if (viewItem) {
  133. flag = Scheduler.FLAG_INSERT;
  134. }
  135. else {
  136. const cellView = this.createCellView(cell);
  137. if (cellView) {
  138. cellView.graph = this.graph;
  139. flag = Scheduler.FLAG_INSERT | cellView.getBootstrapFlag();
  140. viewItem = {
  141. view: cellView,
  142. flag,
  143. options,
  144. state: Scheduler.ViewState.CREATED,
  145. };
  146. this.views[id] = viewItem;
  147. }
  148. }
  149. if (viewItem) {
  150. this.requestViewUpdate(viewItem.view, flag, options, this.getRenderPriority(viewItem.view), false);
  151. }
  152. });
  153. this.flush();
  154. }
  155. renderViewInArea(view, flag, options = {}) {
  156. const cell = view.cell;
  157. const id = cell.id;
  158. const viewItem = this.views[id];
  159. if (!viewItem) {
  160. return;
  161. }
  162. let result = 0;
  163. if (this.isUpdatable(view)) {
  164. result = this.updateView(view, flag, options);
  165. viewItem.flag = result;
  166. }
  167. else {
  168. if (viewItem.state === Scheduler.ViewState.MOUNTED) {
  169. result = this.updateView(view, flag, options);
  170. viewItem.flag = result;
  171. }
  172. else {
  173. viewItem.state = Scheduler.ViewState.WAITING;
  174. }
  175. }
  176. if (result) {
  177. if (cell.isEdge() &&
  178. (result & view.getFlag(['source', 'target'])) === 0) {
  179. this.queue.queueJob({
  180. id,
  181. priority: queueJob_1.JOB_PRIORITY.RenderEdge,
  182. cb: () => {
  183. this.updateView(view, flag, options);
  184. },
  185. });
  186. }
  187. }
  188. }
  189. removeViews(cells) {
  190. cells.forEach((cell) => {
  191. const id = cell.id;
  192. const viewItem = this.views[id];
  193. if (viewItem) {
  194. this.willRemoveViews[id] = viewItem;
  195. delete this.views[id];
  196. this.queue.queueJob({
  197. id,
  198. priority: this.getRenderPriority(viewItem.view),
  199. cb: () => {
  200. this.removeView(viewItem.view);
  201. },
  202. });
  203. }
  204. });
  205. this.flush();
  206. }
  207. flush() {
  208. this.graph.options.async
  209. ? this.queue.queueFlush()
  210. : this.queue.queueFlushSync();
  211. }
  212. flushWaitingViews() {
  213. Object.values(this.views).forEach((viewItem) => {
  214. if (viewItem && viewItem.state === Scheduler.ViewState.WAITING) {
  215. const { view, flag, options } = viewItem;
  216. this.requestViewUpdate(view, flag, options, this.getRenderPriority(view), false);
  217. }
  218. });
  219. this.flush();
  220. }
  221. updateView(view, flag, options = {}) {
  222. if (view == null) {
  223. return 0;
  224. }
  225. if (view_1.CellView.isCellView(view)) {
  226. if (flag & Scheduler.FLAG_REMOVE) {
  227. this.removeView(view.cell);
  228. return 0;
  229. }
  230. if (flag & Scheduler.FLAG_INSERT) {
  231. this.insertView(view);
  232. flag ^= Scheduler.FLAG_INSERT; // eslint-disable-line
  233. }
  234. }
  235. if (!flag) {
  236. return 0;
  237. }
  238. return view.confirmUpdate(flag, options);
  239. }
  240. insertView(view) {
  241. const viewItem = this.views[view.cell.id];
  242. if (viewItem) {
  243. const zIndex = view.cell.getZIndex();
  244. const pivot = this.addZPivot(zIndex);
  245. this.container.insertBefore(view.container, pivot);
  246. if (!view.cell.isVisible()) {
  247. this.toggleVisible(view.cell, false);
  248. }
  249. viewItem.state = Scheduler.ViewState.MOUNTED;
  250. this.graph.trigger('view:mounted', { view });
  251. }
  252. }
  253. resetViews() {
  254. this.willRemoveViews = Object.assign(Object.assign({}, this.views), this.willRemoveViews);
  255. Object.values(this.willRemoveViews).forEach((viewItem) => {
  256. if (viewItem) {
  257. this.removeView(viewItem.view);
  258. }
  259. });
  260. this.views = {};
  261. this.willRemoveViews = {};
  262. }
  263. removeView(view) {
  264. const cell = view.cell;
  265. const viewItem = this.willRemoveViews[cell.id];
  266. if (viewItem && view) {
  267. viewItem.view.remove();
  268. delete this.willRemoveViews[cell.id];
  269. this.graph.trigger('view:unmounted', { view });
  270. }
  271. }
  272. toggleVisible(cell, visible) {
  273. const edges = this.model.getConnectedEdges(cell);
  274. for (let i = 0, len = edges.length; i < len; i += 1) {
  275. const edge = edges[i];
  276. if (visible) {
  277. const source = edge.getSourceCell();
  278. const target = edge.getTargetCell();
  279. if ((source && !source.isVisible()) ||
  280. (target && !target.isVisible())) {
  281. continue;
  282. }
  283. this.toggleVisible(edge, true);
  284. }
  285. else {
  286. this.toggleVisible(edge, false);
  287. }
  288. }
  289. const viewItem = this.views[cell.id];
  290. if (viewItem) {
  291. x6_common_1.Dom.css(viewItem.view.container, {
  292. display: visible ? 'unset' : 'none',
  293. });
  294. }
  295. }
  296. addZPivot(zIndex = 0) {
  297. if (this.zPivots == null) {
  298. this.zPivots = {};
  299. }
  300. const pivots = this.zPivots;
  301. let pivot = pivots[zIndex];
  302. if (pivot) {
  303. return pivot;
  304. }
  305. pivot = pivots[zIndex] = document.createComment(`z-index:${zIndex + 1}`);
  306. let neighborZ = -Infinity;
  307. // eslint-disable-next-line
  308. for (const key in pivots) {
  309. const currentZ = +key;
  310. if (currentZ < zIndex && currentZ > neighborZ) {
  311. neighborZ = currentZ;
  312. if (neighborZ === zIndex - 1) {
  313. continue;
  314. }
  315. }
  316. }
  317. const layer = this.container;
  318. if (neighborZ !== -Infinity) {
  319. const neighborPivot = pivots[neighborZ];
  320. layer.insertBefore(pivot, neighborPivot.nextSibling);
  321. }
  322. else {
  323. layer.insertBefore(pivot, layer.firstChild);
  324. }
  325. return pivot;
  326. }
  327. removeZPivots() {
  328. if (this.zPivots) {
  329. Object.values(this.zPivots).forEach((elem) => {
  330. if (elem && elem.parentNode) {
  331. elem.parentNode.removeChild(elem);
  332. }
  333. });
  334. }
  335. this.zPivots = {};
  336. }
  337. createCellView(cell) {
  338. const options = { graph: this.graph };
  339. const createViewHook = this.graph.options.createCellView;
  340. if (createViewHook) {
  341. const ret = x6_common_1.FunctionExt.call(createViewHook, this.graph, cell);
  342. if (ret) {
  343. return new ret(cell, options); // eslint-disable-line new-cap
  344. }
  345. if (ret === null) {
  346. // null means not render
  347. return null;
  348. }
  349. }
  350. const view = cell.view;
  351. if (view != null && typeof view === 'string') {
  352. const def = view_1.CellView.registry.get(view);
  353. if (def) {
  354. return new def(cell, options); // eslint-disable-line new-cap
  355. }
  356. return view_1.CellView.registry.onNotFound(view);
  357. }
  358. if (cell.isNode()) {
  359. return new view_1.NodeView(cell, options);
  360. }
  361. if (cell.isEdge()) {
  362. return new view_1.EdgeView(cell, options);
  363. }
  364. return null;
  365. }
  366. getEffectedEdges(view) {
  367. const effectedEdges = [];
  368. const cell = view.cell;
  369. const edges = this.model.getConnectedEdges(cell);
  370. for (let i = 0, n = edges.length; i < n; i += 1) {
  371. const edge = edges[i];
  372. const viewItem = this.views[edge.id];
  373. if (!viewItem) {
  374. continue;
  375. }
  376. const edgeView = viewItem.view;
  377. if (!this.isViewMounted(edgeView)) {
  378. continue;
  379. }
  380. const flagLabels = ['update'];
  381. if (edge.getTargetCell() === cell) {
  382. flagLabels.push('target');
  383. }
  384. if (edge.getSourceCell() === cell) {
  385. flagLabels.push('source');
  386. }
  387. effectedEdges.push({
  388. id: edge.id,
  389. view: edgeView,
  390. flag: edgeView.getFlag(flagLabels),
  391. });
  392. }
  393. return effectedEdges;
  394. }
  395. isUpdatable(view) {
  396. if (view.isNodeView()) {
  397. if (this.renderArea) {
  398. return this.renderArea.isIntersectWithRect(view.cell.getBBox());
  399. }
  400. return true;
  401. }
  402. if (view.isEdgeView()) {
  403. const edge = view.cell;
  404. const sourceCell = edge.getSourceCell();
  405. const targetCell = edge.getTargetCell();
  406. if (this.renderArea && sourceCell && targetCell) {
  407. return (this.renderArea.isIntersectWithRect(sourceCell.getBBox()) ||
  408. this.renderArea.isIntersectWithRect(targetCell.getBBox()));
  409. }
  410. }
  411. return true;
  412. }
  413. getRenderPriority(view) {
  414. return view.cell.isNode()
  415. ? queueJob_1.JOB_PRIORITY.RenderNode
  416. : queueJob_1.JOB_PRIORITY.RenderEdge;
  417. }
  418. dispose() {
  419. this.stopListening();
  420. // clear views
  421. Object.keys(this.views).forEach((id) => {
  422. this.views[id].view.dispose();
  423. });
  424. this.views = {};
  425. }
  426. }
  427. __decorate([
  428. x6_common_1.Disposable.dispose()
  429. ], Scheduler.prototype, "dispose", null);
  430. exports.Scheduler = Scheduler;
  431. (function (Scheduler) {
  432. Scheduler.FLAG_INSERT = 1 << 30;
  433. Scheduler.FLAG_REMOVE = 1 << 29;
  434. Scheduler.FLAG_RENDER = (1 << 26) - 1;
  435. })(Scheduler = exports.Scheduler || (exports.Scheduler = {}));
  436. (function (Scheduler) {
  437. let ViewState;
  438. (function (ViewState) {
  439. ViewState[ViewState["CREATED"] = 0] = "CREATED";
  440. ViewState[ViewState["MOUNTED"] = 1] = "MOUNTED";
  441. ViewState[ViewState["WAITING"] = 2] = "WAITING";
  442. })(ViewState = Scheduler.ViewState || (Scheduler.ViewState = {}));
  443. })(Scheduler = exports.Scheduler || (exports.Scheduler = {}));
  444. //# sourceMappingURL=scheduler.js.map