Skip to content

Commit 133f008

Browse files
committed
create bundled versions, move browser versions
Signed-off-by: Daniel Sieradski <daniel@self.agency>
1 parent 0145953 commit 133f008

File tree

10 files changed

+455
-1437
lines changed

10 files changed

+455
-1437
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ issues](https://img.shields.io/github/issues/grid-js/gridjs-vue?color=41B883&sty
1818
npm install gridjs-vue
1919
```
2020

21-
Also available on [unpkg](https://unpkg.com/browse/gridjs-vue@5.0.1/dist/) and [Skypack](https://www.skypack.dev/view/gridjs-vue)!
21+
Also available on [unpkg](https://unpkg.com/browse/gridjs-vue/dist/) and [Skypack](https://www.skypack.dev/view/gridjs-vue)!
2222

2323
```html
2424
<script>
25-
import Grid from 'gridjs-vue'
25+
import { Grid } from 'gridjs-vue'
2626
2727
export default {
2828
components: {

bili.config.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module.exports = {
22
jsCompiler: 'babel',
3-
input: 'src/index.js',
3+
input: ['src/index.browser.mjs', 'src/index.mjs'],
4+
bundleNodeModules: true,
45
output: {
56
format: ['cjs', 'esm', 'umd'],
6-
bundleNodeModules: true,
77
minify: true,
88
moduleName: 'Grid',
99
extractCSS: false
@@ -13,15 +13,12 @@ module.exports = {
1313
include: 'node_modules/**/*.css'
1414
},
1515
vue: {
16-
target: 'browser',
17-
babelHelopers: 'runtime',
18-
css: true
16+
target: 'browser'
1917
},
2018
babel: {
2119
presets: ['vue', ['@babel/preset-env', { useBuiltIns: 'usage', corejs: 3 }]],
2220
plugins: ['@babel/plugin-transform-runtime'],
2321
babelHelpers: 'runtime',
24-
exclude: 'node_modules/**',
2522
configFile: false
2623
},
2724
'node-resolve': true

examples/basic/loading-state.mjs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ export default {
1010
columns: ['Name', 'Email', 'Phone Number'],
1111
sort: true,
1212
search: true,
13-
rows:() => {
13+
rows: () => {
1414
return new Promise(resolve => {
15-
setTimeout(() =>
16-
resolve([
17-
['John', 'john@example.com', '(353) 01 222 3333'],
18-
['Mark', 'mark@gmail.com', '(01) 22 888 4444']
19-
]), 2000);
15+
setTimeout(
16+
() =>
17+
resolve([
18+
['John', 'john@example.com', '(353) 01 222 3333'],
19+
['Mark', 'mark@gmail.com', '(01) 22 888 4444']
20+
]),
21+
2000
22+
)
2023
})
2124
}
2225
}

examples/customizing/vue-component-in-cells.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default {
1313
{
1414
name: 'Name',
1515
formatter: cell =>
16-
return this.$gridjs.helper({
16+
this.$gridjs.helper({
1717
components: { TestComponent },
1818
template: `<test-component :content="content"></test-component>`,
1919
data() {

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"module": "./dist/index.esm.js",
2020
"scripts": {
2121
"dev": "baechli -c 'yarn build'",
22-
"build": "rm -rf dist/*; bili src/index.mjs;"
22+
"build": "rm -rf dist/*; bili;"
2323
},
2424
"files": [
2525
"dist/*",
@@ -35,6 +35,8 @@
3535
"@babel/core": "^7.14.3",
3636
"@babel/plugin-transform-runtime": "^7.14.3",
3737
"@babel/preset-env": "^7.14.4",
38+
"@juggle/resize-observer": "^3.3.1",
39+
"@lesniewski.pro/necktie": "^1.1.3",
3840
"@release-it/keep-a-changelog": "^2.3.0",
3941
"@vue/compiler-sfc": "^3.1.1",
4042
"@vue/eslint-config-prettier": "^6.0.0",
@@ -50,18 +52,20 @@
5052
"eslint-plugin-import": "^2.23.4",
5153
"eslint-plugin-prettier": "^3.4.0",
5254
"eslint-plugin-vue": "^7.10.0",
55+
"gridjs": "5.0.1",
56+
"parse-css-stylesheet": "^0.0.1",
5357
"prettier": "^2.3.1",
54-
"release-it": "^14.8.0",
5558
"rollup": "^2.51.1",
5659
"rollup-plugin-babel": "^4.4.0",
5760
"rollup-plugin-commonjs": "^10.1.0",
5861
"rollup-plugin-node-resolve": "^5.2.0",
5962
"rollup-plugin-string": "^3.0.0",
6063
"rollup-plugin-vue": "^6.0.0",
64+
"styl-injector": "^1.4.0",
6165
"typescript": "^4.3.2",
66+
"uid": "^2.0.0",
6267
"vue": "^2.6.11",
6368
"vue-runtime-helpers": "1.1.2",
6469
"vue-template-compiler": "^2.6.14"
65-
},
66-
"dependencies": {}
70+
}
6771
}

src/gridjs-vue.browser.mjs

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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

Comments
 (0)