Commit 5ce17b918b2a52eb4061d92fafef78a27da90dfc

Authored by Tetranoir
1 parent c30d9c98d6

feed controller, functions to columnize things

Showing 1 changed file with 59 additions and 28 deletions Side-by-side Diff

scripts/FeedController.js View file @ 5ce17b9
1 1 angular.module('flashy.FeedController', ['ui.router']).
2 2  
3 3 controller('FeedController', ['$scope', '$stateParams', '$state', '$http', function ($scope, $stateParams, $state, $http) {
4   -
5   -
6   -
7 4 console.log('Hello from feed');
8 5 sectionId = $stateParams.sectionId;
9   - $scope.cards = [];
10   -
  6 + $scope.cardCols = [];
  7 + $scope.cols = 1;
  8 +
  9 + /* Handles number of columns */
  10 + function calculate_cols() {
  11 + avail = $window.innerWidth - 17;
  12 + if (avail > 992) avail -= 240;
  13 + cols = Math.floor(avail / 250);
  14 + return cols;
  15 + }
  16 + angular.element($window).bind('resize', $scope.refreshCards);
  17 + console.log('cols:' + calculate_cols());
  18 +
  19 + $scope.refreshCards = function() {
  20 + $http.get('/api/sections/' + sectionId + '/feed/').
  21 + success(function(data) {
  22 + console.log(data);
  23 + $scope.cardCols = [];
  24 + for (i = 0; i < $scope.cols; i++) $scope.cardCols.push([]);
  25 + var i = 0;
  26 + for (card in data) {
  27 + if (i >= $scope.cols) i = 0;
  28 + $scope.cardCols[i++].push(card);
  29 + }
  30 + console.log('success in refresh cards...');
  31 + }).
  32 + error(function(err) {
  33 + console.log('refresh fail');
  34 + });
  35 + };
  36 +
  37 + $scope.add = function(card) {
  38 + col = Math.floor(Math.random() * $scope.cols);
  39 + $scope.cols[col].unshift(card);
  40 + };
  41 +
  42 + $scope.hide = function(card) {
  43 +
  44 + };
  45 +
  46 + /* Instance creation */
  47 + $http.get('/api/sections/' + sectionId + '/feed/').
  48 + success(function(data) {
  49 + console.log(data);
  50 + $scope.cardCols = [];
  51 + for (i = 0; i < $scope.cols; i++) $scope.cardCols.push([]);
  52 + var i = 0;
  53 + for (card in data) {
  54 + if (i >= $scope.cols) i = 0;
  55 + $scope.cardCols[i++].push(card);
  56 + }
  57 + console.log('success in refresh cards...');
  58 + }).
  59 + error(function(err) {
  60 + console.log('pulling feed failed');
  61 + });
  62 +
11 63 var loc = window.location, new_uri;
12 64 if (loc.protocol === 'https:') {
13 65 new_uri = 'wss:';
... ... @@ -22,7 +74,7 @@
22 74 };
23 75 ws.onmessage = function(e) {
24 76 console.log('got websocket message ' + e.data);
25   - $scope.refreshCards();
  77 + $scope.add(e.data); // .data???
26 78 };
27 79 ws.onerror = function(e) {
28 80 console.error(e);
... ... @@ -31,15 +83,6 @@
31 83 console.log('connection closed');
32 84 };
33 85  
34   - $http.get('/api/sections/' + sectionId + '/feed/').
35   - success(function(data) {
36   - console.log(data);
37   - $scope.cards = data;
38   - }).
39   - error(function(err) {
40   - console.log('pulling feed failed');
41   - });
42   -
43 86 $scope.viewDeck = function() {
44 87 $state.go('deck', {sectionId: sectionId});
45 88 console.log('go to deck');
... ... @@ -55,7 +98,7 @@
55 98 'text': $scope.text,
56 99 'material_date': pushed,
57 100 'mask': '[]',
58   - section: sectionId
  101 + 'section': sectionId
59 102 };
60 103 $http.post('/api/flashcards/', myCard).
61 104 success(function(data) {
... ... @@ -67,18 +110,6 @@
67 110 });
68 111  
69 112 $scope.text = '';
70   - };
71   -
72   - $scope.refreshCards = function() {
73   - $http.get('/api/sections/' + sectionId + '/feed/').
74   - success(function(data) {
75   - console.log(data);
76   - $scope.cards = data;
77   - console.log('success in refresh cards...');
78   - }).
79   - error(function(err) {
80   - console.log('refresh fail');
81   - });
82 113 };
83 114  
84 115 /* Key bindings for the whole feed window. Hotkey it up! */