-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
124 lines (110 loc) · 3.71 KB
/
script.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
var sliderApp = angular.module('MyApp', ['ngAnimate', 'ngResource']);
sliderApp
.directive('slider', ['$resource', '$interval', function($resource, $interval) {
return {
restrict: 'AE',
replace: true,
link: function(scope, elem, attrs) {
// Initial
scope.dataLoaded = false;
var data = $resource('json/images.json').query();
data.$promise.then(function() {
scope.images = data;
scope.dataLoaded = true;
});
// Add images
scope.addImages = function() {
var newData = $resource('json/images.json').query();
newData.$promise.then(function() {
if(scope.images.length < newData.length) {
scope.images.push(newData[scope.images.length]);
}
});
}
$interval(function() {
scope.addImages();
}, 3000);
// Intial index number
scope.currentIndex = 0;
scope.randomIndex = function() {
scope.currentIndex = Math.floor(Math.random() * (scope.images.length));
scope.images.forEach(function(image) {
image.visible = false;
});
scope.images[scope.currentIndex].visible = true;
};
// Loop images
var t = $interval(function() {
scope.randomIndex();
}, 4000);
scope.onclick = function() {
$interval.cancel(t);
};
},
templateUrl: 'slider.html'
};
}])
.controller('randomController', ['$scope', '$resource', 'sharedService', function($scope, $resource, sharedService) {
$scope.start = true;
// Initial
$scope.onStart = function(){
$scope.start = false;
$scope.loading = true;
var data = $resource('json/random.json').query();
data.$promise.then(function() {
$scope.images = sharedService.shuffleArray(data);
// Waiting a few sec.
var time = new Date().getTime();
while (new Date().getTime() < time + 3000);
$scope.loading = false;
var i = 1;
$scope.images.forEach(function(image) {
if(i <= 15) {
image.visible = true;
}
i++;
});
});
}
}])
.service('sharedService', function(){
return {
shuffleArray: function(array) {
return array.map(function(a){return {weight:Math.random(), value:a}}).sort(function(a, b){return a.weight - b.weight}).map(function(a){return a.value});
}
}
})
.controller('top3Controller', ['$scope', '$resource', function($scope, $resource) {
var data = $resource('json/contest.json').get();
data.$promise.then(function() {
$scope.contests = data;
});
$scope.getResult = function(award, number) {
$scope.top = false;
$scope.award = award;
if(number == 0) {
$scope.first = $scope.contests[award][number];
} else {
$scope.second = $scope.contests[award][number];
$scope.third = $scope.contests[award][number+1];
}
}
$scope.showResultAll = function() {
$scope.top = false;
$scope.show = true;
}
$scope.backToList = function() {
resetResultScope();
$scope.top = true;
}
$scope.onStart = function(){
$scope.start = false;
$scope.top = true;
}
// Reset previous award from scope
function resetResultScope() {
$scope.first = null;
$scope.second = null;
$scope.third = null;
}
}])