-
Notifications
You must be signed in to change notification settings - Fork 2
/
cuba-entities.html
168 lines (152 loc) · 4.65 KB
/
cuba-entities.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
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
<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-entities` element provides an ability to load list of entities.
-->
<dom-module id="cuba-entities">
<script>
Polymer({
is: 'cuba-entities',
behaviors: [CubaAppAwareBehavior, CubaDataLoadingBehavior],
properties: {
/**
* Entity name as specified in domain model class via `@Entity` annotation e.g. sec$User
*/
entityName: String,
/**
* Name of the view which is used for loading entities.
*/
view: {
type: String,
value: null
},
/**
* Name of the field to be sorted by. If the name is preceeding by the '+' character,
* then the sort order is ascending, if by the '-' character then descending.
* If there is no special character before the property name, then ascending sort will be used.
*/
sort: {
type: String,
value: null
},
/**
* Number of extracted entities.
*/
limit: {
type: Number,
value: null
},
/**
* Position of the first result to retrieve.
*/
offset: {
type: Number,
value: null
},
provideCount: {
type: Boolean,
value: false
},
count: {
type: Number,
notify: true,
readOnly: true
},
_loadMoreOffset: {
type: Number,
value: null
}
},
observers: [
'_optionsChanged(app, entityName, view, sort, limit, offset, auto)'
],
/**
* Deletes entity and removes it from `data` collection.
* @paprm {Object} must contain `id` and `_entityName` fields
* @return {Promise}
*/
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.limit) {
throw new Error('Limit is not specified');
}
if (!this._loadMoreOffset) {
this._loadMoreOffset = this.offset + this.limit;
} else {
this._loadMoreOffset += this.limit;
}
var options = this._getOptions();
options.offset = this._loadMoreOffset;
this._setLoading(true);
return this.app.loadEntities(this.entityName, options).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) {
if (!this.app) {
return;
}
var loadCountPromise = this.app.queryCount(this.entityName, 'all', null);
loadCountPromise.then(function (count) {
this._setCount(parseInt(count));
}.bind(this));
return loadCountPromise;
},
/**
* @param fetchOptions
* @returns Promise<object[]>
* @private
*/
_load: function (fetchOptions) {
if (this.app == null || this.entityName == null) {
return;
}
this._loadMoreOffset = null;
if (this.provideCount) {
return this.app.loadEntitiesWithCount(this.entityName, this._getOptions(), fetchOptions)
.then(function (entitiesWithCount) {
this._setCount(parseInt(entitiesWithCount.count));
return entitiesWithCount.result;
}.bind(this));
} else {
return this.app.loadEntities(this.entityName, this._getOptions(), fetchOptions);
}
},
_getOptions: function () {
var opts = {};
if (this.view) opts.view = this.view;
if (this.sort) opts.sort = this.sort;
if (this.limit) opts.limit = this.limit;
if (this.offset) opts.offset = this.offset;
return opts;
}
});
</script>
</dom-module>