CardListController.js
5.01 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
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
angular.module('flashy.CardListController', ['ui.router', 'angular.filter', 'ngSanitize', 'flashy.DeckFactory']).
controller('CardListController', function($scope, $rootScope, $state, $http, $stateParams, Flashcard, Deck) {
// cards array
sectionId = parseInt($stateParams.sectionId);
$scope.deck = new Deck(sectionId, {
cardPullCallback: function(card) {
Materialize.toast('Pulled!', 3000);
},
cardUnpullCallback: function(card) {
Materialize.toast('Unpulled!', 3000);
},
cardHideCallback: function(card) {
card.is_hidden = true;
Materialize.toast('Hidden!', 3000);
},
cardUnhideCallback: function(card) {
card.is_hidden = false;
Materialize.toast('Unhidden!', 3000);
}
});
$rootScope.currentSection = $rootScope.SectionResource.get({sectionId: sectionId});
$scope.cards = [];
$http.get('/api/sections/' + sectionId + '/flashcards/?hidden=yes').
success(function(data) {
for (i in data) $scope.cards[data[i].id] = new Flashcard(data[i], $scope.deck);
}).
error(function(err) {
console.log('pulling feed failed');
});
// flag/report card
$scope.flag = function(card) {
$http.post('/api/flashcards/' + card.id + '/report/').
success(function(data) {
console.log(card.text + ' reported');
}).
error(function(err) {
console.log('no flag for you');
});
};
// toggle button text from show to hide
$(function() {
$('#showHidden').click(function() {
$(this).text(function(i, text) {
return text === 'Show Hidden' ? 'Hide Hidden' : 'Show Hidden';
});
});
});
$scope.$on('$destroy', function() {
$rootScope.currentSection = {};
$(document).off('keydown');
});
$(document).ready(function() {
$('.tooltipped').tooltip({delay: 50});
//back to top
var offset = 300;
var duration = 300;
$(window).scroll(function() {
if ($(this).scrollTop() > offset) {
$('.back-to-top').fadeIn(duration);
} else {
$('.back-to-top').fadeOut(duration);
}
});
$('.back-to-top').click(function(event) {
event.preventDefault();
$('html, body').animate({scrollTop: 0}, duration);
return false;
});
});
// to display day of the week badges
$scope.dayofweek = function(item) {
var date = new Date(item.material_date);
return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][date.getDay()];
};
// checkbox filter
$scope.filter = {
'week1': true,
'week2': true,
'week3': true,
'week4': true,
'week5': true,
'week6': true,
'week7': true,
'week8': true,
'week9': true,
'week10': true,
};
$scope.filterByDate = function(item) {
var week = item.material_week_num;
return (week == 1 && $scope.filter['week1']) ||
(week == 2 && $scope.filter['week2']) ||
(week == 3 && $scope.filter['week3']) ||
(week == 4 && $scope.filter['week4']) ||
(week == 5 && $scope.filter['week5']) ||
(week == 6 && $scope.filter['week6']) ||
(week == 7 && $scope.filter['week7']) ||
(week == 8 && $scope.filter['week8']) ||
(week == 9 && $scope.filter['week9']) ||
(week == 10 && $scope.filter['week10']);
};
$scope.$on('$destroy', function() {
$scope.deck.cleanup();
Flashcard.cleanup();
});
}
).
filter('displayCard', function($sce) {
return function(card) {
// text to display as html
var cardText = '';
var start = 0; // where to start next string break
// get all display pieces and blank pieces
for (var i = 0; i < card.mask.length; i++) {
cardText = cardText.concat(card.text.substring(start, card.mask[i][0]));
cardText = cardText.concat('<b>');
cardText = cardText.concat(card.text.substring(card.mask[i][0], card.mask[i][1]));
cardText = cardText.concat('</b>');
start = card.mask[i][1];
}
// get remaining dislay pieces, if any
if (start != card.mask.length - 1)
cardText = cardText.concat(card.text.substring(start));
return $sce.trustAsHtml(cardText);
};
});