////////////////////////////////////////////////////////////// // Copyright (c) 2017 Ben Jackman // All Rights Reserved // please contact ben@jackman.biz // for licensing inquiries // Created by bjackman @ 5/22/17 4:53 PM ////////////////////////////////////////////////////////////// import * as _ from "lodash"; import {SlickEditorLongText, SlickEditorText} from "./SlickWrap"; import Column = Slick.Column; import SlickData = Slick.SlickData; type QTableColSchemaT = { idx: number key: string name: string width: number editor?: typeof SlickEditorText getFilterValue?: GetFilterValueFn formatter?: any } interface ColByKeyT { [key: string]: QTableColSchemaT } export class QTableSchema { colsByIdx: QTableColSchemaT[] colsByKey: ColByKeyT constructor(cols: any) { this.colsByKey = {...cols} this.colsByIdx = _.sortBy(Object.keys(this.colsByKey).map(k => this.colsByKey[k]), (c: QTableColSchemaT) => c.idx) } getByIndex(idx: number): QTableColSchemaT { return this.colsByIdx[idx] as QTableColSchemaT } getByKey(key: string): QTableColSchemaT { return this.colsByKey[key] as QTableColSchemaT } } export type GetFilterValueFn = (item: T) => {} | null export type FormatterFnInT = { row: number, cell: number, value: any, columnDef: Column, dataContext: any } export type FormatterFnOutT = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => string export function formatterFn(f: (ps: FormatterFnInT) => string): FormatterFnOutT { return (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => f({ row, cell, value, columnDef, dataContext }) } export function QTableColSchemaBuilder() { let idx = 0 const w = (width: number) => ({width}) const w5 = w(5) const w10 = w(10) const w20 = w(20) const w40 = w(40) const w60 = w(60) const w80 = w(80) const w100 = w(100) const w140 = w(140) const editorText = {editor: SlickEditorText} const editorLongText = {editor: SlickEditorLongText } function setFormatter(f: FormatterFnOutT) { return {formatter: f} } function getFilterValue(fn: GetFilterValueFn) { return {getFilterValue: fn} } function c(...props: Partial[]): QTableColSchemaT { const defaultCol: QTableColSchemaT = {idx, key: "", name: "", width: 50} idx++ let ret = _.merge({}, defaultCol) props.forEach(p => _.merge(ret, p)) return ret } function setKeys(ret: T): QTableSchema { Object.keys(ret).forEach(k => { const rtt = (ret as ColByKeyT) rtt[k].name = rtt[k].key = k }) return new QTableSchema(ret) } return {w, w5, w10, w20, w40, w60, w80, w100, w140, c, editorText, editorLongText, setKeys, getFilterValue, setFormatter } }