-
Notifications
You must be signed in to change notification settings - Fork 135
/
tr-boards.client.directive.js
67 lines (57 loc) · 2 KB
/
tr-boards.client.directive.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
import photos from '@/modules/core/client/services/photos.service';
/**
* Directive that simply picks a background image given element and emits copyright info down the scope.
*
* Usage:
* <div tr-boards="imageid">
*
* Or from many:
* <div tr-boards="['imageid1', 'imageid2']">
*
* To stop cover image being set for small screens (<= 480px width),
* use `tr-boards-ignore-small` attribute.
*
*/
angular.module('core').directive('trBoards', trBoardsDirective);
/* @ngInject */
function trBoardsDirective($window) {
return {
restrict: 'A',
replace: false,
scope: {
trBoards: '=',
},
link(scope, elem, attrs) {
// Don't set background images for mobile screens if defined so via attribute
if (
angular.isDefined(attrs.trBoardsIgnoreSmall) &&
$window.innerWidth <= 480
) {
return;
}
// If requested photo is missing or request is invalid, rely on this photo
const defaultPhoto = 'bokeh';
// scope.trBoards might be an array (therefore just pick one key from it) or a string (thus just use it as is)
const key = angular.isArray(scope.trBoards)
? scope.trBoards[Math.floor(Math.random() * scope.trBoards.length)]
: scope.trBoards;
// Pick the photo
const photo = photos[key] || photos[defaultPhoto];
// Add photo as a background to the element
elem.addClass('board-' + key);
// For small screens, if mobile image exists, use it
const file =
$window.innerWidth <= 480 && photo.file_mobile
? photo.file_mobile
: photo.file;
elem.css({
'background-image': 'url(/img/board/' + file + ')',
});
// To prevent key being literally `key`: `{key: ...}`, we want it to be actual keyname such as `hitchroad`.
const photoObject = {};
photoObject[key] = photo;
// Send copyright info down the scope... something will pick it up! (pst, core/app-controller)
scope.$emit('photoCreditsUpdated', photoObject);
},
};
}