tool.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ToolsView = void 0;
  4. const x6_common_1 = require("@antv/x6-common");
  5. const tool_1 = require("../registry/tool");
  6. const view_1 = require("./view");
  7. const cell_1 = require("./cell");
  8. const markup_1 = require("./markup");
  9. class ToolsView extends view_1.View {
  10. get name() {
  11. return this.options.name;
  12. }
  13. get graph() {
  14. return this.cellView.graph;
  15. }
  16. get cell() {
  17. return this.cellView.cell;
  18. }
  19. get [Symbol.toStringTag]() {
  20. return ToolsView.toStringTag;
  21. }
  22. constructor(options = {}) {
  23. super();
  24. this.svgContainer = this.createContainer(true, options);
  25. this.htmlContainer = this.createContainer(false, options);
  26. this.config(options);
  27. }
  28. createContainer(svg, options) {
  29. const container = svg
  30. ? view_1.View.createElement('g', true)
  31. : view_1.View.createElement('div', false);
  32. x6_common_1.Dom.addClass(container, this.prefixClassName('cell-tools'));
  33. if (options.className) {
  34. x6_common_1.Dom.addClass(container, options.className);
  35. }
  36. return container;
  37. }
  38. config(options) {
  39. this.options = Object.assign(Object.assign({}, this.options), options);
  40. if (!cell_1.CellView.isCellView(options.view) || options.view === this.cellView) {
  41. return this;
  42. }
  43. this.cellView = options.view;
  44. if (this.cell.isEdge()) {
  45. x6_common_1.Dom.addClass(this.svgContainer, this.prefixClassName('edge-tools'));
  46. x6_common_1.Dom.addClass(this.htmlContainer, this.prefixClassName('edge-tools'));
  47. }
  48. else if (this.cell.isNode()) {
  49. x6_common_1.Dom.addClass(this.svgContainer, this.prefixClassName('node-tools'));
  50. x6_common_1.Dom.addClass(this.htmlContainer, this.prefixClassName('node-tools'));
  51. }
  52. this.svgContainer.setAttribute('data-cell-id', this.cell.id);
  53. this.htmlContainer.setAttribute('data-cell-id', this.cell.id);
  54. if (this.name) {
  55. this.svgContainer.setAttribute('data-tools-name', this.name);
  56. this.htmlContainer.setAttribute('data-tools-name', this.name);
  57. }
  58. const tools = this.options.items;
  59. if (!Array.isArray(tools)) {
  60. return this;
  61. }
  62. this.tools = [];
  63. const normalizedTools = [];
  64. tools.forEach((meta) => {
  65. if (ToolsView.ToolItem.isToolItem(meta)) {
  66. if (meta.name === 'vertices') {
  67. normalizedTools.unshift(meta);
  68. }
  69. else {
  70. normalizedTools.push(meta);
  71. }
  72. }
  73. else {
  74. const name = typeof meta === 'object' ? meta.name : meta;
  75. if (name === 'vertices') {
  76. normalizedTools.unshift(meta);
  77. }
  78. else {
  79. normalizedTools.push(meta);
  80. }
  81. }
  82. });
  83. for (let i = 0; i < normalizedTools.length; i += 1) {
  84. const meta = normalizedTools[i];
  85. let tool;
  86. if (ToolsView.ToolItem.isToolItem(meta)) {
  87. tool = meta;
  88. }
  89. else {
  90. const name = typeof meta === 'object' ? meta.name : meta;
  91. const args = typeof meta === 'object' ? meta.args || {} : {};
  92. if (name) {
  93. if (this.cell.isNode()) {
  94. const ctor = tool_1.NodeTool.registry.get(name);
  95. if (ctor) {
  96. tool = new ctor(args); // eslint-disable-line
  97. }
  98. else {
  99. return tool_1.NodeTool.registry.onNotFound(name);
  100. }
  101. }
  102. else if (this.cell.isEdge()) {
  103. const ctor = tool_1.EdgeTool.registry.get(name);
  104. if (ctor) {
  105. tool = new ctor(args); // eslint-disable-line
  106. }
  107. else {
  108. return tool_1.EdgeTool.registry.onNotFound(name);
  109. }
  110. }
  111. }
  112. }
  113. if (tool) {
  114. tool.config(this.cellView, this);
  115. tool.render();
  116. const container = tool.options.isSVGElement !== false
  117. ? this.svgContainer
  118. : this.htmlContainer;
  119. container.appendChild(tool.container);
  120. this.tools.push(tool);
  121. }
  122. }
  123. return this;
  124. }
  125. update(options = {}) {
  126. const tools = this.tools;
  127. if (tools) {
  128. tools.forEach((tool) => {
  129. if (options.toolId !== tool.cid && tool.isVisible()) {
  130. tool.update();
  131. }
  132. });
  133. }
  134. return this;
  135. }
  136. focus(focusedTool) {
  137. const tools = this.tools;
  138. if (tools) {
  139. tools.forEach((tool) => {
  140. if (focusedTool === tool) {
  141. tool.show();
  142. }
  143. else {
  144. tool.hide();
  145. }
  146. });
  147. }
  148. return this;
  149. }
  150. blur(blurredTool) {
  151. const tools = this.tools;
  152. if (tools) {
  153. tools.forEach((tool) => {
  154. if (tool !== blurredTool && !tool.isVisible()) {
  155. tool.show();
  156. tool.update();
  157. }
  158. });
  159. }
  160. return this;
  161. }
  162. hide() {
  163. return this.focus(null);
  164. }
  165. show() {
  166. return this.blur(null);
  167. }
  168. remove() {
  169. const tools = this.tools;
  170. if (tools) {
  171. tools.forEach((tool) => tool.remove());
  172. this.tools = null;
  173. }
  174. x6_common_1.Dom.remove(this.svgContainer);
  175. x6_common_1.Dom.remove(this.htmlContainer);
  176. return super.remove();
  177. }
  178. mount() {
  179. const tools = this.tools;
  180. const cellView = this.cellView;
  181. if (cellView && tools) {
  182. const hasSVG = tools.some((tool) => tool.options.isSVGElement !== false);
  183. const hasHTML = tools.some((tool) => tool.options.isSVGElement === false);
  184. if (hasSVG) {
  185. const parent = this.options.local
  186. ? cellView.container
  187. : cellView.graph.view.decorator;
  188. parent.appendChild(this.svgContainer);
  189. }
  190. if (hasHTML) {
  191. this.graph.container.appendChild(this.htmlContainer);
  192. }
  193. }
  194. return this;
  195. }
  196. }
  197. exports.ToolsView = ToolsView;
  198. (function (ToolsView) {
  199. ToolsView.toStringTag = `X6.${ToolsView.name}`;
  200. function isToolsView(instance) {
  201. if (instance == null) {
  202. return false;
  203. }
  204. if (instance instanceof ToolsView) {
  205. return true;
  206. }
  207. const tag = instance[Symbol.toStringTag];
  208. const view = instance;
  209. if ((tag == null || tag === ToolsView.toStringTag) &&
  210. view.graph != null &&
  211. view.cell != null &&
  212. typeof view.config === 'function' &&
  213. typeof view.update === 'function' &&
  214. typeof view.focus === 'function' &&
  215. typeof view.blur === 'function' &&
  216. typeof view.show === 'function' &&
  217. typeof view.hide === 'function') {
  218. return true;
  219. }
  220. return false;
  221. }
  222. ToolsView.isToolsView = isToolsView;
  223. })(ToolsView = exports.ToolsView || (exports.ToolsView = {}));
  224. (function (ToolsView) {
  225. class ToolItem extends view_1.View {
  226. static getDefaults() {
  227. return this.defaults;
  228. }
  229. static config(options) {
  230. this.defaults = this.getOptions(options);
  231. }
  232. static getOptions(options) {
  233. return x6_common_1.ObjectExt.merge(x6_common_1.ObjectExt.cloneDeep(this.getDefaults()), options);
  234. }
  235. get graph() {
  236. return this.cellView.graph;
  237. }
  238. get cell() {
  239. return this.cellView.cell;
  240. }
  241. get name() {
  242. return this.options.name;
  243. }
  244. get [Symbol.toStringTag]() {
  245. return ToolItem.toStringTag;
  246. }
  247. constructor(options = {}) {
  248. super();
  249. this.visible = true;
  250. this.options = this.getOptions(options);
  251. this.container = view_1.View.createElement(this.options.tagName || 'g', this.options.isSVGElement !== false);
  252. x6_common_1.Dom.addClass(this.container, this.prefixClassName('cell-tool'));
  253. if (typeof this.options.className === 'string') {
  254. x6_common_1.Dom.addClass(this.container, this.options.className);
  255. }
  256. this.init();
  257. }
  258. init() { }
  259. getOptions(options) {
  260. const ctor = this.constructor;
  261. return ctor.getOptions(options);
  262. }
  263. delegateEvents() {
  264. if (this.options.events) {
  265. super.delegateEvents(this.options.events);
  266. }
  267. return this;
  268. }
  269. config(view, toolsView) {
  270. this.cellView = view;
  271. this.parent = toolsView;
  272. this.stamp(this.container);
  273. if (this.cell.isEdge()) {
  274. x6_common_1.Dom.addClass(this.container, this.prefixClassName('edge-tool'));
  275. }
  276. else if (this.cell.isNode()) {
  277. x6_common_1.Dom.addClass(this.container, this.prefixClassName('node-tool'));
  278. }
  279. if (this.name) {
  280. this.container.setAttribute('data-tool-name', this.name);
  281. }
  282. this.delegateEvents();
  283. return this;
  284. }
  285. render() {
  286. this.empty();
  287. const markup = this.options.markup;
  288. if (markup) {
  289. const meta = markup_1.Markup.parseJSONMarkup(markup);
  290. this.container.appendChild(meta.fragment);
  291. this.childNodes = meta.selectors;
  292. }
  293. this.onRender();
  294. return this;
  295. }
  296. onRender() { }
  297. update() {
  298. return this;
  299. }
  300. stamp(elem) {
  301. if (elem) {
  302. elem.setAttribute('data-cell-id', this.cellView.cell.id);
  303. }
  304. }
  305. show() {
  306. this.container.style.display = '';
  307. this.visible = true;
  308. return this;
  309. }
  310. hide() {
  311. this.container.style.display = 'none';
  312. this.visible = false;
  313. return this;
  314. }
  315. isVisible() {
  316. return this.visible;
  317. }
  318. focus() {
  319. const opacity = this.options.focusOpacity;
  320. if (opacity != null && Number.isFinite(opacity)) {
  321. this.container.style.opacity = `${opacity}`;
  322. }
  323. this.parent.focus(this);
  324. return this;
  325. }
  326. blur() {
  327. this.container.style.opacity = '';
  328. this.parent.blur(this);
  329. return this;
  330. }
  331. guard(evt) {
  332. if (this.graph == null || this.cellView == null) {
  333. return true;
  334. }
  335. return this.graph.view.guard(evt, this.cellView);
  336. }
  337. }
  338. // #region static
  339. ToolItem.defaults = {
  340. isSVGElement: true,
  341. tagName: 'g',
  342. };
  343. ToolsView.ToolItem = ToolItem;
  344. (function (ToolItem) {
  345. let counter = 0;
  346. function getClassName(name) {
  347. if (name) {
  348. return x6_common_1.StringExt.pascalCase(name);
  349. }
  350. counter += 1;
  351. return `CustomTool${counter}`;
  352. }
  353. function define(options) {
  354. const tool = x6_common_1.ObjectExt.createClass(getClassName(options.name), this);
  355. tool.config(options);
  356. return tool;
  357. }
  358. ToolItem.define = define;
  359. })(ToolItem = ToolsView.ToolItem || (ToolsView.ToolItem = {}));
  360. (function (ToolItem) {
  361. ToolItem.toStringTag = `X6.${ToolItem.name}`;
  362. function isToolItem(instance) {
  363. if (instance == null) {
  364. return false;
  365. }
  366. if (instance instanceof ToolItem) {
  367. return true;
  368. }
  369. const tag = instance[Symbol.toStringTag];
  370. const view = instance;
  371. if ((tag == null || tag === ToolItem.toStringTag) &&
  372. view.graph != null &&
  373. view.cell != null &&
  374. typeof view.config === 'function' &&
  375. typeof view.update === 'function' &&
  376. typeof view.focus === 'function' &&
  377. typeof view.blur === 'function' &&
  378. typeof view.show === 'function' &&
  379. typeof view.hide === 'function' &&
  380. typeof view.isVisible === 'function') {
  381. return true;
  382. }
  383. return false;
  384. }
  385. ToolItem.isToolItem = isToolItem;
  386. })(ToolItem = ToolsView.ToolItem || (ToolsView.ToolItem = {}));
  387. })(ToolsView = exports.ToolsView || (exports.ToolsView = {}));
  388. //# sourceMappingURL=tool.js.map