tools.js 12 KB

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