/
index.js
135 lines (116 loc) · 3.2 KB
/
index.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
/**
* URL-Cycle
*
* Cycles through a configurable list of URLs of sites or images
*/
var app = angular.module('cycleApp', ['ngRoute']);
app.factory('$shared', function($rootScope) {
var storage = {
configUrl: window.location.origin + '/config.json'
},
cached = angular.fromJson(localStorage.getItem('cycleApp'));
if (cached) {
storage = cached;
}
$rootScope.$on('save', function() {
localStorage.setItem('cycleApp', angular.toJson(storage));
});
return storage;
});
app.controller('CycleCtrl', function($scope, $shared, $http, $sce) {
$scope.status = '';
$scope.entryIndex = -1;
$scope.imgActive = [false, false];
$scope.imgUrl = ['', ''];
$scope.imgIndex = 1;
$scope.frameActive = [false, false];
$scope.frameSize = [['100%', '100%'], ['100%', '100%']];
$scope.frameUrl = ['', ''];
$scope.frameIndex = 1;
$scope.next = function() {
var entry = $scope.entries[++$scope.entryIndex % $scope.entries.length];
// console.log('next', $scope.entryIndex, entry);
if (entry.type === 'img') {
$scope.showImage(entry.url);
}
else {
$scope.showFrame(entry);
}
setTimeout($scope.next, entry.duration * 1000);
$scope.$apply();
};
function prepareUrl(url) {
return url + (url.indexOf('?') > -1 ? '&' : '?') + 'r=' + Math.random() * 1000000;
}
$scope.showImage = function(url) {
$scope.imgIndex = Math.abs($scope.imgIndex - 1);
$scope.imgUrl[$scope.imgIndex] = prepareUrl(url);
setTimeout(function() {
$scope.status = url;
$scope.imgActive[$scope.imgIndex] = true;
$scope.imgActive[Math.abs($scope.imgIndex - 1)] = false;
$scope.frameActive[0] = false;
$scope.frameActive[1] = false;
$scope.$apply();
}, 1000);
};
$scope.showFrame = function(entry) {
$scope.frameIndex = Math.abs($scope.frameIndex - 1);
$scope.frameUrl[$scope.frameIndex] = $sce.trustAsResourceUrl(prepareUrl(entry.url));
setTimeout(function() {
$scope.status = entry.url;
$scope.frameActive[$scope.frameIndex] = true;
$scope.frameActive[Math.abs($scope.frameIndex - 1)] = false;
$scope.frameSize[$scope.frameIndex] = [
entry.width || '100%',
entry.height || '100%'
];
$scope.imgActive[0] = false;
$scope.imgActive[1] = false;
$scope.$apply();
}, 1000);
};
$http.get($shared.configUrl)
.success(function(data) {
$scope.index = 0;
$scope.entries = data.entries;
setTimeout($scope.next, 0);
})
.error(function() {
console.log('$http.get error', arguments);
});
});
app.controller('ConfigCtrl', function($scope, $shared, $http, $rootScope, $location) {
$scope.saved = false;
$scope.configUrl = $shared.configUrl;
function items() {
$http.get($shared.configUrl)
.success(function(data) {
$scope.items = data.entries;
})
.error(function() {
console.log('$http.get error', arguments);
});
}
items();
$scope.save = function(e) {
$shared.configUrl = $scope.configUrl;
$rootScope.$broadcast('save');
$scope.saved = true;
items();
};
});
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/cycle', {
templateUrl: 'cycle.html',
controller: 'CycleCtrl'
})
.when('/config', {
templateUrl: 'config.html',
controller: 'ConfigCtrl'
})
.otherwise({
redirectTo: '/cycle'
});
}]);