SGraphSelectContainer.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2016-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SGraphItem } from "./SGraphItem";
  27. import { SObject } from "@persagy-web/base";
  28. import { SGraphLayoutType } from "./enums/SGraphLayoutType";
  29. import { SOrderSetType } from "./enums/SOrderSetType";
  30. /**
  31. * 基本选择器
  32. *
  33. * @author 郝建龙
  34. */
  35. export class SGraphSelectContainer extends SObject {
  36. /** 选择对象list */
  37. private itemList: SGraphItem[] = [];
  38. /** 统计选中item的数量 */
  39. get count(): number {
  40. return this.itemList.length;
  41. }
  42. /** 置顶zorder增加基数 */
  43. private radix: number = 0.001;
  44. /**
  45. * 构造体
  46. * */
  47. constructor() {
  48. super();
  49. } // Constructor
  50. /**
  51. * 添加item到list
  52. *
  53. * @param item 当前选中的item
  54. * */
  55. addItem(item: SGraphItem): void {
  56. for (let i = 0; i < this.itemList.length; i++) {
  57. if (this.itemList[i] == item) {
  58. return;
  59. }
  60. }
  61. item.selected = true;
  62. this.itemList.push(item);
  63. this.$emit("listChange", this.itemList);
  64. } // Function addItem()
  65. /**
  66. * 清空再添加(事件+复制数组)
  67. *
  68. * @param item 当前选中的item
  69. * */
  70. setItem(item: SGraphItem): void {
  71. this.itemList.forEach((t: SGraphItem): void => {
  72. t.selected = false;
  73. });
  74. this.itemList.length = 0;
  75. item.selected = true;
  76. this.itemList.push(item);
  77. this.$emit("listChange", this.itemList);
  78. } // Function setItem()
  79. /**
  80. * 清空选择对象
  81. *
  82. * */
  83. clear(): void {
  84. this.itemList.forEach((t: SGraphItem): void => {
  85. t.selected = false;
  86. });
  87. this.itemList.length = 0;
  88. this.$emit("listChange", this.itemList);
  89. } // Function clear()
  90. /**
  91. * 切换选择对象
  92. *
  93. * @param item 当前选中的item
  94. * */
  95. toggleItem(item: SGraphItem): void {
  96. for (let i = 0; i < this.itemList.length; i++) {
  97. if (this.itemList[i] == item) {
  98. this.itemList[i].selected = false;
  99. this.itemList.splice(i, 1);
  100. this.$emit("listChange", this.itemList, item);
  101. return;
  102. }
  103. }
  104. // 多选时,父级item需要一致
  105. if (this.itemList.length) {
  106. if (item.parent != this.itemList[0].parent) {
  107. return;
  108. }
  109. }
  110. item.selected = true;
  111. this.itemList.push(item);
  112. this.$emit("listChange", this.itemList);
  113. } // Function toggleItem()
  114. /**
  115. * 对齐
  116. *
  117. * @param type 对齐方式
  118. * */
  119. layout(type: SGraphLayoutType): void {
  120. if (this.itemList.length < 2) {
  121. return;
  122. }
  123. switch (type) {
  124. case SGraphLayoutType.Left:
  125. this.alignLeft();
  126. break;
  127. case SGraphLayoutType.Bottom:
  128. this.alignBottom();
  129. break;
  130. case SGraphLayoutType.Center:
  131. this.alignCenter();
  132. break;
  133. case SGraphLayoutType.Horizontal:
  134. this.alignHorizontal();
  135. break;
  136. case SGraphLayoutType.Middle:
  137. this.alignMiddle();
  138. break;
  139. case SGraphLayoutType.Right:
  140. this.alignRight();
  141. break;
  142. case SGraphLayoutType.Top:
  143. this.alignTop();
  144. break;
  145. case SGraphLayoutType.Vertical:
  146. this.alignVertical();
  147. break;
  148. default:
  149. console.log("对齐类型不存在");
  150. break;
  151. }
  152. } // Function layout()
  153. /**
  154. * 图层操作
  155. *
  156. * @param type 操作类型
  157. * */
  158. setOrder(type: SOrderSetType): void {
  159. if (this.itemList.length < 1) {
  160. return;
  161. }
  162. switch (type) {
  163. case SOrderSetType.Top:
  164. this.setTop();
  165. break;
  166. case SOrderSetType.Bottom:
  167. this.setBottom();
  168. break;
  169. case SOrderSetType.After:
  170. this.setAfter();
  171. break;
  172. case SOrderSetType.Before:
  173. this.setBefore();
  174. break;
  175. default:
  176. console.log("图层操作类型不存在");
  177. break;
  178. }
  179. } // Function setOrder()
  180. /**
  181. * 左对齐
  182. *
  183. * */
  184. private alignLeft(): void {
  185. let standardItem = this.itemList[0];
  186. // 计算第一个外接矩阵左上角坐标在场景中的坐标
  187. let p = standardItem.mapToScene(
  188. standardItem.boundingRect().x,
  189. standardItem.boundingRect().y
  190. );
  191. for (let i = 1; i < this.itemList.length; i++) {
  192. let curItem = this.itemList[i];
  193. if (curItem.moveable) {
  194. // 将p转换为当前item中的坐标
  195. let p1 = curItem.mapFromScene(p.x, p.y);
  196. // 根据等式差值相等 newboundx - oldboundx = newposx - oldposx => newposx = newboundx - oldboundx + oldposx
  197. curItem.x =
  198. (p1.x - curItem.boundingRect().x) * curItem.inverseScale +
  199. curItem.x;
  200. }
  201. }
  202. } // Function alignLeft()
  203. /**
  204. * 顶对齐
  205. *
  206. * */
  207. private alignTop(): void {
  208. let standardItem = this.itemList[0];
  209. let p = standardItem.mapToScene(
  210. standardItem.boundingRect().x,
  211. standardItem.boundingRect().y
  212. );
  213. for (let i = 1; i < this.itemList.length; i++) {
  214. let curItem = this.itemList[i];
  215. if (curItem.moveable) {
  216. let p1 = curItem.mapFromScene(p.x, p.y);
  217. curItem.y =
  218. (p1.y - curItem.boundingRect().y) * curItem.inverseScale +
  219. curItem.y;
  220. }
  221. }
  222. } // Function alignTop()
  223. /**
  224. * 右对齐
  225. *
  226. * */
  227. private alignRight(): void {
  228. let standardItem = this.itemList[0];
  229. let p = standardItem.mapToScene(
  230. standardItem.boundingRect().right,
  231. standardItem.boundingRect().bottom
  232. );
  233. for (let i = 1; i < this.itemList.length; i++) {
  234. let curItem = this.itemList[i];
  235. if (curItem.moveable) {
  236. let p1 = curItem.mapFromScene(p.x, p.y);
  237. curItem.x =
  238. (p1.x - curItem.boundingRect().right) *
  239. curItem.inverseScale +
  240. curItem.x;
  241. }
  242. }
  243. } // Function alignRight()
  244. /**
  245. * 底对齐
  246. *
  247. * */
  248. private alignBottom(): void {
  249. let standardItem = this.itemList[0];
  250. let p = standardItem.mapToScene(
  251. standardItem.boundingRect().right,
  252. standardItem.boundingRect().bottom
  253. );
  254. for (let i = 1; i < this.itemList.length; i++) {
  255. let curItem = this.itemList[i];
  256. if (curItem.moveable) {
  257. let p1 = curItem.mapFromScene(p.x, p.y);
  258. curItem.y =
  259. (p1.y - curItem.boundingRect().bottom) *
  260. curItem.inverseScale +
  261. curItem.y;
  262. }
  263. }
  264. } // Function alignBottom()
  265. /**
  266. * 水平居中对齐
  267. *
  268. * */
  269. private alignCenter(): void {
  270. // 第一个选中的item即为基准对象
  271. let standardItem = this.itemList[0];
  272. // 基准对象的中心点
  273. let center = standardItem.boundingRect().center();
  274. // 中心点转换为场景坐标
  275. let p = standardItem.mapToScene(center.x, center.y);
  276. for (let i = 1; i < this.itemList.length; i++) {
  277. let curItem = this.itemList[i];
  278. if (curItem.moveable) {
  279. let p1 = curItem.mapFromScene(p.x, p.y);
  280. // 根据等式差值相等 newcenterx - oldcenterx = newposx - oldposx => newposx = newcenterx - oldcenterx + oldposx
  281. curItem.x =
  282. (p1.x - curItem.boundingRect().center().x) *
  283. curItem.inverseScale +
  284. curItem.x;
  285. }
  286. }
  287. } // Function alignCenter()
  288. /**
  289. * 垂直居中对齐
  290. *
  291. * */
  292. private alignMiddle(): void {
  293. // 第一个选中的item即为基准对象
  294. let standardItem = this.itemList[0];
  295. // 基准对象的中心点
  296. let center = standardItem.boundingRect().center();
  297. // 中心点转换为场景坐标
  298. let p = standardItem.mapToScene(center.x, center.y);
  299. for (let i = 1; i < this.itemList.length; i++) {
  300. let curItem = this.itemList[i];
  301. if (curItem.moveable) {
  302. let p1 = curItem.mapFromScene(p.x, p.y);
  303. curItem.y =
  304. (p1.y - curItem.boundingRect().center().y) *
  305. curItem.inverseScale +
  306. curItem.y;
  307. }
  308. }
  309. } // Function alignMiddle()
  310. /**
  311. * 垂直分布
  312. *
  313. * */
  314. private alignVertical(): void {
  315. if (this.itemList.length < 3) {
  316. return;
  317. }
  318. for (let i = 0; i < this.itemList.length - 1; i++) {
  319. for (let j = 0; j < this.itemList.length - 1 - i; j++) {
  320. let curItemCenter = this.itemList[j].boundingRect().center();
  321. let nextItemCenter = this.itemList[j + 1]
  322. .boundingRect()
  323. .center();
  324. let curCenterY = this.itemList[j].mapToScene(
  325. curItemCenter.x,
  326. curItemCenter.y
  327. ).y;
  328. let nextCenterY = this.itemList[j + 1].mapToScene(
  329. nextItemCenter.x,
  330. nextItemCenter.y
  331. ).y;
  332. if (curCenterY > nextCenterY) {
  333. let temp = this.itemList[j];
  334. this.itemList[j] = this.itemList[j + 1];
  335. this.itemList[j + 1] = temp;
  336. }
  337. }
  338. }
  339. let top = this.itemList[0];
  340. let bottom = this.itemList[this.itemList.length - 1];
  341. let topCenter = top.boundingRect().center();
  342. let bottomCenter = bottom.boundingRect().center();
  343. let leftToRight =
  344. bottom.mapToScene(bottomCenter.x, bottomCenter.y).y -
  345. top.mapToScene(topCenter.x, topCenter.y).y;
  346. let average = leftToRight / (this.itemList.length - 1);
  347. for (let k = 1; k < this.itemList.length - 1; k++) {
  348. let curItem = this.itemList[k];
  349. if (curItem.moveable) {
  350. let p1 = top.mapToScene(
  351. top.boundingRect().center().x,
  352. top.boundingRect().center().y
  353. );
  354. let p2 = curItem.mapFromScene(p1.x, p1.y + average * k);
  355. curItem.y =
  356. (p2.y - curItem.boundingRect().center().y) *
  357. curItem.inverseScale +
  358. curItem.y;
  359. }
  360. }
  361. } // Function alignVertical()
  362. /**
  363. * 水平分布
  364. *
  365. * */
  366. private alignHorizontal(): void {
  367. if (this.itemList.length < 3) {
  368. return;
  369. }
  370. for (let i = 0; i < this.itemList.length - 1; i++) {
  371. for (let j = 0; j < this.itemList.length - 1 - i; j++) {
  372. let curItemCenter = this.itemList[j].boundingRect().center();
  373. let nextItemCenter = this.itemList[j + 1]
  374. .boundingRect()
  375. .center();
  376. let curCenterX = this.itemList[j].mapToScene(
  377. curItemCenter.x,
  378. curItemCenter.y
  379. ).x;
  380. let nextCenterX = this.itemList[j + 1].mapToScene(
  381. nextItemCenter.x,
  382. nextItemCenter.y
  383. ).x;
  384. if (curCenterX > nextCenterX) {
  385. let temp = this.itemList[j];
  386. this.itemList[j] = this.itemList[j + 1];
  387. this.itemList[j + 1] = temp;
  388. }
  389. }
  390. }
  391. let left = this.itemList[0];
  392. let right = this.itemList[this.itemList.length - 1];
  393. let leftCenter = left.boundingRect().center();
  394. let rightCenter = right.boundingRect().center();
  395. let leftToRight =
  396. right.mapToScene(rightCenter.x, rightCenter.y).x -
  397. left.mapToScene(leftCenter.x, leftCenter.y).x;
  398. let average = leftToRight / (this.itemList.length - 1);
  399. for (let k = 1; k < this.itemList.length - 1; k++) {
  400. let curItem = this.itemList[k];
  401. if (curItem.moveable) {
  402. let p1 = left.mapToScene(
  403. left.boundingRect().center().x,
  404. left.boundingRect().center().y
  405. );
  406. let p2 = curItem.mapFromScene(p1.x + average * k, p1.y);
  407. // 根据等式 newposx - oldposx = newcenterx - oldcenterx
  408. curItem.x =
  409. (p2.x - curItem.boundingRect().center().x) *
  410. curItem.inverseScale +
  411. curItem.x;
  412. }
  413. }
  414. } // Function alignHorizontal()
  415. // 修改层级操作步骤:
  416. // 排序(保持相对层级不变)
  417. // children里边要取 整数部分相同的 点:children是已经排好序的
  418. // 然后再取最大值
  419. /**
  420. * 置顶
  421. *
  422. * */
  423. private setTop(): void {
  424. const arr: SGraphItem[] = this.sortItemList(this.itemList, true);
  425. // 按顺序zOrder增加
  426. arr.forEach(it => {
  427. let max = it.zOrder;
  428. if (it.parent) {
  429. it.parent.children.forEach(item => {
  430. if (
  431. Number(max.toFixed()) == Number(item.zOrder.toFixed())
  432. ) {
  433. if (item.zOrder > max) {
  434. max = item.zOrder;
  435. }
  436. }
  437. });
  438. it.zOrder = max + this.radix;
  439. }
  440. });
  441. } // Function setTop()
  442. /**
  443. * 置底
  444. *
  445. * */
  446. private setBottom(): void {
  447. const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
  448. // 按顺序zOrder增加
  449. arr.forEach(it => {
  450. let min = it.zOrder;
  451. if (it.parent) {
  452. it.parent.children.forEach(item => {
  453. if (
  454. Number(min.toFixed()) == Number(item.zOrder.toFixed())
  455. ) {
  456. if (item.zOrder < min) {
  457. min = item.zOrder;
  458. }
  459. }
  460. });
  461. it.zOrder = min - this.radix;
  462. }
  463. });
  464. } // Function setBottom()
  465. /**
  466. * 下移一层zorder减小
  467. *
  468. * */
  469. private setBefore(): void {
  470. const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
  471. arr.forEach(it => {
  472. if (it.parent) {
  473. const min = it.zOrder;
  474. let temp = it.parent.children
  475. .map(child => {
  476. if (
  477. Number(child.zOrder.toFixed()) ==
  478. Number(min.toFixed())
  479. ) {
  480. return child.zOrder;
  481. }
  482. })
  483. .filter(c => c);
  484. temp = [...new Set(temp)];
  485. // 当前层有多个
  486. const index = temp.indexOf(it.zOrder);
  487. if (index <= 1) {
  488. // 当前item 索引为1时 将当前item放至第一个前边
  489. // @ts-ignore
  490. it.zOrder = temp[0] - this.radix;
  491. } else if (index > 1) {
  492. // 当前item 索引大于等于2,将当前item放至前两个中间
  493. // @ts-ignore
  494. it.zOrder = (temp[index - 1] + temp[index - 2]) / 2;
  495. }
  496. }
  497. });
  498. } // Function setAfter()
  499. /**
  500. * 上移一层zorder增加
  501. *
  502. * */
  503. private setAfter(): void {
  504. const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
  505. arr.forEach(it => {
  506. if (it.parent) {
  507. const max = it.zOrder;
  508. let temp = it.parent.children
  509. .map(child => {
  510. if (
  511. Number(child.zOrder.toFixed()) ==
  512. Number(max.toFixed())
  513. ) {
  514. return child.zOrder;
  515. }
  516. })
  517. .filter(c => c);
  518. temp = [...new Set(temp)].reverse();
  519. // 当前层有多个
  520. const index = temp.indexOf(it.zOrder);
  521. if (index <= 1) {
  522. // 当前item 索引为1时 将当前item放至第一个前边
  523. // @ts-ignore
  524. it.zOrder = temp[0] + this.radix;
  525. } else if (index > 1) {
  526. // 当前item 索引大于等于2,将当前item放至前两个中间
  527. // @ts-ignore
  528. it.zOrder = (temp[index - 1] + temp[index - 2]) / 2;
  529. }
  530. }
  531. });
  532. } // Function setBefore()
  533. /**
  534. * 数组排序
  535. *
  536. * @param arr 排序前的数组
  537. * @param flag 正序or逆序
  538. * @return list 排序后的数组
  539. * */
  540. sortItemList(arr: SGraphItem[], flag: boolean = true): SGraphItem[] {
  541. const list: SGraphItem[] = arr.map(t => t);
  542. list.sort(this.compare("zOrder", flag));
  543. return list;
  544. } // Function sortItemList()
  545. /**
  546. * 按照某个属性排序
  547. *
  548. * @param prop 属性
  549. * @param flag 默认正序
  550. * */
  551. private compare(prop: string, flag: boolean) {
  552. const f = flag ? 1 : -1;
  553. // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  554. return (a: any, b: any) => {
  555. a = a[prop];
  556. b = b[prop];
  557. if (a < b) {
  558. return f * -1;
  559. }
  560. if (a > b) {
  561. return f * 1;
  562. }
  563. return 0;
  564. };
  565. } // Function compare()
  566. } // Class SGraphSelectContainer