import { DataType } from 'devextreme/common' import { PivotGridDataType } from 'devextreme/ui/pivot_grid/data_source' import { MULTIVALUE_DELIMITER } from '../../constants/app.constant' import { ChartSeriesDto } from '@/proxy/admin/charts/models' export interface GridExtraFilterState { fieldName: string operator: string controlType: string value: string } export function extractSearchParamsFields(filter: any): [string, string, any][] { if (!Array.isArray(filter)) return [] // [field, op, val] formu mu? if (filter.length === 3 && typeof filter[0] === 'string') { return [filter as [string, string, any]] } // içinde başka filter array’leri olabilir return filter.flatMap((f) => extractSearchParamsFields(f)) } // sayfaya dinamik olarak css style ekler export function addCss(css: string) { if (css.startsWith('http') || css.startsWith('/')) { const style = document.createElement('link') style.rel = 'stylesheet' style.href = css document.getElementsByTagName('head')[0].appendChild(style) } else { const style = document.createElement('style') style.type = 'text/css' style.innerHTML = css document.getElementsByTagName('head')[0].appendChild(style) } } // sayfaya dinamik olarak js ekler export function addJs(js: string) { if (js.startsWith('http') || js.startsWith('/')) { const script = document.createElement('script') script.type = 'text/javascript' script.src = js document.getElementsByTagName('head')[0].appendChild(script) } else { eval(js) } } export function controlStyleCondition(data: any, fieldName: any, style: any) { // condition sadece data satırlarında yapılır if (style.rowType != 'data') return true switch (style.condition) { case '=': return style.conditionValue == data[fieldName] case '<>': return style.conditionValue != data[fieldName] case '<': return style.conditionValue > data[fieldName] case '<=': return style.conditionValue >= data[fieldName] case '>': return style.conditionValue < data[fieldName] case '>=': return style.conditionValue <= data[fieldName] case 'contains': return data[fieldName].includes(style.conditionValue) case 'endswith': return data[fieldName].endsWith(style.conditionValue) case 'isblank': return data[fieldName] == null case 'isnotblank': return data[fieldName] != null case 'notcontains': return data[fieldName].indexOf(style.conditionValue) === -1 case 'startswith': return data[fieldName].startsWith(style.conditionValue) case 'between': return false // TODO: case 'anyof': // anyof icin style.conditionValue obje olmalıdır: ['Val1','Val2'] return JSON.parse(style.conditionValue).some(data[fieldName]) case 'noneof': // noneof icin style.conditionValue obje olmalıdır: ['Val1','Val2'] return JSON.parse(style.conditionValue).findIndex((el: any) => el == data[fieldName]) < 0 default: return false } return false } export function pivotFieldConvertDataType(fieldDbType?: DataType): PivotGridDataType { switch (fieldDbType) { case 'date': case 'datetime': return 'date' case 'number': return 'number' case 'boolean': return 'number' case 'object': case 'string': return 'string' default: return 'string' } } export function setGridPanelColor(color: any) { //DataGrid icin const pnlGrid = document.getElementsByClassName( 'dx-datagrid-header-panel', ) as HTMLCollectionOf if (pnlGrid?.length) { pnlGrid[0].style.borderBottom = `3px solid ${color}` } //TreeList icin const pnlTree = document.getElementsByClassName( 'dx-treelist-header-panel', ) as HTMLCollectionOf if (pnlTree?.length) { pnlTree[0].style.borderBottom = `3px solid ${color}` } } // CustomStore icerisindeki insert, update ve delete islemleri icin servis adresini olusturur export function getServiceAddress(value: string) { if (value && value.startsWith('http')) return value if (value && value.startsWith('/')) value = value.slice(1) return value } export function isNotEmpty(value: any) { return value !== undefined && value !== null && value !== '' } export function getLoadOptions(loadOptions: any, params: any) { // Muhtemel loadOptions'lar ;[ 'skip', 'take', 'sort', 'totalSummary', 'requireTotalCount', 'searchOperation', 'filter', 'group', 'groupSummary', 'requireGroupCount', 'parentIds', 'searchExpr', 'searchValue', 'select', 'userData', 'chart', ].forEach(function (i) { //if (i in loadOptions && isNotEmpty(loadOptions[i])) { // params[i] = JSON.stringify(loadOptions[i]); //} if (i in loadOptions && isNotEmpty(loadOptions[i])) { if (i == 'skip') { params.skip = loadOptions[i] } else if (i == 'take') { params.take = loadOptions[i] } else if (i == 'sort') { //Gelen veri yapisi: [{ selector: 'CultureName', desc: false }, { selector: 'X', desc: true }] params.sort = loadOptions[i] .map((x: any) => `${x.selector} ${x.desc ? 'desc' : 'asc'}`) .join(MULTIVALUE_DELIMITER) } else if (i == 'totalSummary') { //gelen veri yapisi: [{"selector":"OrderNumber","summaryType":"count"},{"selector":"SaleAmount","summaryType":"sum"}] params.totalSummary = loadOptions[i] .map((x: any) => `${x.selector} ${x.summaryType}`) .join(MULTIVALUE_DELIMITER) } else if (i == 'requireTotalCount') { params.requireTotalCount = loadOptions[i] } else if (i == 'searchOperation') { params.searchOperation = loadOptions[i] } else if (i == 'filter') { //[ // ["dataField", "=", 10], // "and", // [ // ["anotherDataField", "<", 3], // "or", // ["anotherDataField", ">", 11] // ] //] //console.log('filter', loadOptions) params.filter = loadOptions[i] } else if (i == 'requireGroupCount') { params.requireGroupCount = loadOptions[i] } // Yukaridakiler servis tarafinda da implemente edildi // else if (i == 'group') { //Gelen veri yapisi: // [{ selector: 'CultureName', desc: false }, { selector: 'X', groupInterval: 'year', desc: true }] // GroupInterval sadece tarihlerde var //group Interval türleri: HeaderFilterGroupInterval //console.log('group:', loadOptions[i]) params.group = loadOptions[i] .map( (x: any) => `${x.selector} ${x.desc ? 'desc' : 'asc'} ${x.isExpanded}${ x.groupInterval ? ' ' + x.groupInterval : '' }`, ) .join(MULTIVALUE_DELIMITER) // => CultureName asc false|TextDate asc false year } else if (i == 'groupSummary') { //Gelen veri yapisi: [{"selector":"Id","summaryType":"count"}] //Not: Birden fazla eleman ile karsilasilmadi params.groupSummary = loadOptions[i] .map((x: any) => `${x.selector} ${x.summaryType}`) .join(MULTIVALUE_DELIMITER) // => SalesAmount sum|SalesAmount avg } //else if (i == 'userData') { // params.searchOperation = loadOptions[i]; // console.log('userData: ', loadOptions[i]); //} else if (i == 'searchValue') { params.searchValue = loadOptions[i] } else if (i == 'searchExpr') { params.searchExpr = loadOptions[i] } } }) return params } // FormEditingExtraItem // Devexpress Gridde kaydete basılınca // formda gözükecek olan EditingFormDto.Items elemanları gelmiyor // Sadece grid'de tanımlanmış columnları getiriyor. // Bu elemanları da getirmesi için aşağıdaki şekilde, // JSON value varsa bunları kendi field'ına setliyoruz export function setFormEditingExtraItemValues(values: any) { var jsonFields: Record> = {} // jsonField örn: // { // Options: { // TemplateName: 1, // MailTemplate: 2 // } // } Object.keys(values).forEach((key) => { if (key.includes(':')) { const fieldData = key.split(':') // Options:TemplateName if (!jsonFields[fieldData[0]]) { jsonFields[fieldData[0]] = {} } jsonFields[fieldData[0]][fieldData[1]] = values[key] delete values[key] } }) Object.keys(jsonFields).forEach((jsonField) => { values[jsonField] = JSON.stringify(jsonFields[jsonField]) }) return values } export function buildSeriesDto(seriesList: ChartSeriesDto[]) { if (!seriesList || seriesList.length === 0) return [] // her seri için dto oluştur return seriesList.map((s) => ({ type: s.type, argumentField: s.argumentField, valueField: s.valueField, name: s.name || s.valueField, axis: s.axis, color: s.color, dashStyle: s.dashStyle, width: s.width, visible: s.visible, showInLegend: s.showInLegend, label: { visible: s.label?.visible || false, backgroundColor: s.label?.backgroundColor || 'transparent', customizeText: s.label?.customizeText || undefined, format: s.label?.format || undefined, font: s.label?.font || { color: '#000000', size: 12, }, }, })) } export function autoNumber() { const now = new Date() const pad = (n: number, width: number) => n.toString().padStart(width, '0') return ( `${now.getFullYear()}` + `${pad(now.getMonth() + 1, 2)}` + `${pad(now.getDate(), 2)}` + `${pad(now.getHours(), 2)}` + `${pad(now.getMinutes(), 2)}` + `${pad(now.getSeconds(), 2)}` + `${pad(now.getMilliseconds(), 3)}` ) }