aio-class 2.0.2
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.
- package/index.d.ts +271 -0
- package/index.js +2018 -0
- package/package.json +24 -0
package/index.js
ADDED
|
@@ -0,0 +1,2018 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { AIOGeo } from 'aio-geo';
|
|
11
|
+
import AIODate from 'aio-date';
|
|
12
|
+
export { AIOGeo, AIODate };
|
|
13
|
+
const singletons = new Map();
|
|
14
|
+
class DebounceClass {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.execute = (fn, delay) => {
|
|
17
|
+
clearTimeout(this.ref);
|
|
18
|
+
this.ref = setTimeout(() => fn(), delay || 500);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export const GetSingleton = (key, factory) => {
|
|
23
|
+
if (!singletons.has(key)) {
|
|
24
|
+
singletons.set(key, factory());
|
|
25
|
+
}
|
|
26
|
+
return singletons.get(key);
|
|
27
|
+
};
|
|
28
|
+
export const Debounce = (fn, delay) => {
|
|
29
|
+
const inst = GetSingleton('debounce', () => new DebounceClass());
|
|
30
|
+
inst.execute(fn, delay);
|
|
31
|
+
};
|
|
32
|
+
export const Ellipsis = (text, length, position) => {
|
|
33
|
+
if (text.length <= length) {
|
|
34
|
+
return text;
|
|
35
|
+
}
|
|
36
|
+
if (position === 'end') {
|
|
37
|
+
return text.slice(0, length) + '...';
|
|
38
|
+
}
|
|
39
|
+
if (position === 'start') {
|
|
40
|
+
return '...' + text.slice(text.length - length, text.length);
|
|
41
|
+
}
|
|
42
|
+
const startIndex = Math.floor(length / 2);
|
|
43
|
+
const endIndex = text.length - Math.ceil(length / 2);
|
|
44
|
+
const start = text.slice(0, startIndex);
|
|
45
|
+
const end = text.slice(endIndex, text.length);
|
|
46
|
+
return (start + ' ... ' + end);
|
|
47
|
+
};
|
|
48
|
+
export const SplitNumber = (price, p) => {
|
|
49
|
+
if (price === 0) {
|
|
50
|
+
return "0";
|
|
51
|
+
}
|
|
52
|
+
if (!price) {
|
|
53
|
+
return '';
|
|
54
|
+
}
|
|
55
|
+
const { count = 3, splitter = ',', toFixed } = p || {};
|
|
56
|
+
let str = price.toString();
|
|
57
|
+
const isNeg = str[0] === '-';
|
|
58
|
+
if (isNeg) {
|
|
59
|
+
str = str.slice(1, str.length);
|
|
60
|
+
}
|
|
61
|
+
let dotIndex = str.indexOf('.');
|
|
62
|
+
let per = '';
|
|
63
|
+
if (dotIndex !== -1) {
|
|
64
|
+
per = `${str.slice(dotIndex, str.length)}`;
|
|
65
|
+
if (toFixed === 0) {
|
|
66
|
+
per = '';
|
|
67
|
+
}
|
|
68
|
+
else if (toFixed) {
|
|
69
|
+
if (per.length - 1 > toFixed) {
|
|
70
|
+
per = per.slice(0, toFixed + 1);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
str = str.slice(0, dotIndex);
|
|
74
|
+
}
|
|
75
|
+
let res = '', index = 0;
|
|
76
|
+
for (let i = str.length - 1; i >= 0; i--) {
|
|
77
|
+
res = str[i] + res;
|
|
78
|
+
if (index === count - 1) {
|
|
79
|
+
index = 0;
|
|
80
|
+
if (i > 0) {
|
|
81
|
+
res = splitter + res;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
index++;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return (isNeg ? '-' : '') + res + per;
|
|
89
|
+
};
|
|
90
|
+
export const ShuffleArray = (array) => {
|
|
91
|
+
let shuffledArray = [...array];
|
|
92
|
+
for (let i = shuffledArray.length - 1; i > 0; i--) {
|
|
93
|
+
const randomIndex = Math.floor(Math.random() * (i + 1));
|
|
94
|
+
[shuffledArray[i], shuffledArray[randomIndex]] = [shuffledArray[randomIndex], shuffledArray[i]];
|
|
95
|
+
}
|
|
96
|
+
return shuffledArray;
|
|
97
|
+
};
|
|
98
|
+
export const UrlToJson = (url) => {
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(`{"${decodeURI(url.split('?')[1]).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`);
|
|
101
|
+
}
|
|
102
|
+
catch (_a) {
|
|
103
|
+
return {};
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
export const Get2Digit = (n) => { let ns = n.toString(); return ns.length === 1 ? '0' + ns : ns; };
|
|
107
|
+
export const GetArray = (count, fn, step) => {
|
|
108
|
+
fn = fn || ((index) => index);
|
|
109
|
+
if (step) {
|
|
110
|
+
const arr = new Array(count).fill(0);
|
|
111
|
+
const res = [];
|
|
112
|
+
for (let i = 0; i < arr.length; i++) {
|
|
113
|
+
if (i % step !== 0) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
res.push(fn(i));
|
|
117
|
+
}
|
|
118
|
+
return res;
|
|
119
|
+
}
|
|
120
|
+
return new Array(count).fill(0).map((_, i) => fn ? fn(i) : i);
|
|
121
|
+
};
|
|
122
|
+
export const Normalize = (str, trim) => {
|
|
123
|
+
const res = str.replace(/\u200C/g, '').normalize();
|
|
124
|
+
return trim ? res.trim() : res;
|
|
125
|
+
};
|
|
126
|
+
export const getClockHandleAngle = (p) => {
|
|
127
|
+
let hour = (p === null || p === void 0 ? void 0 : p.hour) === undefined ? new Date().getHours() : p.hour;
|
|
128
|
+
let minute = (p === null || p === void 0 ? void 0 : p.minute) === undefined ? new Date().getMinutes() : p.minute;
|
|
129
|
+
let { offset = 0 } = p || {};
|
|
130
|
+
if (hour > 11) {
|
|
131
|
+
hour = hour - 12;
|
|
132
|
+
}
|
|
133
|
+
if (minute > 59 || minute < 0) {
|
|
134
|
+
minute = 0;
|
|
135
|
+
}
|
|
136
|
+
let hourAngle = 90 - hour * 30 - minute / 2;
|
|
137
|
+
let minuteAngle = 90 - minute * 6;
|
|
138
|
+
return [hourAngle + (offset || 0), minuteAngle + (offset || 0)];
|
|
139
|
+
};
|
|
140
|
+
export const GetRandomNumber = (from, to) => from + Math.round(Math.random() * (to - from));
|
|
141
|
+
export const ToRadians = (degree) => degree * (Math.PI / 180);
|
|
142
|
+
export const LocationDistance = (pos1, pos2, unit, round) => {
|
|
143
|
+
const R = 6371; // Radius of the Earth in kilometers
|
|
144
|
+
const [lat1, lng1] = pos1;
|
|
145
|
+
const [lat2, lng2] = pos2;
|
|
146
|
+
const dLat = ToRadians(lat2 - lat1);
|
|
147
|
+
const dLng = ToRadians(lng2 - lng1);
|
|
148
|
+
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
149
|
+
Math.cos(ToRadians(lat1)) * Math.cos(ToRadians(lat2)) *
|
|
150
|
+
Math.sin(dLng / 2) * Math.sin(dLng / 2);
|
|
151
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
152
|
+
const distance = R * c;
|
|
153
|
+
let res = distance;
|
|
154
|
+
if (unit === 'm') {
|
|
155
|
+
return res = distance * 1000;
|
|
156
|
+
}
|
|
157
|
+
return round ? Math.round(res) : res;
|
|
158
|
+
};
|
|
159
|
+
export const SortArray = (arr, sorts) => {
|
|
160
|
+
return arr.slice().sort((a, b) => {
|
|
161
|
+
for (const { getValue = (a) => a, dir = "inc" } of sorts) {
|
|
162
|
+
let va = getValue(a, dir), vb = getValue(b, dir);
|
|
163
|
+
let cmp = (typeof va === "number" && typeof vb === "number")
|
|
164
|
+
? va - vb
|
|
165
|
+
: String(va).localeCompare(String(vb));
|
|
166
|
+
if (cmp !== 0)
|
|
167
|
+
return dir === "inc" ? cmp : -cmp;
|
|
168
|
+
}
|
|
169
|
+
return 0;
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
export const ScoreText = (text, query) => {
|
|
173
|
+
text = text.toLowerCase().trim();
|
|
174
|
+
query = query.toLowerCase().trim();
|
|
175
|
+
if (!query) {
|
|
176
|
+
return 0;
|
|
177
|
+
}
|
|
178
|
+
if (text === query) {
|
|
179
|
+
return 10000;
|
|
180
|
+
}
|
|
181
|
+
if (text.startsWith(query)) {
|
|
182
|
+
return 8000 - text.length;
|
|
183
|
+
}
|
|
184
|
+
const words = text.split(/\s+/);
|
|
185
|
+
if (words.some(word => word.startsWith(query))) {
|
|
186
|
+
return 6000 - text.length;
|
|
187
|
+
}
|
|
188
|
+
const includesIndex = text.indexOf(query);
|
|
189
|
+
if (includesIndex !== -1) {
|
|
190
|
+
return 4000 - includesIndex - text.length;
|
|
191
|
+
}
|
|
192
|
+
let qi = 0;
|
|
193
|
+
let score = 0;
|
|
194
|
+
let consecutive = 0;
|
|
195
|
+
for (let i = 0; i < text.length && qi < query.length; i++) {
|
|
196
|
+
if (text[i] === query[qi]) {
|
|
197
|
+
score += 10 + consecutive * 5;
|
|
198
|
+
if (i === 0 || text[i - 1] === " ") {
|
|
199
|
+
score += 15;
|
|
200
|
+
}
|
|
201
|
+
consecutive++;
|
|
202
|
+
qi++;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
consecutive = 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (qi !== query.length) {
|
|
209
|
+
return 0;
|
|
210
|
+
}
|
|
211
|
+
return 1000 + score - text.length;
|
|
212
|
+
};
|
|
213
|
+
export const Search = (arr, searchValue, getRowValue) => {
|
|
214
|
+
const query = searchValue.trim();
|
|
215
|
+
if (!query) {
|
|
216
|
+
return arr;
|
|
217
|
+
}
|
|
218
|
+
return arr
|
|
219
|
+
.map((row, i) => ({ row, score: ScoreText(getRowValue(row, i), query) }))
|
|
220
|
+
.filter(x => x.score > 0)
|
|
221
|
+
.sort((a, b) => b.score - a.score)
|
|
222
|
+
.map(x => x.row);
|
|
223
|
+
};
|
|
224
|
+
export const IsTouch = () => "ontouchstart" in document.documentElement;
|
|
225
|
+
export const GetClient = (e, scroll) => {
|
|
226
|
+
const isTouch = IsTouch();
|
|
227
|
+
const scrollLeft = scroll ? e.target.scrollLeft : 0;
|
|
228
|
+
const scrollTop = scroll ? e.target.scrollTop : 0;
|
|
229
|
+
if (isTouch && e.changedTouches) {
|
|
230
|
+
return { x: e.changedTouches[0].clientX + scrollLeft - window.pageXOffset, y: e.changedTouches[0].clientY + scrollTop - window.pageYOffset };
|
|
231
|
+
}
|
|
232
|
+
return { x: e.clientX, y: e.clientY };
|
|
233
|
+
};
|
|
234
|
+
export const EventHandler = (selector, event, action, type = 'bind') => {
|
|
235
|
+
const eventMap = { mousedown: 'touchstart', mousemove: 'touchmove', mouseup: 'touchend', click: 'click' };
|
|
236
|
+
const fixedEvent = 'ontouchstart' in document.documentElement ? eventMap[event] : event;
|
|
237
|
+
const element = typeof selector === 'string' ? selector === 'window' ? window : document.querySelector(selector) : selector;
|
|
238
|
+
if (!element)
|
|
239
|
+
return;
|
|
240
|
+
element.removeEventListener(fixedEvent, action);
|
|
241
|
+
if (type === 'bind') {
|
|
242
|
+
element.addEventListener(fixedEvent, action);
|
|
243
|
+
}
|
|
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)); });
|
|
246
|
+
export const DetectOS = () => {
|
|
247
|
+
const userAgent = window.navigator.userAgent;
|
|
248
|
+
const platform = window.navigator.platform;
|
|
249
|
+
const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'];
|
|
250
|
+
const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
|
|
251
|
+
const iosPlatforms = ['iPhone', 'iPad', 'iPod'];
|
|
252
|
+
let os = null;
|
|
253
|
+
if (macosPlatforms.includes(platform)) {
|
|
254
|
+
os = 'macOS';
|
|
255
|
+
}
|
|
256
|
+
else if (iosPlatforms.includes(platform)) {
|
|
257
|
+
os = 'iOS';
|
|
258
|
+
}
|
|
259
|
+
else if (windowsPlatforms.includes(platform)) {
|
|
260
|
+
os = 'Windows';
|
|
261
|
+
}
|
|
262
|
+
else if (/Android/.test(userAgent)) {
|
|
263
|
+
os = 'Android';
|
|
264
|
+
}
|
|
265
|
+
else if (/Linux/.test(platform)) {
|
|
266
|
+
os = 'Linux';
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
os = 'Unknown';
|
|
270
|
+
}
|
|
271
|
+
return os;
|
|
272
|
+
};
|
|
273
|
+
const FakeNames = {
|
|
274
|
+
firstname_male_fa: [
|
|
275
|
+
'آرمان', 'آرمین', 'آراد', 'آریا', 'آرشام', 'آمین', 'آرتین',
|
|
276
|
+
'ابراهیم', 'اردلان', 'ارستو', 'ارسلان', 'ارشاد', 'اسماعیل', 'اهورا', 'ایمان', 'ایلیا', 'احمد', 'احمد رضا', 'امیر', 'امیر علی', 'امیر رضا', 'امیر حسین', 'امیر مسعود', 'امیر محمد', 'امین', 'اسفندیار', 'اکبر', 'اصفر', 'احسان',
|
|
277
|
+
'بهمن', 'باقر', 'بردیا', 'برنا', 'باراد', 'بهروز', 'بیژن', 'بهادر', 'بهنام', 'بهرنگ', 'بهداد',
|
|
278
|
+
'پویا', 'پوریا', 'پیمان', 'پدرام', 'پارسا', 'پرهام', 'پویان', 'حسن', 'حسین', 'حامد', 'حمید', 'حافظ', 'حسام',
|
|
279
|
+
'خشایار', 'داریوش', 'داوود', 'دانیال', 'رضا', 'رامین', 'رحمت', 'رحمان', 'رحیم', 'رایبد', 'رادمان', 'رادین', 'رضوان', 'ساسان', 'سامان', 'سینا', 'سعید', 'سپهر', 'سجاد', 'سورنا', 'سیروس', 'سالار', 'شریف', 'شهاب', 'شاهین', 'شایان', 'شاهد', 'شهریار', 'صادق', 'عادل', 'علی', 'عباس', 'عماد', 'عرفان', 'علی رضا', 'علی محمد',
|
|
280
|
+
'فرخ', 'فرهاد', 'فرشاد', 'فرشید', 'فرید', 'فربد', 'فرامرز', 'فرزین', 'فراز', 'فاضل', 'کاظم', 'کوروش', 'کریم',
|
|
281
|
+
'مجید', 'محمد', 'محمود', 'مصطفی', 'محسن', 'مهران', 'مهدی', 'مهبد', 'ماهور', 'ماهان', 'محمد رضا', 'محمد مهدی', 'مجید رضا', 'محمد حسین', 'مهیار',
|
|
282
|
+
'نیما', 'وحید', 'هادی', 'هامون', 'یوسف'
|
|
283
|
+
],
|
|
284
|
+
firstname_female_fa: [
|
|
285
|
+
'الناز', 'المیرا', 'الهام', 'السا', 'احترام', 'بهار', 'باران', 'بیتا', 'بهناز', 'بهاره', 'بنفشه', 'بهاره', 'پارمیدا', 'پانته آ', 'پریا', 'پروین', 'پرستو', 'پرنیا', 'پریماه', 'پریناز', 'پریسا', 'تهمینه', 'تارا', 'تینا', 'ترنم', 'ترمه', 'جمیله', 'جیران', 'جانان',
|
|
286
|
+
'حمیده', 'حسنی', 'حسنا', 'حمیرا', 'دینا', 'دلارام', 'رها', 'رویا', 'ریما', 'ریحانه', 'راضیه', 'ژیلا', 'ژینا', 'ژاله', 'سمانه', 'ساحل', 'سمیرا', 'سارا', 'سوسن', 'ساناز', 'سیما', 'سهیلا', 'سحر', 'شیما', 'شهلا', 'شهره', 'شهرزاد', 'شیلا',
|
|
287
|
+
'عاطفه', 'عطیه', 'غزل', 'غزاله', 'فرهناز', 'فریده', 'فروزنده', 'فرزانه', 'فرگل', 'فهیمه', 'فرناز', 'فرنگیس', 'فاطمه', 'فرخنده', 'فریماه', 'فرشته', 'کتایون', 'کیمیا', 'کیانا', 'مهنوش', 'مهسا', 'مهناز', 'مهشاد', 'مهشید', 'مینا', 'منا', 'مهرنوش'
|
|
288
|
+
],
|
|
289
|
+
lastname_fa: [
|
|
290
|
+
'احمدی', 'اردستانی', 'امینی', 'الهی', 'احسانی', 'اشتیاق', 'ابوالحسنی', 'اسماعیلی', 'اختیاری', 'اشراقی', 'اثباتی', 'بقایی', 'براتی', 'بینا', 'باقری', 'بهرامی', 'پیروز', 'پرویزی', 'پهلوانی', 'پیروی', 'پور غلامی', 'پور صمیمی', 'پازوکی', 'پاشایی', 'ترابی', 'تیموری', 'تشکری', 'جمالی', 'جهرمی', 'جلالی', 'جبلی', 'جباری',
|
|
291
|
+
'چنگیزی', 'حمیدی', 'حسنی', 'حسینی', 'حبیبی', 'حیدری', 'خالقی', 'خندان', 'خسروی', 'خاقانی', 'خراسانی', 'دارابی', 'دهنوی', 'دشتی', 'دهستانی', 'زمانی', 'زواری', 'زرندی', 'زنگنه', 'سامانی', 'سلیمانی', 'سپهری', 'سزاوار', 'سرمد', 'سرافراز', 'رضایی', 'رمضانی', 'رحمانی', 'رحمتی', 'رضوانی', 'ربیعی', 'رادمهر', 'ریحانی', 'رستمی', 'رواقی', 'رشیدی', 'رشادی', 'رضوی', 'رفیعی',
|
|
292
|
+
'فقیهی', 'فریدونی', 'فردوسی', 'فرامرزی', 'فرید نیا', 'فرحی', 'فرهمند', 'فرهودی', 'فهیمی', 'فراهانی', 'فاضلی', 'قهرمانی', 'قریبی', 'قوامی', 'قاسمی', 'کاظمی', 'کلهر', 'کرمی', 'کریمی', 'کوچک زاده', 'کمالی', 'کیوانی', 'کلاهدوز', 'کاملی', 'کامیاب', 'گودرزی', 'گرشاسبی', 'گایینی', 'لسانی', 'لرستانی', 'لقمانی', 'لشکری',
|
|
293
|
+
'محمدی', 'مرندی', 'محسنی', 'مجیدی', 'میثاقی', 'مروتی', 'مردانی', 'محمودی', 'مسجد جامعی', 'مصطفوی', 'محلوجی', 'مرادی', 'مهستانی', 'مرودشتی', 'مشکاتی', 'مرتضوی', 'مسعودی', 'مقصودی', 'منافی', 'مطلق', 'منطقی', 'نریمانی', 'نیشابوری', 'نیستانی', 'نبوی', 'ناصری', 'نیمایی', 'ناسوتی', 'ناطقی', 'نشتارودی', 'نوری', 'نور محمدی', 'نوایی', 'نهری', 'نامی', 'نامدار', 'نایینی',
|
|
294
|
+
'وحیدی', 'واسقی', 'ورامینی', 'واصلی', 'واحدی', 'وحدت', 'ورعی', 'والایی', 'وزیری', 'هادیان', 'همراه', 'همایونی', 'هویدا', 'هدایتی', 'هاتفی', 'هشترودی', 'هاشمی', 'یحیوی', 'یاسری', 'یادگار', 'یثربی', 'یونسی', 'یوسفی', 'یارمحمدی', 'یداللهی',
|
|
295
|
+
'درخشنده', 'درخشان', 'داوودی', 'دهقان', 'دقیقی', 'بهمنیار', 'بهادری', 'شیخ بهایی', 'شیخی', 'ساعدی', 'پور کرمی', 'ساجدی', 'لواسانی', 'بوشهری', 'باستانی', 'بستانی', 'زرین اقبال', 'اقبالی', 'امین پور', 'امانی', 'معتمدی', 'اعتمادی',
|
|
296
|
+
'اخلاقی', 'توتونچی', 'برازنده', 'چراغی', 'شاهمرادی', 'شاه محمدی',
|
|
297
|
+
'ثابت', 'سماواتی', 'شاکری', 'سالمی', 'نعیمی', 'همایونفر', 'مفیدی', 'شهیدی', 'صابری', 'امامی', 'اطمینان', 'گنابادی', 'پروری', 'خادم', 'بارانی', 'پیردوست',
|
|
298
|
+
'شهیدزاده', 'سهیلی', 'انصاری', 'صادقی', 'برقعی', 'محمد پور', 'اسفندیاری', 'فاطمی', 'فیروز آبادی', 'شیری', 'شیرودی', 'شفیعی', 'ثقفی', 'نجفی', 'نجف زاده', 'خوشبخت', 'وطن پرست', 'صارمی',
|
|
299
|
+
'طارمی', 'حاتمی', 'قمی', 'مدیری', 'اصغری', 'آذری', 'شهابی', 'داوودخانی', 'شهسواری', 'رسایی', 'همزه ای', 'دشتستانی', 'شریفی', 'کیانی', 'کی نژاد', 'نیکو سرشت', 'ساداتی', 'سروش', 'مازندرانی', 'گیلانی', 'کاردان', 'کارامد', 'فاضلیان', 'فرنیا'
|
|
300
|
+
],
|
|
301
|
+
firstname_male_en: [
|
|
302
|
+
"David", "Peter", "Thomas", "Michael", "John",
|
|
303
|
+
"James", "Robert", "William", "Daniel", "Richard",
|
|
304
|
+
"Christopher", "Matthew", "Andrew", "Joseph", "Charles",
|
|
305
|
+
"Anthony", "Mark", "Steven", "Paul", "George",
|
|
306
|
+
"Edward", "Brian", "Kevin", "Jason", "Ryan",
|
|
307
|
+
"Eric", "Jonathan", "Nicholas", "Alexander", "Benjamin",
|
|
308
|
+
"Samuel", "Henry", "Jack", "Samuel", "Oliver",
|
|
309
|
+
"Lucas", "Ethan", "Jacob", "Noah", "Liam",
|
|
310
|
+
"Logan", "Mason", "Dylan", "Nathan", "Adam",
|
|
311
|
+
"Aaron", "Justin", "Brandon", "Jason", "Jordan",
|
|
312
|
+
"Tyler", "Austin", "Christian", "Derek", "Kyle",
|
|
313
|
+
"Sean", "Scott", "Patrick", "Stephen", "Timothy",
|
|
314
|
+
"Gregory", "Jeffrey", "Gary", "Larry", "Frank",
|
|
315
|
+
"Raymond", "Dennis", "Kenneth", "Donald", "Ronald",
|
|
316
|
+
"Edward", "Steven", "Brian", "Richard", "Martin",
|
|
317
|
+
"Simon", "Jack", "Charles", "Arthur", "Albert",
|
|
318
|
+
"Louis", "Victor", "Vincent", "Patrick", "Philip",
|
|
319
|
+
"Samuel", "Max", "Leo", "Oscar", "Felix",
|
|
320
|
+
"Adrian", "Julian", "Sebastian", "Gabriel", "Nathaniel",
|
|
321
|
+
"Isaac", "Caleb", "Evan", "Owen", "Luke",
|
|
322
|
+
"Cameron", "Connor", "Colin", "Adam", "Alex",
|
|
323
|
+
"Ryan", "Tony", "Chris", "Nick", "Mike",
|
|
324
|
+
"Steve", "Alex", "Ben", "Sam", "Tom"
|
|
325
|
+
],
|
|
326
|
+
firstname_female_en: [
|
|
327
|
+
"Emma", "Olivia", "Sophia", "Ava", "Isabella",
|
|
328
|
+
"Mia", "Charlotte", "Amelia", "Harper", "Evelyn",
|
|
329
|
+
"Abigail", "Emily", "Ella", "Elizabeth", "Camila",
|
|
330
|
+
"Luna", "Sofia", "Avery", "Mila", "Aria",
|
|
331
|
+
"Scarlett", "Penelope", "Layla", "Chloe", "Victoria",
|
|
332
|
+
"Madison", "Eleanor", "Grace", "Nora", "Riley",
|
|
333
|
+
"Zoey", "Hannah", "Hazel", "Lily", "Ellie",
|
|
334
|
+
"Violet", "Lillian", "Zoe", "Aurora", "Natalie",
|
|
335
|
+
"Emilia", "Everly", "Leah", "Aubrey", "Willow",
|
|
336
|
+
"Addison", "Lucy", "Audrey", "Bella", "Claire",
|
|
337
|
+
"Skylar", "Ivy", "Stella", "Samantha", "Caroline",
|
|
338
|
+
"Genesis", "Maya", "Valentina", "Ruby", "Naomi",
|
|
339
|
+
"Alice", "Elena", "Clara", "Vivian", "Eva",
|
|
340
|
+
"Sarah", "Anna", "Julia", "Sophie", "Ariana",
|
|
341
|
+
"Madeline", "Lydia", "Peyton", "Delilah", "Kennedy",
|
|
342
|
+
"Maria", "Isla", "Eliza", "Rose", "Cecilia",
|
|
343
|
+
"Daisy", "Adeline", "Melanie", "Maya", "Iris",
|
|
344
|
+
"Elise", "Jasmine", "Nicole", "Natalia", "Laura",
|
|
345
|
+
"Rebecca", "Victoria", "Rachel", "Lauren", "Amy",
|
|
346
|
+
"Jessica", "Jennifer", "Michelle", "Ashley", "Amanda",
|
|
347
|
+
"Melissa", "Stephanie", "Kimberly", "Christina", "Angela",
|
|
348
|
+
"Diana", "Catherine", "Alexandra", "Gabriella", "Victoria",
|
|
349
|
+
"Isabel", "Alina", "Adriana", "Camille", "Naomi",
|
|
350
|
+
"Claudia", "Bianca", "Lara", "Elise", "Monica"
|
|
351
|
+
],
|
|
352
|
+
lastname_en: [
|
|
353
|
+
"Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis", "Rodriguez", "Martinez", "Hernandez",
|
|
354
|
+
"Lopez", "Gonzalez", "Wilson", "Anderson", "Thomas", "Taylor", "Moore", "Jackson", "Martin", "Lee",
|
|
355
|
+
"Perez", "Thompson", "White", "Harris", "Sanchez", "Clark", "Ramirez", "Lewis", "Robinson", "Walker", "Young",
|
|
356
|
+
"Allen", "King", "Wright", "Scott", "Torres", "Nguyen", "Hill", "Flores", "Green", "Adams", "Nelson",
|
|
357
|
+
"Baker", "Hall", "Rivera", "Campbell", "Mitchell", "Carter", "Roberts", "Gomez", "Phillips", "Evans", "Turner",
|
|
358
|
+
"Diaz", "Parker", "Cruz", "Edwards", "Collins", "Reyes", "Stewart", "Morris", "Morales", "Murphy", "Cook",
|
|
359
|
+
"Rogers", "Gutierrez", "Ortiz", "Morgan", "Cooper", "Peterson", "Bailey", "Reed", "Kelly", "Howard", "Ramos",
|
|
360
|
+
"Kim", "Cox", "Ward", "Richardson", "Watson", "Brooks", "Chavez", "Wood", "James", "Bennett", "Gray",
|
|
361
|
+
"Mendoza", "Ruiz", "Hughes", "Price", "Alvarez", "Castillo", "Sanders", "Patel", "Myers", "Long", "Ross",
|
|
362
|
+
"Foster", "Jimenez", "Powell", "Jenkins", "Perry", "Russell", "Sullivan", "Bell", "Coleman", "Butler", "Henderson",
|
|
363
|
+
"Barnes", "Fisher", "Vasquez", "Simmons", "Romero", "Jordan", "Patterson", "Alexander", "Hamilton", "Graham"
|
|
364
|
+
]
|
|
365
|
+
};
|
|
366
|
+
export const FakeName = (p) => {
|
|
367
|
+
const grn = GetRandomNumber;
|
|
368
|
+
const getfirstnames = () => {
|
|
369
|
+
if (p.gender === 'male') {
|
|
370
|
+
return FakeNames[`firstname_male_${p.lang}`];
|
|
371
|
+
}
|
|
372
|
+
else if (p.gender === 'female') {
|
|
373
|
+
return FakeNames[`firstname_female_${p.lang}`];
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
return [...FakeNames[`firstname_male_${p.lang}`], ...FakeNames[`firstname_female_${p.lang}`]];
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
const getfirstname = () => {
|
|
380
|
+
const firstnames = getfirstnames();
|
|
381
|
+
const index = grn(0, firstnames.length - 1);
|
|
382
|
+
return firstnames[index];
|
|
383
|
+
};
|
|
384
|
+
const getlastname = () => {
|
|
385
|
+
const lastnames = FakeNames[`lastname_${p.lang}`];
|
|
386
|
+
const index = grn(0, lastnames.length - 1);
|
|
387
|
+
return lastnames[index];
|
|
388
|
+
};
|
|
389
|
+
if (p.type === "firstname") {
|
|
390
|
+
return getfirstname();
|
|
391
|
+
}
|
|
392
|
+
if (p.type === "lastname") {
|
|
393
|
+
return getlastname();
|
|
394
|
+
}
|
|
395
|
+
return `${getfirstname()} ${getlastname()}`;
|
|
396
|
+
};
|
|
397
|
+
export const IsEqual = (a, b) => {
|
|
398
|
+
if (a === b)
|
|
399
|
+
return true;
|
|
400
|
+
if (typeof a !== typeof b)
|
|
401
|
+
return false;
|
|
402
|
+
if (typeof a !== 'object' || a === null || b === null)
|
|
403
|
+
return false;
|
|
404
|
+
const aKeys = Object.keys(a), bKeys = Object.keys(b);
|
|
405
|
+
if (aKeys.length !== bKeys.length)
|
|
406
|
+
return false;
|
|
407
|
+
for (const key of aKeys) {
|
|
408
|
+
if (!b.hasOwnProperty(key))
|
|
409
|
+
return false;
|
|
410
|
+
if (!IsEqual(a[key], b[key]))
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
return true;
|
|
414
|
+
};
|
|
415
|
+
export const AddQueryParamsToUrl = (url, params) => {
|
|
416
|
+
const queryParts = [];
|
|
417
|
+
function serialize(obj, parentKey = '') {
|
|
418
|
+
for (const key in obj) {
|
|
419
|
+
if (obj.hasOwnProperty(key)) {
|
|
420
|
+
const fullKey = parentKey ? `${parentKey}.${key}` : key;
|
|
421
|
+
const value = obj[key];
|
|
422
|
+
if (typeof value === 'object' && value !== null) {
|
|
423
|
+
queryParts.push(`${encodeURIComponent(fullKey)}=${encodeURIComponent(JSON.stringify(value))}`);
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
queryParts.push(`${encodeURIComponent(fullKey)}=${encodeURIComponent(value)}`);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
serialize(params);
|
|
432
|
+
if (queryParts.length > 0) {
|
|
433
|
+
url += (url.includes('?') ? '&' : '?') + queryParts.join('&');
|
|
434
|
+
}
|
|
435
|
+
return url;
|
|
436
|
+
};
|
|
437
|
+
export const IsNetworkDisconnected = (errorResponse) => __awaiter(void 0, void 0, void 0, function* () {
|
|
438
|
+
const msg = typeof errorResponse.message === "string" ? errorResponse.message : "";
|
|
439
|
+
if (msg.includes("Failed to fetch"))
|
|
440
|
+
return true;
|
|
441
|
+
if (errorResponse.isAxiosError && msg === "Network Error")
|
|
442
|
+
return true;
|
|
443
|
+
if (errorResponse.request && !errorResponse.response)
|
|
444
|
+
return true;
|
|
445
|
+
if (msg.includes("ERR_INTERNET_DISCONNECTED") || msg.includes("ERR_NETWORK_CHANGED"))
|
|
446
|
+
return true;
|
|
447
|
+
if (typeof navigator !== "undefined" && !navigator.onLine)
|
|
448
|
+
return true;
|
|
449
|
+
return false;
|
|
450
|
+
});
|
|
451
|
+
export const ReOrder = (data, dragIndex, dropIndex) => {
|
|
452
|
+
if (!Array.isArray(data)) {
|
|
453
|
+
console.error('reOrder first parameter(data) should be array');
|
|
454
|
+
}
|
|
455
|
+
if (typeof dragIndex !== 'number') {
|
|
456
|
+
console.error('reOrder second parameter(dragIndex) should be array');
|
|
457
|
+
}
|
|
458
|
+
if (typeof dropIndex !== 'number') {
|
|
459
|
+
console.error('reOrder third parameter(dropIndex) should be array');
|
|
460
|
+
}
|
|
461
|
+
let from = data[dragIndex];
|
|
462
|
+
let newData = data.filter((o, i) => {
|
|
463
|
+
if (o) {
|
|
464
|
+
return i !== dragIndex;
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
newData.splice(dropIndex, 0, from);
|
|
471
|
+
return newData;
|
|
472
|
+
};
|
|
473
|
+
export const ClassListToString = (classes) => {
|
|
474
|
+
let list = [];
|
|
475
|
+
const getClasses = (cls) => {
|
|
476
|
+
if (Array.isArray(cls)) {
|
|
477
|
+
return cls.filter((o) => !!o && typeof o === 'string');
|
|
478
|
+
}
|
|
479
|
+
else if (!!cls && typeof cls === 'string') {
|
|
480
|
+
return cls.split(' ');
|
|
481
|
+
}
|
|
482
|
+
return [];
|
|
483
|
+
};
|
|
484
|
+
for (let i = 0; i < classes.length; i++) {
|
|
485
|
+
const cls = classes[i];
|
|
486
|
+
list = [...list, ...getClasses(cls)];
|
|
487
|
+
}
|
|
488
|
+
return list.length ? list.join(' ') : '';
|
|
489
|
+
};
|
|
490
|
+
export const AddToAttrs = (attrs, p) => {
|
|
491
|
+
attrs = attrs || {};
|
|
492
|
+
const className = ClassListToString([attrs.className, p === null || p === void 0 ? void 0 : p.className]);
|
|
493
|
+
let newStyle = Object.assign(Object.assign({}, attrs.style), p === null || p === void 0 ? void 0 : p.style);
|
|
494
|
+
const res = Object.assign(Object.assign(Object.assign({}, attrs), { className, style: newStyle }), p === null || p === void 0 ? void 0 : p.attrs);
|
|
495
|
+
return res;
|
|
496
|
+
};
|
|
497
|
+
export const StyleObjectToString = (styleObject) => {
|
|
498
|
+
return Object.entries(styleObject)
|
|
499
|
+
.map(([key, value]) => {
|
|
500
|
+
// تبدیل camelCase به kebab-case
|
|
501
|
+
const kebabKey = key.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`);
|
|
502
|
+
return `${kebabKey}:${value}`;
|
|
503
|
+
})
|
|
504
|
+
.join(';');
|
|
505
|
+
};
|
|
506
|
+
export const GetValueByField = (data, field, def) => {
|
|
507
|
+
if (!data || !field)
|
|
508
|
+
return def;
|
|
509
|
+
try {
|
|
510
|
+
const parts = field.split(".");
|
|
511
|
+
let result = data;
|
|
512
|
+
for (const part of parts) {
|
|
513
|
+
if (/^\d+$/.test(part)) {
|
|
514
|
+
const index = parseInt(part, 10);
|
|
515
|
+
if (!Array.isArray(result))
|
|
516
|
+
return def;
|
|
517
|
+
result = result[index];
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
if (result == null || !(part in result))
|
|
521
|
+
return def;
|
|
522
|
+
result = result[part];
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return result === undefined ? def : result;
|
|
526
|
+
}
|
|
527
|
+
catch (_a) {
|
|
528
|
+
return def;
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
export const SetValueByField = (data = {}, field, value) => {
|
|
532
|
+
try {
|
|
533
|
+
field = field.replace(/\[/g, '.').replace(/\]/g, '');
|
|
534
|
+
}
|
|
535
|
+
catch (_a) { }
|
|
536
|
+
let fields = field.split('.');
|
|
537
|
+
let node = data;
|
|
538
|
+
for (let i = 0; i < fields.length - 1; i++) {
|
|
539
|
+
let f = fields[i];
|
|
540
|
+
if (!f) {
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
if (node[f] === undefined || node[f] === null) {
|
|
544
|
+
if (isNaN(+fields[i + 1])) {
|
|
545
|
+
node[f] = {};
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
node[f] = [];
|
|
549
|
+
}
|
|
550
|
+
node = node[f];
|
|
551
|
+
}
|
|
552
|
+
else {
|
|
553
|
+
node = node[f];
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
node[fields[fields.length - 1]] = value;
|
|
557
|
+
return data;
|
|
558
|
+
};
|
|
559
|
+
export const DisabledContextMenu = () => { window.addEventListener(`contextmenu`, (e) => e.preventDefault()); };
|
|
560
|
+
export const ArrayToObject = (keys, fn) => {
|
|
561
|
+
return Object.assign({}, ...keys.map((key) => ({ [key]: fn(key) })));
|
|
562
|
+
};
|
|
563
|
+
export const TextFilter = (p) => {
|
|
564
|
+
let value = p.value.toString();
|
|
565
|
+
if (!value) {
|
|
566
|
+
return '';
|
|
567
|
+
}
|
|
568
|
+
if (p.maxLength && value.length > p.maxLength) {
|
|
569
|
+
value = value.slice(0, p.maxLength);
|
|
570
|
+
}
|
|
571
|
+
const charMapArabicToPersian = {
|
|
572
|
+
// Arabic digits
|
|
573
|
+
"٠": "0", "١": "1", "٢": "2", "٣": "3", "٤": "4",
|
|
574
|
+
"٥": "5", "٦": "6", "٧": "7", "٨": "8", "٩": "9",
|
|
575
|
+
// Arabic letters
|
|
576
|
+
"ي": "ی",
|
|
577
|
+
"ك": "ک",
|
|
578
|
+
"ة": "ه",
|
|
579
|
+
"ى": "ی",
|
|
580
|
+
};
|
|
581
|
+
const charMapLatinNumbers = {
|
|
582
|
+
// Persian digits
|
|
583
|
+
"۰": "0", "۱": "1", "۲": "2", "۳": "3", "۴": "4",
|
|
584
|
+
"۵": "5", "۶": "6", "۷": "7", "۸": "8", "۹": "9",
|
|
585
|
+
"٠": "0", "١": "1", "٢": "2", "٣": "3", "٤": "4",
|
|
586
|
+
"٥": "5", "٦": "6", "٧": "7", "٨": "8", "٩": "9",
|
|
587
|
+
};
|
|
588
|
+
if (p.arabicToPersian) {
|
|
589
|
+
value = value.replace(/[۰-۹٠-٩يكةى]/g, c => { var _a; return (_a = charMapArabicToPersian[c]) !== null && _a !== void 0 ? _a : c; });
|
|
590
|
+
}
|
|
591
|
+
if (p.latinNumbers) {
|
|
592
|
+
value = value.replace(/[۰-۹٠-٩]/g, c => { var _a; return (_a = charMapLatinNumbers[c]) !== null && _a !== void 0 ? _a : c; });
|
|
593
|
+
}
|
|
594
|
+
if (p.chars || p.number !== undefined || p.symbol !== undefined || p.letter !== undefined) {
|
|
595
|
+
let res = '';
|
|
596
|
+
for (let i = 0; i < value.length; i++) {
|
|
597
|
+
const isNumber = !isNaN(+value[i]) && value[i] !== ' ';
|
|
598
|
+
const isSymbol = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]+/.test(value[i]);
|
|
599
|
+
const isLetter = !isNumber && !isSymbol;
|
|
600
|
+
let isOk = true;
|
|
601
|
+
if (p.number === true && !isNumber) {
|
|
602
|
+
isOk = false;
|
|
603
|
+
}
|
|
604
|
+
if (p.number === false && isNumber) {
|
|
605
|
+
isOk = false;
|
|
606
|
+
}
|
|
607
|
+
if (p.symbol === true && !isSymbol) {
|
|
608
|
+
isOk = false;
|
|
609
|
+
}
|
|
610
|
+
if (p.symbol === false && isSymbol) {
|
|
611
|
+
isOk = false;
|
|
612
|
+
}
|
|
613
|
+
if (p.letter === true && !isLetter) {
|
|
614
|
+
isOk = false;
|
|
615
|
+
}
|
|
616
|
+
if (p.letter === false && isLetter) {
|
|
617
|
+
isOk = false;
|
|
618
|
+
}
|
|
619
|
+
if (p.chars && p.chars[value[i]] !== undefined) {
|
|
620
|
+
if (p.chars[value[i]] === true) {
|
|
621
|
+
isOk = true;
|
|
622
|
+
}
|
|
623
|
+
if (p.chars[value[i]] === false) {
|
|
624
|
+
isOk = false;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (isOk) {
|
|
628
|
+
res = res + value[i];
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return res;
|
|
632
|
+
}
|
|
633
|
+
return value;
|
|
634
|
+
};
|
|
635
|
+
export const GetErrorByRequired = (p) => {
|
|
636
|
+
const isNull = p.value === undefined || p.value === '' || p.value === null;
|
|
637
|
+
if (isNull && p.required) {
|
|
638
|
+
return p.fa ? `${p.label} ضروری است` : `${p.label} is required`;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
export const GetCurrentLocation = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
642
|
+
if (!(navigator === null || navigator === void 0 ? void 0 : navigator.geolocation) || !(navigator === null || navigator === void 0 ? void 0 : navigator.permissions)) {
|
|
643
|
+
return { success: false, errorMessage: 'Geolocation is not supported in this environment.' };
|
|
644
|
+
}
|
|
645
|
+
try {
|
|
646
|
+
const status = yield navigator.permissions.query({ name: "geolocation" });
|
|
647
|
+
if (status.state === 'denied') {
|
|
648
|
+
return { success: false, errorMessage: 'Location permission was denied.', };
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
catch ( /* Safari & friends: intentionally ignored*/_a) { /* Safari & friends: intentionally ignored*/ }
|
|
652
|
+
return new Promise((resolve) => {
|
|
653
|
+
navigator.geolocation.getCurrentPosition((position) => { resolve({ success: true, errorMessage: '', latitude: position.coords.latitude, longitude: position.coords.longitude, }); }, (error) => {
|
|
654
|
+
let message = 'Failed to retrieve location.';
|
|
655
|
+
switch (error.code) {
|
|
656
|
+
case error.PERMISSION_DENIED:
|
|
657
|
+
message = 'Location permission was denied by the user.';
|
|
658
|
+
break;
|
|
659
|
+
case error.POSITION_UNAVAILABLE:
|
|
660
|
+
message = 'Location information is unavailable.';
|
|
661
|
+
break;
|
|
662
|
+
case error.TIMEOUT:
|
|
663
|
+
message = 'Location request timed out.';
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
resolve({ success: false, errorMessage: message, });
|
|
667
|
+
}, Object.assign({ enableHighAccuracy: true, timeout: 10000, maximumAge: 0 }, options));
|
|
668
|
+
});
|
|
669
|
+
});
|
|
670
|
+
export const PriceToText = (num) => {
|
|
671
|
+
if (num === 0)
|
|
672
|
+
return "صفر";
|
|
673
|
+
const yekan = ["", "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه"];
|
|
674
|
+
const dahgan = ["", "ده", "بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود"];
|
|
675
|
+
const sadgan = ["", "یکصد", "دویست", "سیصد", "چهارصد", "پانصد", "ششصد", "هفتصد", "هشتصد", "نهصد"];
|
|
676
|
+
const dahYek = ["ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده"];
|
|
677
|
+
const parts = [];
|
|
678
|
+
function convertThreeDigits(n) {
|
|
679
|
+
let str = "";
|
|
680
|
+
const sad = Math.floor(n / 100);
|
|
681
|
+
const dah = Math.floor((n % 100) / 10);
|
|
682
|
+
const yek = n % 10;
|
|
683
|
+
if (sad)
|
|
684
|
+
str += sadgan[sad];
|
|
685
|
+
if (dah === 1) {
|
|
686
|
+
str += (str ? " و " : "") + dahYek[yek];
|
|
687
|
+
}
|
|
688
|
+
else {
|
|
689
|
+
if (dah)
|
|
690
|
+
str += (str ? " و " : "") + dahgan[dah];
|
|
691
|
+
if (yek)
|
|
692
|
+
str += (str ? " و " : "") + yekan[yek];
|
|
693
|
+
}
|
|
694
|
+
return str;
|
|
695
|
+
}
|
|
696
|
+
const units = ["", "هزار", "میلیون", "میلیارد", "تریلیون"];
|
|
697
|
+
let i = 0;
|
|
698
|
+
while (num > 0) {
|
|
699
|
+
const chunk = num % 1000;
|
|
700
|
+
if (chunk) {
|
|
701
|
+
const chunkText = convertThreeDigits(chunk);
|
|
702
|
+
parts.unshift(chunkText + (units[i] ? " " + units[i] : ""));
|
|
703
|
+
}
|
|
704
|
+
num = Math.floor(num / 1000);
|
|
705
|
+
i++;
|
|
706
|
+
}
|
|
707
|
+
return parts.join(" و ");
|
|
708
|
+
};
|
|
709
|
+
export const GenerateId = (type) => {
|
|
710
|
+
const grn = GetRandomNumber;
|
|
711
|
+
const num = grn(10000000, 99999999);
|
|
712
|
+
return (type === 'string' ? ('id' + num) : num);
|
|
713
|
+
};
|
|
714
|
+
export class FileClass {
|
|
715
|
+
constructor() {
|
|
716
|
+
this.isUrl = (input) => input.startsWith("http://") || input.startsWith("https://") || input.startsWith("blob:");
|
|
717
|
+
this.isDataUrl = (input) => input.startsWith("data:");
|
|
718
|
+
this.toDataUrl = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
719
|
+
const blobRes = yield this.toBlob(input);
|
|
720
|
+
if (!blobRes.success) {
|
|
721
|
+
return { success: false, result: "", message: blobRes.message };
|
|
722
|
+
}
|
|
723
|
+
const fr = new FileReader();
|
|
724
|
+
return new Promise((resolve) => {
|
|
725
|
+
fr.onload = () => resolve({ success: true, result: fr.result, message: "" });
|
|
726
|
+
fr.onerror = () => resolve({ success: false, result: "", message: "FileReader error" });
|
|
727
|
+
fr.readAsDataURL(blobRes.result);
|
|
728
|
+
});
|
|
729
|
+
});
|
|
730
|
+
this.fileToText = (file) => __awaiter(this, void 0, void 0, function* () {
|
|
731
|
+
return new Promise((resolve, reject) => {
|
|
732
|
+
const fr = new FileReader();
|
|
733
|
+
fr.onload = () => {
|
|
734
|
+
try {
|
|
735
|
+
const result = fr.result;
|
|
736
|
+
resolve(result);
|
|
737
|
+
}
|
|
738
|
+
catch (error) {
|
|
739
|
+
reject(new Error('Error parsing JSON: ' + error.message));
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
fr.onerror = () => reject(new Error('Error reading file.'));
|
|
743
|
+
fr.readAsText(file);
|
|
744
|
+
});
|
|
745
|
+
});
|
|
746
|
+
this.readText = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
747
|
+
try {
|
|
748
|
+
const blobRes = yield this.toBlob(input);
|
|
749
|
+
if (!blobRes.success)
|
|
750
|
+
return blobRes;
|
|
751
|
+
let result = yield blobRes.result.text();
|
|
752
|
+
return { success: true, result, message: "" };
|
|
753
|
+
}
|
|
754
|
+
catch (e) {
|
|
755
|
+
return { success: false, result: "", message: (e === null || e === void 0 ? void 0 : e.message) || `read text failed` };
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
this.toSrc = (file) => typeof file === 'string' ? file : window.URL.createObjectURL(file);
|
|
759
|
+
this.toBase64 = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
760
|
+
const dataUrl = yield this.toDataUrl(input);
|
|
761
|
+
if (!dataUrl.success) {
|
|
762
|
+
return { success: false, result: "", message: dataUrl.message };
|
|
763
|
+
}
|
|
764
|
+
return this.dataUrlToBase64(dataUrl.result);
|
|
765
|
+
});
|
|
766
|
+
this.GetDurationByUrl = (url) => __awaiter(this, void 0, void 0, function* () {
|
|
767
|
+
return new Promise((resolve) => {
|
|
768
|
+
const video = document.createElement("video");
|
|
769
|
+
video.preload = "metadata";
|
|
770
|
+
video.src = url;
|
|
771
|
+
video.onloadedmetadata = () => { resolve(isNaN(video.duration) ? null : video.duration); video.remove(); };
|
|
772
|
+
video.onerror = () => { resolve(null); video.remove(); };
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
this.GetSizeByUrl = (url) => __awaiter(this, void 0, void 0, function* () {
|
|
776
|
+
let size = null;
|
|
777
|
+
try {
|
|
778
|
+
const res = yield fetch(url, { method: "HEAD" });
|
|
779
|
+
const contentLength = res.headers.get("content-length");
|
|
780
|
+
if (contentLength)
|
|
781
|
+
size = parseInt(contentLength, 10);
|
|
782
|
+
}
|
|
783
|
+
catch (err) { }
|
|
784
|
+
return size;
|
|
785
|
+
});
|
|
786
|
+
this.GetNameByUrl = (url) => { var _a; return decodeURIComponent(((_a = url.split("/").pop()) === null || _a === void 0 ? void 0 : _a.split("?")[0]) || "unknown"); };
|
|
787
|
+
this.GetFileDetailsByUrl = (url) => __awaiter(this, void 0, void 0, function* () {
|
|
788
|
+
const name = this.GetNameByUrl(url);
|
|
789
|
+
const size = yield this.GetSizeByUrl(url);
|
|
790
|
+
const duration = yield this.GetDurationByUrl(url);
|
|
791
|
+
return { name, size, duration };
|
|
792
|
+
});
|
|
793
|
+
this.fetchBlob = (url) => __awaiter(this, void 0, void 0, function* () {
|
|
794
|
+
try {
|
|
795
|
+
const res = yield fetch(url);
|
|
796
|
+
if (!res.ok) {
|
|
797
|
+
return { success: false, result: null, message: `HTTP Error: ${res.status}` };
|
|
798
|
+
}
|
|
799
|
+
const blob = yield res.blob();
|
|
800
|
+
return { success: true, result: blob, message: "" };
|
|
801
|
+
}
|
|
802
|
+
catch (e) {
|
|
803
|
+
return { success: false, result: null, message: (e === null || e === void 0 ? void 0 : e.message) || "Fetch failed" };
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
this.fetchBlob2 = () => __awaiter(this, void 0, void 0, function* () {
|
|
807
|
+
});
|
|
808
|
+
this.base64ToBlob = (base64) => {
|
|
809
|
+
try {
|
|
810
|
+
const byteCharacters = atob(base64);
|
|
811
|
+
const byteNumbers = new Array(byteCharacters.length);
|
|
812
|
+
for (let i = 0; i < byteCharacters.length; i++) {
|
|
813
|
+
byteNumbers[i] = byteCharacters.charCodeAt(i);
|
|
814
|
+
}
|
|
815
|
+
const byteArray = new Uint8Array(byteNumbers);
|
|
816
|
+
return { success: true, result: new Blob([byteArray]), message: "" };
|
|
817
|
+
}
|
|
818
|
+
catch (e) {
|
|
819
|
+
return { success: false, result: null, message: (e === null || e === void 0 ? void 0 : e.message) || "Invalid base64" };
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
this.handleFileName = (selectedName) => selectedName || window.prompt('Please Enter File Name') || `file-${Date.now()}.json`;
|
|
823
|
+
this.generateFileName = (mimeType) => {
|
|
824
|
+
const map = {
|
|
825
|
+
'application/json': 'file.json',
|
|
826
|
+
'text/plain': 'file.txt',
|
|
827
|
+
'text/html': 'file.html',
|
|
828
|
+
'text/csv': 'file.csv',
|
|
829
|
+
'application/pdf': 'file.pdf',
|
|
830
|
+
'image/png': 'image.png',
|
|
831
|
+
'image/jpeg': 'image.jpg',
|
|
832
|
+
'image/webp': 'image.webp',
|
|
833
|
+
'application/zip': 'archive.zip',
|
|
834
|
+
'application/octet-stream': 'file.bin'
|
|
835
|
+
};
|
|
836
|
+
const base = map[mimeType || ''] || 'file.bin';
|
|
837
|
+
const timestamp = Date.now();
|
|
838
|
+
const [name, ext] = base.split('.');
|
|
839
|
+
return `${name}-${timestamp}.${ext}`;
|
|
840
|
+
};
|
|
841
|
+
this.saveWeb = (url, fileName) => __awaiter(this, void 0, void 0, function* () {
|
|
842
|
+
const a = document.createElement('a');
|
|
843
|
+
a.href = url;
|
|
844
|
+
a.download = fileName;
|
|
845
|
+
document.body.appendChild(a);
|
|
846
|
+
a.click();
|
|
847
|
+
document.body.removeChild(a);
|
|
848
|
+
URL.revokeObjectURL(url);
|
|
849
|
+
return { success: true, message: '' };
|
|
850
|
+
});
|
|
851
|
+
this.dataUrlToBase64 = (dataUrl) => {
|
|
852
|
+
try {
|
|
853
|
+
const base64 = dataUrl.split(",")[1];
|
|
854
|
+
return { success: true, result: base64, message: "" };
|
|
855
|
+
}
|
|
856
|
+
catch (_a) {
|
|
857
|
+
return { success: false, result: "", message: "Invalid data url" };
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
this.toBlob = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
861
|
+
try {
|
|
862
|
+
if (input instanceof Blob) {
|
|
863
|
+
return { success: true, result: input, message: "" };
|
|
864
|
+
}
|
|
865
|
+
if (input instanceof ArrayBuffer) {
|
|
866
|
+
const result = new Blob([input], { type: 'application/octet-stream' });
|
|
867
|
+
return { success: true, result };
|
|
868
|
+
}
|
|
869
|
+
if (input instanceof Uint8Array) {
|
|
870
|
+
const buffer = input.buffer instanceof ArrayBuffer ? input.buffer : new ArrayBuffer(input.byteLength);
|
|
871
|
+
const result = new Blob([new Uint8Array(buffer)], { type: 'application/octet-stream' });
|
|
872
|
+
return { success: true, result };
|
|
873
|
+
}
|
|
874
|
+
if (typeof input !== "string") {
|
|
875
|
+
const result = new Blob([JSON.stringify(input)], { type: 'application/json' });
|
|
876
|
+
return { success: true, result, message: "" };
|
|
877
|
+
}
|
|
878
|
+
if (this.isDataUrl(input)) {
|
|
879
|
+
const base64 = this.dataUrlToBase64(input);
|
|
880
|
+
if (!base64.success) {
|
|
881
|
+
return { success: false, result: null, message: base64.message };
|
|
882
|
+
}
|
|
883
|
+
return this.base64ToBlob(base64.result);
|
|
884
|
+
}
|
|
885
|
+
if (this.isUrl(input)) {
|
|
886
|
+
return yield this.fetchBlob(input);
|
|
887
|
+
}
|
|
888
|
+
return { success: false, result: null, message: "Unsupported input" };
|
|
889
|
+
}
|
|
890
|
+
catch (e) {
|
|
891
|
+
return { success: false, result: null, message: e === null || e === void 0 ? void 0 : e.message };
|
|
892
|
+
}
|
|
893
|
+
});
|
|
894
|
+
this.saveText = (text, path) => __awaiter(this, void 0, void 0, function* () {
|
|
895
|
+
try {
|
|
896
|
+
const blob = new Blob([text], { type: "text/plain" });
|
|
897
|
+
const url = URL.createObjectURL(blob);
|
|
898
|
+
try {
|
|
899
|
+
return yield this.saveWeb(url, path);
|
|
900
|
+
}
|
|
901
|
+
finally {
|
|
902
|
+
URL.revokeObjectURL(url);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
catch (e) {
|
|
906
|
+
return { success: false, message: (e === null || e === void 0 ? void 0 : e.message) || "Text download failed" };
|
|
907
|
+
}
|
|
908
|
+
});
|
|
909
|
+
this.saveFile = (input, path) => __awaiter(this, void 0, void 0, function* () {
|
|
910
|
+
const blobRes = yield new FileClass().toBlob(input);
|
|
911
|
+
if (!blobRes.success) {
|
|
912
|
+
return { success: false, message: blobRes.message };
|
|
913
|
+
}
|
|
914
|
+
const url = URL.createObjectURL(blobRes.result);
|
|
915
|
+
yield this.saveWeb(url, path);
|
|
916
|
+
return { success: true, message: '' };
|
|
917
|
+
});
|
|
918
|
+
this.sizeToString = (size) => {
|
|
919
|
+
if (typeof size !== "number")
|
|
920
|
+
size = 0;
|
|
921
|
+
const gb = size / (Math.pow(1024, 3));
|
|
922
|
+
const mb = size / (Math.pow(1024, 2));
|
|
923
|
+
const kb = size / 1024;
|
|
924
|
+
if (gb >= 1)
|
|
925
|
+
return gb.toFixed(2) + " GB";
|
|
926
|
+
if (mb >= 1)
|
|
927
|
+
return mb.toFixed(2) + " MB";
|
|
928
|
+
if (kb >= 1)
|
|
929
|
+
return kb.toFixed(2) + " KB";
|
|
930
|
+
return size + " byte";
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
export class StorageClass {
|
|
935
|
+
constructor(id, context) {
|
|
936
|
+
var _a, _b;
|
|
937
|
+
this.copy = (v) => JSON.parse(JSON.stringify(v));
|
|
938
|
+
this.setModel = (model) => {
|
|
939
|
+
this.model = model;
|
|
940
|
+
if (typeof localStorage !== 'undefined') {
|
|
941
|
+
localStorage.setItem('storageClass' + this.id, JSON.stringify(model));
|
|
942
|
+
}
|
|
943
|
+
return this.copy(model);
|
|
944
|
+
};
|
|
945
|
+
this.getNow = () => new Date().getTime();
|
|
946
|
+
this.init = () => {
|
|
947
|
+
if (typeof localStorage === 'undefined')
|
|
948
|
+
return;
|
|
949
|
+
let storage = localStorage.getItem('storageClass' + this.id);
|
|
950
|
+
const model = storage ? JSON.parse(storage) : {};
|
|
951
|
+
this.setModel(this.removeExpireds(model));
|
|
952
|
+
};
|
|
953
|
+
this.save = (field, value, expiredIn) => {
|
|
954
|
+
if (value === undefined)
|
|
955
|
+
return this.copy(this.model);
|
|
956
|
+
const newModel = Object.assign({}, this.model);
|
|
957
|
+
newModel[field] = { value, saveTime: this.getNow(), expiredIn: expiredIn !== null && expiredIn !== void 0 ? expiredIn : Infinity };
|
|
958
|
+
return this.setModel(newModel);
|
|
959
|
+
};
|
|
960
|
+
this.remove = (field) => {
|
|
961
|
+
const newModel = {};
|
|
962
|
+
for (let prop in this.model) {
|
|
963
|
+
if (prop !== field)
|
|
964
|
+
newModel[prop] = this.model[prop];
|
|
965
|
+
}
|
|
966
|
+
return this.setModel(newModel);
|
|
967
|
+
};
|
|
968
|
+
this.isExpired = (field, model) => {
|
|
969
|
+
model = model || this.model;
|
|
970
|
+
if (!model[field])
|
|
971
|
+
return true;
|
|
972
|
+
if (typeof model[field].expiredIn !== 'number')
|
|
973
|
+
return false;
|
|
974
|
+
return model[field].expiredIn < this.getNow();
|
|
975
|
+
};
|
|
976
|
+
this.removeExpireds = (model) => {
|
|
977
|
+
const res = {};
|
|
978
|
+
for (let field in model) {
|
|
979
|
+
if (!this.isExpired(field, model))
|
|
980
|
+
res[field] = model[field];
|
|
981
|
+
}
|
|
982
|
+
return res;
|
|
983
|
+
};
|
|
984
|
+
this.load = (field, def, expiredIn) => {
|
|
985
|
+
const obj = this.model[field];
|
|
986
|
+
if (!obj) {
|
|
987
|
+
if (def !== undefined)
|
|
988
|
+
this.save(field, def, expiredIn);
|
|
989
|
+
return def;
|
|
990
|
+
}
|
|
991
|
+
if (this.isExpired(field)) {
|
|
992
|
+
this.save(field, def, expiredIn);
|
|
993
|
+
return def;
|
|
994
|
+
}
|
|
995
|
+
return obj.value;
|
|
996
|
+
};
|
|
997
|
+
this.clear = () => this.setModel({});
|
|
998
|
+
this.getKeys = () => Object.keys(this.model);
|
|
999
|
+
this.getList = () => this.getKeys().map((key) => this.model[key].value);
|
|
1000
|
+
this.dump = () => {
|
|
1001
|
+
const key = 'storageClass' + this.id;
|
|
1002
|
+
const data = this.copy(this.model);
|
|
1003
|
+
return {
|
|
1004
|
+
__CACHE__: {
|
|
1005
|
+
[key]: data
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
1008
|
+
};
|
|
1009
|
+
this.update = (data) => {
|
|
1010
|
+
if (data)
|
|
1011
|
+
this.setModel(data);
|
|
1012
|
+
};
|
|
1013
|
+
this.model = {};
|
|
1014
|
+
this.id = id;
|
|
1015
|
+
// اگر context پاس داده شده و initialCache وجود دارد، مستقیم hydrate انجام بده
|
|
1016
|
+
if ((_b = (_a = context === null || context === void 0 ? void 0 : context.query) === null || _a === void 0 ? void 0 : _a.initialCache) === null || _b === void 0 ? void 0 : _b.__CACHE__) {
|
|
1017
|
+
const initialCache = context.query.initialCache.__CACHE__;
|
|
1018
|
+
let myCacheData = initialCache['storageClass' + this.id];
|
|
1019
|
+
if (!myCacheData) {
|
|
1020
|
+
myCacheData = {};
|
|
1021
|
+
}
|
|
1022
|
+
this.setModel(myCacheData);
|
|
1023
|
+
}
|
|
1024
|
+
else {
|
|
1025
|
+
this.init(); // حالت معمول client (localStorage)
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
export function Validate(param) {
|
|
1030
|
+
const { value, fa, label = '' } = param;
|
|
1031
|
+
const IsJustNumber = () => /^\d+$/.test(param.value);
|
|
1032
|
+
const HasNumber = (str) => /\d/.test(str);
|
|
1033
|
+
const HasLetter = (str) => /[a-zA-Z]/.test(str);
|
|
1034
|
+
const HasUppercase = (str) => /[A-Z]/.test(str);
|
|
1035
|
+
const HasLowercase = (str) => /[a-z]/.test(str);
|
|
1036
|
+
const HasSymbol = (str) => /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]+/.test(str);
|
|
1037
|
+
const HasSpace = (str) => {
|
|
1038
|
+
const index = str.indexOf(' ');
|
|
1039
|
+
return index !== -1;
|
|
1040
|
+
};
|
|
1041
|
+
const getValidationOptions = (validation, type, types) => {
|
|
1042
|
+
const { value } = param;
|
|
1043
|
+
if (types) {
|
|
1044
|
+
const valueType = Array.isArray(value) ? 'array' : typeof value;
|
|
1045
|
+
if (types.indexOf(valueType) === -1) {
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
if (typeof validation !== 'string') {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
const index = validation.indexOf(type);
|
|
1053
|
+
if (index !== 0 && index !== 1) {
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
const camaIndex = validation.indexOf(',');
|
|
1057
|
+
const operatorStr = validation.slice(0, camaIndex);
|
|
1058
|
+
const targetStr = validation.slice(camaIndex + 1);
|
|
1059
|
+
if (!targetStr) {
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
const not = operatorStr[0] === '!';
|
|
1063
|
+
const targets = targetStr.split('|');
|
|
1064
|
+
const operator = operatorStr[operatorStr.length - 1];
|
|
1065
|
+
return { not, targets, operator };
|
|
1066
|
+
};
|
|
1067
|
+
const isTargetSpecial = (target) => {
|
|
1068
|
+
return ['space', 'number', 'letter', 'uppercase', 'lowercase', 'symbol'].indexOf(target) !== -1;
|
|
1069
|
+
};
|
|
1070
|
+
const checkContainSpecial = (str, target) => {
|
|
1071
|
+
if (target === 'space' && !HasSpace(str)) {
|
|
1072
|
+
return false;
|
|
1073
|
+
}
|
|
1074
|
+
if (target === 'number' && !HasNumber(str)) {
|
|
1075
|
+
return false;
|
|
1076
|
+
}
|
|
1077
|
+
if (target === 'letter' && !HasLetter(str)) {
|
|
1078
|
+
return false;
|
|
1079
|
+
}
|
|
1080
|
+
if (target === 'uppercase' && !HasUppercase(str)) {
|
|
1081
|
+
return false;
|
|
1082
|
+
}
|
|
1083
|
+
if (target === 'lowercase' && !HasLowercase(str)) {
|
|
1084
|
+
return false;
|
|
1085
|
+
}
|
|
1086
|
+
if (target === 'symbol' && !HasSymbol(str)) {
|
|
1087
|
+
return false;
|
|
1088
|
+
}
|
|
1089
|
+
return true;
|
|
1090
|
+
};
|
|
1091
|
+
const email = () => {
|
|
1092
|
+
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
1093
|
+
return !!emailRegex.test(param.value);
|
|
1094
|
+
};
|
|
1095
|
+
const irNationalCode = () => {
|
|
1096
|
+
if (!/^\d{10}$/.test(param.value))
|
|
1097
|
+
return false;
|
|
1098
|
+
const check = parseInt(param.value[9], 10);
|
|
1099
|
+
let sum = 0;
|
|
1100
|
+
for (let i = 0; i < 9; i++) {
|
|
1101
|
+
sum += parseInt(param.value[i], 10) * (10 - i);
|
|
1102
|
+
}
|
|
1103
|
+
const remainder = sum % 11;
|
|
1104
|
+
return (remainder < 2 && check === remainder) || (remainder >= 2 && check === 11 - remainder);
|
|
1105
|
+
};
|
|
1106
|
+
const irMobile = () => {
|
|
1107
|
+
if (value.indexOf('09') !== 0) {
|
|
1108
|
+
return fa ? `${label} باید با 09 شروع شود` : `${label} should start with 09`;
|
|
1109
|
+
}
|
|
1110
|
+
if (value.length < 11) {
|
|
1111
|
+
return fa ? `${label} باید 11 رقم باشد` : `${label} should be 11 character`;
|
|
1112
|
+
}
|
|
1113
|
+
if (!IsJustNumber()) {
|
|
1114
|
+
return fa ? `${label} باید فقط شامل عدد باشد` : `${label} should be contain just numbers`;
|
|
1115
|
+
}
|
|
1116
|
+
};
|
|
1117
|
+
const length = (validation) => {
|
|
1118
|
+
const options = getValidationOptions(validation, 'length', ['string', 'number', 'array']);
|
|
1119
|
+
if (!options) {
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
const { not, targets, operator } = options;
|
|
1123
|
+
if (['=', '>', '<'].indexOf(operator) === -1) {
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
const isMatchUnit = (target) => {
|
|
1127
|
+
const getValueStr = () => {
|
|
1128
|
+
if (Array.isArray(value)) {
|
|
1129
|
+
let res = '';
|
|
1130
|
+
for (let i = 0; i < value.length; i++) {
|
|
1131
|
+
res += 'a';
|
|
1132
|
+
}
|
|
1133
|
+
return res;
|
|
1134
|
+
}
|
|
1135
|
+
if (typeof value === 'number') {
|
|
1136
|
+
return value.toString();
|
|
1137
|
+
}
|
|
1138
|
+
return value;
|
|
1139
|
+
};
|
|
1140
|
+
const valueStr = getValueStr();
|
|
1141
|
+
let res = true;
|
|
1142
|
+
if (operator === '=' && valueStr.length !== +target) {
|
|
1143
|
+
res = false;
|
|
1144
|
+
}
|
|
1145
|
+
if (operator === '>' && valueStr.length <= +target) {
|
|
1146
|
+
res = false;
|
|
1147
|
+
}
|
|
1148
|
+
if (operator === '<' && valueStr.length >= +target) {
|
|
1149
|
+
res = false;
|
|
1150
|
+
}
|
|
1151
|
+
return not ? !res : res;
|
|
1152
|
+
};
|
|
1153
|
+
const isMatch = () => {
|
|
1154
|
+
for (let i = 0; i < targets.length; i++) {
|
|
1155
|
+
if (isMatchUnit(targets[i])) {
|
|
1156
|
+
return true;
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
return false;
|
|
1160
|
+
};
|
|
1161
|
+
const getText = () => {
|
|
1162
|
+
const operatorStr = {
|
|
1163
|
+
en: {
|
|
1164
|
+
'=': { be: 'should be', not: 'should not be' },
|
|
1165
|
+
'>': { be: 'should be more than', not: 'should not be more than' },
|
|
1166
|
+
'<': { be: 'should be less than', not: 'should not be less than' }
|
|
1167
|
+
},
|
|
1168
|
+
fa: {
|
|
1169
|
+
'=': { be: 'باید', not: 'نباید' },
|
|
1170
|
+
'>': { be: 'باید بیش از', not: 'نباید بیش از' },
|
|
1171
|
+
'<': { be: 'باید کمتر از', not: 'نباید کمتر از' }
|
|
1172
|
+
}
|
|
1173
|
+
}[fa ? 'fa' : 'en'][operator][not ? 'not' : 'be'];
|
|
1174
|
+
const entityName = Array.isArray(value) ? (fa ? 'مورد' : 'item') : (fa ? 'کاراکتر' : 'characters');
|
|
1175
|
+
const after = fa ? ' باشد' : '';
|
|
1176
|
+
return `${label} ${operatorStr} ${targets.join(fa ? ' یا ' : ' or ')} ${entityName}${after}`;
|
|
1177
|
+
};
|
|
1178
|
+
return isMatch() ? undefined : getText();
|
|
1179
|
+
};
|
|
1180
|
+
const contain = (validation) => {
|
|
1181
|
+
const options = getValidationOptions(validation, 'contain', ['string', 'array']);
|
|
1182
|
+
if (!options) {
|
|
1183
|
+
return;
|
|
1184
|
+
}
|
|
1185
|
+
const { not, targets } = options;
|
|
1186
|
+
const isMatchUnit = (not, target) => {
|
|
1187
|
+
let res = true;
|
|
1188
|
+
if (typeof value === 'string') {
|
|
1189
|
+
if (isTargetSpecial(target)) {
|
|
1190
|
+
if (!checkContainSpecial(value, target)) {
|
|
1191
|
+
res = false;
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
else {
|
|
1195
|
+
if (value.indexOf(target) === -1) {
|
|
1196
|
+
res = false;
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
else {
|
|
1201
|
+
if (value.indexOf(target) === -1) {
|
|
1202
|
+
res = false;
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
return not ? !res : res;
|
|
1206
|
+
};
|
|
1207
|
+
const isMatch = () => {
|
|
1208
|
+
for (let i = 0; i < targets.length; i++) {
|
|
1209
|
+
if (isMatchUnit(not, targets[i])) {
|
|
1210
|
+
return true;
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
return false;
|
|
1214
|
+
};
|
|
1215
|
+
const getText = () => {
|
|
1216
|
+
const operatorStr = {
|
|
1217
|
+
en: { be: 'should be contain', not: 'should not be contain' },
|
|
1218
|
+
fa: { be: 'باید شامل', not: 'نباید شامل' }
|
|
1219
|
+
}[fa ? 'fa' : 'en'][not ? 'not' : 'be'];
|
|
1220
|
+
const after = fa ? ' باشد' : '';
|
|
1221
|
+
return `${label} ${operatorStr} ${targets.join(fa ? ' یا ' : ' or ')}${after}`;
|
|
1222
|
+
};
|
|
1223
|
+
return isMatch() ? undefined : getText();
|
|
1224
|
+
};
|
|
1225
|
+
const startEnd = (validation, type) => {
|
|
1226
|
+
const options = getValidationOptions(validation, type, ['string']);
|
|
1227
|
+
if (!options) {
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
const { not, targets } = options;
|
|
1231
|
+
const isMatchUnit = (target) => {
|
|
1232
|
+
let res = true;
|
|
1233
|
+
const char = type === "start" ? value[0] : value[value.length - 1];
|
|
1234
|
+
if (isTargetSpecial(target)) {
|
|
1235
|
+
if (!checkContainSpecial(char, target)) {
|
|
1236
|
+
res = false;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
else if (value.length < target.length) {
|
|
1240
|
+
res = false;
|
|
1241
|
+
}
|
|
1242
|
+
else if (type === "start") {
|
|
1243
|
+
if (value.indexOf(target) !== 0) {
|
|
1244
|
+
res = false;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
else {
|
|
1248
|
+
if (value.slice(-target.length) !== target) {
|
|
1249
|
+
res = false;
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
return not ? !res : res;
|
|
1253
|
+
};
|
|
1254
|
+
const isMatch = () => {
|
|
1255
|
+
for (let i = 0; i < targets.length; i++) {
|
|
1256
|
+
if (isMatchUnit(targets[i])) {
|
|
1257
|
+
return true;
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
return false;
|
|
1261
|
+
};
|
|
1262
|
+
const getText = () => {
|
|
1263
|
+
const faOperator = type === 'start' ? 'شروع' : 'تمام';
|
|
1264
|
+
if (!fa) {
|
|
1265
|
+
return `${label} should${not ? ' not' : ''} ${type} with ${targets.join(' or ')}`;
|
|
1266
|
+
}
|
|
1267
|
+
else {
|
|
1268
|
+
return `${label} ${not ? 'نباید' : 'باید'} با ${targets.join(' یا ')} ${faOperator} شود`;
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
return isMatch() ? undefined : getText();
|
|
1272
|
+
};
|
|
1273
|
+
const lessMore = (validation, type) => {
|
|
1274
|
+
const options = getValidationOptions(validation, type, ['number']);
|
|
1275
|
+
if (!options) {
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
const { not, targets } = options;
|
|
1279
|
+
const isMatchUnit = (target) => {
|
|
1280
|
+
let res = true;
|
|
1281
|
+
const targetNum = +target;
|
|
1282
|
+
if (isNaN(targetNum)) {
|
|
1283
|
+
return false;
|
|
1284
|
+
}
|
|
1285
|
+
if (type === "less" && value >= targetNum) {
|
|
1286
|
+
res = false;
|
|
1287
|
+
}
|
|
1288
|
+
if (type === "more" && value <= targetNum) {
|
|
1289
|
+
res = false;
|
|
1290
|
+
}
|
|
1291
|
+
return not ? !res : res;
|
|
1292
|
+
};
|
|
1293
|
+
const isMatch = () => {
|
|
1294
|
+
for (let i = 0; i < targets.length; i++) {
|
|
1295
|
+
if (isMatchUnit(targets[i])) {
|
|
1296
|
+
return true;
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
return false;
|
|
1300
|
+
};
|
|
1301
|
+
const getText = () => {
|
|
1302
|
+
const faOperator = type === 'less' ? 'کمتر از' : 'بیشتر از';
|
|
1303
|
+
if (!fa) {
|
|
1304
|
+
return `${label} should${not ? ' not be' : ' be'} ${type} than ${targets.join(' or ')}`;
|
|
1305
|
+
}
|
|
1306
|
+
else {
|
|
1307
|
+
return `${label} ${not ? 'نباید' : 'باید'} ${faOperator} ${targets.join(' یا ')} باشد`;
|
|
1308
|
+
}
|
|
1309
|
+
};
|
|
1310
|
+
return isMatch() ? undefined : getText();
|
|
1311
|
+
};
|
|
1312
|
+
const equal = (validation) => {
|
|
1313
|
+
const options = getValidationOptions(validation, 'equal', ['string', 'number', 'booalean', 'undefined', null]);
|
|
1314
|
+
if (!options) {
|
|
1315
|
+
return;
|
|
1316
|
+
}
|
|
1317
|
+
const { not, targets } = options;
|
|
1318
|
+
const isMatchUnit = (not, target) => {
|
|
1319
|
+
let res = true;
|
|
1320
|
+
if (value !== target) {
|
|
1321
|
+
res = false;
|
|
1322
|
+
}
|
|
1323
|
+
return not ? !res : res;
|
|
1324
|
+
};
|
|
1325
|
+
const isMatch = () => {
|
|
1326
|
+
for (let i = 0; i < targets.length; i++) {
|
|
1327
|
+
if (isMatchUnit(not, targets[i])) {
|
|
1328
|
+
return true;
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
return false;
|
|
1332
|
+
};
|
|
1333
|
+
const getText = () => {
|
|
1334
|
+
const operatorStr = {
|
|
1335
|
+
en: { be: 'should be equal', not: 'should not be equal' },
|
|
1336
|
+
fa: { be: 'باید برابر', not: 'نباید برابر' }
|
|
1337
|
+
}[fa ? 'fa' : 'en'][not ? 'not' : 'be'];
|
|
1338
|
+
const after = fa ? ' باشد' : '';
|
|
1339
|
+
return `${label} ${operatorStr} ${targets.join(fa ? ' یا ' : ' or ')}${after}`;
|
|
1340
|
+
};
|
|
1341
|
+
return isMatch() ? undefined : getText();
|
|
1342
|
+
};
|
|
1343
|
+
const getPatternList = (pattern) => {
|
|
1344
|
+
let patternText = pattern;
|
|
1345
|
+
const list = [];
|
|
1346
|
+
while (patternText !== '') {
|
|
1347
|
+
if (patternText.indexOf('_n_') === 0) {
|
|
1348
|
+
list.push('number');
|
|
1349
|
+
patternText = patternText.slice(3);
|
|
1350
|
+
}
|
|
1351
|
+
else if (patternText.indexOf('_l_') === 0) {
|
|
1352
|
+
list.push('letter');
|
|
1353
|
+
patternText = patternText.slice(3);
|
|
1354
|
+
}
|
|
1355
|
+
else if (patternText.indexOf('_s_') === 0) {
|
|
1356
|
+
list.push('symbol');
|
|
1357
|
+
patternText = patternText.slice(3);
|
|
1358
|
+
}
|
|
1359
|
+
else if (patternText.indexOf('_[') === 0) {
|
|
1360
|
+
const closeIndex = patternText.indexOf(']');
|
|
1361
|
+
const listStr = patternText.slice(2, closeIndex);
|
|
1362
|
+
const listArray = listStr.split(',');
|
|
1363
|
+
list.push(listArray);
|
|
1364
|
+
patternText = patternText.slice(closeIndex + 2);
|
|
1365
|
+
}
|
|
1366
|
+
else if (patternText) {
|
|
1367
|
+
list.push(patternText[0]);
|
|
1368
|
+
patternText = patternText.slice(1);
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
return list;
|
|
1372
|
+
};
|
|
1373
|
+
const patternMatch = (pattern) => {
|
|
1374
|
+
const list = getPatternList(pattern);
|
|
1375
|
+
if (value.length !== list.length) {
|
|
1376
|
+
return false;
|
|
1377
|
+
}
|
|
1378
|
+
for (let i = 0; i < value.length; i++) {
|
|
1379
|
+
const char = value[i];
|
|
1380
|
+
const pattern = list[i];
|
|
1381
|
+
if (pattern === 'number') {
|
|
1382
|
+
if (!HasNumber(char)) {
|
|
1383
|
+
return false;
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
else if (pattern === 'letter') {
|
|
1387
|
+
if (!HasLetter(char)) {
|
|
1388
|
+
return false;
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
else if (pattern === 'symbol') {
|
|
1392
|
+
if (!HasSymbol(char)) {
|
|
1393
|
+
return false;
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
else if (Array.isArray(pattern)) {
|
|
1397
|
+
if (pattern.indexOf(char) === -1) {
|
|
1398
|
+
return false;
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
else {
|
|
1402
|
+
if (char !== pattern) {
|
|
1403
|
+
return false;
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
return true;
|
|
1408
|
+
};
|
|
1409
|
+
const pattern = (validation) => {
|
|
1410
|
+
if (!value || !validation) {
|
|
1411
|
+
return;
|
|
1412
|
+
}
|
|
1413
|
+
const options = getValidationOptions(validation, 'pattern', ['string']);
|
|
1414
|
+
if (!options) {
|
|
1415
|
+
return;
|
|
1416
|
+
}
|
|
1417
|
+
const { not, targets } = options;
|
|
1418
|
+
const isMatchUnit = (not, target) => {
|
|
1419
|
+
let res = true;
|
|
1420
|
+
if (!!target && !patternMatch(target)) {
|
|
1421
|
+
res = false;
|
|
1422
|
+
}
|
|
1423
|
+
return not ? !res : res;
|
|
1424
|
+
};
|
|
1425
|
+
const isMatch = () => {
|
|
1426
|
+
for (let i = 0; i < targets.length; i++) {
|
|
1427
|
+
if (isMatchUnit(not, targets[i])) {
|
|
1428
|
+
return true;
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
return false;
|
|
1432
|
+
};
|
|
1433
|
+
const getText = () => {
|
|
1434
|
+
if (fa) {
|
|
1435
|
+
return `فرمت ${label} صحیح نیست`;
|
|
1436
|
+
}
|
|
1437
|
+
return `${label} format is not correct`;
|
|
1438
|
+
};
|
|
1439
|
+
return isMatch() ? undefined : getText();
|
|
1440
|
+
};
|
|
1441
|
+
const validateUnit = (validation) => {
|
|
1442
|
+
if (validation === 'required' && (value === '' || value === undefined)) {
|
|
1443
|
+
return fa ? `${label} ضروری است` : `${label} is required`;
|
|
1444
|
+
}
|
|
1445
|
+
if (validation === 'email' && !email()) {
|
|
1446
|
+
return fa ? `فرمت ${label} صحیح نیست` : `${label} format is incorrect`;
|
|
1447
|
+
}
|
|
1448
|
+
if (validation === "irNationalCode" && !irNationalCode()) {
|
|
1449
|
+
return fa ? `فرمت ${label} صحیح نیست` : `${label} format is incorrect`;
|
|
1450
|
+
}
|
|
1451
|
+
if (validation === 'irMobile') {
|
|
1452
|
+
return irMobile();
|
|
1453
|
+
}
|
|
1454
|
+
const lessResult = lessMore(validation, 'less');
|
|
1455
|
+
if (lessResult) {
|
|
1456
|
+
return lessResult;
|
|
1457
|
+
}
|
|
1458
|
+
const moreResult = lessMore(validation, 'more');
|
|
1459
|
+
if (moreResult) {
|
|
1460
|
+
return moreResult;
|
|
1461
|
+
}
|
|
1462
|
+
const lengthResult = length(validation);
|
|
1463
|
+
if (lengthResult) {
|
|
1464
|
+
return lengthResult;
|
|
1465
|
+
}
|
|
1466
|
+
const containResult = contain(validation);
|
|
1467
|
+
if (containResult) {
|
|
1468
|
+
return containResult;
|
|
1469
|
+
}
|
|
1470
|
+
const startResult = startEnd(validation, 'start');
|
|
1471
|
+
if (startResult) {
|
|
1472
|
+
return startResult;
|
|
1473
|
+
}
|
|
1474
|
+
const endResult = startEnd(validation, 'end');
|
|
1475
|
+
if (endResult) {
|
|
1476
|
+
return endResult;
|
|
1477
|
+
}
|
|
1478
|
+
const equalResult = equal(validation);
|
|
1479
|
+
if (equalResult) {
|
|
1480
|
+
return equalResult;
|
|
1481
|
+
}
|
|
1482
|
+
const patternResult = pattern(validation);
|
|
1483
|
+
if (patternResult) {
|
|
1484
|
+
return patternResult;
|
|
1485
|
+
}
|
|
1486
|
+
};
|
|
1487
|
+
const validate = (validation) => {
|
|
1488
|
+
if (validation && !Array.isArray(validation)) {
|
|
1489
|
+
return validateUnit(validation);
|
|
1490
|
+
}
|
|
1491
|
+
for (let i = 0; i < validation.length; i++) {
|
|
1492
|
+
const res = validate(validation[i]);
|
|
1493
|
+
if (res) {
|
|
1494
|
+
return res;
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
};
|
|
1498
|
+
return validate(param.validation);
|
|
1499
|
+
}
|
|
1500
|
+
export class EntityClass {
|
|
1501
|
+
constructor(p) {
|
|
1502
|
+
this.getData = () => [];
|
|
1503
|
+
this.setData = () => { };
|
|
1504
|
+
this.getId = (row) => this.params.getId(row);
|
|
1505
|
+
this.setId = (row) => this.params.setId(row);
|
|
1506
|
+
this.getChilds = (row) => this.params.getChilds(row);
|
|
1507
|
+
this.setChilds = (row, childs) => this.params.setChilds(row, childs);
|
|
1508
|
+
this.getRowDetailsByIds = (data, rowIds) => {
|
|
1509
|
+
const fn = (data, parents, temp) => {
|
|
1510
|
+
const isFoundAll = () => Object.keys(temp).filter((rowId) => temp[rowId] === false).length === 0;
|
|
1511
|
+
for (let i = 0; i < data.length; i++) {
|
|
1512
|
+
if (isFoundAll()) {
|
|
1513
|
+
return;
|
|
1514
|
+
}
|
|
1515
|
+
const isFirstChild = i === 0;
|
|
1516
|
+
const isLastChild = i === data.length - 1;
|
|
1517
|
+
const row = data[i];
|
|
1518
|
+
const rowId = this.getId(row);
|
|
1519
|
+
if (temp[rowId.toString()] === false) {
|
|
1520
|
+
temp[rowId.toString()] = this.getRowDetailByRowAndParents(row, i, parents, isFirstChild, isLastChild);
|
|
1521
|
+
}
|
|
1522
|
+
const childs = this.getChilds(row);
|
|
1523
|
+
if (childs && childs.length) {
|
|
1524
|
+
fn(childs, [...parents, row], temp);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
};
|
|
1528
|
+
let temp = {};
|
|
1529
|
+
for (let i = 0; i < rowIds.length; i++) {
|
|
1530
|
+
temp[rowIds[i].toString()] = false;
|
|
1531
|
+
}
|
|
1532
|
+
fn(data, [], temp);
|
|
1533
|
+
return temp;
|
|
1534
|
+
};
|
|
1535
|
+
this.getRowDetailByRowAndParents = (row, index, parentRows, isFirstChild, isLastChild) => {
|
|
1536
|
+
const rowId = this.getId(row);
|
|
1537
|
+
const childs = this.getChilds(row);
|
|
1538
|
+
const childsLength = childs ? childs.length : 0;
|
|
1539
|
+
const parentRow = parentRows.length ? parentRows[parentRows.length - 1] : false;
|
|
1540
|
+
const parentId = parentRow ? this.getId(parentRow) : false;
|
|
1541
|
+
const isLeaf = !childs;
|
|
1542
|
+
const parentChilds = parentRow ? this.getChilds(parentRow) : false;
|
|
1543
|
+
let beforeSibling = false;
|
|
1544
|
+
let afterSibling = false;
|
|
1545
|
+
if (parentChilds) {
|
|
1546
|
+
beforeSibling = parentChilds[index - 1] || false;
|
|
1547
|
+
afterSibling = parentChilds[index + 1] || false;
|
|
1548
|
+
}
|
|
1549
|
+
return { row, parentRows, rowId, childsLength, parentId, isLeaf, parentRow, index, beforeSibling, afterSibling, isFirstChild, isLastChild };
|
|
1550
|
+
};
|
|
1551
|
+
this.getRowByAddChild = (parentRow, row, index) => {
|
|
1552
|
+
const childs = this.getChilds(parentRow) || [];
|
|
1553
|
+
const newChilds = [...childs];
|
|
1554
|
+
newChilds.splice(index === undefined ? newChilds.length : index, 0, row);
|
|
1555
|
+
return this.setChilds(parentRow, newChilds);
|
|
1556
|
+
};
|
|
1557
|
+
this.removeChildOfParent = (parentRow, rowId) => {
|
|
1558
|
+
let childs = this.getChilds(parentRow);
|
|
1559
|
+
if (!childs || !childs.length) {
|
|
1560
|
+
return parentRow;
|
|
1561
|
+
}
|
|
1562
|
+
childs = childs.filter((o) => this.getId(o).toString() !== rowId.toString());
|
|
1563
|
+
return this.setChilds(parentRow, childs);
|
|
1564
|
+
};
|
|
1565
|
+
this.getRowDetailById = (data, rowId) => {
|
|
1566
|
+
const fn = (data, rowId, parents, temp) => {
|
|
1567
|
+
if (temp.res) {
|
|
1568
|
+
return;
|
|
1569
|
+
}
|
|
1570
|
+
for (let i = 0; i < data.length; i++) {
|
|
1571
|
+
const isFirstChild = i === 0;
|
|
1572
|
+
const isLastChild = i === data.length - 1;
|
|
1573
|
+
const row = data[i];
|
|
1574
|
+
const childs = this.getChilds(row);
|
|
1575
|
+
if (this.getId(row).toString() === rowId.toString()) {
|
|
1576
|
+
temp.res = this.getRowDetailByRowAndParents(row, i, parents, isFirstChild, isLastChild);
|
|
1577
|
+
break;
|
|
1578
|
+
}
|
|
1579
|
+
else {
|
|
1580
|
+
if (childs && childs.length) {
|
|
1581
|
+
fn(childs, rowId, [...parents, row], temp);
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
};
|
|
1586
|
+
let temp = { res: false };
|
|
1587
|
+
fn(data, rowId, [], temp);
|
|
1588
|
+
return temp.res ? temp.res : false;
|
|
1589
|
+
};
|
|
1590
|
+
this.getFilteredTree = (data, filter) => {
|
|
1591
|
+
const getNodesDic = (data, filter) => {
|
|
1592
|
+
const detailDic = {};
|
|
1593
|
+
this.traverse(data, (row, detail) => {
|
|
1594
|
+
const res = filter(row, detail);
|
|
1595
|
+
if (res === false) {
|
|
1596
|
+
return false;
|
|
1597
|
+
}
|
|
1598
|
+
if (res === true) {
|
|
1599
|
+
for (let i = 0; i < detail.parentRows.length; i++) {
|
|
1600
|
+
const rowId = this.getId(detail.parentRows[i]);
|
|
1601
|
+
detailDic[rowId] = true;
|
|
1602
|
+
}
|
|
1603
|
+
detailDic[this.getId(row).toString()] = true;
|
|
1604
|
+
}
|
|
1605
|
+
});
|
|
1606
|
+
return detailDic;
|
|
1607
|
+
};
|
|
1608
|
+
const fn = (data, parentRows) => {
|
|
1609
|
+
const res = data.filter((row, i) => {
|
|
1610
|
+
const isFirstChild = i === 0;
|
|
1611
|
+
const isLastChild = i === data.length - 1;
|
|
1612
|
+
const detail = this.getRowDetailByRowAndParents(row, i, parentRows, isFirstChild, isLastChild);
|
|
1613
|
+
return !!nodesDic[detail.rowId.toString()];
|
|
1614
|
+
});
|
|
1615
|
+
return res.map((row) => {
|
|
1616
|
+
const childs = this.getChilds(row);
|
|
1617
|
+
if (childs) {
|
|
1618
|
+
const newChilds = fn(childs, (parentRows || []).concat(row));
|
|
1619
|
+
return this.setChilds(row, newChilds);
|
|
1620
|
+
}
|
|
1621
|
+
else {
|
|
1622
|
+
return row;
|
|
1623
|
+
}
|
|
1624
|
+
});
|
|
1625
|
+
};
|
|
1626
|
+
const nodesDic = getNodesDic(data, filter);
|
|
1627
|
+
return fn(data, []);
|
|
1628
|
+
};
|
|
1629
|
+
this.sortData = (rows, sorts) => {
|
|
1630
|
+
let sortedRows = SortArray(rows, sorts);
|
|
1631
|
+
return sortedRows.map((o) => {
|
|
1632
|
+
const childs = this.getChilds(o);
|
|
1633
|
+
if (!childs || !childs.length) {
|
|
1634
|
+
return o;
|
|
1635
|
+
}
|
|
1636
|
+
const newChilds = this.sortData(childs, sorts);
|
|
1637
|
+
return this.setChilds(Object.assign({}, o), newChilds);
|
|
1638
|
+
});
|
|
1639
|
+
};
|
|
1640
|
+
this.addRow = (data, row, p) => {
|
|
1641
|
+
const { parentId, index } = p;
|
|
1642
|
+
const newRow = this.setId(row);
|
|
1643
|
+
if (parentId !== undefined) {
|
|
1644
|
+
const detail = this.getRowDetailById(data, parentId);
|
|
1645
|
+
if (!detail) {
|
|
1646
|
+
return { rows: data, row };
|
|
1647
|
+
}
|
|
1648
|
+
const parent = this.getRowByAddChild(detail.row, newRow);
|
|
1649
|
+
const newData = this.editRow(data, parent);
|
|
1650
|
+
return { rows: newData, row: newRow };
|
|
1651
|
+
}
|
|
1652
|
+
else {
|
|
1653
|
+
const newData = [...data];
|
|
1654
|
+
newData.splice(index === undefined ? newData.length : index, 0, newRow);
|
|
1655
|
+
return { rows: newData, row: newRow };
|
|
1656
|
+
}
|
|
1657
|
+
};
|
|
1658
|
+
this.editRow = (data, newRow) => {
|
|
1659
|
+
const rowId = this.getId(newRow);
|
|
1660
|
+
const detail = this.getRowDetailById(data, rowId);
|
|
1661
|
+
const parentRow = detail.parentRow;
|
|
1662
|
+
if (parentRow) {
|
|
1663
|
+
const childs = this.getChilds(parentRow);
|
|
1664
|
+
const newChilds = childs.map((o) => this.getId(o).toString() === rowId.toString() ? newRow : o);
|
|
1665
|
+
const newParentRow = this.setChilds(parentRow, newChilds);
|
|
1666
|
+
return this.editRow(data, newParentRow);
|
|
1667
|
+
}
|
|
1668
|
+
else {
|
|
1669
|
+
return data.map((o) => this.getId(o).toString() === this.getId(newRow).toString() ? newRow : o);
|
|
1670
|
+
}
|
|
1671
|
+
};
|
|
1672
|
+
this.editById = (data, rowId, change) => {
|
|
1673
|
+
const detail = this.getRowDetailById(data, rowId);
|
|
1674
|
+
if (!detail) {
|
|
1675
|
+
return data;
|
|
1676
|
+
}
|
|
1677
|
+
const newRow = Object.assign(Object.assign({}, detail.row), change);
|
|
1678
|
+
return this.editRow(data, newRow);
|
|
1679
|
+
};
|
|
1680
|
+
this.removeRow = (data, rowId) => {
|
|
1681
|
+
let detail = this.getRowDetailById(data, rowId);
|
|
1682
|
+
if (!detail) {
|
|
1683
|
+
return data;
|
|
1684
|
+
}
|
|
1685
|
+
let parentRow = detail.parentRow;
|
|
1686
|
+
if (parentRow) {
|
|
1687
|
+
parentRow = this.removeChildOfParent(parentRow, rowId);
|
|
1688
|
+
return this.editRow(data, parentRow);
|
|
1689
|
+
}
|
|
1690
|
+
else {
|
|
1691
|
+
return data.filter((o) => this.getId(o).toString() !== rowId.toString());
|
|
1692
|
+
}
|
|
1693
|
+
};
|
|
1694
|
+
this.removeByRowIds = (data, rowIds) => {
|
|
1695
|
+
let tempRows = [...data];
|
|
1696
|
+
for (let i = 0; i < rowIds.length; i++) {
|
|
1697
|
+
tempRows = this.removeRow(tempRows, rowIds[i]);
|
|
1698
|
+
}
|
|
1699
|
+
return tempRows;
|
|
1700
|
+
};
|
|
1701
|
+
this.reOrder = (data, fromId, toId, pos) => {
|
|
1702
|
+
if (fromId.toString() === toId.toString()) {
|
|
1703
|
+
return data;
|
|
1704
|
+
}
|
|
1705
|
+
pos = pos || 'before';
|
|
1706
|
+
//مبدا و مقصد رو بگیر
|
|
1707
|
+
const details = this.getRowDetailsByIds(data, [fromId, toId]);
|
|
1708
|
+
const fromDetail = details[fromId];
|
|
1709
|
+
if (!fromDetail) {
|
|
1710
|
+
return data;
|
|
1711
|
+
}
|
|
1712
|
+
const toDetail = details[toId];
|
|
1713
|
+
if (!toDetail) {
|
|
1714
|
+
return data;
|
|
1715
|
+
}
|
|
1716
|
+
// نباید یک نود را نسبت به یکی از فرزندان خودش جابجا کنیم
|
|
1717
|
+
const isToChildOfFrom = toDetail.parentRows.some((row) => this.getId(row).toString() === fromId.toString());
|
|
1718
|
+
if (isToChildOfFrom) {
|
|
1719
|
+
return data;
|
|
1720
|
+
}
|
|
1721
|
+
//ردیف مبدا رو با آی دی جدید بگیر
|
|
1722
|
+
const fromRow = this.setId(fromDetail.row);
|
|
1723
|
+
//ایندکس مقصد رو بگیر
|
|
1724
|
+
const toIndex = toDetail.index + (pos === 'after' ? 1 : 0);
|
|
1725
|
+
//پرنت مقصد رو بگیر
|
|
1726
|
+
const toParent = toDetail.parentRow;
|
|
1727
|
+
//اگر پرنت داشت
|
|
1728
|
+
if (toParent) {
|
|
1729
|
+
//مبدا رو با آی دی جدید به پرنت مقصد در ایندکس ردیف مقصد اضافه کن
|
|
1730
|
+
const newToParent = this.getRowByAddChild(toParent, fromRow, toIndex);
|
|
1731
|
+
//دیتا رو با این ردیف جدید ویرایش کن
|
|
1732
|
+
let newData = this.editRow(data, newToParent);
|
|
1733
|
+
//مبدا رو از دیتای جدید حذف کن
|
|
1734
|
+
return this.removeRow(newData, fromDetail.rowId);
|
|
1735
|
+
}
|
|
1736
|
+
//اگر پرنت نداشت
|
|
1737
|
+
else {
|
|
1738
|
+
//مبدا رو با آی دی جدید به پرنت مقصد در ایندکس ردیف مقصد اضافه کن
|
|
1739
|
+
let newData = this.addRow(data, fromRow, { index: toIndex });
|
|
1740
|
+
//مبدا رو از دیتای جدید حذف کن
|
|
1741
|
+
return this.removeRow(newData.rows, fromDetail.rowId);
|
|
1742
|
+
}
|
|
1743
|
+
};
|
|
1744
|
+
this.reOrderByDir = (data, rowId, dir) => {
|
|
1745
|
+
const from = this.getRowDetailById(data, rowId);
|
|
1746
|
+
if (!from) {
|
|
1747
|
+
return data;
|
|
1748
|
+
}
|
|
1749
|
+
const sibling = dir === 1 ? from.afterSibling : from.beforeSibling;
|
|
1750
|
+
if (!sibling) {
|
|
1751
|
+
return data;
|
|
1752
|
+
}
|
|
1753
|
+
const fromId = rowId;
|
|
1754
|
+
const toId = this.getId(sibling);
|
|
1755
|
+
return this.reOrder(data, fromId, toId, dir === 1 ? 'after' : 'before') || data;
|
|
1756
|
+
};
|
|
1757
|
+
this.moveTo = (data, fromIds, toId) => {
|
|
1758
|
+
const fn = (data, fromId, toId) => {
|
|
1759
|
+
const details = this.getRowDetailsByIds(data, [fromId, toId]);
|
|
1760
|
+
let toDetail = details[toId];
|
|
1761
|
+
if (!toDetail) {
|
|
1762
|
+
return data;
|
|
1763
|
+
}
|
|
1764
|
+
let fromDetail = details[fromId];
|
|
1765
|
+
if (!fromDetail) {
|
|
1766
|
+
return data;
|
|
1767
|
+
}
|
|
1768
|
+
const isToChildOfFrom = toDetail.parentRows.some((o) => this.getId(o).toString() === fromId.toString());
|
|
1769
|
+
if (isToChildOfFrom) {
|
|
1770
|
+
return data;
|
|
1771
|
+
}
|
|
1772
|
+
const newData = this.removeRow(data, fromId);
|
|
1773
|
+
//اگر بروز نشه مشکل بوجود میاد
|
|
1774
|
+
toDetail = this.getRowDetailById(newData, toId);
|
|
1775
|
+
if (!toDetail) {
|
|
1776
|
+
return data;
|
|
1777
|
+
}
|
|
1778
|
+
const newToRow = this.getRowByAddChild(toDetail.row, fromDetail.row);
|
|
1779
|
+
return this.editRow(newData, newToRow);
|
|
1780
|
+
};
|
|
1781
|
+
let rows = data;
|
|
1782
|
+
for (let i = 0; i < fromIds.length; i++) {
|
|
1783
|
+
rows = fn(rows, fromIds[i], toId);
|
|
1784
|
+
}
|
|
1785
|
+
return rows;
|
|
1786
|
+
};
|
|
1787
|
+
this.editByTraverse = (rows, callback) => {
|
|
1788
|
+
const fn = (data, parentRow) => {
|
|
1789
|
+
for (let i = 0; i < data.length; i++) {
|
|
1790
|
+
let row = data[i];
|
|
1791
|
+
const res = callback(row);
|
|
1792
|
+
if (res === false) {
|
|
1793
|
+
continue;
|
|
1794
|
+
}
|
|
1795
|
+
if (parentRow) {
|
|
1796
|
+
const childs = this.getChilds(parentRow);
|
|
1797
|
+
row = res;
|
|
1798
|
+
childs[i] = res;
|
|
1799
|
+
}
|
|
1800
|
+
else {
|
|
1801
|
+
rows[i] = res;
|
|
1802
|
+
}
|
|
1803
|
+
const childs = this.getChilds(row);
|
|
1804
|
+
if (!childs || !childs.length) {
|
|
1805
|
+
continue;
|
|
1806
|
+
}
|
|
1807
|
+
fn(childs, row);
|
|
1808
|
+
}
|
|
1809
|
+
};
|
|
1810
|
+
fn(rows);
|
|
1811
|
+
return rows;
|
|
1812
|
+
};
|
|
1813
|
+
this.traverse = (rows, callback) => {
|
|
1814
|
+
const fn = (data, parentRows) => {
|
|
1815
|
+
for (let i = 0; i < data.length; i++) {
|
|
1816
|
+
let row = data[i];
|
|
1817
|
+
const isFirstChild = i === 0;
|
|
1818
|
+
const isLastChild = i === data.length - 1;
|
|
1819
|
+
const detail = this.getRowDetailByRowAndParents(row, i, parentRows, isFirstChild, isLastChild);
|
|
1820
|
+
const res = callback(row, detail);
|
|
1821
|
+
if (res === false || !detail.childsLength) {
|
|
1822
|
+
continue;
|
|
1823
|
+
}
|
|
1824
|
+
const childs = this.getChilds(row);
|
|
1825
|
+
fn(childs, [...parentRows, row]);
|
|
1826
|
+
}
|
|
1827
|
+
};
|
|
1828
|
+
fn(rows, []);
|
|
1829
|
+
return rows;
|
|
1830
|
+
};
|
|
1831
|
+
this._dataMethods = {
|
|
1832
|
+
getChilds: this.getChilds,
|
|
1833
|
+
getId: this.getId,
|
|
1834
|
+
traverse: this.traverse,
|
|
1835
|
+
getRowDetailById: this.getRowDetailById,
|
|
1836
|
+
addRow: (row, p) => {
|
|
1837
|
+
const data = this.getData();
|
|
1838
|
+
const res = this.addRow(data, row, p);
|
|
1839
|
+
this.setData(res.rows);
|
|
1840
|
+
return res;
|
|
1841
|
+
},
|
|
1842
|
+
editRow: (newRow) => {
|
|
1843
|
+
const data = this.getData();
|
|
1844
|
+
const res = this.editRow(data, newRow);
|
|
1845
|
+
this.setData(res);
|
|
1846
|
+
return res;
|
|
1847
|
+
},
|
|
1848
|
+
editById: (rowId, change) => {
|
|
1849
|
+
const data = this.getData();
|
|
1850
|
+
const res = this.editById(data, rowId, change);
|
|
1851
|
+
this.setData(res);
|
|
1852
|
+
return res;
|
|
1853
|
+
},
|
|
1854
|
+
removeRow: (rowId) => {
|
|
1855
|
+
const data = this.getData();
|
|
1856
|
+
const res = this.removeRow(data, rowId);
|
|
1857
|
+
this.setData(res);
|
|
1858
|
+
return res;
|
|
1859
|
+
},
|
|
1860
|
+
removeByRowIds: (rowIds) => {
|
|
1861
|
+
const data = this.getData();
|
|
1862
|
+
const res = this.removeByRowIds(data, rowIds);
|
|
1863
|
+
this.setData(res);
|
|
1864
|
+
return res;
|
|
1865
|
+
},
|
|
1866
|
+
reOrder: (fromId, toId) => {
|
|
1867
|
+
const data = this.getData();
|
|
1868
|
+
const res = this.reOrder(data, fromId, toId);
|
|
1869
|
+
this.setData(res);
|
|
1870
|
+
return res;
|
|
1871
|
+
},
|
|
1872
|
+
reOrderByDir: (rowId, dir) => {
|
|
1873
|
+
const data = this.getData();
|
|
1874
|
+
const res = this.reOrderByDir(data, rowId, dir);
|
|
1875
|
+
this.setData(res);
|
|
1876
|
+
return res;
|
|
1877
|
+
},
|
|
1878
|
+
moveTo: (fromIds, toId) => {
|
|
1879
|
+
const data = this.getData();
|
|
1880
|
+
const res = this.moveTo(data, fromIds, toId);
|
|
1881
|
+
this.setData(res);
|
|
1882
|
+
return res;
|
|
1883
|
+
},
|
|
1884
|
+
editByTraverse: (callback) => {
|
|
1885
|
+
const data = this.getData();
|
|
1886
|
+
const res = this.editByTraverse(data, callback);
|
|
1887
|
+
this.setData(res);
|
|
1888
|
+
return res;
|
|
1889
|
+
}
|
|
1890
|
+
};
|
|
1891
|
+
this.params = p;
|
|
1892
|
+
if (p.getData) {
|
|
1893
|
+
this.getData = p.getData;
|
|
1894
|
+
}
|
|
1895
|
+
if (p.setData) {
|
|
1896
|
+
this.setData = p.setData;
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
export class AIOColors {
|
|
1901
|
+
constructor() {
|
|
1902
|
+
this.number_to_hex = (num) => {
|
|
1903
|
+
const str = num.toString(16);
|
|
1904
|
+
return str.length === 1 ? "0" + str : str;
|
|
1905
|
+
};
|
|
1906
|
+
this.getType = (c) => {
|
|
1907
|
+
if (Array.isArray(c)) {
|
|
1908
|
+
return 'array';
|
|
1909
|
+
}
|
|
1910
|
+
return c.indexOf('rgb') !== -1 ? 'rgb' : 'hex';
|
|
1911
|
+
};
|
|
1912
|
+
this.to_array = (c) => {
|
|
1913
|
+
if (Array.isArray(c)) {
|
|
1914
|
+
return c;
|
|
1915
|
+
}
|
|
1916
|
+
if (c.indexOf('rgb(') === 0) {
|
|
1917
|
+
return c.slice(c.indexOf('(') + 1, c.indexOf(')')).split(',').map((o) => +o);
|
|
1918
|
+
}
|
|
1919
|
+
c = c.substr(1);
|
|
1920
|
+
let values = c.split(''), r, g, b;
|
|
1921
|
+
if (c.length === 3) {
|
|
1922
|
+
r = parseInt(values[0] + values[0], 16);
|
|
1923
|
+
g = parseInt(values[1] + values[1], 16);
|
|
1924
|
+
b = parseInt(values[2] + values[2], 16);
|
|
1925
|
+
}
|
|
1926
|
+
else if (c.length === 6) {
|
|
1927
|
+
r = parseInt(values[0] + values[1], 16);
|
|
1928
|
+
g = parseInt(values[2] + values[3], 16);
|
|
1929
|
+
b = parseInt(values[4] + values[5], 16);
|
|
1930
|
+
}
|
|
1931
|
+
return [r, g, b];
|
|
1932
|
+
};
|
|
1933
|
+
this.between = (c1, c2, count) => {
|
|
1934
|
+
let [r1, g1, b1] = this.to_array(c1);
|
|
1935
|
+
let [r2, g2, b2] = this.to_array(c2);
|
|
1936
|
+
let rDelta = (r2 - r1) / (count - 1);
|
|
1937
|
+
let gDelta = (g2 - g1) / (count - 1);
|
|
1938
|
+
let bDelta = (b2 - b1) / (count - 1);
|
|
1939
|
+
let colors = [];
|
|
1940
|
+
for (var i = 0; i < count; i++) {
|
|
1941
|
+
let color = `rgb(${Math.round(r1 + rDelta * i)},${Math.round(g1 + gDelta * i)},${Math.round(b1 + bDelta * i)})`;
|
|
1942
|
+
colors.push(color);
|
|
1943
|
+
}
|
|
1944
|
+
return colors;
|
|
1945
|
+
};
|
|
1946
|
+
this.getBetween = (array) => {
|
|
1947
|
+
let res = [];
|
|
1948
|
+
let colors = [];
|
|
1949
|
+
let counts = [];
|
|
1950
|
+
for (let i = 0; i < array.length; i++) {
|
|
1951
|
+
let a = array[i];
|
|
1952
|
+
let b = Array.isArray(a) ? this.to_rgb(a) : a;
|
|
1953
|
+
if (typeof b === 'string') {
|
|
1954
|
+
colors.push(b);
|
|
1955
|
+
}
|
|
1956
|
+
else if (typeof b === 'number') {
|
|
1957
|
+
counts.push(b);
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
for (let i = 0; i < colors.length - 1; i++) {
|
|
1961
|
+
const c1 = colors[i];
|
|
1962
|
+
const c2 = colors[i + 1];
|
|
1963
|
+
const count = counts[i];
|
|
1964
|
+
res = [...res, this.between(c1, c2, count)];
|
|
1965
|
+
}
|
|
1966
|
+
return res;
|
|
1967
|
+
};
|
|
1968
|
+
this.to_dark = (c, percent) => {
|
|
1969
|
+
let [r, g, b] = this.to_array(c);
|
|
1970
|
+
r = Math.round(r - (r * (percent / 100)));
|
|
1971
|
+
g = Math.round(g - (g * (percent / 100)));
|
|
1972
|
+
b = Math.round(b - (b * (percent / 100)));
|
|
1973
|
+
const type = this.getType(c);
|
|
1974
|
+
const key = 'to_' + type;
|
|
1975
|
+
const fn = this[key];
|
|
1976
|
+
return fn([r, g, b]);
|
|
1977
|
+
};
|
|
1978
|
+
this.to_light = (c, percent) => {
|
|
1979
|
+
let [r, g, b] = this.to_array(c);
|
|
1980
|
+
r = Math.round(r + ((255 - r) * (percent / 100)));
|
|
1981
|
+
g = Math.round(g + ((255 - g) * (percent / 100)));
|
|
1982
|
+
b = Math.round(b + ((255 - b) * (percent / 100)));
|
|
1983
|
+
const type = this.getType(c);
|
|
1984
|
+
const key = 'to_' + type;
|
|
1985
|
+
const fn = this[key];
|
|
1986
|
+
return fn([r, g, b]);
|
|
1987
|
+
};
|
|
1988
|
+
this.to_hex = (c) => { return `#${this.to_array(c).map((o) => this.number_to_hex(o)).join('')}`; };
|
|
1989
|
+
this.to_rgb = (c) => { return `rgb(${this.to_array(c).toString()})`; };
|
|
1990
|
+
this.brightness = (c, percent) => {
|
|
1991
|
+
if (percent === 0) {
|
|
1992
|
+
return c;
|
|
1993
|
+
}
|
|
1994
|
+
if (percent < 0) {
|
|
1995
|
+
return this.to_dark(c, percent * -1);
|
|
1996
|
+
}
|
|
1997
|
+
if (percent > 0) {
|
|
1998
|
+
return this.to_light(c, percent);
|
|
1999
|
+
}
|
|
2000
|
+
};
|
|
2001
|
+
this.log = (c) => {
|
|
2002
|
+
for (let i = 0; i < c.length; i++) {
|
|
2003
|
+
let color = this.to_rgb(c[i]);
|
|
2004
|
+
console.log(`%c ${this.reverse(color)}`, 'background: ' + color + '; color: #000');
|
|
2005
|
+
}
|
|
2006
|
+
};
|
|
2007
|
+
this.getRandomRGB = (c1 = '#000', c2 = '#fff') => {
|
|
2008
|
+
const array1 = this.to_array(c1);
|
|
2009
|
+
const array2 = this.to_array(c2);
|
|
2010
|
+
const grn = GetRandomNumber;
|
|
2011
|
+
let r = grn(array1[0], array2[0]);
|
|
2012
|
+
let g = grn(array1[1], array2[1]);
|
|
2013
|
+
let b = grn(array1[2], array2[2]);
|
|
2014
|
+
return this.to_rgb([r, g, b]);
|
|
2015
|
+
};
|
|
2016
|
+
this.reverse = (c) => { return this['to_' + this.getType(c)](this.to_array(c).map((o) => 255 - o)); };
|
|
2017
|
+
}
|
|
2018
|
+
}
|