Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
vincjo committed Mar 2, 2024
1 parent 380a5c4 commit 53d0d97
Show file tree
Hide file tree
Showing 101 changed files with 890 additions and 555 deletions.
20 changes: 4 additions & 16 deletions src/lib/local/Comparator.ts → src/lib/client/Comparator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Criterion } from '$lib/local'
import type { Criterion } from '$lib/client'
import { isNull, isNotNull, stringify } from './utils'

export const check = {

Expand Down Expand Up @@ -67,11 +68,11 @@ export const check = {
},

isNull: (entry: null, _: any) => {
return entry === null || entry === undefined
return isNull(entry)
},

isNotNull: (entry: any, _: any) => {
return entry === null || entry === undefined ? false : true
return isNotNull(entry)
},

whereIn: (entry: any, values: Criterion[] = []) => {
Expand All @@ -94,16 +95,3 @@ export const check = {
},
}


/* utils */

function stringify(value: string | number | boolean = null) {
return String(value)
.toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
}

function isNull(entry: any) {
if (entry === null || entry === undefined) return true
}
148 changes: 70 additions & 78 deletions src/lib/local/Context.ts → src/lib/client/Context.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
import { writable, derived, type Writable, type Readable } from 'svelte/store'
import type { Filter, Sort, Comparator, Criterion, Field } from '$lib/local'
import type { Params } from '$lib/local/DataHandler'
import { isNull, parseField } from './utils'
import { check } from './Comparator'
import EventHandler from './handlers/EventHandler'
import type { Filter, Sort, Field } from '$lib/client'
import type { Params } from '$lib/client/DataHandler'
import { parseField, match } from './utils'
import EventsHandler from './handlers/EventsHandler'


export default class Context<Row>
{
public event : EventHandler
public events : EventsHandler
public rowsPerPage : Writable<number | null>
public pageNumber : Writable<number>
public currentPage : Writable<number>
public search : Writable<{ value?: string, scope?: (keyof Row)[] }>
public filters : Writable<Filter<Row>[]>
public filterCount : Readable<number>
public rawRows : Writable<Row[]>
public filteredRows : Readable<Row[]>
public pagedRows : Readable<Row[]>
public rowCount : Readable<{ total: number, start: number, end: number }>
public rowCount : Readable<{ start: number, end: number, total: number, selected: number }>
public pages : Readable<number[]>
public pagesWithEllipsis : Readable<number[]>
public pageCount : Readable<number>
public sort : Writable<(Sort<Row>)>
public selected : Writable<(Row | Row[keyof Row])[]>
public selectBy : string
public selectScope : Writable<'all' | 'currentPage'>
public isAllSelected : Readable<boolean>

constructor(data: Row[], params: Params)
{
this.event = new EventHandler()
this.rowsPerPage = writable(params.rowsPerPage)
this.pageNumber = writable(1)
this.search = writable({})
this.filters = writable([])
this.filterCount = this.createFilterCount()
this.rawRows = writable(data)
this.filteredRows = this.createFilteredRows()
this.pagedRows = this.createPagedRows()
this.rowCount = this.createRowCount()
this.pages = this.createPages()
this.pagesWithEllipsis = this.createPagesWithEllipsis()
this.pageCount = this.createPageCount()
this.sort = writable({})
this.selected = writable([])
this.selectScope = writable('all')
this.isAllSelected = this.createIsAllSelected()
this.events = new EventsHandler()
this.rowsPerPage = writable(params.rowsPerPage)
this.currentPage = writable(1)
this.search = writable({})
this.filters = writable([])
this.filterCount = this.createFilterCount()
this.rawRows = writable(data)
this.filteredRows = this.createFilteredRows()
this.pagedRows = this.createPagedRows()
this.pages = this.createPages()
this.pagesWithEllipsis = this.createPagesWithEllipsis()
this.pageCount = this.createPageCount()
this.sort = writable({})
this.selected = writable([])
this.selectBy = params.selectBy
this.selectScope = writable('all')
this.isAllSelected = this.createIsAllSelected()
this.rowCount = this.createRowCount()
}

private createFilterCount()
{
return derived(this.filters, ($filters) => $filters.length)
}

private createFilteredRows()
private createFilteredRows()
{
return derived(
[this.rawRows, this.search, this.filters],
Expand All @@ -65,59 +66,41 @@ export default class Context<Row>
return callback
})
return scope.some((callback) => {
return this.match(callback(row), $search.value)
return match(callback(row), $search.value)
})
})
this.pageNumber.set(1)
this.selected.set([])
this.event.trigger('change')
this.currentPage.set(1)
// this.selected.set([])
this.events.trigger('change')
}

