scheduler.js 15 KB

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