aio-class 2.1.5 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +60 -35
  2. package/index.js +80 -81
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="emscripten" />
1
2
  /// <reference types="jquery" />
2
3
  /// <reference types="jquery" />
3
4
  import { AIOGeo } from 'aio-geo';
@@ -78,7 +79,7 @@ export declare const ArrayToObject: <T>(keys: T[], fn: (key: T) => any) => any;
78
79
  export declare const TextFilter: (p: {
79
80
  value: string | number;
80
81
  chars?: {
81
- [key: string]: boolean | undefined;
82
+ [key: string]: boolean;
82
83
  };
83
84
  number?: boolean;
84
85
  symbol?: boolean;
@@ -118,13 +119,13 @@ export declare class FileClass {
118
119
  duration: null | number;
119
120
  }>;
120
121
  fetchBlob: (url: string) => Promise<FileResult<Blob>>;
121
- fetchBlob2: () => Promise<void>;
122
122
  base64ToBlob: (base64: string) => FileResult<Blob>;
123
123
  handleFileName: (selectedName?: string) => string;
124
124
  generateFileName: (mimeType?: string) => string;
125
125
  private saveWeb;
126
126
  dataUrlToBase64: (dataUrl: string) => FileResult<string>;
127
127
  toBlob: (input: File | Blob | string | ArrayBuffer | Uint8Array | any) => Promise<FileResult<Blob>>;
128
+ dataUrlToFile(dataUrl: string, fileName?: string): File;
128
129
  saveText: (text: string, path: string) => Promise<{
129
130
  success: boolean;
130
131
  message: string;
@@ -196,6 +197,8 @@ export type I_EntityRowDetail<T> = {
196
197
  isLeaf: boolean;
197
198
  isFirstChild: boolean;
198
199
  isLastChild: boolean;
200
+ level: number;
201
+ isRoot: boolean;
199
202
  };
200
203
  export declare class EntityClass<T> {
201
204
  map: I_EntityMap<T>;
@@ -205,6 +208,7 @@ export declare class EntityClass<T> {
205
208
  map: I_EntityMap<T>;
206
209
  getData?: () => T[];
207
210
  setData?: (v: T[]) => void;
211
+ sorts?: I_sort<T>[];
208
212
  });
209
213
  private getId;
210
214
  private setId;
@@ -232,46 +236,67 @@ export declare class EntityClass<T> {
232
236
  setId: (row: T) => T;
233
237
  getChilds: (row: T) => T[];
234
238
  setChilds: (row: T, childs: T[] | undefined) => T;
235
- getRowDetailById: (data: T[], rowId: I_id) => false | I_EntityRowDetail<T>;
236
- getFilteredTree: (data: T[], filter: (row: T, detail: I_EntityRowDetail<T>) => boolean | undefined) => T[];
237
- getSortedTree: (rows: T[], sorts: I_sort<T>[]) => T[];
238
- addRow: (data: T[], row: T, p: {
239
- parentId?: I_id;
240
- index?: number;
241
- }) => {
242
- rows: T[];
239
+ getRowDetailById: (p: {
240
+ rowId: I_id;
241
+ rows?: T[];
242
+ }) => false | I_EntityRowDetail<T>;
243
+ getFilteredTree: (p: {
244
+ filter: (row: T, detail: I_EntityRowDetail<T>) => boolean | undefined;
245
+ rows?: T[];
246
+ }) => T[];
247
+ getSortedTree: (p: {
248
+ sorts: I_sort<T>[];
249
+ rows?: T[];
250
+ }) => T[];
251
+ addRow: (p: {
243
252
  row: T;
244
- };
245
- editRow: (data: T[], newRow: T) => T[];
246
- editById: (data: T[], rowId: I_id, change: Partial<T>) => T[];
247
- removeRow: (data: T[], rowId: I_id) => T[];
248
- removeByRowIds: (data: T[], rowIds: I_id[]) => T[];
249
- reOrder: (data: T[], fromId: I_id, toId: I_id, pos?: 'before' | 'after') => T[];
250
- reOrderByDir: (data: T[], rowId: I_id, dir: 1 | -1) => T[];
251
- moveTo: (data: T[], fromIds: I_id[], toId: I_id) => T[];
252
- editByTraverse: (rows: T[], callback: (row: T) => T | false) => T[];
253
- traverse: (rows: T[], callback: (row: T, detail: I_EntityRowDetail<T>) => false | undefined) => void;
254
- };
255
- dataActions: {
256
- getRowDetailById: (rowId: any) => false | I_EntityRowDetail<T>;
257
- addRow: (row: T, p: {
258
253
  parentId?: I_id;
259
254
  index?: number;
255
+ rows?: T[];
260
256
  }) => {
261
257
  rows: T[];
262
258
  row: T;
263
259
  };
264
- editRow: (newRow: T) => T[];
265
- editById: (rowId: I_id, change: Partial<T>) => T[];
266
- removeRow: (rowId: I_id) => T[];
267
- removeByRowIds: (rowIds: I_id[]) => T[];
268
- reOrder: (fromId: I_id, toId: I_id) => T[];
269
- reOrderByDir: (rowId: I_id, dir: 1 | -1) => T[];
270
- moveTo: (fromIds: I_id[], toId: I_id) => T[];
271
- editByTraverse: (callback: (row: T) => T | false) => T[];
272
- getSortedTree: (sorts: I_sort<T>[]) => T[];
273
- getFilteredTree: (filter: (row: T, detail: I_EntityRowDetail<T>) => boolean | undefined) => T[];
274
- traverse: (callback: (row: T, detail: I_EntityRowDetail<T>) => false | undefined) => void;
260
+ editRow: (p: {
261
+ row: T;
262
+ rows?: T[];
263
+ }) => T[];
264
+ editById: (p: {
265
+ rowId: I_id;
266
+ change: Partial<T>;
267
+ rows?: T[];
268
+ }) => T[];
269
+ removeRow: (p: {
270
+ rowId: I_id;
271
+ rows?: T[];
272
+ }) => T[];
273
+ removeByRowIds: (p: {
274
+ rowIds: I_id[];
275
+ rows?: T[];
276
+ }) => T[];
277
+ reOrder: (p: {
278
+ fromId: I_id;
279
+ toId: I_id;
280
+ rows?: T[];
281
+ }) => T[];
282
+ reOrderByDir: (p: {
283
+ rowId: I_id;
284
+ dir: 1 | -1;
285
+ rows?: T[];
286
+ }) => T[];
287
+ moveTo: (p: {
288
+ fromIds: I_id[];
289
+ toId: I_id;
290
+ rows?: T[];
291
+ }) => T[];
292
+ editByTraverse: (p: {
293
+ callback: (row: T) => T | false;
294
+ rows?: T[];
295
+ }) => T[];
296
+ traverse: (p: {
297
+ callback: (row: T, detail: I_EntityRowDetail<T>) => false | undefined;
298
+ rows?: T[];
299
+ }) => void;
275
300
  };
276
301
  }
277
302
  type I_color = any;
package/index.js CHANGED
@@ -242,7 +242,7 @@ export const EventHandler = (selector, event, action, type = 'bind') => {
242
242
  element.addEventListener(fixedEvent, action);
243
243
  }
244
244
  };
245
- export const Stall = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (stallTime = 3000) { return yield new Promise(resolve => setTimeout(resolve, stallTime)); });
245
+ export const Stall = (stallTime = 3000) => __awaiter(void 0, void 0, void 0, function* () { return yield new Promise(resolve => setTimeout(resolve, stallTime)); });
246
246
  export const DetectOS = () => {
247
247
  const userAgent = window.navigator.userAgent;
248
248
  const platform = window.navigator.platform;
@@ -803,8 +803,6 @@ export class FileClass {
803
803
  return { success: false, result: null, message: (e === null || e === void 0 ? void 0 : e.message) || "Fetch failed" };
804
804
  }
805
805
  });
806
- this.fetchBlob2 = () => __awaiter(this, void 0, void 0, function* () {
807
- });
808
806
  this.base64ToBlob = (base64) => {
809
807
  try {
810
808
  const byteCharacters = atob(base64);
@@ -930,6 +928,18 @@ export class FileClass {
930
928
  return size + " byte";
931
929
  };
932
930
  }
931
+ dataUrlToFile(dataUrl, fileName = 'upload.bin') {
932
+ var _a, _b;
933
+ const arr = dataUrl.split(',');
934
+ const mime = ((_b = (_a = arr[0]) === null || _a === void 0 ? void 0 : _a.match(/:(.*?);/)) === null || _b === void 0 ? void 0 : _b[1]) || 'application/octet-stream';
935
+ const bstr = atob(arr[1] || arr[0]); // اگه header نداشت، کل base64 رو بگیر
936
+ const n = bstr.length;
937
+ const u8arr = new Uint8Array(n);
938
+ for (let i = 0; i < n; i++) {
939
+ u8arr[i] = bstr.charCodeAt(i);
940
+ }
941
+ return new File([u8arr], fileName, { type: mime });
942
+ }
933
943
  }
934
944
  export class StorageClass {
935
945
  constructor(id, context) {
@@ -1506,7 +1516,7 @@ export class EntityClass {
1506
1516
  this.getChilds = (row) => this.map.getChilds(row);
1507
1517
  this.setChilds = (row, childs) => this.map.setChilds(row, childs);
1508
1518
  this.getRowDetailsByIds = (data, rowIds) => {
1509
- const fn = (data, parents, temp) => {
1519
+ const fn = (data, parents, level, temp) => {
1510
1520
  const isFoundAll = () => Object.keys(temp).filter((rowId) => temp[rowId] === false).length === 0;
1511
1521
  for (let i = 0; i < data.length; i++) {
1512
1522
  if (isFoundAll()) {
@@ -1517,11 +1527,11 @@ export class EntityClass {
1517
1527
  const row = data[i];
1518
1528
  const rowId = this.getId(row);
1519
1529
  if (temp[rowId.toString()] === false) {
1520
- temp[rowId.toString()] = this.getRowDetailByRowAndParents(row, i, parents, isFirstChild, isLastChild);
1530
+ temp[rowId.toString()] = this.getRowDetailByRowAndParents(row, i, parents, isFirstChild, isLastChild, level);
1521
1531
  }
1522
1532
  const childs = this.getChilds(row);
1523
1533
  if (childs && childs.length) {
1524
- fn(childs, [...parents, row], temp);
1534
+ fn(childs, [...parents, row], level + 1, temp);
1525
1535
  }
1526
1536
  }
1527
1537
  };
@@ -1529,10 +1539,10 @@ export class EntityClass {
1529
1539
  for (let i = 0; i < rowIds.length; i++) {
1530
1540
  temp[rowIds[i].toString()] = false;
1531
1541
  }
1532
- fn(data, [], temp);
1542
+ fn(data, [], 0, temp);
1533
1543
  return temp;
1534
1544
  };
1535
- this.getRowDetailByRowAndParents = (row, index, parentRows, isFirstChild, isLastChild) => {
1545
+ this.getRowDetailByRowAndParents = (row, index, parentRows, isFirstChild, isLastChild, level) => {
1536
1546
  const rowId = this.getId(row);
1537
1547
  const childs = this.getChilds(row);
1538
1548
  const childsLength = childs ? childs.length : 0;
@@ -1546,7 +1556,7 @@ export class EntityClass {
1546
1556
  beforeSibling = parentChilds[index - 1] || false;
1547
1557
  afterSibling = parentChilds[index + 1] || false;
1548
1558
  }
1549
- return { row, parentRows, rowId, childsLength, parentId, isLeaf, parentRow, index, beforeSibling, afterSibling, isFirstChild, isLastChild };
1559
+ return { row, parentRows, rowId, childsLength, parentId, isLeaf, parentRow, index, beforeSibling, afterSibling, isFirstChild, isLastChild, level, isRoot: level === 0 };
1550
1560
  };
1551
1561
  this.removeChildOfParent = (parentRow, rowId) => {
1552
1562
  let childs = this.getChilds(parentRow);
@@ -1563,7 +1573,7 @@ export class EntityClass {
1563
1573
  return this.setChilds(parentRow, newChilds);
1564
1574
  };
1565
1575
  this.getRowDetailById = (data, rowId) => {
1566
- const fn = (data, rowId, parents, temp) => {
1576
+ const fn = (data, rowId, parents, level, temp) => {
1567
1577
  if (temp.res) {
1568
1578
  return;
1569
1579
  }
@@ -1573,18 +1583,18 @@ export class EntityClass {
1573
1583
  const row = data[i];
1574
1584
  const childs = this.getChilds(row);
1575
1585
  if (this.getId(row).toString() === rowId.toString()) {
1576
- temp.res = this.getRowDetailByRowAndParents(row, i, parents, isFirstChild, isLastChild);
1586
+ temp.res = this.getRowDetailByRowAndParents(row, i, parents, isFirstChild, isLastChild, level);
1577
1587
  break;
1578
1588
  }
1579
1589
  else {
1580
1590
  if (childs && childs.length) {
1581
- fn(childs, rowId, [...parents, row], temp);
1591
+ fn(childs, rowId, [...parents, row], level + 1, temp);
1582
1592
  }
1583
1593
  }
1584
1594
  }
1585
1595
  };
1586
1596
  let temp = { res: false };
1587
- fn(data, rowId, [], temp);
1597
+ fn(data, rowId, [], 0, temp);
1588
1598
  return temp.res ? temp.res : false;
1589
1599
  };
1590
1600
  this.getFilteredTree = (data, filter) => {
@@ -1605,17 +1615,17 @@ export class EntityClass {
1605
1615
  });
1606
1616
  return detailDic;
1607
1617
  };
1608
- const fn = (data, parentRows) => {
1618
+ const fn = (data, parentRows, level) => {
1609
1619
  const res = data.filter((row, i) => {
1610
1620
  const isFirstChild = i === 0;
1611
1621
  const isLastChild = i === data.length - 1;
1612
- const detail = this.getRowDetailByRowAndParents(row, i, parentRows, isFirstChild, isLastChild);
1622
+ const detail = this.getRowDetailByRowAndParents(row, i, parentRows, isFirstChild, isLastChild, level);
1613
1623
  return !!nodesDic[detail.rowId.toString()];
1614
1624
  });
1615
1625
  return res.map((row) => {
1616
1626
  const childs = this.getChilds(row);
1617
1627
  if (childs) {
1618
- const newChilds = fn(childs, (parentRows || []).concat(row));
1628
+ const newChilds = fn(childs, (parentRows || []).concat(row), level + 1);
1619
1629
  return this.setChilds(row, newChilds);
1620
1630
  }
1621
1631
  else {
@@ -1624,7 +1634,7 @@ export class EntityClass {
1624
1634
  });
1625
1635
  };
1626
1636
  const nodesDic = getNodesDic(data, filter);
1627
- return fn(data, []);
1637
+ return fn(data, [], 0);
1628
1638
  };
1629
1639
  this.getSortedTree = (rows, sorts) => {
1630
1640
  let sortedRows = SortArray(rows, sorts);
@@ -1811,124 +1821,113 @@ export class EntityClass {
1811
1821
  return rows;
1812
1822
  };
1813
1823
  this.traverse = (rows, callback) => {
1814
- const fn = (data, parentRows) => {
1824
+ const fn = (data, parentRows, level) => {
1815
1825
  for (let i = 0; i < data.length; i++) {
1816
1826
  let row = data[i];
1817
1827
  const isFirstChild = i === 0;
1818
1828
  const isLastChild = i === data.length - 1;
1819
- const detail = this.getRowDetailByRowAndParents(row, i, parentRows, isFirstChild, isLastChild);
1829
+ const detail = this.getRowDetailByRowAndParents(row, i, parentRows, isFirstChild, isLastChild, level);
1820
1830
  const res = callback(row, detail);
1821
1831
  if (res === false || !detail.childsLength) {
1822
1832
  continue;
1823
1833
  }
1824
1834
  const childs = this.getChilds(row);
1825
- fn(childs, [...parentRows, row]);
1835
+ fn(childs, [...parentRows, row], level + 1);
1826
1836
  }
1827
1837
  };
1828
- fn(rows, []);
1838
+ fn(rows, [], 0);
1829
1839
  };
1830
1840
  this.actions = {
1831
1841
  getId: this.getId,
1832
1842
  setId: this.setId,
1833
1843
  getChilds: this.getChilds,
1834
1844
  setChilds: this.setChilds,
1835
- getRowDetailById: this.getRowDetailById,
1836
- getFilteredTree: this.getFilteredTree,
1837
- getSortedTree: this.getSortedTree,
1838
- addRow: this.addRow,
1839
- editRow: this.editRow,
1840
- editById: this.editById,
1841
- removeRow: this.removeRow,
1842
- removeByRowIds: this.removeByRowIds,
1843
- reOrder: this.reOrder,
1844
- reOrderByDir: this.reOrderByDir,
1845
- moveTo: this.moveTo,
1846
- editByTraverse: this.editByTraverse,
1847
- traverse: this.traverse
1848
- };
1849
- this.dataActions = {
1850
- getRowDetailById: (rowId) => {
1851
- const data = this.getData();
1852
- return this.getRowDetailById(data, rowId);
1845
+ getRowDetailById: (p) => {
1846
+ const rows = p.rows || this.getData();
1847
+ return this.getRowDetailById(rows, p.rowId);
1853
1848
  },
1854
- addRow: (row, p) => {
1855
- const data = this.getData();
1856
- const res = this.addRow(data, row, p);
1857
- this.setData(res.rows);
1858
- return res;
1849
+ getFilteredTree: (p) => {
1850
+ const rows = p.rows || this.getData();
1851
+ return this.getFilteredTree(rows, p.filter);
1859
1852
  },
1860
- editRow: (newRow) => {
1861
- const data = this.getData();
1862
- const res = this.editRow(data, newRow);
1853
+ getSortedTree: (p) => {
1854
+ const rows = p.rows || this.getData();
1855
+ const res = this.getSortedTree(rows, p.sorts);
1863
1856
  this.setData(res);
1864
1857
  return res;
1865
1858
  },
1866
- editById: (rowId, change) => {
1867
- const data = this.getData();
1868
- const res = this.editById(data, rowId, change);
1869
- this.setData(res);
1859
+ addRow: (p) => {
1860
+ const rows = p.rows || this.getData();
1861
+ const res = this.addRow(rows, p.row, p);
1862
+ this.setData(res.rows);
1870
1863
  return res;
1871
1864
  },
1872
- removeRow: (rowId) => {
1873
- const data = this.getData();
1874
- const res = this.removeRow(data, rowId);
1865
+ editRow: (p) => {
1866
+ const rows = p.rows || this.getData();
1867
+ const res = this.editRow(rows, p.row);
1875
1868
  this.setData(res);
1876
1869
  return res;
1877
1870
  },
1878
- removeByRowIds: (rowIds) => {
1879
- const data = this.getData();
1880
- const res = this.removeByRowIds(data, rowIds);
1871
+ editById: (p) => {
1872
+ const rows = p.rows || this.getData();
1873
+ const res = this.editById(rows, p.rowId, p.change);
1881
1874
  this.setData(res);
1882
1875
  return res;
1883
1876
  },
1884
- reOrder: (fromId, toId) => {
1885
- const data = this.getData();
1886
- const res = this.reOrder(data, fromId, toId);
1877
+ removeRow: (p) => {
1878
+ const rows = p.rows || this.getData();
1879
+ const res = this.removeRow(rows, p.rowId);
1887
1880
  this.setData(res);
1888
1881
  return res;
1889
1882
  },
1890
- reOrderByDir: (rowId, dir) => {
1891
- const data = this.getData();
1892
- const res = this.reOrderByDir(data, rowId, dir);
1883
+ removeByRowIds: (p) => {
1884
+ const rows = p.rows || this.getData();
1885
+ const res = this.removeByRowIds(rows, p.rowIds);
1893
1886
  this.setData(res);
1894
1887
  return res;
1895
1888
  },
1896
- moveTo: (fromIds, toId) => {
1897
- const data = this.getData();
1898
- const res = this.moveTo(data, fromIds, toId);
1889
+ reOrder: (p) => {
1890
+ const rows = p.rows || this.getData();
1891
+ const res = this.reOrder(rows, p.fromId, p.toId);
1899
1892
  this.setData(res);
1900
1893
  return res;
1901
1894
  },
1902
- editByTraverse: (callback) => {
1903
- const data = this.getData();
1904
- const res = this.editByTraverse(data, callback);
1895
+ reOrderByDir: (p) => {
1896
+ const rows = p.rows || this.getData();
1897
+ const res = this.reOrderByDir(rows, p.rowId, p.dir);
1905
1898
  this.setData(res);
1906
1899
  return res;
1907
1900
  },
1908
- getSortedTree: (sorts) => {
1909
- const data = this.getData();
1910
- const res = this.getSortedTree(data, sorts);
1901
+ moveTo: (p) => {
1902
+ const rows = p.rows || this.getData();
1903
+ const res = this.moveTo(rows, p.fromIds, p.toId);
1911
1904
  this.setData(res);
1912
1905
  return res;
1913
1906
  },
1914
- getFilteredTree: (filter) => {
1915
- const data = this.getData();
1916
- const res = this.getFilteredTree(data, filter);
1907
+ editByTraverse: (p) => {
1908
+ const rows = p.rows || this.getData();
1909
+ const res = this.editByTraverse(rows, p.callback);
1917
1910
  this.setData(res);
1918
1911
  return res;
1919
1912
  },
1920
- traverse: (callback) => {
1921
- const data = this.getData();
1922
- this.traverse(data, callback);
1913
+ traverse: (p) => {
1914
+ const rows = p.rows || this.getData();
1915
+ this.traverse(rows, p.callback);
1923
1916
  }
1924
1917
  };
1925
1918
  this.map = p.map;
1926
1919
  if (p.getData) {
1927
1920
  this.getData = p.getData;
1928
1921
  }
1929
- if (p.setData) {
1930
- this.setData = p.setData;
1931
- }
1922
+ this.setData = (v) => {
1923
+ if (p.setData) {
1924
+ let newData = v;
1925
+ if (p.sorts && p.sorts.length) {
1926
+ newData = this.getSortedTree(newData, p.sorts);
1927
+ }
1928
+ p.setData(newData);
1929
+ }
1930
+ };
1932
1931
  }
1933
1932
  }
1934
1933
  export class AIOColors {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-class",
3
- "version": "2.1.5",
3
+ "version": "2.2.0",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/mohammadFeiz/aio-class#readme",
6
6
  "bugs": {