-
Notifications
You must be signed in to change notification settings - Fork 2
/
cuba-query.html
129 lines (112 loc) · 3.68 KB
/
cuba-query.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../cuba-app/cuba-app-aware-behavior.html">
<link rel="import" href="cuba-data-loading-behavior.html">
<!--
The `cuba-query` element provides an ability to load list of entities using predefined query.
See https://doc.cuba-platform.com/manual-latest/rest_api_v2_queries_config.html
-->
<dom-module id="cuba-query">
<script>
Polymer({
is: 'cuba-query',
behaviors: [CubaAppAwareBehavior, CubaDataLoadingBehavior],
properties: {
/**
* Entity name as specified in domain model class via `@Entity` annotation e.g. sec$User
*/
entityName: String,
queryName: String,
/**
* Query params
*/
params: {
type: Object,
value: function() {
return {}
}
},
provideCount: {
type: Boolean,
value: false
},
count: {
type: Number,
notify: true,
readOnly: true
},
_loadMoreOffset: {
type: Number,
value: null
}
},
observers: [
'_optionsChanged(app, entityName, queryName, params.*, auto)'
],
remove: function(entity) {
return this.app.deleteEntity(entity._entityName, entity.id).then(function() {
if (!this.data || !Array.isArray(this.data)) {
return;
}
if (this.provideCount) {
this._setCount(this.count - 1);
}
for (var i = 0; i < this.data.length; i++) {
if (this.data[i].id === entity.id) {
this.splice("data", i, 1);
}
}
}.bind(this));
},
loadMore: function () {
if (!this.data) {
throw new Error('Data is not loaded yet');
}
if (!this.params.limit) {
throw new Error('Limit is not specified');
}
if (!this._loadMoreOffset) {
this._loadMoreOffset = (this.offset ? parseInt(this.offset) : 0) + parseInt(this.params.limit);
} else {
this._loadMoreOffset += parseInt(this.params.limit);
}
var params = JSON.parse(JSON.stringify(this.params)); //todo
params.offset = this._loadMoreOffset;
this._setLoading(true);
return this.app.query(this.entityName, this.queryName, params).then(
function (data) {
data.forEach(function (v) {
this.push('data', v);
}.bind(this));
this._setLoading(false);
}.bind(this),
function() {
this._setLoading(false);
this.fire("cuba-data-loading-error");
}.bind(this)
);
},
loadCount: function(abortable) {
var loadCountPromise = this.app.queryCount(this.entityName, this.queryName, this.params);
loadCountPromise.then(function (count) {
this._setCount(parseInt(count));
}.bind(this));
return loadCountPromise;
},
_load: function(fetchOptions) {
if (this.app == null || this.entityName == null || this.queryName == null){
return;
}
this._loadMoreOffset = null;
if (this.provideCount) {
return this.app.queryWithCount(this.entityName, this.queryName, this.params, fetchOptions)
.then(function (entitiesWithCount) {
this._setCount(parseInt(entitiesWithCount.count));
return entitiesWithCount.result;
}.bind(this));
} else {
return this.app.query(this.entityName, this.queryName, this.params, fetchOptions);
}
}
});
</script>
</dom-module>