Blame view
scripts/CardGridController.js
4.84 KB
ee30a48eb
|
1 |
angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).controller = |
180aa08bb
|
2 |
function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) { |
140291561
|
3 |
$scope.cards = []; |
bf0941f00
|
4 |
$scope.deck = []; |
c3350cb1f
|
5 6 7 8 9 10 |
$scope.cardCols = []; // organized data $scope.numCols = 0; $scope.cardTable = {}; // look up table of cards, {'colNum':col, 'obj':card} $scope.sectionId = parseInt($stateParams.sectionId); $scope.section = $rootScope.SectionResource.get({sectionId: $scope.sectionId}); $rootScope.currentSection = $scope.section; |
c3350cb1f
|
11 12 13 |
if (!UserService.isInSection($scope.sectionId)) { console.log('user is not enrolled in ' + $scope.sectionId); |
88ca25463
|
14 |
$state.go('addclass'); |
c3350cb1f
|
15 |
} |
c3350cb1f
|
16 |
$scope.refreshColumnWidth = function() { |
c3350cb1f
|
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
avail = $window.innerWidth - 17; width = Math.floor(avail / Math.floor(avail / 250)); $('.cardColumn').css({ width: width + 'px', 'font-size': 100 * width / 250 + '%' }); $('.cardColumn .card.flashy').css({ width: width - 12 + 'px', height: (width * 3 / 5) + 'px' }); }; $scope.refreshLayout = function() { numCols = Math.max(1, Math.floor(($window.innerWidth - 17) / 250)); // check if we actually need to refresh the whole layout if (numCols == $scope.numCols) return $scope.refreshColumnWidth(); $scope.numCols = numCols; console.log('refreshing layout for ' + $scope.numCols + ' columns'); $scope.cardCols = []; var cols = []; for (i = 0; i < $scope.numCols; i++) cols.push([]); $scope.cards.forEach(function(card, i) { |
180aa08bb
|
40 41 |
card.colNum = i % $scope.numCols; cols[card.colNum].push(card); |
c3350cb1f
|
42 |
}); |
180aa08bb
|
43 |
for (i in cols) $scope.updateColRanks(cols[i]); |
1653dec57
|
44 |
console.log(cols); |
c3350cb1f
|
45 46 47 48 49 50 |
$timeout(function() { $scope.cardCols = cols; $timeout($scope.refreshColumnWidth); }); }; |
a7ee2b64a
|
51 |
angular.element($window).bind('resize', $scope.refreshLayout); |
ee30a48eb
|
52 53 54 55 56 |
$scope.ws_host = window.location.origin.replace('http', 'ws'); $scope.deck_ws = $websocket($scope.ws_host + '/ws/deck/' + $scope.sectionId + '?subscribe-user'); $scope.deck_ws.onOpen(function() { console.log('deck ws open'); }); |
07a06ca9d
|
57 |
$scope.deck_ws.onMessage(function(message) { |
1653dec57
|
58 59 |
data = JSON.parse(message.data); console.log('message', data); |
180aa08bb
|
60 |
card = new Flashcard(data.flashcard.id); |
bf0941f00
|
61 |
if (data.event_type == 'card_pulled') { |
bf0941f00
|
62 |
$scope.deck[card.id] = card; |
1653dec57
|
63 |
} |
bf0941f00
|
64 |
if (data.event_type == 'card_unpulled') { |
bf0941f00
|
65 |
$scope.deck[card.id] = undefined; |
ba553b18a
|
66 |
} |
180aa08bb
|
67 68 69 |
if (data.event_type == 'card_hidden') { $scope.hideCardFromGrid(card); } |
07a06ca9d
|
70 |
}); |
ba553b18a
|
71 |
$scope.cardInDeck = function(id) { |
bf0941f00
|
72 |
return $scope.deck[id]; |
ba553b18a
|
73 |
}; |
bf0941f00
|
74 |
$scope.addCardToGrid = function(card) { |
c3350cb1f
|
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
var colNum = 0; var lowestCol = $scope.cardCols[0]; var lowestColNum = 0; while (colNum < $scope.numCols) { if ($scope.cardCols[colNum].length == 0) { lowestCol = $scope.cardCols[colNum]; break; } else if ($scope.cardCols[colNum].length < lowestCol.length) { lowestCol = $scope.cardCols[colNum]; lowestColNum = colNum; lowestColLen = $scope.cardCols[colNum].length; } colNum++; } console.log(card); $scope.cards.push(data); |
63d40a399
|
91 92 93 |
lowestCol.unshift(card); $scope.cardTable[card.id] = {'colNum': lowestColNum, 'obj': card}; $timeout($scope.refreshColumnWidth); |
0dfc72586
|
94 |
|
c3350cb1f
|
95 |
}; |
180aa08bb
|
96 97 98 99 100 101 102 103 104 |
$scope.updateColRanks = function(col) { for (i in col) col[i].colRank = i; }; $scope.hideCardFromGrid = function(card) { console.log('hiding', card); $scope.cardCols[card.colNum].splice(card.colRank, 1); $scope.updateColRanks($scope.cardCols[card.colNum]); }; |
140291561
|
105 |
|
07a06ca9d
|
106 107 |
$http.get('/api/sections/' + $scope.sectionId + '/deck/'). success(function(data) { |
bf0941f00
|
108 |
for (i in data) $scope.deck[data[i].id] = data[i]; |
07a06ca9d
|
109 |
console.log("got user's deck"); |
07a06ca9d
|
110 111 112 113 |
}). error(function(err) { console.log('get deck failed'); }); |
c3350cb1f
|
114 |
$scope.$on('$destroy', function() { |
ee30a48eb
|
115 |
$scope.deck_ws.close(); |
c3350cb1f
|
116 117 118 119 |
$rootScope.currentSection = {}; $(document).off('keydown'); }); }; |