-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deploying to package from @ be7e875 🚀
- Loading branch information
1 parent
32656ca
commit f32cce8
Showing
17 changed files
with
826 additions
and
824 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,138 @@ | ||
import { context } from './context.js' | ||
import { context } from './Context.js'; | ||
|
||
export default class Component | ||
{ | ||
constructor( id = 'svelte-simple-datatable' ) | ||
{ | ||
this.context = context.get(id) | ||
this.id = id | ||
} | ||
init() | ||
{ | ||
this.setColumns() | ||
this.resize() | ||
this.addEventScrollX() | ||
new ResizeObserver((mutations) => { | ||
this.resize() | ||
}).observe(document.querySelector(`#${this.id}`).parentElement) | ||
} | ||
export default class Component { | ||
constructor(id = 'svelte-simple-datatable') { | ||
this.context = context.get(id); | ||
this.id = id; | ||
} | ||
init() { | ||
this.setColumns(); | ||
this.resize(); | ||
this.addEventScrollX(); | ||
new ResizeObserver(() => { | ||
this.resize(); | ||
}).observe(document.querySelector(`#${this.id}`).parentElement); | ||
} | ||
|
||
reset () | ||
{ | ||
this.context.getPageNumber().update(store => store = 1) | ||
this.context.getGlobalFilter().remove() | ||
this.context.getLocalFilters().remove() | ||
this.context.getColumns().set([]) | ||
} | ||
reset() { | ||
// eslint-disable-next-line no-unused-vars | ||
this.context.getPageNumber().update((store) => (store = 1)); | ||
this.context.getGlobalFilter().remove(); | ||
this.context.getLocalFilters().remove(); | ||
this.context.getColumns().set([]); | ||
} | ||
|
||
setRows(data) | ||
{ | ||
for ( const item of data ) { | ||
setRows(data) { | ||
for (const item of data) { | ||
for (const key of Object.keys(item)) { | ||
if (item[key] === null) { | ||
item[key] = ''; | ||
} | ||
} | ||
} | ||
this.context.getData().set(data); | ||
return; | ||
} | ||
|
||
for ( const key of Object.keys(item) ) { | ||
if (item[key] === null) { | ||
item[key] = '' | ||
} | ||
} | ||
getSize() { | ||
const parent = document.querySelector(`#${this.id}`).parentNode; | ||
const style = getComputedStyle(parent); | ||
const rect = parent.getBoundingClientRect(); | ||
const getNumber = (pxValue) => { | ||
return parseFloat(pxValue.replace('px', '')); | ||
}; | ||
return { | ||
parentWidth: rect.width, | ||
parentHeight: rect.height, | ||
width: | ||
(rect.width - | ||
getNumber(style.paddingLeft) - | ||
getNumber(style.paddingRight) - | ||
getNumber(style.borderLeftWidth) - | ||
getNumber(style.borderRightWidth)) / | ||
rect.width, | ||
height: | ||
(rect.height - | ||
getNumber(style.paddingTop) - | ||
getNumber(style.paddingBottom) - | ||
getNumber(style.borderTopWidth) - | ||
getNumber(style.borderBottomWidth)) / | ||
rect.height, | ||
top: style.paddingTop, | ||
right: style.paddingRight, | ||
bottom: style.paddingBottom, | ||
left: style.paddingLeft | ||
}; | ||
} | ||
|
||
} | ||
this.context.getData().set(data) | ||
return | ||
} | ||
resize() { | ||
if (!document.querySelector(`#${this.id}`)) return; | ||
const size = this.getSize(); | ||
const tableContainer = document.querySelector(`#${this.id} .dt-table`); | ||
// if ( this.getOptions().scrollY ) { | ||
// tableContainer.style.height = this.setTableContainerHeight(size.parentHeight * size.height) + 'px' | ||
// this.context.getColumns().redraw() | ||
// } | ||
this.context.getDatatableWidth().set(size.parentWidth * size.width); | ||
if (size.parentWidth * size.width < document.querySelector(`#${this.id} table`).offsetWidth) { | ||
tableContainer.style.overflowX = 'auto'; | ||
} | ||
} | ||
|
||
getSize() | ||
{ | ||
const parent = document.querySelector(`#${this.id}`).parentNode | ||
const style = getComputedStyle(parent) | ||
const rect = parent.getBoundingClientRect() | ||
const getNumber = (pxValue) => { return parseFloat(pxValue.replace('px', '')) } | ||
return { | ||
parentWidth: rect.width, | ||
parentHeight: rect.height, | ||
width: (rect.width - getNumber(style.paddingLeft) - getNumber(style.paddingRight) - getNumber(style.borderLeftWidth) - getNumber(style.borderRightWidth)) / rect.width, | ||
height: (rect.height - getNumber(style.paddingTop) - getNumber(style.paddingBottom) - getNumber(style.borderTopWidth) - getNumber(style.borderBottomWidth)) / rect.height, | ||
top: style.paddingTop, | ||
right: style.paddingRight, | ||
bottom: style.paddingBottom, | ||
left: style.paddingLeft | ||
} | ||
} | ||
setTableContainerHeight(height) { | ||
const calc = [ | ||
document.querySelector(`#${this.id} .dt-search`).getBoundingClientRect().height, | ||
document.querySelector(`#${this.id} .dt-pagination`).getBoundingClientRect().height | ||
]; | ||
const sum = (a, b) => a + b; | ||
document.querySelector(`#${this.id} .dt-table`).style.height = height - calc.reduce(sum) + 'px'; | ||
} | ||
|
||
resize() | ||
{ | ||
if ( !document.querySelector(`#${this.id}`) ) return | ||
const size = this.getSize() | ||
const tableContainer = document.querySelector(`#${this.id} .dt-table`) | ||
// if ( this.getOptions().scrollY ) { | ||
// tableContainer.style.height = this.setTableContainerHeight(size.parentHeight * size.height) + 'px' | ||
// this.context.getColumns().redraw() | ||
// } | ||
this.context.getDatatableWidth().set( size.parentWidth * size.width ) | ||
if (size.parentWidth * size.width < document.querySelector(`#${this.id} table`).offsetWidth) { | ||
tableContainer.style.overflowX = 'auto' | ||
} | ||
} | ||
addEventScrollX() { | ||
if (this.getOptions().scrollY) { | ||
document.querySelector(`#${this.id} .dt-table`).addEventListener('scroll', (e) => { | ||
document.querySelector(`#${this.id} .dt-header`).style.left = | ||
-1 * e.target.scrollLeft + 'px'; | ||
}); | ||
} | ||
} | ||
|
||
setTableContainerHeight(height) | ||
{ | ||
const calc = [ | ||
document.querySelector(`#${this.id} .dt-search`).getBoundingClientRect().height, | ||
document.querySelector(`#${this.id} .dt-pagination`).getBoundingClientRect().height | ||
] | ||
const sum = (a, b) => a + b | ||
document.querySelector(`#${this.id} .dt-table`).style.height = height - calc.reduce(sum) + 'px' | ||
} | ||
|
||
addEventScrollX() | ||
{ | ||
if ( this.getOptions().scrollY ) { | ||
document.querySelector(`#${this.id} .dt-table`).addEventListener('scroll', (e) => { | ||
document.querySelector(`#${this.id} .dt-header`).style.left = (-1 * e.target.scrollLeft) + 'px' | ||
}) | ||
} | ||
} | ||
|
||
setColumns() { | ||
setTimeout( () => { | ||
const columnList = [] | ||
let i = 0 | ||
document.querySelectorAll(`#${this.id} table thead th`).forEach(th => { | ||
columnList.push({ | ||
index: i, | ||
html: th.innerHTML, | ||
key: this.getKey(th.dataset.key), | ||
setColumns() { | ||
setTimeout(() => { | ||
const columnList = []; | ||
let i = 0; | ||
document.querySelectorAll(`#${this.id} table thead th`).forEach((th) => { | ||
columnList.push({ | ||
index: i, | ||
html: th.innerHTML, | ||
key: this.getKey(th.dataset.key), | ||
name: th.dataset.name, | ||
sort: null, | ||
classList: th.classList, | ||
minWidth: th.getBoundingClientRect().width | ||
}) | ||
th.addEventListener('click', (e) => { | ||
this.context.getColumns().sort(e.target, this.getKey(th.dataset.key)) | ||
}, true) | ||
i++ | ||
}) | ||
this.context.getColumns().set(columnList) | ||
}, 25) | ||
} | ||
sort: null, | ||
classList: th.classList, | ||
minWidth: th.getBoundingClientRect().width | ||
}); | ||
th.addEventListener( | ||
'click', | ||
(e) => { | ||
this.context.getColumns().sort(e.target, this.getKey(th.dataset.key)); | ||
}, | ||
true | ||
); | ||
i++; | ||
}); | ||
this.context.getColumns().set(columnList); | ||
}, 25); | ||
} | ||
|
||
getKey(key) | ||
{ | ||
if (!key) return | ||
if (key && key.indexOf('=>') > 0) { | ||
return new Function(`'use strict';return (${key})`)() | ||
} | ||
return (x) => x[key] | ||
} | ||
getKey(key) { | ||
if (!key) return; | ||
if (key && key.indexOf('=>') > 0) { | ||
return new Function(`'use strict';return (${key})`)(); | ||
} | ||
return (x) => x[key]; | ||
} | ||
|
||
getOptions() | ||
{ | ||
return this.context.getOptions().get(this.context.getOptions()) | ||
} | ||
} | ||
getOptions() { | ||
return this.context.getOptions().get(this.context.getOptions()); | ||
} | ||
} |
Oops, something went wrong.