This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
controllers.js
480 lines (429 loc) · 16.2 KB
/
controllers.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
'use strict';
define(['angular'], function(angular) {
var app = angular.module('portal.widgets.controllers', []);
/**
* Base widget controller (for /partials/widget-card.html). First point of access for all widget types -- determines
* the type and sets launch button url for un-typed (basic) widgets.
*/
app.controller('WidgetCardController', ['$scope', '$log', '$localStorage', 'widgetService', function($scope, $log, $localStorage, widgetService) {
/**
* Check for widget types that require extra configuration (including null/undefined case), default to provided
* widget type.
* @param widget The widget object provided by widgetService
* @returns {*} A string for the widget type
*/
var widgetType = function widgetType(widget) {
// Check for types that need handling
switch(widget.widgetType) {
case 'list-of-links':
// If the list of links only has one link and it's the same as the launch button url, display a basic widget
if (widget.widgetConfig.links.length === 1 && widget.altMaxUrl && widget.widgetConfig.links[0].href === widget.url) {
return 'basic';
} else {
return 'list-of-links'
}
case 'generic':
// DEPRECATED: Included for backwards compatibility. Use 'custom' instead.
return 'custom';
case null:
// If widgetType doesn't exist, show a basic widget
return 'basic';
default:
return widget.widgetType;
}
};
/**
* Initial widget setup -- gets data for a single widget from the provided fname attribute
*/
var initializeWidget = function(fname, removable) {
// Initialize scope variables
$scope.widget = {};
$scope.widgetType = '';
// Temporarily necessary for CSS to enforce max-width on widgets not constrained by a responsive grid (i.e. not in ng-portal)
$scope.isSingleWidget = true;
// Get widget data for provided app (fname)
widgetService.getSingleWidgetData(fname)
.then(function(data) {
// Set scope variables
if (data) {
$scope.widget = data;
$scope.widgetType = widgetType(data);
$scope.removable = removable;
}
})
.catch(function(error) {
$log.warn('WidgetCardController couldn\'t get data for: ' + fname);
$log.error(error);
});
};
/**
* Set the launch button url for static content, exclusive mode, and basic widgets
* @returns {*}
*/
$scope.renderUrl = function renderUrl() {
var widget = $scope.widget;
// Check for static content or exclusive mode portlets (rare)
if (widget.altMaxUrl == false) {
if (widget.staticContent != null) {
return 'static/' + widget.fname;
} else if (widget.renderOnWeb || $localStorage.webPortletRender) {
return 'exclusive/' + widget.fname;
}
} else {
// For basic widget, just use the provided url
return widget.url;
}
};
// Initialize the widget
if ($scope.fname) {
initializeWidget($scope.fname, $scope.removable);
} else {
$log.warn('WidgetCardController didn\'t get an fname.');
}
}]);
// OPTION LINK widget type
app.controller('OptionLinkController', ['$scope', '$log', 'widgetService', function($scope, $log, widgetService) {
/**
* Set up default configuration if no config exists
*/
var configInit = function() {
$scope.config = {
singleElement: false,
arrayName: 'array',
value: 'value',
display: 'display'
};
};
/**
* Fetch additional widget data
*/
var populateWidgetContent = function() {
if ($scope.widget.widgetURL && $scope.widget.widgetType) {
// Initialize widget data
$scope.widget.widgetData = [];
// Get data and set widget content
widgetService.getWidgetJson($scope.widget).then(function(data) {
if (data) {
if ($scope.config.singleElement) {
// Set the default selected url
$scope.widget.selectedUrl = $scope.widget.widgetData[$scope.config.value];
} else if ($scope.widget.widgetData[$scope.config.arrayName] && $scope.widget.widgetData[$scope.config.arrayName].length > 0) {
// If multiple values come back, set selected url to the first entry
$scope.widget.selectedUrl = $scope.widget.widgetData[$scope.config.arrayName][0][$scope.config.value];
}
} else {
$log.warn('OptionLinkController couldn\'t get json for: ' + $scope.widget.fname);
}
});
}
};
// Set default values if no config was received
if (!$scope.config) {
configInit();
$log.warn('OptionLinkController didn\'t receive a widget config');
}
// Set up widget content
populateWidgetContent();
}]);
// SEARCH WITH LINKS widget type
app.controller('SearchWithLinksController', ['$scope', '$sce', function($scope, $sce) {
// Have faith our entity files aren't trying to bamboozle us
$scope.secureURL = $sce.trustAsResourceUrl($scope.config.actionURL);
}]);
// RSS widget type
app.controller('RssWidgetController', ['$scope', '$log', 'widgetService', function($scope, $log, widgetService) {
/**
* Turn the provided date string into an actual Date so it can be filtered into something prettier.
* @param dateString A hideously ugly date string
* @returns {*} A Date object or null
*/
$scope.getPrettyDate = function(dateString) {
// Create a new date if a date string was provided, otherwise return null
return dateString ? new Date(dateString) : null;
};
/**
* Set defaults if any values are missing from provided widgetConfig
*/
var checkForWidgetConfig = function() {
if (!$scope.config) {
$scope.config = {};
}
if (!$scope.config.lim) {
$scope.config.lim = 5;
}
if (!$scope.config.titleLim) {
$scope.config.titleLim = 40;
}
if (!$scope.config.showShowing) {
$scope.config.showShowing = false;
}
};
/**
* Initialize rss widget
*/
var initializeRssWidget = function() {
// Trigger loading spinner
$scope.loading = true;
// Only initialize if widget's json provides all the stuff we need
if ($scope.widget && $scope.widget.widgetURL) {
// Make sure config has values
checkForWidgetConfig();
// Get rss feed from provided url
widgetService.getRssAsJson($scope.widget.widgetURL)
.then(function(result) {
// If we got data, load it and turn off loading spinner
$scope.data = result;
$scope.loading = false;
// Check for errors or emptiness and update display as needed
if ($scope.status !== 'ok') {
$scope.error = true;
$scope.loading = false;
} else {
if (!$scope.items || $scope.items.length == 0) {
$scope.isEmpty = true;
$scope.loading = false;
$scope.error = true;
} else {
// Show 'show more' if the feed is longer than the display limit
if (!$scope.config.showShowing && $scope.items.length > $scope.config.lim) {
$scope.config.showShowing = true;
}
}
}
})
.catch(function(error) {
// If the service couldn't get data, display error messages
$log.warn('Couldn\'t get rss as JSON');
$log.error(error);
$scope.error = true;
$scope.isEmpty = true;
$scope.loading = false;
});
}
};
// Initialize the widget
initializeRssWidget();
}]);
// CUSTOM & GENERIC widget types
app.controller('CustomWidgetController', ['$scope', '$log', 'widgetService', function($scope, $log, widgetService) {
/**
* Fetch additional widget data
*/
var populateWidgetContent = function() {
if ($scope.widget.widgetURL && $scope.widget.widgetType) {
// Trigger loading spinner
$scope.loading = true;
// Fetch additional widget data
widgetService.getWidgetJson($scope.widget).then(function(data) {
$scope.loading = false;
if (data) {
$scope.widget.widgetData = data;
$scope.content = $scope.widget.widgetData;
if (angular.isArray($scope.content) && $scope.content.length == 0) {
$scope.isEmpty = true;
} else if ($scope.widget.widgetConfig && $scope.widget.widgetConfig.evalString && eval($scope.widget.widgetConfig.evalString)) {
$scope.isEmpty = true;
}
} else {
$log.warn('Got nothing back from widget fetch from: ' + $scope.widget.widgetURL);
$scope.isEmpty = true;
}
}, function() {
// After we get widget data, turn off loading spinner
$scope.loading = false;
});
}
};
/**
* Initialize scope variables before getting widget content
* @param template The provided custom HTML template
*/
var initializeCustomWidget = function(template) {
$scope.content = [];
$scope.template = template;
$scope.isEmpty = false;
$scope.widget.widgetData = [];
populateWidgetContent();
};
// Initialize widget if we received a custom HTML template, otherwise display empty widget and log a warning
if ($scope.widget.widgetTemplate) {
initializeCustomWidget($scope.widget.widgetTemplate);
} else {
$scope.loading = false;
$scope.isEmpty = true;
$log.warn($scope.widget.fname + ' said it\'s a custom/generic widget, but didn\'t provide a template.');
}
}]);
// WEATHER widget type
app.controller('WeatherWidgetController', ['$scope', '$log', '$q', 'widgetService', 'keyValueService', function($scope, $log, $q, widgetService, keyValueService) {
// Local variables
var fetchKey = 'userWeatherPreference';
/**
* Change temperature unit of measurement
*/
$scope.cycleUnits = function() {
var userPreference = $scope.nextUnits;
var value = {};
// Switch temperature based on which unit is next
switch(userPreference) {
case 'F':
convertKelvinToFahrenheit();
$scope.currentUnits = 'F';
$scope.nextUnits = 'C';
break;
case 'C':
convertFahrenheitToCelsius();
$scope.currentUnits = 'C';
$scope.nextUnits = 'K';
break;
case 'K':
convertCelsiusToKelvin();
$scope.currentUnits = 'K';
$scope.nextUnits = 'F';
break;
default:
convertCelsiusToKelvin();
$scope.currentUnits = 'K';
$scope.nextUnits = 'F';
}
// Set user preference
value.userWeatherPreference = $scope.currentUnits;
// Remember the user's preferred unit of measurement
keyValueService.setValue(fetchKey, value);
};
/**
* Fetch additional widget data
*/
var populateWidgetContent = function() {
// Declare asynchronous promises to be executed in $q.all()
var widgetPromise = widgetService.getWidgetJson($scope.widget);
var userPreferencesPromise = keyValueService.getValue(fetchKey);
// Execute promises, then resolve when both have returned something
$q.all( [widgetPromise, userPreferencesPromise] ).then(function(data) {
// Turn off loading spinner
$scope.loading = false;
// If we got some data, populate widget content
if (data) {
// Set local variables
var allTheWeathers = data[0];
var myPref = data[1];
var userPreference = myPref.userWeatherPreference;
// Set scope variables
if (allTheWeathers) {
$scope.weatherData = allTheWeathers.weathers;
}
$scope.currentUnits = 'F';
$scope.nextUnits = 'C';
// If the user doesn't have a preference, default to Fahrenheit
if (userPreference === null || userPreference === '' || typeof userPreference === 'undefined') {
userPreference = 'F';
}
// Cycle units until we match with the user's preference
while (userPreference != $scope.currentUnits) {
$scope.cycleUnits();
}
} else {
// Log any errors getting data
$scope.error = true;
$log.warn('WeatherWidgetController could\'t get widget json');
}
// Return to resolve $q.all()
return data;
}).catch(function() {
$scope.loading = false;
$scope.error = true;
$log.warn('WeatherWidgetController wrote a check it simply couldn\'t cash');
});
};
/**
* Convert from Fahrenheit to Celsius
*/
var convertFahrenheitToCelsius = function() {
var ratio = (5 / 9);
var offset = 32;
for (var i = 0; i < $scope.weatherData.length; i++) {
$scope.weatherData[i].currentWeather.temperature =
($scope.weatherData[i].currentWeather.temperature -
offset) * ratio;
for (var j = 0; j < $scope.weatherData[i].forecast.length; j++) {
$scope.weatherData[i].forecast[j].highTemperature =
($scope.weatherData[i].forecast[j].highTemperature -
offset) * ratio;
$scope.weatherData[i].forecast[j].lowTemperature =
($scope.weatherData[i].forecast[j].lowTemperature -
offset) * ratio;
}
}
};
/**
* Convert from Celsius to Fahrenheit
*/
var convertCelsiusToFahrenheit = function() {
var ratio = (9 / 5);
var offset = 32;
for (var i = 0; i < $scope.weatherData.length; i++) {
$scope.weatherData[i].currentWeather.temperature =
($scope.weatherData[i].currentWeather.temperature *
ratio) + offset;
for (var j = 0; j < $scope.weatherData[i].forecast.length; j++) {
$scope.weatherData[i].forecast[j].highTemperature =
($scope.weatherData[i].forecast[j].highTemperature *
ratio) + offset;
$scope.weatherData[i].forecast[j].lowTemperature =
($scope.weatherData[i].forecast[j].lowTemperature *
ratio) + offset;
}
}
};
/**
* Convert from Celsius to Kelvin
*/
var convertCelsiusToKelvin = function() {
var offset = 273;
for (var i = 0; i < $scope.weatherData.length; i++) {
$scope.weatherData[i].currentWeather.temperature =
($scope.weatherData[i].currentWeather.temperature + offset);
for (var j = 0; j < $scope.weatherData[i].forecast.length; j++) {
$scope.weatherData[i].forecast[j].highTemperature =
($scope.weatherData[i].forecast[j].highTemperature + offset);
$scope.weatherData[i].forecast[j].lowTemperature =
($scope.weatherData[i].forecast[j].lowTemperature + offset);
}
}
};
/**
* Convert from Kelvin to Fahrenheit (via Celsius)
*/
var convertKelvinToFahrenheit = function() {
convertKelvinToCelsius();
convertCelsiusToFahrenheit();
};
/**
* Convert from Kelvin to Celsius
*/
var convertKelvinToCelsius = function() {
var offset = 273;
for (var i = 0; i < $scope.weatherData.length; i++) {
$scope.weatherData[i].currentWeather.temperature =
($scope.weatherData[i].currentWeather.temperature - offset);
for (var j = 0; j < $scope.weatherData[i].forecast.length; j++) {
$scope.weatherData[i].forecast[j].highTemperature =
($scope.weatherData[i].forecast[j].highTemperature - offset);
$scope.weatherData[i].forecast[j].lowTemperature =
($scope.weatherData[i].forecast[j].lowTemperature - offset);
}
}
};
// Initialize weather widget
$scope.loading = false;
if ($scope.widget.widgetURL) {
$scope.loading = true;
$scope.weatherData = [];
$scope.currentUnits = 'F';
$scope.nextUnits = 'C';
populateWidgetContent();
} else {
$log.warn('WeatherWidgetController did not receive a widgetURL');
}
}]);
});