NoSpoilersController.js
2.2 KB
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
angular.module('footyroom').controller('NoSpoilersController', ['$scope', 'noSpoilers', function ($scope, noSpoilersService) {
var ctrl = this;
ctrl.change = change;
ctrl.isEnabled = noSpoilersService.get();
$scope.$on('noSpoilers', function (event, isEnabled) {
ctrl.isEnabled = isEnabled;
});
function change() {
noSpoilersService.set(ctrl.isEnabled);
}
}]);
angular.module('footyroom').service('noSpoilers', ['$rootScope', '$http', '$window', function ($rootScope, $http, $window) {
var service = this;
var isEnabled = DataStore.noSpoilers;
service.set = set;
service.get = get;
// For guests we want to persist query string override of local storage
// setting so that subsequent page requests continue to hide spoilers.
if (DataStore.isGuest) {
persistForGuest();
}
return service;
function set(newValue) {
isEnabled = newValue;
noSpoilers(isEnabled);
if (DataStore.isGuest) {
persistForGuest();
} else if (!DataStore.isGuest) {
$http.post('/profile/settings/no-spoilers', { enabled: isEnabled });
}
$rootScope.$broadcast('noSpoilers', isEnabled);
}
function persistForGuest() {
if ($window.localStorage && $window.localStorage.noSpoilers !== isEnabled) {
$window.localStorage.setItem('noSpoilers', JSON.stringify(isEnabled));
}
}
function get() {
return isEnabled;
}
}]);
function noSpoilers(enabled) {
if (enabled) {
$('html').addClass('no-spoilers');
document.title = document.title
.replace(/ [0-9]+ \- [0-9]+ /g, ' vs ')
.replace(/ \[PEN: [0-9]+\-[0-9]+\]/g, '');
} else {
$('html').removeClass('no-spoilers');
}
}
(function initNoSpoilers() {
if (DataStore.isGuest && !DataStore.noSpoilers) {
try {
DataStore.noSpoilers = window.localStorage
? JSON.parse(window.localStorage.noSpoilers || false)
: false;
} catch (e) {
DataStore.noSpoilers = false;
}
}
// Run this before page is loaded to prevent flash of spoilers.
noSpoilers(DataStore.noSpoilers);
}());