SGraphSelectContainer.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SGraphSelectContainer = void 0;
  4. const lib_1 = require("@persagy-web/base/lib");
  5. const SGraphLayoutType_1 = require("./enums/SGraphLayoutType");
  6. const SOrderSetType_1 = require("./enums/SOrderSetType");
  7. class SGraphSelectContainer extends lib_1.SObject {
  8. constructor() {
  9. super();
  10. this.itemList = [];
  11. this.radix = 0.001;
  12. }
  13. get count() {
  14. return this.itemList.length;
  15. }
  16. addItem(item) {
  17. for (let i = 0; i < this.itemList.length; i++) {
  18. if (this.itemList[i] == item) {
  19. return;
  20. }
  21. }
  22. item.selected = true;
  23. this.itemList.push(item);
  24. this.$emit("listChange", this.itemList);
  25. }
  26. setItem(item) {
  27. this.itemList.forEach((t) => {
  28. t.selected = false;
  29. });
  30. this.itemList.length = 0;
  31. item.selected = true;
  32. this.itemList.push(item);
  33. this.$emit("listChange", this.itemList);
  34. }
  35. clear() {
  36. this.itemList.forEach((t) => {
  37. t.selected = false;
  38. });
  39. this.itemList.length = 0;
  40. this.$emit("listChange", this.itemList);
  41. }
  42. toggleItem(item) {
  43. for (let i = 0; i < this.itemList.length; i++) {
  44. if (this.itemList[i] == item) {
  45. this.itemList[i].selected = false;
  46. this.itemList.splice(i, 1);
  47. this.$emit("listChange", this.itemList, item);
  48. return;
  49. }
  50. }
  51. if (this.itemList.length) {
  52. if (item.parent != this.itemList[0].parent) {
  53. return;
  54. }
  55. }
  56. item.selected = true;
  57. this.itemList.push(item);
  58. this.$emit("listChange", this.itemList);
  59. }
  60. layout(type) {
  61. if (this.itemList.length < 2) {
  62. return;
  63. }
  64. switch (type) {
  65. case SGraphLayoutType_1.SGraphLayoutType.Left:
  66. this.alignLeft();
  67. break;
  68. case SGraphLayoutType_1.SGraphLayoutType.Bottom:
  69. this.alignBottom();
  70. break;
  71. case SGraphLayoutType_1.SGraphLayoutType.Center:
  72. this.alignCenter();
  73. break;
  74. case SGraphLayoutType_1.SGraphLayoutType.Horizontal:
  75. this.alignHorizontal();
  76. break;
  77. case SGraphLayoutType_1.SGraphLayoutType.Middle:
  78. this.alignMiddle();
  79. break;
  80. case SGraphLayoutType_1.SGraphLayoutType.Right:
  81. this.alignRight();
  82. break;
  83. case SGraphLayoutType_1.SGraphLayoutType.Top:
  84. this.alignTop();
  85. break;
  86. case SGraphLayoutType_1.SGraphLayoutType.Vertical:
  87. this.alignVertical();
  88. break;
  89. default:
  90. console.log("对齐类型不存在");
  91. break;
  92. }
  93. }
  94. setOrder(type) {
  95. if (this.itemList.length < 1) {
  96. return;
  97. }
  98. switch (type) {
  99. case SOrderSetType_1.SOrderSetType.Top:
  100. this.setTop();
  101. break;
  102. case SOrderSetType_1.SOrderSetType.Bottom:
  103. this.setBottom();
  104. break;
  105. case SOrderSetType_1.SOrderSetType.After:
  106. this.setAfter();
  107. break;
  108. case SOrderSetType_1.SOrderSetType.Before:
  109. this.setBefore();
  110. break;
  111. default:
  112. console.log("图层操作类型不存在");
  113. break;
  114. }
  115. }
  116. alignLeft() {
  117. let standardItem = this.itemList[0];
  118. let p = standardItem.mapToScene(standardItem.boundingRect().x, standardItem.boundingRect().y);
  119. for (let i = 1; i < this.itemList.length; i++) {
  120. let curItem = this.itemList[i];
  121. if (curItem.moveable) {
  122. let p1 = curItem.mapFromScene(p.x, p.y);
  123. curItem.x =
  124. (p1.x - curItem.boundingRect().x) * curItem.inverseScale +
  125. curItem.x;
  126. }
  127. }
  128. }
  129. alignTop() {
  130. let standardItem = this.itemList[0];
  131. let p = standardItem.mapToScene(standardItem.boundingRect().x, standardItem.boundingRect().y);
  132. for (let i = 1; i < this.itemList.length; i++) {
  133. let curItem = this.itemList[i];
  134. if (curItem.moveable) {
  135. let p1 = curItem.mapFromScene(p.x, p.y);
  136. curItem.y =
  137. (p1.y - curItem.boundingRect().y) * curItem.inverseScale +
  138. curItem.y;
  139. }
  140. }
  141. }
  142. alignRight() {
  143. let standardItem = this.itemList[0];
  144. let p = standardItem.mapToScene(standardItem.boundingRect().right, standardItem.boundingRect().bottom);
  145. for (let i = 1; i < this.itemList.length; i++) {
  146. let curItem = this.itemList[i];
  147. if (curItem.moveable) {
  148. let p1 = curItem.mapFromScene(p.x, p.y);
  149. curItem.x =
  150. (p1.x - curItem.boundingRect().right) *
  151. curItem.inverseScale +
  152. curItem.x;
  153. }
  154. }
  155. }
  156. alignBottom() {
  157. let standardItem = this.itemList[0];
  158. let p = standardItem.mapToScene(standardItem.boundingRect().right, standardItem.boundingRect().bottom);
  159. for (let i = 1; i < this.itemList.length; i++) {
  160. let curItem = this.itemList[i];
  161. if (curItem.moveable) {
  162. let p1 = curItem.mapFromScene(p.x, p.y);
  163. curItem.y =
  164. (p1.y - curItem.boundingRect().bottom) *
  165. curItem.inverseScale +
  166. curItem.y;
  167. }
  168. }
  169. }
  170. alignCenter() {
  171. let standardItem = this.itemList[0];
  172. let center = standardItem.boundingRect().center();
  173. let p = standardItem.mapToScene(center.x, center.y);
  174. for (let i = 1; i < this.itemList.length; i++) {
  175. let curItem = this.itemList[i];
  176. if (curItem.moveable) {
  177. let p1 = curItem.mapFromScene(p.x, p.y);
  178. curItem.x =
  179. (p1.x - curItem.boundingRect().center().x) *
  180. curItem.inverseScale +
  181. curItem.x;
  182. }
  183. }
  184. }
  185. alignMiddle() {
  186. let standardItem = this.itemList[0];
  187. let center = standardItem.boundingRect().center();
  188. let p = standardItem.mapToScene(center.x, center.y);
  189. for (let i = 1; i < this.itemList.length; i++) {
  190. let curItem = this.itemList[i];
  191. if (curItem.moveable) {
  192. let p1 = curItem.mapFromScene(p.x, p.y);
  193. curItem.y =
  194. (p1.y - curItem.boundingRect().center().y) *
  195. curItem.inverseScale +
  196. curItem.y;
  197. }
  198. }
  199. }
  200. alignVertical() {
  201. if (this.itemList.length < 3) {
  202. return;
  203. }
  204. for (let i = 0; i < this.itemList.length - 1; i++) {
  205. for (let j = 0; j < this.itemList.length - 1 - i; j++) {
  206. let curItemCenter = this.itemList[j].boundingRect().center();
  207. let nextItemCenter = this.itemList[j + 1]
  208. .boundingRect()
  209. .center();
  210. let curCenterY = this.itemList[j].mapToScene(curItemCenter.x, curItemCenter.y).y;
  211. let nextCenterY = this.itemList[j + 1].mapToScene(nextItemCenter.x, nextItemCenter.y).y;
  212. if (curCenterY > nextCenterY) {
  213. let temp = this.itemList[j];
  214. this.itemList[j] = this.itemList[j + 1];
  215. this.itemList[j + 1] = temp;
  216. }
  217. }
  218. }
  219. let top = this.itemList[0];
  220. let bottom = this.itemList[this.itemList.length - 1];
  221. let topCenter = top.boundingRect().center();
  222. let bottomCenter = bottom.boundingRect().center();
  223. let leftToRight = bottom.mapToScene(bottomCenter.x, bottomCenter.y).y -
  224. top.mapToScene(topCenter.x, topCenter.y).y;
  225. let average = leftToRight / (this.itemList.length - 1);
  226. for (let k = 1; k < this.itemList.length - 1; k++) {
  227. let curItem = this.itemList[k];
  228. if (curItem.moveable) {
  229. let p1 = top.mapToScene(top.boundingRect().center().x, top.boundingRect().center().y);
  230. let p2 = curItem.mapFromScene(p1.x, p1.y + average * k);
  231. curItem.y =
  232. (p2.y - curItem.boundingRect().center().y) *
  233. curItem.inverseScale +
  234. curItem.y;
  235. }
  236. }
  237. }
  238. alignHorizontal() {
  239. if (this.itemList.length < 3) {
  240. return;
  241. }
  242. for (let i = 0; i < this.itemList.length - 1; i++) {
  243. for (let j = 0; j < this.itemList.length - 1 - i; j++) {
  244. let curItemCenter = this.itemList[j].boundingRect().center();
  245. let nextItemCenter = this.itemList[j + 1]
  246. .boundingRect()
  247. .center();
  248. let curCenterX = this.itemList[j].mapToScene(curItemCenter.x, curItemCenter.y).x;
  249. let nextCenterX = this.itemList[j + 1].mapToScene(nextItemCenter.x, nextItemCenter.y).x;
  250. if (curCenterX > nextCenterX) {
  251. let temp = this.itemList[j];
  252. this.itemList[j] = this.itemList[j + 1];
  253. this.itemList[j + 1] = temp;
  254. }
  255. }
  256. }
  257. let left = this.itemList[0];
  258. let right = this.itemList[this.itemList.length - 1];
  259. let leftCenter = left.boundingRect().center();
  260. let rightCenter = right.boundingRect().center();
  261. let leftToRight = right.mapToScene(rightCenter.x, rightCenter.y).x -
  262. left.mapToScene(leftCenter.x, leftCenter.y).x;
  263. let average = leftToRight / (this.itemList.length - 1);
  264. for (let k = 1; k < this.itemList.length - 1; k++) {
  265. let curItem = this.itemList[k];
  266. if (curItem.moveable) {
  267. let p1 = left.mapToScene(left.boundingRect().center().x, left.boundingRect().center().y);
  268. let p2 = curItem.mapFromScene(p1.x + average * k, p1.y);
  269. curItem.x =
  270. (p2.x - curItem.boundingRect().center().x) *
  271. curItem.inverseScale +
  272. curItem.x;
  273. }
  274. }
  275. }
  276. setTop() {
  277. const arr = this.sortItemList(this.itemList, true);
  278. arr.forEach(it => {
  279. let max = it.zOrder;
  280. if (it.parent) {
  281. it.parent.children.forEach(item => {
  282. if (Number(max.toFixed()) == Number(item.zOrder.toFixed())) {
  283. if (item.zOrder > max) {
  284. max = item.zOrder;
  285. }
  286. }
  287. });
  288. it.zOrder = max + this.radix;
  289. }
  290. });
  291. }
  292. setBottom() {
  293. const arr = this.sortItemList(this.itemList, false);
  294. arr.forEach(it => {
  295. let min = it.zOrder;
  296. if (it.parent) {
  297. it.parent.children.forEach(item => {
  298. if (Number(min.toFixed()) == Number(item.zOrder.toFixed())) {
  299. if (item.zOrder < min) {
  300. min = item.zOrder;
  301. }
  302. }
  303. });
  304. it.zOrder = min - this.radix;
  305. }
  306. });
  307. }
  308. setBefore() {
  309. const arr = this.sortItemList(this.itemList, false);
  310. arr.forEach(it => {
  311. if (it.parent) {
  312. const min = it.zOrder;
  313. let temp = it.parent.children
  314. .map(child => {
  315. if (Number(child.zOrder.toFixed()) ==
  316. Number(min.toFixed())) {
  317. return child.zOrder;
  318. }
  319. })
  320. .filter(c => c);
  321. temp = [...new Set(temp)];
  322. const index = temp.indexOf(it.zOrder);
  323. if (index <= 1) {
  324. it.zOrder = temp[0] - this.radix;
  325. }
  326. else if (index > 1) {
  327. it.zOrder = (temp[index - 1] + temp[index - 2]) / 2;
  328. }
  329. }
  330. });
  331. }
  332. setAfter() {
  333. const arr = this.sortItemList(this.itemList, false);
  334. arr.forEach(it => {
  335. if (it.parent) {
  336. const max = it.zOrder;
  337. let temp = it.parent.children
  338. .map(child => {
  339. if (Number(child.zOrder.toFixed()) ==
  340. Number(max.toFixed())) {
  341. return child.zOrder;
  342. }
  343. })
  344. .filter(c => c);
  345. temp = [...new Set(temp)].reverse();
  346. const index = temp.indexOf(it.zOrder);
  347. if (index <= 1) {
  348. it.zOrder = temp[0] + this.radix;
  349. }
  350. else if (index > 1) {
  351. it.zOrder = (temp[index - 1] + temp[index - 2]) / 2;
  352. }
  353. }
  354. });
  355. }
  356. sortItemList(arr, flag = true) {
  357. const list = arr.map(t => t);
  358. list.sort(this.compare("zOrder", flag));
  359. return list;
  360. }
  361. compare(prop, flag) {
  362. const f = flag ? 1 : -1;
  363. return (a, b) => {
  364. a = a[prop];
  365. b = b[prop];
  366. if (a < b) {
  367. return f * -1;
  368. }
  369. if (a > b) {
  370. return f * 1;
  371. }
  372. return 0;
  373. };
  374. }
  375. }
  376. exports.SGraphSelectContainer = SGraphSelectContainer;