tools.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. import Handsontable from "handsontable-pro"
  2. const tools = {}
  3. let arrX = []
  4. let arrY = []
  5. tools.getPointDetails = (arr) => {
  6. arr.map(item => {
  7. if (item.PointList && item.PointList.length) {
  8. item.PointList.map(em => {
  9. arrX.push(em.X)
  10. arrY.push(em.Y)
  11. })
  12. }
  13. })
  14. }
  15. /**
  16. *
  17. * @param {*} obj 将point标签的x,y进行比对
  18. * @return object 返回数组,其中包括最大值最小值X,Y
  19. */
  20. tools.getPoint = function(obj) {
  21. arrX = []
  22. arrY = []
  23. for (let i in obj) {
  24. tools.getPointDetails(obj[i])
  25. }
  26. let maxX = Math.max.apply(null, arrX)
  27. let minX = Math.min.apply(null, arrX)
  28. let maxY = Math.max.apply(null, arrY)
  29. let minY = Math.min.apply(null, arrY)
  30. let data = {
  31. maxX,
  32. minX,
  33. maxY,
  34. minY
  35. }
  36. return data
  37. }
  38. /**
  39. *
  40. * @param {*} array 比较数组1
  41. * @param {*} array2 比较数组2
  42. *
  43. * 返回两个数组中不同的元素
  44. */
  45. tools.compareArr = function(array, array2) {
  46. var arr3 = [];
  47. for (key in array) {
  48. var stra = array[key];
  49. var count = 0;
  50. for (var j = 0; j < array2.length; j++) {
  51. var strb = array2[j];
  52. if (stra == strb) {
  53. count++;
  54. }
  55. }
  56. if (count === 0) { //表示数组1的这个值没有重复的,放到arr3列表中
  57. arr3.push(stra);
  58. }
  59. }
  60. return arr3;
  61. }
  62. /**
  63. *
  64. * @param {*} arr 需要改变的数组
  65. * @param {*} k 其中Y值需要变换的倍数
  66. * @param {*} name arr中的key值
  67. *
  68. * 返回arr map 后arr[mapIndex]name * k 的数组
  69. */
  70. tools.changeMap = function(arr, k, name) {
  71. let data = arr.map(items => {
  72. if (items[name] && items[name].length) {
  73. items[name].map(children => {
  74. if (Array.isArray(children)) {
  75. return children.map(res => {
  76. res.Y = res.Y * k
  77. return res
  78. })
  79. } else {
  80. children.Y = children.Y * k
  81. return children
  82. }
  83. })
  84. }
  85. })
  86. return data
  87. }
  88. tools.getMouseCanvas = function(view, e) {
  89. let bbox = view.canvasView.getBoundingClientRect();
  90. let x = e.clientX - bbox.left,
  91. y = e.clientY - bbox.top;
  92. return view.mapToScene({
  93. x: x,
  94. y: y
  95. });
  96. }
  97. /**
  98. *
  99. * @param {*} view graphy class
  100. * @param {*} e mouse元素e
  101. */
  102. tools.mouseInElement = function(view, e, type) {
  103. let mouse = tools.getMouseCanvas(view, e),
  104. falg = false,
  105. items = view.scene.root.children,
  106. i = 0,
  107. t = type || 3;
  108. for (; i < items.length; i++) {
  109. if (items[i].type == t) {
  110. if (items[i].contains(mouse)) {
  111. falg = true
  112. break
  113. }
  114. }
  115. }
  116. return {
  117. falg,
  118. item: items[i] || [],
  119. index: i
  120. }
  121. }
  122. /**
  123. *
  124. * @param {graphy中的所有children} items
  125. * @param {类型} type
  126. * @param {list} codes
  127. * @param {item中的code} code
  128. */
  129. tools.clear = function(items, type, codes, code) {
  130. let indexArr = [];
  131. if (items && items.length) {
  132. let i = 0;
  133. for (i; i < items.length; i++) {
  134. let j = 0;
  135. for (j; j < codes.length; j++) {
  136. if (items[i][type] = type && items[i][code] == codes[j][code]) {
  137. indexArr.push(i)
  138. }
  139. }
  140. }
  141. }
  142. return indexArr
  143. }
  144. tools.getText = function(arr, name) {
  145. let text = '';
  146. if (arr && arr.length) {
  147. arr.map(item => {
  148. text += (!!text ? '、' : '') + item[name]
  149. })
  150. }
  151. return text
  152. }
  153. tools.cutString = function(string, num, text) {
  154. return string.substring(0, num) + '...' + text
  155. }
  156. tools.deepCopy = function(obj) {
  157. var out = [],
  158. i = 0,
  159. len = obj.length;
  160. for (; i < len; i++) {
  161. if (obj[i] instanceof Array) {
  162. out[i] = tools.deepCopy(obj[i]);
  163. } else out[i] = obj[i];
  164. }
  165. return out;
  166. }
  167. tools.copyArr = (source) => {
  168. if (!source || typeof source !== 'object') {
  169. throw new Error('error arguments', 'shallowClone');
  170. }
  171. var targetObj = source.constructor === Array ? [] : {};
  172. for (var keys in source) {
  173. if (source.hasOwnProperty(keys)) {
  174. if (source[keys] && typeof source[keys] === 'object') {
  175. targetObj[keys] = source[keys].constructor === Array ? [] : {};
  176. targetObj[keys] = tools.copyArr(source[keys]);
  177. } else {
  178. targetObj[keys] = source[keys];
  179. }
  180. }
  181. }
  182. return targetObj;
  183. }
  184. tools.deepCopyObj = function(v) {
  185. var o = v.constructor === Array ? [] : {};
  186. for (var key in v) {
  187. o[key] = typeof v[key] === 'Object' ? tools.deepCopyObj(v[key]) : v[key];
  188. }
  189. return o;
  190. }
  191. tools.deepClone = (obj) => {
  192. var proto = Object.getPrototypeOf(obj);
  193. return Object.assign({}, Object.create(proto), obj);
  194. }
  195. //hansontable插件的修改
  196. tools.customDropdownRenderer = function(
  197. instance,
  198. td,
  199. row,
  200. col,
  201. prop,
  202. value,
  203. cellProperties
  204. ) {
  205. var selectedId;
  206. var optionsList = cellProperties.chosenOptions.data;
  207. if (
  208. typeof optionsList === "undefined" ||
  209. typeof optionsList.length === "undefined" ||
  210. !optionsList.length
  211. ) {
  212. Handsontable.renderers.TextRenderer(
  213. instance,
  214. td,
  215. row,
  216. col,
  217. prop,
  218. value,
  219. cellProperties
  220. );
  221. return td;
  222. }
  223. var values = (value + "").split(",");
  224. value = [];
  225. for (var index = 0; index < optionsList.length; index++) {
  226. if (optionsList[index].content && optionsList[index].content.length) {
  227. for (let i = 0; i < optionsList[index].content.length; i++) {
  228. if (
  229. optionsList[index].content[i] &&
  230. optionsList[index].content[i].length
  231. ) {
  232. for (let j = 0; j < optionsList[index].content[i].length; j++) {
  233. if (
  234. values.indexOf(
  235. optionsList[index].content[i].content[j].Code + ""
  236. ) > -1
  237. ) {
  238. selectedId = optionsList[index].content[i].content[j].Code;
  239. value.push(optionsList[index].content[i].content[j].Name);
  240. }
  241. }
  242. } else {
  243. if (
  244. values.indexOf(optionsList[index].content[i].Code + "") > -1
  245. ) {
  246. selectedId = optionsList[index].content[i].Code;
  247. value.push(optionsList[index].content[i].Name);
  248. }
  249. }
  250. }
  251. } else {
  252. if (values.indexOf(optionsList[index].Code + "") > -1) {
  253. selectedId = optionsList[index].Code;
  254. value.push(optionsList[index].Name);
  255. }
  256. }
  257. }
  258. value = value.join(", ");
  259. Handsontable.renderers.TextRenderer(
  260. instance,
  261. td,
  262. row,
  263. col,
  264. prop,
  265. value,
  266. cellProperties
  267. );
  268. return td;
  269. }
  270. //hansontable插件查看详情
  271. tools.lookDetails = function(instance, td, row, col, prop, value, cellProperties) {
  272. td.style.color = "#409EFF";
  273. td.style.cursor = "pointer";
  274. if (!!value) {
  275. if (typeof(value) == 'object') {
  276. td.innerHTML = "已有值,查看";
  277. } else {
  278. td.innerHTML = value
  279. }
  280. } else {
  281. td.innerHTML = "查看详情";
  282. }
  283. return td;
  284. }
  285. //是否关联
  286. tools.hasRelation = function(instance, td, row, col, prop, value, cellProperties) {
  287. if (value) {
  288. td.innerHTML = "已关联";
  289. } else {
  290. td.innerHTML = "未关联";
  291. }
  292. return td;
  293. }
  294. //关联的资产
  295. tools.LinkEquipLocalName = function(instance, td, row, col, prop, value, cellProperties) {
  296. if (value) {
  297. td.innerHTML = value;
  298. } else {
  299. td.innerHTML = instance.getDataAtRowProp(row,'LinkEquipName');
  300. }
  301. return td;
  302. }
  303. tools.num = function(instance, td, row, col, prop, value, cellProperties) {
  304. td.style.color = "#409EFF";
  305. td.style.cursor = "pointer";
  306. td.innerHTML = value;
  307. return td;
  308. }
  309. tools.setItem = (key, value) => {
  310. if (typeof value == 'string') {
  311. localStorage.setItem(key, value);
  312. } else {
  313. localStorage.setItem(key, JSON.stringify(value));
  314. }
  315. }
  316. tools.getItem = (key) => {
  317. const re = /^\[|\{|\}|\]$/g //判断字符中是否有[]{}
  318. let getIt = localStorage.getItem(key)
  319. if (re.test(getIt)) {
  320. return JSON.parse(getIt)
  321. } else {
  322. return getIt
  323. }
  324. }
  325. tools.removeItem = (key) => {
  326. localStorage.removeItem(key)
  327. }
  328. //取两个数组的差值
  329. tools.differenceArr = (a, b) => {
  330. return a.concat(b).filter(v => !a.includes(v) || !b.includes(v))
  331. }
  332. //取两个数组的相同值
  333. tools.sameArr = (a, b) => {
  334. return a.filter(v => b.includes(v))
  335. }
  336. tools.sortArr = (arr, key, sequence) => {
  337. function compare(property) {
  338. return function(a, b) {
  339. var value1 = a[property];
  340. var value2 = b[property];
  341. if (sequence) {
  342. return value1 - value2;
  343. } else {
  344. return value2 - value1
  345. }
  346. }
  347. }
  348. return arr.sort(compare(key))
  349. }
  350. tools.pagination = (pageNo, pageSize, array) => {
  351. let offset = (pageNo - 1) * pageSize;
  352. return offset + pageSize >= array.length ?
  353. array.slice(offset, array.length) :
  354. array.slice(offset, offset + pageSize);
  355. }
  356. tools.getSameItems = (arr1, key1, arr2, key2) => {
  357. let data = []
  358. arr1.map(item => {
  359. arr2.map(child => {
  360. if (item[key1] == child[key2]) {
  361. data.push(item)
  362. }
  363. })
  364. })
  365. return data
  366. }
  367. tools.isIn = (x, y, json) => {
  368. let nCross = 0,
  369. point = typeof(x) == 'object' ? [x.x, x.y] : [x, y],
  370. APoints = json,
  371. length = APoints.length,
  372. p1, p2, i, xinters;
  373. p1 = APoints[0];
  374. for (i = 1; i <= length; i++) {
  375. p2 = APoints[i % length];
  376. if (
  377. point[0] > Math.min(p1[0], p2[0]) &&
  378. point[0] <= Math.max(p1[0], p2[0])
  379. ) {
  380. if (point[1] <= Math.max(p1[1], p2[1])) {
  381. if (p1[0] != p2[0]) {
  382. //计算位置信息
  383. xinters = (point[0] - p1[0]) * (p2[1] - p1[1]) / (p2[0] - p1[0]) + p1[1];
  384. if (p1[1] == p2[1] || point[1] <= xinters) {
  385. nCross++
  386. }
  387. }
  388. }
  389. }
  390. p1 = p2;
  391. }
  392. if (nCross % 2 == 0) {
  393. return false
  394. } else {
  395. return true
  396. }
  397. }
  398. tools.arrayUnique = (arr, name) => {
  399. var hash = {};
  400. return arr.reduce(function(item, next) {
  401. hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
  402. return item;
  403. }, []);
  404. }
  405. tools.dateAddYear = function(date, yearCount) {
  406. var tempDate = dateToDate(date);
  407. var count = parseInt(yearCount);
  408. var oldYear = tempDate.getFullYear();
  409. var oldMonth = tempDate.getMonth();
  410. var oldDate = tempDate.getDate();
  411. var newYear = oldYear + count;
  412. var newDate = new Date(newYear, oldMonth, oldDate);
  413. //防止月份数不一致,进行微调
  414. while (newDate.getMonth() != oldMonth) {
  415. oldDate--;
  416. newDate = new Date(newYear, oldMonth, oldDate);
  417. }
  418. var month = newDate.getMonth() + 1;
  419. var day = newDate.getDate();
  420. month = (month.toString().length == 1) ? ("0" + month) : month;
  421. day = (day.toString().length == 1) ? ("0" + day) : day;
  422. var result = newDate.getFullYear() + '-' + month + '-' + day; //当前日期
  423. return result;
  424. }
  425. tools.formatDataSource = function (data) {
  426. let options
  427. let arr = []
  428. function recursion (data) {
  429. let arr = []
  430. data.map((item) =>{
  431. if (item.Code && item.Name)
  432. arr.push({ Code: item.Code, Name: item.Name })
  433. if (item.Content && item.Content.length)
  434. arr = arr.concat(recursion(item.Content))
  435. })
  436. return arr
  437. }
  438. try{
  439. options = JSON.parse(data)
  440. if(options) {
  441. return recursion(options)
  442. } else {
  443. return arr
  444. }
  445. }catch(err){
  446. return arr
  447. }
  448. }
  449. tools.isObjectValueEqual = function (a, b) {//判断两个对象的可枚举属性值是否相等(认为null,"",undefinde相等)
  450. //取对象a和b的属性名
  451. var aProps = Object.keys(a)
  452. var bProps = Object.keys(b)
  453. //判断属性名的length是否一致
  454. if (aProps.length != bProps.length) {
  455. return false
  456. }
  457. //循环取出属性名,再判断属性值是否一致
  458. for (var i = 0; i < aProps.length; i++) {
  459. var propName = aProps[i]
  460. if(!(a[propName] instanceof Object || b[propName] instanceof Object)) {
  461. if (!(a[propName] == b[propName] || (!a[propName] && !b[propName]))) {
  462. return false
  463. }
  464. } else {
  465. if(!tools.isObjectValueEqual(a[propName],b[propName])) {
  466. return false
  467. }
  468. }
  469. }
  470. return true
  471. }
  472. tools.dataForKey = function (data, key) {
  473. let arr = key.split(".")
  474. if (arr && arr[0]) {
  475. for (let i = 0; i < arr.length; i++) {
  476. if (arr[i] && data[arr[i]]) {
  477. data = data[arr[i]]
  478. } else {
  479. return ''
  480. }
  481. }
  482. return data
  483. }
  484. }
  485. tools.setDataForKey = function (data, key, value) {
  486. let arr = key.split(".")
  487. if (arr && arr[0]) {
  488. for (let i = 0; i < arr.length; i++) {
  489. if (!(arr[i] && data[arr[i]])) {
  490. data[arr[i]] = {}
  491. }
  492. if (value !== undefined) {
  493. if (i == arr.length - 1) {
  494. data[arr[i]] = value
  495. }
  496. }
  497. data = data[arr[i]]
  498. }
  499. }
  500. }
  501. function dateToDate(date) {
  502. var sDate = new Date();
  503. if (typeof date == 'object' && typeof new Date().getMonth == "function") {
  504. sDate = date;
  505. } else if (typeof date == "string") {
  506. var arr = date.split('-');
  507. if (arr.length == 3) {
  508. sDate = new Date(arr[0] + '-' + arr[1] + '-' + arr[2]);
  509. }
  510. }
  511. return sDate;
  512. }
  513. export default tools