index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. export const loadJs = (url) => {
  2. return new Promise((resolve, reject) => {
  3. const script = document.createElement('script')
  4. script.src = url
  5. script.type = 'text/javascript'
  6. document.body.appendChild(script)
  7. script.onload = () => {
  8. resolve()
  9. }
  10. })
  11. }
  12. export const loadCss = (url) => {
  13. return new Promise((resolve, reject) => {
  14. const link = document.createElement('link')
  15. link.rel = 'stylesheet'
  16. link.href = url
  17. document.head.appendChild(link)
  18. link.onload = () => {
  19. resolve()
  20. }
  21. })
  22. }
  23. export const generateUUID = () => {
  24. var d = new Date().getTime();
  25. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  26. var r = (d + Math.random()*16)%16 | 0;
  27. d = Math.floor(d/16);
  28. return (c=='x' ? r : (r&0x7|0x8)).toString(16);
  29. });
  30. return uuid;
  31. }
  32. export const generateKeyToTD = (rows) => {
  33. for (let i = 0; i < rows.length; i++) {
  34. for (let j = 0; j < rows[i].columns.length; j++) {
  35. rows[i].columns[j].key = Math.random().toString(36).slice(-8)
  36. }
  37. }
  38. return rows
  39. }
  40. export const generateKeyToTH = (row) => {
  41. for (let i = 0; i < row.length; i++) {
  42. row[i].key = Math.random().toString(36).slice(-8)
  43. }
  44. return row
  45. }
  46. export const generateKeyToCol = (columns) => {
  47. for (let i = 0; i < columns.length; i++) {
  48. columns[i].key = Math.random().toString(36).slice(-8)
  49. }
  50. return columns
  51. }
  52. export const splitStyleSheets = (str) => {
  53. if (!str) {
  54. return []
  55. }
  56. let r = /}\s+./
  57. let arr = str.split(r).filter(item => item)
  58. return arr.map(sty => {
  59. sty = sty.trim()
  60. if (sty[0] !== '.') {
  61. sty = '.' + sty
  62. }
  63. if (sty[sty.length - 1] !== '}') {
  64. sty = sty + '}'
  65. }
  66. return sty
  67. })
  68. }
  69. export const splitSheetName = (sheets) => {
  70. return Array.from(new Set(sheets.map(sheet => {
  71. let spaceIndex = sheet.indexOf(' ')
  72. let nameIndex = sheet.indexOf('{')
  73. let index = nameIndex
  74. if (spaceIndex > 0 && spaceIndex < nameIndex) {
  75. index = spaceIndex
  76. }
  77. sheet = sheet.substring(1, index)
  78. return sheet
  79. })))
  80. }
  81. export const updateStyleSheets = (sheets, head) => {
  82. let stylesheets = document.styleSheets[0]
  83. if (stylesheets.href) {
  84. let head = document.head || document.getElementsByTagName('head')[0];
  85. let style = document.createElement('style');
  86. style.type = 'text/css';
  87. head.appendChild(style);
  88. stylesheets = style.sheet || style.styleSheet;
  89. }
  90. let index = 0
  91. while (stylesheets.cssRules.length > index) {
  92. if (stylesheets.cssRules[index].selectorText && stylesheets.cssRules[index].selectorText.indexOf(head) === 0) {
  93. stylesheets.deleteRule(index)
  94. } else {
  95. index++
  96. }
  97. }
  98. for (let i = 0; i < sheets.length; i++) {
  99. stylesheets.insertRule(head + sheets[i], 0)
  100. }
  101. }
  102. export const clearStyleSheets = (head) => {
  103. let stylesheets = document.styleSheets[0]
  104. if (stylesheets.href) {
  105. return false
  106. }
  107. let index = 0
  108. while (stylesheets.cssRules.length > index) {
  109. if (stylesheets.cssRules[index].selectorText && stylesheets.cssRules[index].selectorText.indexOf(head) === 0) {
  110. stylesheets.deleteRule(index)
  111. } else {
  112. index++
  113. }
  114. }
  115. }
  116. export const fixDraggbleList = (list, newIndex) => { // 处理拖拽过程中,列表值和返回的newIndex不一致问题
  117. const listIndex = list.findIndex(item => !item.key)
  118. if (listIndex >= 0) {
  119. list.splice(newIndex, 0, list.splice(listIndex, 1)[0])
  120. }
  121. }
  122. export const getBindModels = (models, binds) => {
  123. let resData = {}
  124. Object.keys(binds).forEach(item => {
  125. if (typeof binds[item] === 'object' && models[item]) {
  126. if (Array.isArray(models[item])) {
  127. resData[item] = models[item].map(mitem => {
  128. return getBindModels(mitem, binds[item])
  129. })
  130. } else {
  131. resData[item] = getBindModels(models[item], binds[item])
  132. }
  133. } else {
  134. resData[item] = models[item]
  135. }
  136. })
  137. return resData
  138. }
  139. export const addClass = (el, className) => {
  140. el.classList.add(className)
  141. }
  142. export const removeClass = (el, className) => {
  143. el.classList.remove(className)
  144. }
  145. export const consoleError = (msg) => {
  146. Function(`
  147. window.console && console.error('[FormMaking error]: ${msg}');
  148. `)()
  149. }