Skip to content

Commit e73a286

Browse files
committed
fix #63
1 parent 1e76d7a commit e73a286

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

src/gridjs-vue.mjs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Grid } from 'https://unpkg.com/[email protected]/dist/gridjs.module.js'
2-
import { injectStyle } from 'https://unpkg.com/[email protected]/dist/es2015/index.js'
2+
import parseStylesheet from 'https://unpkg.com/parse-css-stylesheet/index.js'
33
import { uid } from 'https://unpkg.com/uid/single/index.mjs'
44

55
const waitForSelector = selector => {
@@ -70,7 +70,7 @@ export default {
7070
},
7171
theme: {
7272
type: String,
73-
default: 'mermaid'
73+
default: undefined
7474
},
7575
width: {
7676
type: String,
@@ -79,7 +79,6 @@ export default {
7979
},
8080
data() {
8181
return {
82-
activeTheme: null,
8382
dict: {
8483
error: {
8584
columnsUndefined: 'Column headers are undefined',
@@ -120,6 +119,11 @@ export default {
120119

121120
return options
122121
},
122+
activeTheme() {
123+
if (this.theme) return this.theme
124+
if (this.$gridjs.options.theme) return this.$gridjs.options.theme
125+
return 'mermaid'
126+
},
123127
divId() {
124128
return `gridjs__${this.uuid}`
125129
}
@@ -158,6 +162,9 @@ export default {
158162
styles() {
159163
this.update()
160164
},
165+
theme() {
166+
this.update()
167+
},
161168
width() {
162169
this.update()
163170
}
@@ -167,22 +174,22 @@ export default {
167174
// give table a unique id
168175
this.uuid = uid(16)
169176

177+
// get the wrapper
170178
await waitForSelector(this.divId)
171179
this.wrapper = document.getElementById(this.divId)
172180

173-
// assign styles
174-
this.activeTheme = !this.theme && this.$gridjs.options.theme ? this.$gridjs.options.theme : this.theme
175-
if (this.activeTheme !== 'none') this.assignTheme()
176-
if (Object.keys(this.$gridjs.options).length) this.setOptions()
177-
178181
// instantiate grid.js
182+
if (Object.keys(this.$gridjs.options).length) this.setOptions()
179183
if (this.wrapper && (this.options.data || this.options.from || this.options.server)) {
180184
this.grid = new Grid(this.options).render(this.wrapper)
181185
}
182186
} catch (error) {
183187
console.error(error)
184188
}
185189
},
190+
mounted() {
191+
this.assignTheme()
192+
},
186193
destroyed() {
187194
// unload from memory
188195
this.grid = undefined
@@ -191,16 +198,30 @@ export default {
191198
methods: {
192199
async assignTheme() {
193200
try {
194-
const head = document.getElementsByTagName('head')[0]
201+
if (this.activeTheme !== 'none') {
202+
const head = document.getElementsByTagName('head')[0]
203+
204+
let styles = document.createElement('style')
205+
styles.title = `${this.divId}__theme`
206+
styles.type = 'text/css'
207+
head.appendChild(styles)
195208

196-
let styles = document.createElement('style')
197-
styles.id = `${this.divId}__theme`
198-
styles.type = 'text/css'
199-
head.appendChild(styles)
209+
let theme = await fetch(`https://unpkg.com/gridjs/dist/theme/${this.activeTheme}.css`)
210+
theme = parseStylesheet(await theme.text())
200211

201-
let theme = await fetch(`https://unpkg.com/gridjs/dist/theme/${this.activeTheme}.css`)
202-
theme = await theme.text()
203-
injectStyle(theme, styles.id)
212+
for (let index in document.styleSheets) {
213+
if (document.styleSheets[index].title === styles.title) {
214+
styles = document.styleSheets[index]
215+
}
216+
}
217+
218+
for (const index in theme.cssRules) {
219+
let css = theme.cssRules[index].cssText
220+
if (css && !/^@/g.test(css)) {
221+
styles.insertRule(`#${this.divId} ${css}`)
222+
}
223+
}
224+
}
204225
} catch (error) {
205226
console.error(error)
206227
}

test/gridjs-vue-test-no-theme.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Grid } from '../dist/index.esm.js'
2+
3+
export default {
4+
name: 'GridTestNoTheme',
5+
components: {
6+
Grid
7+
},
8+
data() {
9+
return {
10+
cols: ['Permit Type', 'Capacity', 'Issued', 'Waiting', 'Notified', '%Sold'],
11+
rows: [
12+
['Commuter', 1000, 150, 20, 10, 15],
13+
['Disability', 50, 50, 0, 0, 100],
14+
['Employee', 100, 90, 0, 5, 90],
15+
['Guest Permit', 'N/A', 492, 5, 3, 'N/A'],
16+
['Night', 60, 32, 0, 0, 0.53],
17+
['One Day', 'N/A', 101, 8, 3, 'N/A'],
18+
['Resident', 250, 250, 0, 0, 100],
19+
['Temporary', 'N/A', 23, 34, 12, 'N/A']
20+
]
21+
}
22+
},
23+
template: `
24+
<div><grid :cols="cols" :rows="rows" :search="true" :sort="true" theme="none"></grid></div>
25+
`
26+
}

test/test.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99

1010
<script type="module">
1111
import GridTest from './gridjs-vue-test.mjs'
12+
import GridTestNoTheme from './gridjs-vue-test-no-theme.mjs'
1213

1314
var app = new Vue({
1415
el: '#app',
1516
name: 'GridTestApp',
1617
components: {
17-
GridTest
18+
GridTest,
19+
GridTestNoTheme
1820
},
1921
template: `
22+
<div>
2023
<grid-test></grid-test>
24+
25+
<grid-test-no-theme></grid-test-no-theme>
26+
</div>
2127
`
2228
})
2329
</script>

0 commit comments

Comments
 (0)