attr.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AttrManager = void 0;
  4. const x6_common_1 = require("@antv/x6-common");
  5. const x6_geometry_1 = require("@antv/x6-geometry");
  6. const attr_1 = require("../registry/attr");
  7. const view_1 = require("./view");
  8. const util_1 = require("../util");
  9. class AttrManager {
  10. constructor(view) {
  11. this.view = view;
  12. }
  13. get cell() {
  14. return this.view.cell;
  15. }
  16. getDefinition(attrName) {
  17. return this.cell.getAttrDefinition(attrName);
  18. }
  19. processAttrs(elem, raw) {
  20. let normal;
  21. let set;
  22. let offset;
  23. let position;
  24. const specials = [];
  25. // divide the attributes between normal and special
  26. Object.keys(raw).forEach((name) => {
  27. const val = raw[name];
  28. const definition = this.getDefinition(name);
  29. const isValid = x6_common_1.FunctionExt.call(attr_1.Attr.isValidDefinition, this.view, definition, val, {
  30. elem,
  31. attrs: raw,
  32. cell: this.cell,
  33. view: this.view,
  34. });
  35. if (definition && isValid) {
  36. if (typeof definition === 'string') {
  37. if (normal == null) {
  38. normal = {};
  39. }
  40. normal[definition] = val;
  41. }
  42. else if (val !== null) {
  43. specials.push({ name, definition });
  44. }
  45. }
  46. else {
  47. if (normal == null) {
  48. normal = {};
  49. }
  50. const normalName = x6_common_1.Dom.CASE_SENSITIVE_ATTR.includes(name)
  51. ? name
  52. : x6_common_1.StringExt.kebabCase(name);
  53. normal[normalName] = val;
  54. }
  55. });
  56. specials.forEach(({ name, definition }) => {
  57. const val = raw[name];
  58. const setDefine = definition;
  59. if (typeof setDefine.set === 'function') {
  60. if (set == null) {
  61. set = {};
  62. }
  63. set[name] = val;
  64. }
  65. const offsetDefine = definition;
  66. if (typeof offsetDefine.offset === 'function') {
  67. if (offset == null) {
  68. offset = {};
  69. }
  70. offset[name] = val;
  71. }
  72. const positionDefine = definition;
  73. if (typeof positionDefine.position === 'function') {
  74. if (position == null) {
  75. position = {};
  76. }
  77. position[name] = val;
  78. }
  79. });
  80. return {
  81. raw,
  82. normal,
  83. set,
  84. offset,
  85. position,
  86. };
  87. }
  88. mergeProcessedAttrs(allProcessedAttrs, roProcessedAttrs) {
  89. allProcessedAttrs.set = Object.assign(Object.assign({}, allProcessedAttrs.set), roProcessedAttrs.set);
  90. allProcessedAttrs.position = Object.assign(Object.assign({}, allProcessedAttrs.position), roProcessedAttrs.position);
  91. allProcessedAttrs.offset = Object.assign(Object.assign({}, allProcessedAttrs.offset), roProcessedAttrs.offset);
  92. // Handle also the special transform property.
  93. const transform = allProcessedAttrs.normal && allProcessedAttrs.normal.transform;
  94. if (transform != null && roProcessedAttrs.normal) {
  95. roProcessedAttrs.normal.transform = transform;
  96. }
  97. allProcessedAttrs.normal = roProcessedAttrs.normal;
  98. }
  99. findAttrs(cellAttrs, rootNode, selectorCache, selectors) {
  100. const merge = [];
  101. const result = new x6_common_1.Dictionary();
  102. Object.keys(cellAttrs).forEach((selector) => {
  103. const attrs = cellAttrs[selector];
  104. if (!x6_common_1.ObjectExt.isPlainObject(attrs)) {
  105. return;
  106. }
  107. const { isCSSSelector, elems } = view_1.View.find(selector, rootNode, selectors);
  108. selectorCache[selector] = elems;
  109. for (let i = 0, l = elems.length; i < l; i += 1) {
  110. const elem = elems[i];
  111. const unique = selectors && selectors[selector] === elem;
  112. const prev = result.get(elem);
  113. if (prev) {
  114. if (!prev.array) {
  115. merge.push(elem);
  116. prev.array = true;
  117. prev.attrs = [prev.attrs];
  118. prev.priority = [prev.priority];
  119. }
  120. const attributes = prev.attrs;
  121. const selectedLength = prev.priority;
  122. if (unique) {
  123. // node referenced by `selector`
  124. attributes.unshift(attrs);
  125. selectedLength.unshift(-1);
  126. }
  127. else {
  128. // node referenced by `groupSelector` or CSSSelector
  129. const sortIndex = x6_common_1.ArrayExt.sortedIndex(selectedLength, isCSSSelector ? -1 : l);
  130. attributes.splice(sortIndex, 0, attrs);
  131. selectedLength.splice(sortIndex, 0, l);
  132. }
  133. }
  134. else {
  135. result.set(elem, {
  136. elem,
  137. attrs,
  138. priority: unique ? -1 : l,
  139. array: false,
  140. });
  141. }
  142. }
  143. });
  144. merge.forEach((node) => {
  145. const item = result.get(node);
  146. const arr = item.attrs;
  147. item.attrs = arr.reduceRight((memo, attrs) => x6_common_1.ObjectExt.merge(memo, attrs), {});
  148. });
  149. return result;
  150. }
  151. updateRelativeAttrs(elem, processedAttrs, refBBox) {
  152. const rawAttrs = processedAttrs.raw || {};
  153. let nodeAttrs = processedAttrs.normal || {};
  154. const setAttrs = processedAttrs.set;
  155. const positionAttrs = processedAttrs.position;
  156. const offsetAttrs = processedAttrs.offset;
  157. const getOptions = () => ({
  158. elem,
  159. cell: this.cell,
  160. view: this.view,
  161. attrs: rawAttrs,
  162. refBBox: refBBox.clone(),
  163. });
  164. if (setAttrs != null) {
  165. Object.keys(setAttrs).forEach((name) => {
  166. const val = setAttrs[name];
  167. const def = this.getDefinition(name);
  168. if (def != null) {
  169. const ret = x6_common_1.FunctionExt.call(def.set, this.view, val, getOptions());
  170. if (typeof ret === 'object') {
  171. nodeAttrs = Object.assign(Object.assign({}, nodeAttrs), ret);
  172. }
  173. else if (ret != null) {
  174. nodeAttrs[name] = ret;
  175. }
  176. }
  177. });
  178. }
  179. if (elem instanceof HTMLElement) {
  180. // TODO: setting the `transform` attribute on HTMLElements
  181. // via `node.style.transform = 'matrix(...)';` would introduce
  182. // a breaking change (e.g. basic.TextBlock).
  183. this.view.setAttrs(nodeAttrs, elem);
  184. return;
  185. }
  186. // The final translation of the subelement.
  187. const nodeTransform = nodeAttrs.transform;
  188. const transform = nodeTransform ? `${nodeTransform}` : null;
  189. const nodeMatrix = x6_common_1.Dom.transformStringToMatrix(transform);
  190. const nodePosition = new x6_geometry_1.Point(nodeMatrix.e, nodeMatrix.f);
  191. if (nodeTransform) {
  192. delete nodeAttrs.transform;
  193. nodeMatrix.e = 0;
  194. nodeMatrix.f = 0;
  195. }
  196. let positioned = false;
  197. if (positionAttrs != null) {
  198. Object.keys(positionAttrs).forEach((name) => {
  199. const val = positionAttrs[name];
  200. const def = this.getDefinition(name);
  201. if (def != null) {
  202. const ts = x6_common_1.FunctionExt.call(def.position, this.view, val, getOptions());
  203. if (ts != null) {
  204. positioned = true;
  205. nodePosition.translate(x6_geometry_1.Point.create(ts));
  206. }
  207. }
  208. });
  209. }
  210. // The node bounding box could depend on the `size`
  211. // set from the previous loop.
  212. this.view.setAttrs(nodeAttrs, elem);
  213. let offseted = false;
  214. if (offsetAttrs != null) {
  215. // Check if the node is visible
  216. const nodeBoundingRect = this.view.getBoundingRectOfElement(elem);
  217. if (nodeBoundingRect.width > 0 && nodeBoundingRect.height > 0) {
  218. const nodeBBox = util_1.Util.transformRectangle(nodeBoundingRect, nodeMatrix);
  219. Object.keys(offsetAttrs).forEach((name) => {
  220. const val = offsetAttrs[name];
  221. const def = this.getDefinition(name);
  222. if (def != null) {
  223. const ts = x6_common_1.FunctionExt.call(def.offset, this.view, val, {
  224. elem,
  225. cell: this.cell,
  226. view: this.view,
  227. attrs: rawAttrs,
  228. refBBox: nodeBBox,
  229. });
  230. if (ts != null) {
  231. offseted = true;
  232. nodePosition.translate(x6_geometry_1.Point.create(ts));
  233. }
  234. }
  235. });
  236. }
  237. }
  238. if (nodeTransform != null || positioned || offseted) {
  239. nodePosition.round(1);
  240. nodeMatrix.e = nodePosition.x;
  241. nodeMatrix.f = nodePosition.y;
  242. elem.setAttribute('transform', x6_common_1.Dom.matrixToTransformString(nodeMatrix));
  243. }
  244. }
  245. update(rootNode, attrs, options) {
  246. const selectorCache = {};
  247. const nodesAttrs = this.findAttrs(options.attrs || attrs, rootNode, selectorCache, options.selectors);
  248. // `nodesAttrs` are different from all attributes, when
  249. // rendering only attributes sent to this method.
  250. const nodesAllAttrs = options.attrs
  251. ? this.findAttrs(attrs, rootNode, selectorCache, options.selectors)
  252. : nodesAttrs;
  253. const specialItems = [];
  254. nodesAttrs.each((data) => {
  255. const node = data.elem;
  256. const nodeAttrs = data.attrs;
  257. const processed = this.processAttrs(node, nodeAttrs);
  258. if (processed.set == null &&
  259. processed.position == null &&
  260. processed.offset == null) {
  261. this.view.setAttrs(processed.normal, node);
  262. }
  263. else {
  264. const data = nodesAllAttrs.get(node);
  265. const nodeAllAttrs = data ? data.attrs : null;
  266. const refSelector = nodeAllAttrs && nodeAttrs.ref == null
  267. ? nodeAllAttrs.ref
  268. : nodeAttrs.ref;
  269. let refNode;
  270. if (refSelector) {
  271. refNode = (selectorCache[refSelector] ||
  272. this.view.find(refSelector, rootNode, options.selectors))[0];
  273. if (!refNode) {
  274. throw new Error(`"${refSelector}" reference does not exist.`);
  275. }
  276. }
  277. else {
  278. refNode = null;
  279. }
  280. const item = {
  281. node,
  282. refNode,
  283. attributes: nodeAllAttrs,
  284. processedAttributes: processed,
  285. };
  286. // If an element in the list is positioned relative to this one, then
  287. // we want to insert this one before it in the list.
  288. const index = specialItems.findIndex((item) => item.refNode === node);
  289. if (index > -1) {
  290. specialItems.splice(index, 0, item);
  291. }
  292. else {
  293. specialItems.push(item);
  294. }
  295. }
  296. });
  297. const bboxCache = new x6_common_1.Dictionary();
  298. let rotatableMatrix;
  299. specialItems.forEach((item) => {
  300. const node = item.node;
  301. const refNode = item.refNode;
  302. let unrotatedRefBBox;
  303. const isRefNodeRotatable = refNode != null &&
  304. options.rotatableNode != null &&
  305. x6_common_1.Dom.contains(options.rotatableNode, refNode);
  306. // Find the reference element bounding box. If no reference was
  307. // provided, we use the optional bounding box.
  308. if (refNode) {
  309. unrotatedRefBBox = bboxCache.get(refNode);
  310. }
  311. if (!unrotatedRefBBox) {
  312. const target = (isRefNodeRotatable ? options.rotatableNode : rootNode);
  313. unrotatedRefBBox = refNode
  314. ? util_1.Util.getBBox(refNode, { target })
  315. : options.rootBBox;
  316. if (refNode) {
  317. bboxCache.set(refNode, unrotatedRefBBox);
  318. }
  319. }
  320. let processedAttrs;
  321. if (options.attrs && item.attributes) {
  322. // If there was a special attribute affecting the position amongst
  323. // passed-in attributes we have to merge it with the rest of the
  324. // element's attributes as they are necessary to update the position
  325. // relatively (i.e `ref-x` && 'ref-dx').
  326. processedAttrs = this.processAttrs(node, item.attributes);
  327. this.mergeProcessedAttrs(processedAttrs, item.processedAttributes);
  328. }
  329. else {
  330. processedAttrs = item.processedAttributes;
  331. }
  332. let refBBox = unrotatedRefBBox;
  333. if (isRefNodeRotatable &&
  334. options.rotatableNode != null &&
  335. !options.rotatableNode.contains(node)) {
  336. // If the referenced node is inside the rotatable group while the
  337. // updated node is outside, we need to take the rotatable node
  338. // transformation into account.
  339. if (!rotatableMatrix) {
  340. rotatableMatrix = x6_common_1.Dom.transformStringToMatrix(x6_common_1.Dom.attr(options.rotatableNode, 'transform'));
  341. }
  342. refBBox = util_1.Util.transformRectangle(unrotatedRefBBox, rotatableMatrix);
  343. }
  344. this.updateRelativeAttrs(node, processedAttrs, refBBox);
  345. });
  346. }
  347. }
  348. exports.AttrManager = AttrManager;
  349. //# sourceMappingURL=attr.js.map