1
1
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'
3
3
import { uid } from 'https://unpkg.com/uid/single/index.mjs'
4
4
5
5
const waitForSelector = selector => {
@@ -70,7 +70,7 @@ export default {
70
70
} ,
71
71
theme : {
72
72
type : String ,
73
- default : 'mermaid'
73
+ default : undefined
74
74
} ,
75
75
width : {
76
76
type : String ,
@@ -79,7 +79,6 @@ export default {
79
79
} ,
80
80
data ( ) {
81
81
return {
82
- activeTheme : null ,
83
82
dict : {
84
83
error : {
85
84
columnsUndefined : 'Column headers are undefined' ,
@@ -120,6 +119,11 @@ export default {
120
119
121
120
return options
122
121
} ,
122
+ activeTheme ( ) {
123
+ if ( this . theme ) return this . theme
124
+ if ( this . $gridjs . options . theme ) return this . $gridjs . options . theme
125
+ return 'mermaid'
126
+ } ,
123
127
divId ( ) {
124
128
return `gridjs__${ this . uuid } `
125
129
}
@@ -158,6 +162,9 @@ export default {
158
162
styles ( ) {
159
163
this . update ( )
160
164
} ,
165
+ theme ( ) {
166
+ this . update ( )
167
+ } ,
161
168
width ( ) {
162
169
this . update ( )
163
170
}
@@ -167,22 +174,22 @@ export default {
167
174
// give table a unique id
168
175
this . uuid = uid ( 16 )
169
176
177
+ // get the wrapper
170
178
await waitForSelector ( this . divId )
171
179
this . wrapper = document . getElementById ( this . divId )
172
180
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
-
178
181
// instantiate grid.js
182
+ if ( Object . keys ( this . $gridjs . options ) . length ) this . setOptions ( )
179
183
if ( this . wrapper && ( this . options . data || this . options . from || this . options . server ) ) {
180
184
this . grid = new Grid ( this . options ) . render ( this . wrapper )
181
185
}
182
186
} catch ( error ) {
183
187
console . error ( error )
184
188
}
185
189
} ,
190
+ mounted ( ) {
191
+ this . assignTheme ( )
192
+ } ,
186
193
destroyed ( ) {
187
194
// unload from memory
188
195
this . grid = undefined
@@ -191,16 +198,30 @@ export default {
191
198
methods : {
192
199
async assignTheme ( ) {
193
200
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 )
195
208
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 ( ) )
200
211
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
+ }
204
225
} catch ( error ) {
205
226
console . error ( error )
206
227
}
0 commit comments