CardListController.js 5.01 KB
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);
};
});