-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactindo-tree-remote-mixin.html
220 lines (202 loc) · 7.09 KB
/
actindo-tree-remote-mixin.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
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
<script>
ActindoTreeRemoteMixin = function(superClass) {
return class extends superClass {
static get properties() {
return {
items: {
type: Array,
value: function() {return [];},
observer: "onItemsChange",
notify: true
},
/**
* Contains the url from where the data is obtained
*/
remote: {
type: String,
value: "",
observer: "_onRemoteChanged"
},
/**
* Params that are sent when remote data is loaded
*/
remoteParams: {
type: Object,
value: function () {
return {};
},
observer: "_onRemoteParamsChanged"
},
/**
* Determines which field of the response is contains the items
*/
remotePathData: {
type: String,
value: "children"
},
/**
* Determines if the box is automatically loading from remote
*/
autoLoad: {
type: Boolean,
value: false
},
/**
* True if combobox is currently loading
* @private
*/
_loading: {
type: Boolean,
value: false
},
/**
* Amount of active transactions
* @private
*/
_loadingCount: {
type: Number,
value: 0,
observer: "_onLoadingCountChanged"
},
error: {
type: Object
},
header: {
type: Object
}
}
}
/**
* Called whenever the data is changed.
*/
onItemsChange(newValue) {
};
/**
* Determines if there is currently an active call
*
* @param {Number} newValue
* @param {Number} oldValue
*/
_onLoadingCountChanged(newValue, oldValue)
{
this._loading = newValue !== 0;
}
/**
* Called when remote is changed
*
* @param {String} newValue
* @private
*/
_onRemoteChanged(newValue)
{
if( newValue !== "" && this.autoLoad )
{
this.load();
}
}
/**
* reload when params have been changed
*
* @param {Object} newValue
* @private
*/
_onRemoteParamsChanged( newValue )
{
this._selectedItem = null;
if( this.autoLoad )
{
this.load();
}
}
/**
* Trigger load data
* Debounces because of quasi simultaneously changed values of itemsPerPaged and param for example or multiple page changes
*/
load(callback)
{
let me = this;
this._debouncer = Polymer.Debouncer.debounce(this._debouncer,
Polymer.Async.timeOut.after(300),
() => { me._load(callback) });
}
/**
* takes the given object and creates an http query string from it
* usage: let str = _serialize({foo: 'bar', baz: ['a', 'b']}); => 'foo=bar&baz[0]=a&baz[1]=b'
*
* @param {Object} obj the object to serialize
* @param {String} prefix internal only (used in recursive calls for nested objects)
* @returns {String}
* @private
*/
_serialize(obj, prefix) {
var str = [], p;
for(p in obj) {
if (obj.hasOwnProperty(p)) {
var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
str.push((v !== null && typeof v === "object") ?
this._serialize(v, k) :
encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
}
return str.join("&");
}
_getDefaultRequestParams()
{
return {};
}
_processData(items)
{
return items;
}
/**
* Loads the data from remote
* @return {Promise}
* @private
*/
_load(callback)
{
if( this.remote === "" ) //Of course only with remote set
{
return;
}
let me = this;
//Lets build our request
let request = Object.assign({},this.remoteParams);
if( this.searchValue !== "" && this.remoteSearch ) {
request.fields = [this.valueField, this.displayField];
request.query = this.searchValue;
}
++this._loadingCount; //We need this for loading overlay
fetch(this.remote, {
method: 'POST',
credentials: 'same-origin',
body: this._serialize( request ),
headers: {
accept: 'application/json'
}
}).then( function(response){
return response.json()
} ).then( function(json){
if (json instanceof Object && !json['success'])
{
me.set('error', json);
}
let items = Polymer.Path.get(json,me.remotePathData);
items = me._processData(items);
me.set("items", items);
--me._loadingCount;
me.dispatchEvent(
new CustomEvent( "after-load", {detail:json} )
);
if( typeof callback === "function")
{
callback();
}
} ).catch( function(error){
--me._loadingCount;
console.error("Error loading data", error);
} );
}
}
}
</script>