-
Notifications
You must be signed in to change notification settings - Fork 16
/
nuxeo-page-provider.js
521 lines (467 loc) · 13.2 KB
/
nuxeo-page-provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
@license
(C) Copyright Nuxeo Corp. (http://nuxeo.com/)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
import { Debouncer } from '@polymer/polymer/lib/utils/debounce.js';
import { timeOut } from '@polymer/polymer/lib/utils/async.js';
import './nuxeo-element.js';
import './nuxeo-operation.js';
import './nuxeo-resource.js';
{
/**
* `nuxeo-page-provider` performs queries on the repository and provides paginated results.
*
* <nuxeo-page-provider auto
* query="select * from Document"
* page-size="5"
* sort="dc:modified">
* </nuxeo-page-provider>
*
* <nuxeo-page-provider auto page="1"
* provider="default_search"
* page-size="25">
* </nuxeo-page-provider>
*
* <nuxeo-page-provider auto offset="2"
* provider="default_search"
* page-size="25">
* </nuxeo-page-provider>
*
* With `auto` set to `true`, results are fetched whenever
* the `provider`, `query`, `params`, `page` or `pageSize` properties are changed.
*
* When using current page `offset`, the `page` property is ignored.
*
* You can fetch results explicitly by calling `fetch` on the
* element.
*
* @memberof Nuxeo
* @demo demo/nuxeo-page-provider.html
*/
class PageProvider extends Nuxeo.Element {
static get template() {
return html`
<style>
:host {
display: none;
}
</style>
<!-- REST API / GET -->
<nuxeo-resource
id="nxResource"
connection-id="[[connectionId]]"
path="{{path}}"
enrichers="{{enrichers}}"
schemas="[[schemas]]"
headers="{{headers}}"
>
</nuxeo-resource>
<!-- Automation API / POST -->
<nuxeo-operation
id="op"
connection-id="[[connectionId]]"
enrichers="{{enrichers}}"
schemas="[[schemas]]"
headers="{{headers}}"
on-error="_preventPropagation"
>
</nuxeo-operation>
`;
}
static get is() {
return 'nuxeo-page-provider';
}
static get properties() {
return {
/** The id of a nuxeo-connection to use. */
connectionId: {
type: String,
value: '',
},
/**
* If true, automatically execute the operation when either `provider` or `params` changes.
*/
auto: {
type: Boolean,
value: false,
},
/**
* The delay in milliseconds to debounce the auto fetch call when provider, params, etc. changes.
*/
autoDelay: {
type: Number,
value: 300,
},
/**
* The id of a page provider.
*/
provider: {
type: String,
value: '',
},
/**
* The query.
*/
query: {
type: String,
},
/**
* Computed path of the query endpoint to use.
*/
path: {
type: String,
computed: '_computePath(provider, query)',
},
/**
* The query parameters.
*/
params: {
type: Object,
value: {},
},
/**
* The number of results per page.
*/
pageSize: {
type: Number,
value: -1,
},
/**
* The current page. Ignored when current page offset is set.
*/
page: {
type: Number,
value: 1,
},
/**
* The current page offset.
*/
offset: {
type: Number,
notify: true,
},
/**
* The current page entries.
*/
currentPage: {
type: Array,
value: [],
notify: true,
},
/**
* Map of properties and direction 'asc' / 'desc'
*/
sort: {
type: Object,
value: {},
notify: true,
},
/**
* Total number of pages.
*/
numberOfPages: {
type: Number,
notify: true,
},
/**
* Total number of results.
*/
resultsCount: {
type: Number,
notify: true,
},
/**
* Aggregations returned.
*/
aggregations: {
type: Object,
value: {},
notify: true,
},
/**
* Quick filters state.
*/
quickFilters: {
type: Array,
notify: true,
},
/**
* Returns true if a next page is available.
*/
isNextPageAvailable: {
type: Boolean,
value: false,
notify: true,
},
/**
* Returns true if a previous page is available.
*/
isPreviousPageAvailable: {
type: Boolean,
value: false,
notify: true,
},
/**
* Current page's size
*/
currentPageSize: {
type: Number,
notify: true,
},
/**
* The `content enricher` of the resource.
* Can be an object with entity type as keys or list or string (which defaults to `document` entity type).
*/
enrichers: {
type: Object,
value: {},
},
/**
* List of comma separated values of the document schemas to be returned.
* All document schemas are returned by default.
*/
schemas: {
type: String,
},
/**
* The headers of the request.
* 'Accept': 'text/plain,application/json' is already set by default.
*/
headers: {
type: Object,
value: null,
},
/**
* Fetch the aggregate bucket's key if it matches a user
* or directory entry and translate directory label.
*/
fetchAggregates: {
type: Boolean,
value: false,
observer: '_fetchAggregatesChanged',
},
/**
* True while requests are in flight.
*/
loading: {
type: Boolean,
notify: true,
readOnly: true,
},
/**
* If `true`, aggregagtes from page provider definition will not be computed.
*/
skipAggregates: Boolean,
/**
* The HTTP method to use ('get', 'post'). Default is 'get'.
* Use `Nuxeo.UI.config.pageprovider.method = 'post' to change default.
*/
method: {
type: String,
value() {
return (
(Nuxeo.UI && Nuxeo.UI.config && Nuxeo.UI.config.pageprovider && Nuxeo.UI.config.pageprovider.method) ||
'get'
);
},
},
};
}
static get observers() {
return ['_autoFetch(auto, provider, query, params.*, pageSize, page, sort)'];
}
/**
* Fired when the current page is fetched.
*
* @event update
*/
/**
* Fired if an error occurs when fetching the current page.
*
* @event error
*/
ready() {
super.ready();
this.$.nxResource.addEventListener('loading-changed', () => {
this._setLoading(this.$.nxResource.loading);
});
}
/**
* Fetch the currentPage.
*
* @method fetch
* @param {Object} options
* The options of the fetch:
* - "skipAggregates" to do not compute aggregations (boolean)
*/
fetch(options) {
if (!this.headers) {
this.headers = {};
}
if (this.skipAggregates || (options && options.skipAggregates)) {
this.headers.skipAggregates = 'true';
} else {
delete this.headers.skipAggregates;
}
const params = this._params;
// use either REST or Automation API depending on method
let target;
if (this.method.toLowerCase() === 'get') {
target = this.$.nxResource;
// move named parameters
if (params.namedParameters) {
Object.assign(params, params.namedParameters);
delete params.namedParameters;
}
} else {
target = this.$.op;
if (this.query) {
target.op = 'Repository.Query';
} else {
target.op = 'Repository.PageProvider';
params.providerName = this.provider;
}
}
target.params = params;
return target
.execute()
.then((response) => {
this.currentPage = response.entries.slice(0);
this.numberOfPages = response.numberOfPages;
this.resultsCount = response.resultsCount;
if (!options || !options.skipAggregates) {
this.aggregations = response.aggregations;
}
this.quickFilters = response.quickFilters;
this.isNextPageAvailable = response.isNextPageAvailable;
this.offset = response.currentPageOffset;
this.pageSize = response.pageSize;
this.isPreviousPageAvailable = response.isPreviousPageAvailable;
this.currentPageSize = response.currentPageSize;
this.dispatchEvent(
new CustomEvent('update', {
bubbles: true,
composed: true,
}),
);
return response;
})
.catch((error) => {
this.dispatchEvent(
new CustomEvent('error', {
bubbles: true,
composed: true,
detail: {
error,
},
}),
);
throw error;
});
}
get _params() {
const params = {
currentPageIndex: this.page - 1,
offset: this.offset,
pageSize: this.pageSize,
};
// do not add empty sort info, to be able to fallback to default sort defined in page providers
const sortKeys = this._sortKeys;
if (sortKeys.length > 0) {
params.sortBy = this._sortKeys.join(',');
params.sortOrder = this._sortValues.join(',');
}
if (this.query) {
params.query = this.query;
}
if (Array.isArray(this.params)) {
params.queryParams = this.params;
} else {
const { queryParams, ...otherParams } = this.params;
if (queryParams) {
params.queryParams = queryParams;
}
if (otherParams) {
const namedParams = {};
Object.entries(otherParams).forEach(([key, value]) => {
if (value != null) {
if (Array.isArray(value)) {
value = JSON.stringify(value.map((item) => (item['entity-type'] ? item.uid || item.id : item)));
} else if (typeof value !== 'string') {
value = value['entity-type'] ? value.uid || value.id : JSON.stringify(value);
}
namedParams[key] = value;
}
});
params.namedParameters = namedParams;
}
}
// Append quick filters if any
if (this.quickFilters) {
const retainedFilters = [];
for (let i = 0; i < this.quickFilters.length; i++) {
if (this.quickFilters[i].active === true) {
retainedFilters.push(this.quickFilters[i].name);
}
}
params.quickFilters = retainedFilters.join(',');
}
return params;
}
get _sortKeys() {
return Object.keys(this.sort);
}
get _sortValues() {
return this._sortKeys.map((k) => this.sort[k]);
}
_autoFetch() {
// Reset the page if the query changes
if (
this.$.nxResource.params &&
this.query &&
this.query.length === 0 &&
this.query !== this.$.nxResource.params.query
) {
this.page = 1;
}
if (this.auto && (this.query || this.provider)) {
// debounce in case of multiple param changes
this._debouncer = Debouncer.debounce(this._debouncer, timeOut.after(this.autoDelay), () => this.fetch());
}
}
_computePath(provider, query) {
let path = '';
if (query) {
path = '/search/execute';
} else if (provider) {
path = `/search/pp/${provider}/execute`;
}
return path;
}
_fetchAggregatesChanged() {
if (this.headers === null) {
this.headers = {};
}
if (this.fetchAggregates === true) {
this.headers['fetch-aggregate'] = 'key';
} else {
delete this.headers['fetch-aggregate'];
}
}
_preventPropagation(e) {
e.stopPropagation();
}
}
customElements.define(PageProvider.is, PageProvider);
Nuxeo.PageProvider = PageProvider;
}