Blame view
scripts/FlashcardDirective.js
4.15 KB
8e8058a82
|
1 |
angular.module('flashy.FlashcardDirective', []). |
989f32fee
|
2 |
directive('flashcard', ['$http', '$state', '$window', |
a0d3b9b1d
|
3 |
function($http, $state, $window) { |
989f32fee
|
4 5 6 7 8 9 10 |
return { templateUrl: '/app/templates/flashcard.html', restrict: 'E', scope: { flashcard: '=flashcardObj', // flashcard-obj in parent html refresh: '&' // eval refresh in parent html }, |
a0d3b9b1d
|
11 |
link: function(scope, element) { |
989f32fee
|
12 13 14 |
/* Handles width of the card */ function refresh_width() { avail = $window.innerWidth - 17; |
989f32fee
|
15 16 17 18 19 |
width = Math.floor(avail / Math.floor(avail / 250) - 12); element.children().css({ width: width + 'px', height: (width * 3 / 5) + 'px', 'font-size': 100 * width / 250 + '%' |
cb1ad9198
|
20 |
}); |
989f32fee
|
21 22 23 |
}; angular.element($window).bind('resize', refresh_width); refresh_width(); |
714fedf89
|
24 |
scope.textPieces = []; |
714fedf89
|
25 26 27 28 29 30 31 32 33 34 |
scope.flashcard.mask.sort(function(a, b) { return a[0] - b[0]; }); var i = 0; scope.flashcard.mask.forEach(function(blank) { scope.textPieces.push({text: scope.flashcard.text.slice(i, blank[0])}); scope.textPieces.push({text: scope.flashcard.text.slice(blank[0], blank[1]), blank: true}); i = blank[1]; }); scope.textPieces.push({text: scope.flashcard.text.slice(i)}); |
989f32fee
|
35 |
/* Pulls card from feed into deck */ |
a0d3b9b1d
|
36 |
scope.pullCard = function(flashcard) { |
989f32fee
|
37 38 |
if ($state.current.name == 'feed') { $http.post('/api/flashcards/' + flashcard.id + '/pull/', flashcard). |
a0d3b9b1d
|
39 |
success(function(data) { |
989f32fee
|
40 41 42 43 |
console.log('pulled flashcard #' + flashcard.id); scope.startShrink = true; scope.refresh(); }). |
a0d3b9b1d
|
44 |
error(function(data) { |
989f32fee
|
45 46 47 48 |
console.log('failed to pull flashcard #' + flashcard.id); }); } }; |
cb1ad9198
|
49 |
|
989f32fee
|
50 |
/* Unpulls card from deck */ |
a0d3b9b1d
|
51 |
scope.unpullCard = function(flashcard) { |
989f32fee
|
52 53 |
if ($state.current.name == 'deck') { console.log('unpulling card...'); |
323c738e1
|
54 |
|
989f32fee
|
55 56 |
$http.post('/api/flashcards/' + flashcard.id + '/unpull/', flashcard). |
a0d3b9b1d
|
57 |
success(function(data) { |
989f32fee
|
58 59 60 61 |
console.log('card unpull success'); scope.startShrink = true; scope.refresh(); }). |
a0d3b9b1d
|
62 |
error(function(data) { |
989f32fee
|
63 64 65 66 67 68 |
console.log('card unpull FAILURE'); }); } }; /* Hides card from feed */ |
a0d3b9b1d
|
69 |
scope.hideCard = function(flashcard) { |
989f32fee
|
70 71 72 |
if ($state.current.name == 'feed') { $http.post('/api/flashcards/' + flashcard.id + '/hide/', flashcard). |
a0d3b9b1d
|
73 |
success(function(data) { |
989f32fee
|
74 75 76 77 |
console.log('card hide success'); scope.startShrink = true; scope.refresh(); }). |
a0d3b9b1d
|
78 |
error(function(data) { |
989f32fee
|
79 80 81 82 |
console.log('card hide FAILURE'); }); } }; |
cb1ad9198
|
83 |
} |
0cb0637f2
|
84 |
}; |
989f32fee
|
85 |
}]); |