|
| 1 | +import { ResizeObserver as Polyfill } from 'https://cdn.skypack.dev/@juggle/resize-observer' |
| 2 | +import parseStylesheet from 'https://cdn.skypack.dev/parse-css-stylesheet' |
| 3 | +import { injectStyle } from 'https://cdn.skypack.dev/styl-injector' |
| 4 | +import { uid } from 'https://cdn.skypack.dev/uid' |
| 5 | +import { Grid } from 'https://unpkg.com/gridjs@5.0.1/dist/gridjs.module.js' |
| 6 | + |
| 7 | +const waitForSelector = selector => { |
| 8 | + const element = document.querySelector(selector) |
| 9 | + if (element) return element |
| 10 | + |
| 11 | + const mutObserver = new MutationObserver(mutations => { |
| 12 | + for (const mutation of mutations) { |
| 13 | + const nodes = Array.from(mutation.addedNodes) |
| 14 | + for (const node of nodes) { |
| 15 | + if (node.matches && node.matches(selector)) { |
| 16 | + mutObserver.disconnect() |
| 17 | + return node |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + mutObserver.observe(document.documentElement, { childList: true, subtree: true }) |
| 24 | +} |
| 25 | + |
| 26 | +export default { |
| 27 | + name: 'Grid', |
| 28 | + props: { |
| 29 | + autoWidth: { |
| 30 | + type: Boolean, |
| 31 | + default: true |
| 32 | + }, |
| 33 | + className: { |
| 34 | + type: Object, |
| 35 | + default: undefined |
| 36 | + }, |
| 37 | + columns: { |
| 38 | + type: [Array, Function], |
| 39 | + default: undefined |
| 40 | + }, |
| 41 | + fixedHeader: { |
| 42 | + type: Boolean, |
| 43 | + default: false |
| 44 | + }, |
| 45 | + from: { |
| 46 | + type: [String, Function], |
| 47 | + default: undefined |
| 48 | + }, |
| 49 | + height: { |
| 50 | + type: String, |
| 51 | + default: undefined |
| 52 | + }, |
| 53 | + language: { |
| 54 | + type: Object, |
| 55 | + default: undefined |
| 56 | + }, |
| 57 | + pagination: { |
| 58 | + type: [Object, Boolean], |
| 59 | + default: false |
| 60 | + }, |
| 61 | + resizable: { |
| 62 | + type: Boolean, |
| 63 | + default: false |
| 64 | + }, |
| 65 | + rows: { |
| 66 | + type: [Array, Function], |
| 67 | + default: undefined |
| 68 | + }, |
| 69 | + search: { |
| 70 | + type: [Object, Boolean], |
| 71 | + default: false |
| 72 | + }, |
| 73 | + server: { |
| 74 | + type: [Object, Function], |
| 75 | + default: undefined |
| 76 | + }, |
| 77 | + sort: { |
| 78 | + type: [Object, Boolean], |
| 79 | + default: false |
| 80 | + }, |
| 81 | + styles: { |
| 82 | + type: Object, |
| 83 | + default: undefined |
| 84 | + }, |
| 85 | + theme: { |
| 86 | + type: String, |
| 87 | + default: undefined |
| 88 | + }, |
| 89 | + width: { |
| 90 | + type: String, |
| 91 | + default: '100%' |
| 92 | + } |
| 93 | + }, |
| 94 | + data() { |
| 95 | + return { |
| 96 | + grid: null, |
| 97 | + resize: null, |
| 98 | + uuid: null, |
| 99 | + wrapper: null |
| 100 | + } |
| 101 | + }, |
| 102 | + computed: { |
| 103 | + options() { |
| 104 | + let options = { |
| 105 | + autoWidth: this.autoWidth, |
| 106 | + fixedHeader: this.fixedHeader, |
| 107 | + pagination: this.pagination, |
| 108 | + resizable: this.resizable, |
| 109 | + sort: this.sort, |
| 110 | + width: this.width |
| 111 | + } |
| 112 | + |
| 113 | + if (this.columns) options.columns = this.columns |
| 114 | + if (this.rows) options.data = this.rows |
| 115 | + if (this.className) options.className = this.className |
| 116 | + if (this.from) |
| 117 | + options.from = |
| 118 | + typeof this.from === 'string' |
| 119 | + ? document.querySelector(this.from) |
| 120 | + : document.createRange().createContextualFragment(this.from()) |
| 121 | + if (this.height) options.height = this.height |
| 122 | + if (this.language) options.language = this.language |
| 123 | + if (this.search) options.search = this.search |
| 124 | + if (this.server) options.server = this.server |
| 125 | + if (this.styles) options.style = this.styles |
| 126 | + |
| 127 | + return options |
| 128 | + }, |
| 129 | + activeTheme() { |
| 130 | + if (this.theme) return this.theme |
| 131 | + if (this.$gridjs.options.theme) return this.$gridjs.options.theme |
| 132 | + return 'mermaid' |
| 133 | + }, |
| 134 | + divId() { |
| 135 | + return `gridjs__${this.uuid}` |
| 136 | + } |
| 137 | + }, |
| 138 | + watch: { |
| 139 | + autoWidth() { |
| 140 | + this.update() |
| 141 | + }, |
| 142 | + className() { |
| 143 | + this.update() |
| 144 | + }, |
| 145 | + columns() { |
| 146 | + this.update() |
| 147 | + }, |
| 148 | + fixedHeader() { |
| 149 | + this.update() |
| 150 | + }, |
| 151 | + from() { |
| 152 | + this.update() |
| 153 | + }, |
| 154 | + height() { |
| 155 | + this.update() |
| 156 | + }, |
| 157 | + language() { |
| 158 | + this.update() |
| 159 | + }, |
| 160 | + pagination() { |
| 161 | + this.update() |
| 162 | + }, |
| 163 | + resizable() { |
| 164 | + this.update() |
| 165 | + }, |
| 166 | + rows() { |
| 167 | + this.update() |
| 168 | + }, |
| 169 | + search() { |
| 170 | + this.update() |
| 171 | + }, |
| 172 | + server() { |
| 173 | + this.update() |
| 174 | + }, |
| 175 | + sort() { |
| 176 | + this.update() |
| 177 | + }, |
| 178 | + style() { |
| 179 | + this.update() |
| 180 | + }, |
| 181 | + theme() { |
| 182 | + this.update() |
| 183 | + }, |
| 184 | + width() { |
| 185 | + this.update() |
| 186 | + } |
| 187 | + }, |
| 188 | + async created() { |
| 189 | + try { |
| 190 | + // give table a unique id |
| 191 | + this.uuid = uid(16) |
| 192 | + |
| 193 | + // get the wrapper |
| 194 | + await waitForSelector(this.divId) |
| 195 | + this.wrapper = document.getElementById(this.divId) |
| 196 | + |
| 197 | + // instantiate grid.js |
| 198 | + if (Object.keys(this.$gridjs.options).length) this.setOptions() |
| 199 | + if (this.wrapper && (this.options.data || this.options.from || this.options.server)) { |
| 200 | + this.grid = new Grid(this.options).render(this.wrapper) |
| 201 | + } |
| 202 | + } catch (error) { |
| 203 | + console.error(error) |
| 204 | + } |
| 205 | + }, |
| 206 | + mounted() { |
| 207 | + this.assignTheme() |
| 208 | + const ResizeObserver = window.ResizeObserver || Polyfill |
| 209 | + this.resize = new ResizeObserver(() => { |
| 210 | + this.rerender() |
| 211 | + }) |
| 212 | + this.resize.observe(this.$refs[this.uuid]) |
| 213 | + this.$nextTick(() => this.$emit('ready')) |
| 214 | + }, |
| 215 | + destroyed() { |
| 216 | + // unload from memory |
| 217 | + this.resize.disconnect() |
| 218 | + this.grid = undefined |
| 219 | + this.wrapper = undefined |
| 220 | + }, |
| 221 | + methods: { |
| 222 | + async assignTheme() { |
| 223 | + try { |
| 224 | + if (this.activeTheme !== 'none') { |
| 225 | + let theme, |
| 226 | + stylesheet = '' |
| 227 | + |
| 228 | + theme = await (await fetch(`https://unpkg.com/gridjs/dist/theme/${this.activeTheme}.css`)).text() |
| 229 | + theme = parseStylesheet(theme) |
| 230 | + |
| 231 | + for (const index in theme.cssRules) { |
| 232 | + let css = theme.cssRules[index].cssText |
| 233 | + if (css && !/^@/g.test(css)) { |
| 234 | + stylesheet = `${stylesheet}\n\n#${this.divId} ${css}` |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + injectStyle(stylesheet, `${this.divId}__theme`) |
| 239 | + } |
| 240 | + } catch (error) { |
| 241 | + console.error(error) |
| 242 | + } |
| 243 | + }, |
| 244 | + setOptions() { |
| 245 | + try { |
| 246 | + const plugins = this.$gridjs.options.plugins |
| 247 | + if (plugins) { |
| 248 | + plugins.forEach(plugin => { |
| 249 | + this.grid.plugin.add(plugin) |
| 250 | + }) |
| 251 | + } |
| 252 | + } catch (error) { |
| 253 | + console.error(error) |
| 254 | + } |
| 255 | + }, |
| 256 | + rerender() { |
| 257 | + if (this.grid) this.grid.forceRender() |
| 258 | + }, |
| 259 | + update() { |
| 260 | + try { |
| 261 | + if (this.grid) this.grid.updateConfig(this.options).forceRender() |
| 262 | + } catch (error) { |
| 263 | + console.error(error) |
| 264 | + } |
| 265 | + } |
| 266 | + }, |
| 267 | + template: ` |
| 268 | + <article :id="divId" :data-uuid="uuid" :ref="uuid"></article> |
| 269 | + ` |
| 270 | +} |
0 commit comments