SBaseEquipment.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, SAnchorItem } from "@persagy-web/graph/lib";
  27. import { Legend } from "./../types/Legend";
  28. import { SBaseIconTextEdit, SBaseTextEdit } from "@persagy-web/edit";
  29. import { SColor } from '@persagy-web/draw/lib';
  30. import { svgTobase64, uuid } from "./../until"
  31. /**
  32. * 编辑基础设备类
  33. *
  34. * @author 韩耀龙 <han_yao_long@163.com>
  35. */
  36. export class SBaseEquipment extends SBaseIconTextEdit {
  37. /** 图例节点 id */
  38. nodeId: string | null = null;
  39. /** 设备图例 */
  40. _legendData: Legend | null = null;
  41. set legendData(val) {
  42. this._legendData = val;
  43. this.initData()
  44. } // set legendData()
  45. get legendData(): Legend | null {
  46. return this._legendData
  47. } // get legendData()
  48. /** 信息点存储 */
  49. _infoPointList: any[] = []
  50. set infoPointList(val) {
  51. this._infoPointList = val;
  52. } // set infoPointList()
  53. get infoPointList(): any[] {
  54. return this._infoPointList
  55. } // get infoPointList()
  56. defaultUrl: string = '';
  57. /** 图得url */
  58. _url: string = '';
  59. set url(val) {
  60. this._url = val;
  61. this.initUrl()
  62. } // set infoPointList()
  63. get url(): string {
  64. return this._url
  65. } // get infoPointList()
  66. // 设备附加数据
  67. anotherMsg: string = ''
  68. /**
  69. * 构造函数
  70. *
  71. * @param parent 指向父对象
  72. * @param data 数据
  73. */
  74. constructor(parent: SGraphItem | null, data: Legend) {
  75. super(parent);
  76. this.legendData = data;
  77. } // Constructor
  78. /**
  79. * 设置 legendData 时对 item 做设置
  80. */
  81. initData() {
  82. if (!this.legendData) return;
  83. this.nodeId = this.legendData?.nodeId
  84. if (this.legendData.size) {
  85. this.sWidth = this.legendData.size.width;
  86. this.sHeight = this.legendData.size.height;
  87. }
  88. this.img.connect("onMove", this, this.changeAnchorPoint.bind(this));
  89. if (this.legendData.anchorList && this.legendData.anchorList.length) {
  90. this.anchorList = this.legendData.anchorList.map(t => {
  91. let item = new SAnchorItem(this);
  92. if (t.anchorId) {
  93. item.id = t.anchorId;
  94. }
  95. item.moveTo(t.pos.x, t.pos.y);
  96. return item;
  97. });
  98. } else {
  99. const anchorPoint = [
  100. { x: this.img.x, y: this.img.y, anchorId: uuid() },
  101. ];
  102. this.anchorList = anchorPoint.map(t => {
  103. let item = new SAnchorItem(this);
  104. if (t.anchorId) {
  105. item.id = t.anchorId;
  106. }
  107. item.moveTo(t.x, t.y);
  108. return item;
  109. });
  110. }
  111. // 存储信息点
  112. if (this.legendData.properties && this.legendData.properties.infoPointList) {
  113. const infoPointList = this.legendData.properties.infoPointList;
  114. if (infoPointList.length) {
  115. this.infoPointList = infoPointList;
  116. this.infoPointList.forEach((obj, i) => {
  117. this.setTextItem(obj, i)
  118. })
  119. } else {
  120. this.infoPointList = []
  121. }
  122. } else {
  123. this.infoPointList = []
  124. }
  125. this.showAnchor = false;
  126. // this.url = this.legendData.style.default.base64Url ? this.legendData.style.default.base64Url : this.legendData.style.default.url
  127. this.anotherMsg = this.legendData.properties.anotherMsg ? this.legendData.properties.anotherMsg : '';
  128. this.x = this.legendData.pos.x;
  129. this.y = this.legendData.pos.y;
  130. this.moveable = true;
  131. this.selectable = true;
  132. }
  133. initUrl() {
  134. svgTobase64(this.url).then((res) => {
  135. this.img.url = res ? res : '';
  136. }).catch((res) => {
  137. this.img.url = res;
  138. })
  139. }
  140. /**
  141. * 设置信息点
  142. *
  143. * @params obj 设置的信息点
  144. */
  145. setMsgPoint(val: any) {
  146. // 根据是否勾选来判断是够增加或删除
  147. if (val.checked) {
  148. this._infoPointList.push(val);
  149. this.setTextItem(val, (this._infoPointList.length - 1))
  150. } else {
  151. let deleteItemIndex: number = -1;
  152. this._infoPointList.forEach((item, index) => {
  153. if (val.id == item.id) {
  154. deleteItemIndex = index;
  155. }
  156. });
  157. this._infoPointList.splice(deleteItemIndex, 1);
  158. this.setTextItem(val, deleteItemIndex)
  159. }
  160. }
  161. /**
  162. * 根据选中状态增加 textItem
  163. *
  164. * @params obj textItem 文本信息
  165. * @params index 文本索引
  166. */
  167. setTextItem(val: any, index: number): void {
  168. if (val.checked) {
  169. let item = new SBaseTextEdit(this, null);
  170. item.propertyData = val;
  171. item.text = val.name;
  172. if (val.pos) {
  173. item.moveTo(val.pos.x, val.pos.y)
  174. } else {
  175. item.moveTo(
  176. this.img.width * 0.5,
  177. -(this.font.size * 1.25 * 0.5) + (index) * 10
  178. );
  179. }
  180. item.font.size = val.font ? val.font : 12;
  181. item.isTransform = false;
  182. item.showSelect = false;
  183. item.color = val.color ? new SColor(val.color) : new SColor('#000000');
  184. item.connect('textSelect', this, this.textSelectChange)
  185. this.textItemList.push(item)
  186. } else {
  187. this.textItemList.splice(index, 1)
  188. this.scene?.removeItem(this.textItemList[index]);
  189. }
  190. }
  191. /**
  192. * 返回对象储存的相关数据
  193. *
  194. * @return 对象储存的相关数据
  195. */
  196. toData(): any {
  197. if (this.legendData) {
  198. const list = this.anchorList.map((item) => {
  199. return {
  200. anchorId: item.id,
  201. pos: {
  202. x: item.x,
  203. y: item.y
  204. }
  205. }
  206. })
  207. this.legendData.anchorList = list;
  208. // 反馈大小
  209. if (this.legendData.size) {
  210. this.legendData.size.width = this.sWidth;
  211. this.legendData.size.height = this.sHeight
  212. } else {
  213. this.legendData.size = {
  214. width: this.sWidth,
  215. height: this.sHeight
  216. }
  217. }
  218. // 位置
  219. this.legendData.pos = {
  220. x: this.x,
  221. y: this.y
  222. };
  223. // 存储信息点
  224. const infoPoinList: any[] = []
  225. this.textItemList.forEach(item => {
  226. let obj = Object.assign(item.propertyData, {
  227. pos: { x: item.x, y: item.y },
  228. font: item.font.size,
  229. color: item.color.value
  230. })
  231. delete obj.currentEquipMsg
  232. infoPoinList.push(obj)
  233. });
  234. this.legendData.properties.infoPointList = infoPoinList;
  235. this.legendData.properties.anotherMsg = this.anotherMsg;
  236. this.legendData.style.default.base64Url = "";
  237. if (this.defaultUrl) {
  238. this.legendData.style.default.url = this.defaultUrl;
  239. }
  240. return this.legendData
  241. }
  242. }
  243. }