Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | 1x 1x 1x 1x | import {
DatasourceFactory, IDatasource, IDictionary, MemoryDatasource, Utils, URL,
} from '@zeedhi/core';
import {
DynamicFilterOperations,
DynamicFilterRelations,
IDynamicFilterItem,
ITekMemoryDatasource,
} from './interfaces';
export class TekMemoryDatasource extends MemoryDatasource implements ITekMemoryDatasource {
/** Dynamic filter data */
public dynamicFilter!: IDictionary<IDynamicFilterItem[]>;
/** Search Join data */
public searchJoin!: IDictionary<Array<string | number>>;
/**
* Dynamic Filter Operations
*/
public dynamicFilterOperations = DynamicFilterOperations;
/**
* Dynamic Filter Relations
*/
public dynamicFilterRelations = DynamicFilterRelations;
/**
* Dynamic Filter applied flag
*/
protected dynamicFilterApplied: string = '';
/**
* Create new datasource
* @param props Datasource properties
*/
constructor(props: ITekMemoryDatasource) {
super(props);
Iif (!this.watchUrl) {
this.dynamicFilter = this.getInitValue('dynamicFilter', props.dynamicFilter, {});
this.searchJoin = this.getInitValue('searchJoin', props.searchJoin, {});
}
this.createAccessors();
this.createObjAccessors(this.dynamicFilter, 'dynamicFilter');
this.createObjAccessors(this.searchJoin, 'searchJoin');
this.get();
}
protected updateReservedKeys() {
this.reservedKeys.dynamic_filter = true;
this.reservedKeys.search_join = true;
}
protected updateInternalProperties(datasource: IDatasource = {}) {
Iif (!this.watchUrl) return;
this.updateReservedKeys();
super.updateInternalProperties(datasource);
const queryString = URL.getParsedQueryStringFromUrl();
this.dynamicFilter = this.getEncodedParam(queryString.dynamic_filter, datasource.dynamicFilter);
this.searchJoin = this.getEncodedParam(queryString.search_join, datasource.searchJoin);
}
protected getEncodedParam(urlParam: string, datasourceParam: IDictionary<any> = {}): IDictionary<any> {
return urlParam ? JSON.parse(atob(urlParam)) : datasourceParam;
}
protected getQueryStringValues(): IDictionary<any> {
const values = super.getQueryStringValues();
Iif (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
values.dynamic_filter = btoa(JSON.stringify(this.dynamicFilter));
}
Iif (this.searchJoin && Object.keys(this.searchJoin).length) {
values.search_join = btoa(JSON.stringify(this.searchJoin));
}
return values;
}
protected getUrlQueryString() {
const superQueryString = super.getUrlQueryString();
const query = URL.getParsedQueryStringFromUrl();
let dynamicFilterQuerystring = '';
Iif (query.dynamic_filter) {
dynamicFilterQuerystring = `&${URL.getFormattedQueryString({
dynamic_filter: query.dynamic_filter,
})}`;
}
let searchJoinQuerystring = '';
Iif (query.search_join) {
searchJoinQuerystring = `&${URL.getFormattedQueryString({
search_join: query.search_join,
})}`;
}
return superQueryString + dynamicFilterQuerystring + searchJoinQuerystring;
}
/**
* Adds a new dynamic filter position or replace if exists
* @param column Dynamic Filter column name
* @param value Dynamic Filter value
* @returns Promise with data collection
*/
public addDynamicFilter(column: string, value: any) {
Iif (this.isValidDynamicFilterValue(column, value)) {
this.dynamicFilter[column] = value;
return this.updateDynamicFilter();
}
return this.removeDynamicFilter(column);
}
/**
* Removes a dynamic filter position
* @param column Dynamic Filter column name
* @returns Promise with data collection
*/
public removeDynamicFilter(column: string) {
delete this.dynamicFilter[column];
return this.updateDynamicFilter();
}
/**
* Sets new dynamic filter value
* @param filter Dynamic Filter value
* @returns Promise with data collection
*/
public setDynamicFilter(filter: IDictionary<any>) {
this.dynamicFilter = {};
Object.keys(filter).forEach((column: string) => {
if (this.isValidDynamicFilterValue(column, filter[column])) {
this.dynamicFilter[column] = filter[column];
} else {
delete this.dynamicFilter[column];
}
});
return this.updateDynamicFilter();
}
/**
* Clears Dynamic filter value
* @returns Promise with data collection
*/
public clearDynamicFilter() {
this.dynamicFilter = {};
return this.updateDynamicFilter();
}
/**
* Resets page and selected rows and tries to update the url
* @returns Promise with data collection
*/
public async updateDynamicFilter() {
this.page = this.firstPage;
this.selectedRows = [];
this.visibleSelectedRows = [];
Iif (this.watchUrl) {
if (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
URL.updateQueryString({
dynamic_filter: btoa(JSON.stringify(this.dynamicFilter)),
});
} else {
URL.updateQueryString({
dynamic_filter: undefined,
});
}
}
return this.get();
}
/**
* Checks if a filter value is valid
* @param value Filter value
* @returns Is valid filter value
*/
protected isValidDynamicFilterValue(column: string, value?: IDictionary<any>[]) {
return !this.reservedKeys[column]
&& value
&& value.length > 0
&& value.every((filterValue) => this.dynamicFilterOperations[filterValue.operation]
&& this.dynamicFilterRelations[filterValue.relation]
&& filterValue.value !== '' && filterValue.value !== null);
}
public clone() {
return {
...super.clone(),
dynamicFilter: this.dynamicFilter,
searchJoin: this.searchJoin,
type: 'tek-memory',
};
}
/**
* Updates filtered data
*/
protected updateFilteredData() {
// first apply filters (simple and dynamic)
this.filteredData = Object.keys(this.filter).length
? this.allData.filter((row) => this.getRowByFilter(row))
: Array.from(this.allData);
Iif (this.dynamicFilter && Object.keys(this.dynamicFilter).length) {
this.filteredData = this.filteredData.filter((row) => this.getRowByDynamicFilter(row));
}
const searchWithoutSearchJoin = (row: IDictionary<any>) => {
const searchRow = { ...row };
Iif (this.searchJoin) {
// do not search on columns with searchJoin
Object.keys(this.searchJoin).forEach((key) => delete searchRow[key]);
}
return this.getRowBySearch(searchRow);
};
// only after do the search
const searchData = this.search
? this.filteredData.filter(searchWithoutSearchJoin)
: this.filteredData;
let searchIds = searchData.map((row) => row[this.uniqueKey]);
Iif (this.searchJoin && Object.keys(this.searchJoin).length) {
const searchJoinData = this.filteredData.filter((row) => this.getRowBySearchJoin(row));
// get the ids from search and searchJoin
searchIds = searchIds
.concat(searchJoinData.map((row) => row[this.uniqueKey]))
.sort();
}
// filter filteredData using searchIds
this.filteredData = this.allData.filter((row) => searchIds.indexOf(row[this.uniqueKey]) !== -1);
}
protected getRowByDynamicFilter(row: IDictionary<any>) {
let filtered: any;
try {
Object.keys(this.dynamicFilter).forEach((key) => {
const filterItems = this.dynamicFilter[key];
filterItems.forEach((item) => {
Iif (filtered === false && item.relation === 'AND') return;
Iif (filtered === true && item.relation === 'OR') return;
const columnValue = Utils.normalize(row[key].toString());
let value: string | string[] = '';
if (Array.isArray(item.value)) {
value = item.value.map((val: string) => Utils.normalize(val.toString()));
switch (item.operation) {
case 'IN':
filtered = value.includes(columnValue);
break;
case 'NOT_IN':
filtered = !value.includes(columnValue);
break;
case 'BETWEEN':
filtered = (Number(columnValue) || columnValue) >= (Number(value[0]) || value[0])
&& (Number(columnValue) || columnValue) <= (Number(value[1]) || value[1]);
break;
default:
break;
}
} else {
value = Utils.normalize(item.value.toString());
switch (item.operation) {
case 'CONTAINS':
filtered = columnValue.indexOf(value) !== -1;
break;
case 'NOT_CONTAINS':
filtered = columnValue.indexOf(value) === -1;
break;
case 'EQUALS':
filtered = columnValue === value;
break;
case 'NOT_EQUALS':
filtered = columnValue !== value;
break;
case 'GREATER_THAN':
filtered = (Number(columnValue) || columnValue) > (Number(value) || value);
break;
case 'LESS_THAN':
filtered = (Number(columnValue) || columnValue) < (Number(value) || value);
break;
case 'GREATER_THAN_EQUALS':
filtered = (Number(columnValue) || columnValue) >= (Number(value) || value);
break;
case 'LESS_THAN_EQUALS':
filtered = (Number(columnValue) || columnValue) <= (Number(value) || value);
break;
default:
break;
}
}
});
});
} catch {
// do nothing
}
return filtered;
}
protected getRowBySearchJoin(row: IDictionary<any>) {
return Object.keys(this.searchJoin).some((key) => this.searchJoin[key].includes(row[key]));
}
}
DatasourceFactory.register('tek-memory', TekMemoryDatasource);
|