index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. console.log(sheets)
  83. let stylesheets = document.styleSheets[0]
  84. if (stylesheets.href) {
  85. let head = document.head || document.getElementsByTagName('head')[0];
  86. let style = document.createElement('style');
  87. style.type = 'text/css';
  88. head.appendChild(style);
  89. stylesheets = style.sheet || style.styleSheet;
  90. }
  91. let index = 0
  92. while (stylesheets.cssRules.length > index) {
  93. if (stylesheets.cssRules[index].selectorText && stylesheets.cssRules[index].selectorText.indexOf(head) === 0) {
  94. stylesheets.deleteRule(index)
  95. } else {
  96. index++
  97. }
  98. }
  99. for (let i = 0; i < sheets.length; i++) {
  100. console.log(head + sheets[i])
  101. stylesheets.insertRule(head + sheets[i], 0)
  102. }
  103. }
  104. export const clearStyleSheets = (head) => {
  105. let stylesheets = document.styleSheets[0]
  106. if (stylesheets.href) {
  107. return false
  108. }
  109. let index = 0
  110. while (stylesheets.cssRules.length > index) {
  111. if (stylesheets.cssRules[index].selectorText && stylesheets.cssRules[index].selectorText.indexOf(head) === 0) {
  112. stylesheets.deleteRule(index)
  113. } else {
  114. index++
  115. }
  116. }
  117. }
  118. export const fixDraggbleList = (list, newIndex) => { // 处理拖拽过程中,列表值和返回的newIndex不一致问题
  119. const listIndex = list.findIndex(item => !item.key)
  120. if (listIndex >= 0) {
  121. list.splice(newIndex, 0, list.splice(listIndex, 1)[0])
  122. }
  123. }
  124. export const getBindModels = (models, binds) => {
  125. let resData = {}
  126. Object.keys(binds).forEach(item => {
  127. if (typeof binds[item] === 'object' && models[item]) {
  128. if (Array.isArray(models[item])) {
  129. resData[item] = models[item].map(mitem => {
  130. return getBindModels(mitem, binds[item])
  131. })
  132. } else {
  133. resData[item] = getBindModels(models[item], binds[item])
  134. }
  135. } else {
  136. resData[item] = models[item]
  137. }
  138. })
  139. return resData
  140. }
  141. export const addClass = (el, className) => {
  142. el.classList.add(className)
  143. }
  144. export const removeClass = (el, className) => {
  145. el.classList.remove(className)
  146. }
  147. export const consoleError = (msg) => {
  148. Function(`
  149. window.console && console.error('[FormMaking error]: ${msg}');
  150. `)()
  151. }