-
Notifications
You must be signed in to change notification settings - Fork 4
/
filters.js
395 lines (298 loc) · 10.5 KB
/
filters.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
/**
* Filters convert text strings into Cockpit field values.
*
* For native angular filters, see https://docs.angularjs.org/api/ng/filter
* For guide how to write own filter, see https://docs.angularjs.org/guide/filter
*
* For list of cockpit content fields, see assets/js/angular/contentfields.js
*/
(function(angular, App) {
/*
text,
select
boolean
html
markdown
location
wysiwyg
code
date
time
gallery
tags
linkCollection
mediaPathPicker
regionPicker
*/
/**
* Noop filter
*
* @function
*/
angular.module('cockpit.filters').filter('noopCockpitFilter', function() {
noopCockpitFilter.label = App.i18n.get('import.filter.noop');
noopCockpitFilter.supports = ['', 'gallery', 'tags', 'link-collection'];
return noopCockpitFilter;
function noopCockpitFilter(input) {
return input;
}
});
/**
* Text filter
*
* @function
*/
angular.module('cockpit.filters').filter('textCockpitFilter', function() {
textCockpitFilter.label = App.i18n.get('import.filter.text');
textCockpitFilter.supports = ['text', 'html', 'markdown', 'wysiwyg', 'code'];
return textCockpitFilter;
function textCockpitFilter(input) {
return input.toString();
}
});
/**
* Select filter
*
* @function
*/
angular.module('cockpit.filters').filter('selectCockpitFilter', [function() {
selectCockpitFilter.label = App.i18n.get('import.filter.select');
selectCockpitFilter.supports = ['select'];
return selectCockpitFilter;
function selectCockpitFilter(input, field) {
var output = '';
if (field.options === undefined) {
App.notify(
App.i18n.get('import.notify.Canont use this filter', field.label || field.name),
'warning'
);
} else if (field.options.indexOf(input) >= 0) {
output = input;
}
return output;
}
}]);
/**
* Boolean filter
*
* @function
*/
angular.module('cockpit.filters').filter('booleanCockpitFilter', [function() {
booleanCockpitFilter.label = App.i18n.get('import.filter.boolean');
booleanCockpitFilter.supports = ['boolean'];
return booleanCockpitFilter;
function booleanCockpitFilter(input) {
return Number(input == '1' || !!input);
}
}]);
/**
* Location filter
* Delay should be at lest 10 queries per second
*
* @function
*
* @see https://developers.google.com/maps/documentation/geocoding/usage-limits
*/
angular.module('cockpit.filters').filter('locationCockpitFilter',
['$q', '$document', '$timeout', function($q, $document, $timeout) {
locationCockpitFilter.label = App.i18n.get('import.filter.location');
locationCockpitFilter.supports = ['location'];
locationCockpitFilter.$stateful = true;
locationCockpitFilter.delay = 250;
/** @type {google.maps.Geocoder} Cached geocoder */
var geocoder;
var cache = {};
loadApi()
.then(function() {
geocoder = new google.maps.Geocoder();
});
return locationCockpitFilter;
/**
* @param {string} input - Data input
* @param {Object} field
* @param {string} field.label
* @param {boolean} field.lst
* @param {string} field.name
* @param {boolean} field.required
* @param {string} field.type
* @param {Object} field.options - Field specific options
* @param {Object} field.options.deferred
*/
function locationCockpitFilter(input, field, options) {
options = (options || {});
var output = {lat: undefined, lng: undefined, address: undefined};
// Check in cache
if (cache.hasOwnProperty[input]) {
options.deferred.resolve(cache[input]);
return cache[input];
}
geocoder.geocode({address: input}, function(results, status) {
var foundLocation;
if (status === google.maps.GeocoderStatus.OK && results.length > 0) {
var result = results.shift();
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
var data = {
lat : result.geometry.location.lat().toFixed(10) / 1,
lng : result.geometry.location.lng().toFixed(10) / 1,
address: result.formatted_address
};
// jscs:enable
angular.extend(output, data);
cache[input] = data;
if (options.deferred) {
options.deferred.resolve(data);
}
// Probably status is google.maps.GeocoderStatus.OVER_QUERY_LIMIT
} else if (options.deferred) {
options.deferred.reject(status);
}
return;
});
return output;
}
/**
* Load Google JS API and Google Maps API
*
* @return {Object} Promise
*/
function loadApi() {
var deferred = $q.defer();
var locale = $document.prop('documentElement').lang;
var script;
// Maps have alreadby been loaded
if (window.google && window.google.maps) {
deferred.resolve();
// JS API is available
} else if (window.google) {
loadMapsApi();
// Need to load both
} else {
script = window.document.createElement('script');
script.async = true;
script.onload = loadMapsApi;
script.onerror = function() {
App.notify(App.i18n.get('Failed loading google maps api.'), 'warning');
deferred.reject();
};
script.src = 'https://www.google.com/jsapi';
document.body.appendChild(script);
}
return deferred.promise;
function loadMapsApi() {
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
google.load('maps', '3', {other_params: 'libraries=places&language=' + locale, callback: function() {
// jscs:enable
if (google.maps.places) {
deferred.resolve();
}
}});
}
}
}]);
/**
* Make sure it's YYYY-MM-DD
*
* @see http://getuikit.com/docs/datepicker.html
*/
angular.module('cockpit.filters').filter('dateCockpitFilter', function() {
dateCockpitFilter.label = App.i18n.get('import.filter.date');
dateCockpitFilter.supports = ['date'];
// Load Maps API
return dateCockpitFilter;
function dateCockpitFilter(input) {
// Validate date
var date = new Date(input);
var formatted;
// Check that it's not Invalid Date
if (isNaN(date.getTime())) {
return '';
}
formatted = date.toISOString();
return formatted.match(/(.+)\T/)[1];
}
});
/**
* Time picker
*
* @function
*
* @see http://getuikit.com/docs/timepicker.html
*/
angular.module('cockpit.filters').filter('timeCockpitFilter', function() {
timeCockpitFilter.label = App.i18n.get('import.filter.time');
timeCockpitFilter.supports = ['time'];
return timeCockpitFilter;
function timeCockpitFilter(input) {
// Validate time
var date = '2000-01-01\\T' + input;
var formatted;
// Check that it's not Invalid Date
if (isNaN(date.getTime())) {
return '';
}
formatted = date.toISOString();
return formatted.match(/\T(\d{2}:\d{2})/)[1];
}
});
/**
* Link collection by name
*
* @function
*/
angular.module('cockpit.filters').filter('linkCollectionBynameCockpitFilter', ['$http', function($http) {
// Load up all collections
var collections = [];
var collectionEntries = {};
$http.post(
App.route('/api/collections/find'),
{
},
{
responseType: 'json'
}
)
.success(function(data) {
collections = data;
// Get entries of all collections
collections.forEach(function(collection) {
// TODO: something with collection.fields
$http.post(
App.route('/api/collections/entries'),
{
collection: {
_id : collection._id,
sortfield: null
}
},
{
responseType: 'json'
}
)
.success(function(data) {
collectionEntries[collection._id] = data;
})
.error(App.module.callbacks.error)
;
});
})
.error(App.module.callbacks.error.http)
;
linkCollectionBynameCockpitFilter.label = App.i18n.get('import.filter.linkCollectionByname');
linkCollectionBynameCockpitFilter.supports = ['link-collection'];
return linkCollectionBynameCockpitFilter;
function linkCollectionBynameCockpitFilter(input, field, options) {
options = (options || {lookupKey: 'name'});
var foundEntryId;
// Find collection
if (!field.hasOwnProperty('collection') || !collectionEntries.hasOwnProperty(field.collection)) {
return foundEntryId;
}
collectionEntries[field.collection].forEach(function(collectionEntry) {
if (collectionEntry[options.lookupKey] == input) {
foundEntryId = collectionEntry._id;
}
});
return foundEntryId;
}
}]);
})(window.angular, window.App);