11import { Grid } from 'https://unpkg.com/gridjs@5.0.1/dist/gridjs.module.js'
2- import { injectStyle } from 'https://unpkg.com/styl-injector@1.4.0/dist/es2015 /index.js'
2+ import parseStylesheet from 'https://unpkg.com/parse-css-stylesheet /index.js'
33import { uid } from 'https://unpkg.com/uid/single/index.mjs'
44
55const 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 }
0 commit comments