Blame view

scripts/FlashcardDirective.js 4.15 KB
8e8058a82   Rachel Lee   Working on flashc...
1
  angular.module('flashy.FlashcardDirective', []).
989f32fee   Andrew Buss   scale text too
2
      directive('flashcard', ['$http', '$state', '$window',
a0d3b9b1d   Andrew Buss   tiny style tweaks
3
          function($http, $state, $window) {
989f32fee   Andrew Buss   scale text too
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   Andrew Buss   tiny style tweaks
11
                  link: function(scope, element) {
989f32fee   Andrew Buss   scale text too
12
13
14
                      /* Handles width of the card */
                      function refresh_width() {
                          avail = $window.innerWidth - 17;
989f32fee   Andrew Buss   scale text too
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   Rachel Lee   Unpull from deck ...
20
                          });
989f32fee   Andrew Buss   scale text too
21
22
23
                      };
                      angular.element($window).bind('resize', refresh_width);
                      refresh_width();
714fedf89   Andrew Buss   display blanks in...
24
                      scope.textPieces = [];
714fedf89   Andrew Buss   display blanks in...
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   Andrew Buss   scale text too
35
                      /* Pulls card from feed into deck */
a0d3b9b1d   Andrew Buss   tiny style tweaks
36
                      scope.pullCard = function(flashcard) {
989f32fee   Andrew Buss   scale text too
37
38
                          if ($state.current.name == 'feed') {
                              $http.post('/api/flashcards/' + flashcard.id + '/pull/', flashcard).
a0d3b9b1d   Andrew Buss   tiny style tweaks
39
                                  success(function(data) {
989f32fee   Andrew Buss   scale text too
40
41
42
43
                                      console.log('pulled flashcard #' + flashcard.id);
                                      scope.startShrink = true;
                                      scope.refresh();
                                  }).
a0d3b9b1d   Andrew Buss   tiny style tweaks
44
                                  error(function(data) {
989f32fee   Andrew Buss   scale text too
45
46
47
48
                                      console.log('failed to pull flashcard #' + flashcard.id);
                                  });
                          }
                      };
cb1ad9198   Rachel Lee   Unpull from deck ...
49

989f32fee   Andrew Buss   scale text too
50
                      /* Unpulls card from deck */
a0d3b9b1d   Andrew Buss   tiny style tweaks
51
                      scope.unpullCard = function(flashcard) {
989f32fee   Andrew Buss   scale text too
52
53
                          if ($state.current.name == 'deck') {
                              console.log('unpulling card...');
323c738e1   Andrew Buss   stop listening fo...
54

989f32fee   Andrew Buss   scale text too
55
56
                              $http.post('/api/flashcards/' + flashcard.id + '/unpull/',
                                  flashcard).
a0d3b9b1d   Andrew Buss   tiny style tweaks
57
                                  success(function(data) {
989f32fee   Andrew Buss   scale text too
58
59
60
61
                                      console.log('card unpull success');
                                      scope.startShrink = true;
                                      scope.refresh();
                                  }).
a0d3b9b1d   Andrew Buss   tiny style tweaks
62
                                  error(function(data) {
989f32fee   Andrew Buss   scale text too
63
64
65
66
67
68
                                      console.log('card unpull FAILURE');
                                  });
                          }
                      };
  
                      /* Hides card from feed */
a0d3b9b1d   Andrew Buss   tiny style tweaks
69
                      scope.hideCard = function(flashcard) {
989f32fee   Andrew Buss   scale text too
70
71
72
                          if ($state.current.name == 'feed') {
                              $http.post('/api/flashcards/' + flashcard.id + '/hide/',
                                  flashcard).
a0d3b9b1d   Andrew Buss   tiny style tweaks
73
                                  success(function(data) {
989f32fee   Andrew Buss   scale text too
74
75
76
77
                                      console.log('card hide success');
                                      scope.startShrink = true;
                                      scope.refresh();
                                  }).
a0d3b9b1d   Andrew Buss   tiny style tweaks
78
                                  error(function(data) {
989f32fee   Andrew Buss   scale text too
79
80
81
82
                                      console.log('card hide FAILURE');
                                  });
                          }
                      };
cb1ad9198   Rachel Lee   Unpull from deck ...
83
                  }
0cb0637f2   Andrew Buss   refactored css, h...
84
              };
989f32fee   Andrew Buss   scale text too
85
          }]);