tablevu 0.1.4 → 0.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.
package/src/odata.js CHANGED
@@ -1,106 +1,141 @@
1
- /*
2
- Props: {
3
- url: '',
4
-
5
- pageSize
6
- currPage
7
- select
8
- expand
9
- filter
10
- search
11
-
12
- }
13
- */
14
- function filter(q) {
15
-
16
- function fuval(q) {
17
- if (q === null) {
18
- return 'null'
19
- } else if (typeof(q) === 'string') {
20
- return '\'' + q + '\'';
21
- } else {
22
- if (typeof(q) === 'number')
23
- return q;
24
- else {
25
- debugger;
26
- if (q.getMonth && typeof q.getMonth === "function")
27
- return q.toISOString()
28
- else
29
- return q.func + '(' + q.params.join(',') + ')';
30
- }
31
- }
32
- }
33
-
34
- let s = '';
35
-
36
- if (q && q.filters)
37
- for (let i = 0; i < q.filters.length; i++) {
38
- if (i > 0)
39
- s = s + ` ${q.type || 'and'} `;
40
-
41
- if (q.filters[i].column) {
42
- if (q.filters[i].operator == 'in')
43
- s = s + fuval(q.filters[i].value) + ' ' + q.filters[i].operator + ' ' + q.filters[i].column;
44
- else
45
- s = s + q.filters[i].column + ' ' + q.filters[i].operator + ' ' + fuval(q.filters[i].value);
46
- } else {
47
- if (q.filters[i].func)
48
- s = s + q.filters[i].func + '(' + q.filters[i].params.map(m => fuval(m)).join(',') + ')';
49
- else
50
- if (q.filters[i])
51
- s = s + '(' + filter(q.filters[i]) + ')';
52
- }
53
- }
54
-
55
- return s;
56
- }
57
-
58
- function odata(options, props) {
59
- const url = new URL(options.url);
60
-
61
- // Paging //
62
- if (props.pageSize) {
63
- url.searchParams.append('$skip', props.pageSize * (props.currPage - 1));
64
- url.searchParams.append('$top', props.pageSize);
65
- }
66
-
67
- // Select //
68
- if (options.select && options.select.length > 0)
69
- url.searchParams.append('$select', options.select.join(','));
70
-
71
- // Expand //
72
- if (options.expand && options.expand.length > 0)
73
- url.searchParams.append('$expand', options.expand.map(m => m.name + ((m.select && m.select.length > 0) ? '($select=' + m.select.join(',') + ')' : '')).join(','));
74
-
75
- // Filter //
76
- let f = [];
77
-
78
- if (options.filter && Object.prototype.hasOwnProperty.call(options.filter, 'filters')) {
79
- let l = filter(options.filter);
80
- f.push(l);
81
- }
82
-
83
- if (props.filter) {
84
- let l = filter(props.filter);
85
- f.push(l);
86
- }
87
-
88
- // Search //
89
- if (props.search) {
90
- let l = props.cols.filter(q => q.searchable == undefined || q.searchable == true).map(q => "contains(" + q.name.replaceAll('.', '/') + ",'" + props.search + "')").join(" or ");
91
-
92
- if (l)
93
- f.push('(' + l + ')');
94
- }
95
-
96
- if (f && f.length > 0)
97
- url.searchParams.append('$filter', f.join(' and '));
98
-
99
- // Sort //
100
- if (props.sort && props.sort.length > 0)
101
- url.searchParams.append('$orderby', props.sort.map(m => { let n = { index: -1, name: '', dir: null }; if (Object.prototype.hasOwnProperty.call(m, 'index')) { n.index = m.index } else { n.name = m.name }; n.dir = m.dir; return n; }).map(m => { return (m.index > -1 ? props.cols[m.index].name : m.name) + (m.dir ? (' ' + m.dir): '') }).join(','));
102
-
103
- return new Request(url, options.header);
104
- }
105
-
106
- export default odata;
1
+ import get from './get.js';
2
+
3
+ function filter(q) {
4
+
5
+ function fuval(q) {
6
+ if (q === null) {
7
+ return 'null'
8
+ } else if (typeof(q) === 'string') {
9
+ if (q.length == 10 && q[4] == '-' && q[7] == '-' && Date.parse(q)) { // input date return iso string
10
+ return q;
11
+ } else
12
+ return '\'' + q + '\'';
13
+ } else {
14
+ if (typeof(q) === 'number')
15
+ return q;
16
+ else {
17
+ if (q.getMonth && typeof q.getMonth === "function")
18
+ return q.toISOString()
19
+ else
20
+ return q.func + '(' + q.params.join(',') + ')';
21
+ }
22
+ }
23
+ }
24
+
25
+ let s = '';
26
+
27
+ if (q && q.filters)
28
+ for (let i = 0; i < q.filters.length; i++) {
29
+ if (i > 0)
30
+ s = s + ` ${q.type || 'and'} `;
31
+
32
+ if (q.filters[i].column) {
33
+ if (q.filters[i].operator == 'in')
34
+ s = s + fuval(q.filters[i].value) + ' ' + q.filters[i].operator + ' ' + q.filters[i].column;
35
+ else
36
+ s = s + q.filters[i].column + ' ' + q.filters[i].operator + ' ' + fuval(q.filters[i].value);
37
+ } else {
38
+ if (q.filters[i].func)
39
+ s = s + q.filters[i].func + '(' + q.filters[i].params.map(m => fuval(m)).join(',') + ')';
40
+ else
41
+ if (q.filters[i])
42
+ s = s + '(' + filter(q.filters[i]) + ')';
43
+ }
44
+ }
45
+
46
+ return s;
47
+ }
48
+
49
+ function sefs(url, query, seperator) {
50
+ let r = '';
51
+
52
+ if (query.select && query.select.length > 0) {
53
+ if (seperator)
54
+ r += '$select=' + query.select.join(',');
55
+ else
56
+ url.searchParams.append('$select', query.select.join(','));
57
+ };
58
+
59
+ if (query.expand && query.expand.length > 0) {
60
+ let a = [];
61
+
62
+ query.expand.forEach(e => {
63
+ let q = sefs(null, e, ';');
64
+
65
+ if (q)
66
+ a.push(e.name + '(' + q + ')');
67
+ else
68
+ a.push(e.name);
69
+ });
70
+
71
+ if (seperator)
72
+ r += (r.length > 0 ? seperator: '') + '$expand=' + a.join(',');
73
+ else
74
+ url.searchParams.append('$expand', a.join(','));
75
+ };
76
+
77
+ return r;
78
+ }
79
+
80
+ export default class odata {
81
+ constructor(req, data, events) {
82
+ this.adapter = (props) => {
83
+ const u = new URL(req.url);
84
+
85
+ // Paging //
86
+ if (props.pageSize) {
87
+ u.searchParams.append('$skip', props.pageSize * (props.currPage - 1));
88
+ u.searchParams.append('$top', props.pageSize);
89
+ }
90
+
91
+ sefs(u, data);
92
+ /*
93
+ // Select //
94
+ if (data.select && data.select.length > 0)
95
+ u.searchParams.append('$select', data.select.join(','));
96
+
97
+ // Expand //
98
+ if (data.expand && data.expand.length > 0) {
99
+ const a =
100
+ u.searchParams.append('$expand', data.expand.map(m => m.name + ((m.select && m.select.length > 0) ? '($select=' + m.select.join(',') + ')' : '')).join(','));
101
+ }*/
102
+ // Filter //
103
+ let f = [];
104
+
105
+ if (props.filter && Object.prototype.hasOwnProperty.call(props.filter, 'filters')) {
106
+ let l = filter(props.filter);
107
+ if (l)
108
+ f.push(l);
109
+ }
110
+
111
+ if (props.filter) {
112
+ let l = filter(props.filter);
113
+ if (l)
114
+ f.push(l);
115
+ }
116
+
117
+ // Search //
118
+ if (props.search) {
119
+ let l = props.cols.filter(q => q.searchable == undefined || q.searchable == true).map(q => "contains(" + q.name.replaceAll('.', '/') + ",'" + props.search + "')").join(" or ");
120
+
121
+ if (l)
122
+ f.push('(' + l + ')');
123
+ }
124
+
125
+ if (f && f.length > 0)
126
+ u.searchParams.append('$filter', f.join(' and '));
127
+
128
+ // Sort //
129
+ if (props.sort && props.sort.length > 0)
130
+ u.searchParams.append('$orderby', props.sort.map(m => { let n = { index: -1, name: '', dir: null }; if (Object.prototype.hasOwnProperty.call(m, 'index')) { n.index = m.index } else { n.name = m.name }; n.dir = m.dir; return n; }).map(m => { return (m.index > -1 ? props.cols[m.index].name : m.name).replaceAll('.', '/') + (m.dir ? (' ' + m.dir): '') }).join(','));
131
+
132
+ let r = null;
133
+ if (events.onfetch && typeof events.onfetch == 'function')
134
+ r = events.onfetch(new Request(u, req))
135
+ else
136
+ r = new Request(u, req);
137
+
138
+ return get(r);
139
+ }
140
+ }
141
+ }