if ($filters.length > 0) {
$filters.forEach((filter) => {
return ($rawRows = $rawRows.filter((row) => {
const entry = filter.callback(row)
return this.match(entry, filter.value, filter.comparator)
return match(entry, filter.value, filter.comparator)
}))
})
this.pageNumber.set(1)
this.selected.set([])
this.event.trigger('change')
this.currentPage.set(1)
// this.selected.set([])
this.events.trigger('change')
}
return $rawRows
}
)
}

private match(entry: Row[keyof Row], value: string|number|boolean|symbol|number[]|Criterion[], compare: Comparator<Row> = null)
{
if (isNull(value)) {
return true
}
if (!entry && compare) {
return compare(entry, value)
}
if (!entry) return check.isLike(entry, value)
else if (typeof entry === 'object') {
return Object.keys(entry).some((k) => {
return this.match(entry[k], value, compare)
})
}
if (!compare) return check.isLike(entry, value)
return compare(entry, value)
}

private createPagedRows()
{
return derived(
[this.filteredRows, this.rowsPerPage, this.pageNumber],
([$filteredRows, $rowsPerPage, $pageNumber]) => {
[this.filteredRows, this.rowsPerPage, this.currentPage],
([$filteredRows, $rowsPerPage, $currentPage]) => {
if (!$rowsPerPage) {
return $filteredRows
}
return $filteredRows.slice(
($pageNumber - 1) * $rowsPerPage,
$pageNumber * $rowsPerPage
($currentPage - 1) * $rowsPerPage,
$currentPage * $rowsPerPage
)
}
)
Expand All @@ -126,16 +109,17 @@ export default class Context<Row>
private createRowCount()
{
return derived(
[this.filteredRows, this.pageNumber, this.rowsPerPage],
([$filteredRows, $pageNumber, $rowsPerPage]) => {
[this.filteredRows, this.currentPage, this.rowsPerPage, this.selected],
([$filteredRows, $currentPage, $rowsPerPage, $selected]) => {
const total = $filteredRows.length
if (!$rowsPerPage) {
return { total: total, start: 1, end: total }
return { total: total, start: 1, end: total, selected: $selected.length }
}
return {
total: total,
start: $pageNumber * $rowsPerPage - $rowsPerPage + 1,
end: Math.min($pageNumber * $rowsPerPage, $filteredRows.length)
start: $currentPage * $rowsPerPage - $rowsPerPage + 1,
end: Math.min($currentPage * $rowsPerPage, $filteredRows.length),
selected: $selected.length
}
}
)
Expand All @@ -154,56 +138,64 @@ export default class Context<Row>

private createPagesWithEllipsis()
{
return derived([this.pages, this.pageNumber], ([$pages, $pageNumber]) => {
return derived([this.pages, this.currentPage], ([$pages, $currentPage]) => {
if ($pages.length <= 7) {
return $pages
}
const ellipse = null
const firstPage = 1
const lastPage = $pages.length
if ($pageNumber <= 4) {
if ($currentPage <= 4) {
return [
...$pages.slice(0, 5),
ellipse,
lastPage
]
} else if ($pageNumber < $pages.length - 3) {
} else if ($currentPage < $pages.length - 3) {
return [
firstPage,
ellipse,
...$pages.slice($pageNumber - 2, $pageNumber + 1),
...$pages.slice($currentPage - 2, $currentPage + 1),
ellipse,
lastPage
]
} else {
return [
firstPage,
ellipse,
...$pages.slice($pages.length - 5,
$pages.length)
...$pages.slice($pages.length - 5, $pages.length)
]
}
})
}

private createPageCount()
{
return derived(this.pages, ($pages) => {
return $pages.length
})
return derived(this.pages, ($pages) => $pages.length)
}

private createIsAllSelected()
{
return derived(
[this.selected, this.pagedRows, this.filteredRows, this.selectScope],
([$selected, $pagedRows, $filteredRows, $selectScope]) => {
const rowCount = $selectScope === 'currentPage' ? $pagedRows.length : $filteredRows.length
if (rowCount === $selected.length && rowCount !== 0) {
return true
}
// return derived(
// [this.selected, this.pagedRows, this.filteredRows, this.selectScope],
// ([$selected, $pagedRows, $filteredRows, $selectScope]) => {
// const rowCount = $selectScope === 'currentPage' ? $pagedRows.length : $filteredRows.length
// if (rowCount === $selected.length && rowCount !== 0) {
// return true
// }
// return false
// }
// )

return derived([this.selected, this.pagedRows], ([$selected, $pagedRows]) => {
if ($pagedRows.length === 0) {
return false
}
)
if (this.selectBy) {
const ids = $pagedRows.map(row => row[this.selectBy])
return ids.every(id => $selected.includes(id))
}
return $pagedRows.every(row => $selected.includes(row as Row))
})
}
}
Loading

0 comments on commit 53d0d97

Please sign in to comment.