Commit bf0941f00ca8219665f7816f41e640a688ab050d
1 parent
08fac4c5ea
Exists in
master
and in
1 other branch
continue refactoring deck, feed sorting
Showing 6 changed files with 59 additions and 88 deletions Inline Diff
scripts/CardGridController.js
View file @
bf0941f
angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).controller = | 1 | 1 | angular.module('flashy.CardGridController', ['ui.router', 'ngAnimate', 'ngWebSocket']).controller = | |
function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { | 2 | 2 | function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { | |
$scope.cards = []; | 3 | 3 | $scope.cards = []; | |
$scope.deck_cards = []; | 4 | 4 | $scope.deck = []; | |
$scope.cardCols = []; // organized data | 5 | 5 | $scope.cardCols = []; // organized data | |
$scope.numCols = 0; | 6 | 6 | $scope.numCols = 0; | |
$scope.cardTable = {}; // look up table of cards, {'colNum':col, 'obj':card} | 7 | 7 | $scope.cardTable = {}; // look up table of cards, {'colNum':col, 'obj':card} | |
$scope.sectionId = parseInt($stateParams.sectionId); | 8 | 8 | $scope.sectionId = parseInt($stateParams.sectionId); | |
$scope.section = $rootScope.SectionResource.get({sectionId: $scope.sectionId}); | 9 | 9 | $scope.section = $rootScope.SectionResource.get({sectionId: $scope.sectionId}); | |
$rootScope.currentSection = $scope.section; | 10 | 10 | $rootScope.currentSection = $scope.section; | |
11 | 11 | |||
if (!UserService.isInSection($scope.sectionId)) { | 12 | 12 | if (!UserService.isInSection($scope.sectionId)) { | |
console.log('user is not enrolled in ' + $scope.sectionId); | 13 | 13 | console.log('user is not enrolled in ' + $scope.sectionId); | |
$state.go('addclass'); | 14 | 14 | $state.go('addclass'); | |
} | 15 | 15 | } | |
16 | 16 | |||
$scope.refreshColumnWidth = function() { | 17 | 17 | $scope.refreshColumnWidth = function() { | |
avail = $window.innerWidth - 17; | 18 | 18 | avail = $window.innerWidth - 17; | |
width = Math.floor(avail / Math.floor(avail / 250)); | 19 | 19 | width = Math.floor(avail / Math.floor(avail / 250)); | |
$('.cardColumn').css({ | 20 | 20 | $('.cardColumn').css({ | |
width: width + 'px', | 21 | 21 | width: width + 'px', | |
'font-size': 100 * width / 250 + '%' | 22 | 22 | 'font-size': 100 * width / 250 + '%' | |
}); | 23 | 23 | }); | |
$('.cardColumn .card.flashy').css({ | 24 | 24 | $('.cardColumn .card.flashy').css({ | |
width: width - 12 + 'px', | 25 | 25 | width: width - 12 + 'px', | |
height: (width * 3 / 5) + 'px' | 26 | 26 | height: (width * 3 / 5) + 'px' | |
}); | 27 | 27 | }); | |
}; | 28 | 28 | }; | |
29 | 29 | |||
$scope.refreshLayout = function() { | 30 | 30 | $scope.refreshLayout = function() { | |
numCols = Math.max(1, Math.floor(($window.innerWidth - 17) / 250)); | 31 | 31 | numCols = Math.max(1, Math.floor(($window.innerWidth - 17) / 250)); | |
32 | 32 | |||
// check if we actually need to refresh the whole layout | 33 | 33 | // check if we actually need to refresh the whole layout | |
if (numCols == $scope.numCols) return $scope.refreshColumnWidth(); | 34 | 34 | if (numCols == $scope.numCols) return $scope.refreshColumnWidth(); | |
$scope.numCols = numCols; | 35 | 35 | $scope.numCols = numCols; | |
console.log('refreshing layout for ' + $scope.numCols + ' columns'); | 36 | 36 | console.log('refreshing layout for ' + $scope.numCols + ' columns'); | |
$scope.cardCols = []; | 37 | 37 | $scope.cardCols = []; | |
var cols = []; | 38 | 38 | var cols = []; | |
for (i = 0; i < $scope.numCols; i++) cols.push([]); | 39 | 39 | for (i = 0; i < $scope.numCols; i++) cols.push([]); | |
$scope.cards.forEach(function(card, i) { | 40 | 40 | $scope.cards.forEach(function(card, i) { | |
cols[i % $scope.numCols].push(card); | 41 | 41 | cols[i % $scope.numCols].push(card); | |
$scope.cardTable[card.id] = {'colNum': (i % $scope.numCols), 'obj': card}; | 42 | 42 | $scope.cardTable[card.id] = {'colNum': (i % $scope.numCols), 'obj': card}; | |
card.isInDeck(); | 43 | 43 | card.isInDeck(); | |
}); | 44 | 44 | }); | |
// wait until the next digest cycle to update cardCols | 45 | 45 | // wait until the next digest cycle to update cardCols | |
console.log(cols); | 46 | 46 | console.log(cols); | |
$timeout(function() { | 47 | 47 | $timeout(function() { | |
$scope.cardCols = cols; | 48 | 48 | $scope.cardCols = cols; | |
$timeout($scope.refreshColumnWidth); | 49 | 49 | $timeout($scope.refreshColumnWidth); | |
}); | 50 | 50 | }); | |
51 | 51 | |||
}; | 52 | 52 | }; | |
53 | 53 | |||
angular.element($window).bind('resize', $scope.refreshLayout); | 54 | 54 | angular.element($window).bind('resize', $scope.refreshLayout); | |
55 | 55 | |||
$scope.ws_host = window.location.origin.replace('http', 'ws'); | 56 | 56 | $scope.ws_host = window.location.origin.replace('http', 'ws'); | |
$scope.deck_ws = $websocket($scope.ws_host + '/ws/deck/' + $scope.sectionId + '?subscribe-user'); | 57 | 57 | $scope.deck_ws = $websocket($scope.ws_host + '/ws/deck/' + $scope.sectionId + '?subscribe-user'); | |
$scope.deck_ws.onOpen(function() { | 58 | 58 | $scope.deck_ws.onOpen(function() { | |
console.log('deck ws open'); | 59 | 59 | console.log('deck ws open'); | |
}); | 60 | 60 | }); | |
61 | 61 | |||
$scope.deck_ws.onMessage(function(message) { | 62 | 62 | $scope.deck_ws.onMessage(function(message) { | |
data = JSON.parse(message.data); | 63 | 63 | data = JSON.parse(message.data); | |
console.log('message', data); | 64 | 64 | console.log('message', data); | |
if (data.event_type == 'pull_card') { | 65 | 65 | if (data.event_type == 'card_pulled') { | |
card = data.flashcard; | 66 | 66 | card = data.flashcard; | |
console.log('pulling', card); | 67 | 67 | console.log('pulling', card); | |
$scope.deck_cards[card.id] = card; | 68 | 68 | $scope.deck[card.id] = card; | |
} | 69 | 69 | } | |
if (data.event_type == 'unpull_card') { | 70 | 70 | if (data.event_type == 'card_unpulled') { | |
card = data.flashcard; | 71 | 71 | card = data.flashcard; | |
$scope.deck_cards[card.id] = undefined; | 72 | 72 | $scope.deck[card.id] = undefined; | |
} | 73 | 73 | } | |
}); | 74 | 74 | }); | |
75 | 75 | |||
$scope.cardInDeck = function(id) { | 76 | 76 | $scope.cardInDeck = function(id) { | |
return $scope.deck_cards[id]; | 77 | 77 | return $scope.deck[id]; | |
}; | 78 | 78 | }; | |
$scope.add = function(card) { | 79 | 79 | $scope.addCardToGrid = function(card) { | |
var colNum = 0; | 80 | 80 | var colNum = 0; | |
var lowestCol = $scope.cardCols[0]; | 81 | 81 | var lowestCol = $scope.cardCols[0]; | |
var lowestColNum = 0; | 82 | 82 | var lowestColNum = 0; | |
while (colNum < $scope.numCols) { | 83 | 83 | while (colNum < $scope.numCols) { | |
if ($scope.cardCols[colNum].length == 0) { | 84 | 84 | if ($scope.cardCols[colNum].length == 0) { | |
lowestCol = $scope.cardCols[colNum]; | 85 | 85 | lowestCol = $scope.cardCols[colNum]; | |
break; | 86 | 86 | break; | |
} else if ($scope.cardCols[colNum].length < lowestCol.length) { | 87 | 87 | } else if ($scope.cardCols[colNum].length < lowestCol.length) { | |
lowestCol = $scope.cardCols[colNum]; | 88 | 88 | lowestCol = $scope.cardCols[colNum]; | |
lowestColNum = colNum; | 89 | 89 | lowestColNum = colNum; | |
lowestColLen = $scope.cardCols[colNum].length; | 90 | 90 | lowestColLen = $scope.cardCols[colNum].length; | |
} | 91 | 91 | } | |
colNum++; | 92 | 92 | colNum++; | |
} | 93 | 93 | } | |
console.log(card); | 94 | 94 | console.log(card); | |
$scope.cards.push(data); | 95 | 95 | $scope.cards.push(data); | |
lowestCol.unshift(card); | 96 | 96 | lowestCol.unshift(card); | |
$scope.cardTable[card.id] = {'colNum': lowestColNum, 'obj': card}; | 97 | 97 | $scope.cardTable[card.id] = {'colNum': lowestColNum, 'obj': card}; | |
$timeout($scope.refreshColumnWidth); | 98 | 98 | $timeout($scope.refreshColumnWidth); | |
}; | 99 | 99 | }; | |
100 | 100 | |||
101 | 101 | |||
102 | 102 | |||
$http.get('/api/sections/' + $scope.sectionId + '/deck/'). | 103 | 103 | $http.get('/api/sections/' + $scope.sectionId + '/deck/'). | |
success(function(data) { | 104 | 104 | success(function(data) { | |
105 | 105 | |||
for (i in data) $scope.deck_cards[data[i].id] = data[i]; | 106 | 106 | for (i in data) $scope.deck[data[i].id] = data[i]; | |
console.log("got user's deck"); | 107 | 107 | console.log("got user's deck"); | |
}). | 108 | 108 | }). | |
error(function(err) { | 109 | 109 | error(function(err) { | |
console.log('get deck failed'); | 110 | 110 | console.log('get deck failed'); | |
}); | 111 | 111 | }); | |
112 | 112 |
scripts/DeckController.js
View file @
bf0941f
angular.module('flashy.DeckController', ['ui.router', 'ngWebSocket']). | 1 | 1 | angular.module('flashy.DeckController', ['ui.router', 'ngWebSocket']). | |
2 | 2 | |||
controller('DeckController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { | 3 | 3 | controller('DeckController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { | |
angular.module('flashy.CardGridController').controller.apply(this, arguments); | 4 | 4 | angular.module('flashy.CardGridController').controller.apply(this, arguments); | |
$scope.cards = $scope.deck_cards; | 5 | 5 | $scope.cards = $scope.deck; | |
$scope.refreshLayout(); | 6 | 6 | $scope.refreshLayout(); | |
} | 7 | 7 | } | |
); | 8 | 8 | ); |
scripts/FeedController.js
View file @
bf0941f
angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). | 1 | 1 | angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). | |
2 | 2 | |||
controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) { | 3 | 3 | controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService, Flashcard) { | |
angular.module('flashy.CardGridController').controller.apply(this, arguments); | 4 | 4 | angular.module('flashy.CardGridController').controller.apply(this, arguments); | |
$scope.refreshCards = function() { | 5 | 5 | $scope.refreshCards = function() { | |
$http.get('/api/sections/' + $scope.sectionId + '/feed/'). | 6 | 6 | $http.get('/api/sections/' + $scope.sectionId + '/feed/'). | |
success(function(data) { | 7 | 7 | success(function(data) { | |
console.log(data); | 8 | 8 | console.log(data); | |
$scope.cards = []; | 9 | 9 | $scope.cards = []; | |
for (var i in data) $scope.cards.push(new Flashcard(data[i], $scope.deck_cards)); | 10 | 10 | for (var i in data) $scope.cards.push(new Flashcard(data[i], $scope.deck)); | |
$scope.refreshLayout(); | 11 | 11 | $scope.refreshLayout(); | |
console.log('success in refresh cards...'); | 12 | 12 | console.log('success in refresh cards...'); | |
}). | 13 | 13 | }). | |
error(function(err) { | 14 | 14 | error(function(err) { | |
console.log('refresh fail'); | 15 | 15 | console.log('refresh fail'); | |
console.log(err); | 16 | 16 | console.log(err); | |
}); | 17 | 17 | }); | |
}; | 18 | 18 | }; | |
19 | 19 | |||
$scope.sortAdd = function(card, array) { | 20 | 20 | $scope.sortAdd = function(card, array) { | |
console.log('sort add'); | 21 | 21 | console.log('sort add'); | |
array.forEach(function(ele, i, ary) { | 22 | 22 | array.forEach(function(ele, i, ary) { | |
if (ele.score <= card.score) { | 23 | 23 | if (ele.score <= card.score) { | |
ary.splice(i, 0, card); | 24 | 24 | ary.splice(i, 0, card); | |
return; | 25 | 25 | return; | |
} | 26 | 26 | } | |
}); | 27 | 27 | }); | |
}; | 28 | 28 | }; | |
29 | 29 | |||
$scope.hide = function(card) { | 30 | 30 | $scope.updateCardScore = function(flashcard) { | |
var found = -1; | 31 | 31 | card = new Flashcard(flashcard.id); | |
col = $scope.cardTable[card.id].colNum; | 32 | 32 | console.log('old score', card.score); | |
found = $scope.cardCols[col].indexOf(card); | 33 | 33 | card.score = flashcard.score; | |
if (found != -1) { | 34 | 34 | console.log('new score', card.score); | |
$scope.cardCols[col].splice(found, 1); | 35 | 35 | $scope.cardCols[$scope.cardTable[card.id].colNum].sort(function(a, b) { | |
console.log('card hidden'); | 36 | 36 | return b.score - a.score; | |
return col; | 37 | 37 | }); | |
} | 38 | |||
console.log('Error finding card to hide:'); | 39 | |||
console.log(card); | 40 | |||
return -1; | 41 | |||
}; | 42 | 38 | }; | |
43 | 39 | |||
$scope.update = function(id, new_score) { | 44 | |||
card = $scope.cardTable[id].obj; | 45 | |||
if (Math.abs(new_score - card.score) < .0001) { | 46 | |||
console.log('score same, no update required'); | 47 | |||
return; | 48 | |||
} | 49 | |||
console.log('updating'); | 50 | |||
console.log(card); | 51 | |||
var column = $scope.cardCols[$scope.cardTable[id].colNum]; | 52 | |||
var found = column.indexOf(card); | 53 | |||
var i = 0; | 54 | |||
for (; i < column.length; i++) | 55 | |||
if (column[i].score <= new_score) break; | 56 | |||
card.score = new_score; | 57 | |||
if ($scope.$$phase) { // most of the time it is "$digest" | 58 | |||
column.splice(i, 0, column.splice(found, 1)[0]); | 59 | |||
} else { | 60 | |||
$scope.$apply(column.splice(i, 0, column.splice(found, 1)[0])); | 61 | |||
} | 62 | |||
}; | 63 | |||
64 | ||||
$scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); | 65 | 40 | $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); | |
$scope.feed_ws.onOpen(function() { | 66 | |||
console.log('feed ws open'); | 67 | |||
}); | 68 | |||
69 | ||||
$scope.feed_ws.onMessage(function(e) { | 70 | 41 | $scope.feed_ws.onMessage(function(e) { | |
data = JSON.parse(e.data); | 71 | 42 | data = JSON.parse(e.data); | |
console.log('got websocket message ' + e.data); | 72 | 43 | console.log('message', data); | |
console.log(data); | 73 | |||
if (data.event_type == 'new_card') { | 74 | 44 | if (data.event_type == 'new_card') { | |
$scope.add(data.flashcard); | 75 | 45 | $scope.addCardToGrid(new Flashcard(data.flashcard, $scope.deck)); | |
} else if (data.event_type == 'score_change') { | 76 | 46 | } else if (data.event_type == 'score_change') { | |
$scope.update(data.flashcard_id, data.new_score); | 77 | 47 | $scope.updateCardScore(data.flashcard); | |
} | 78 | 48 | } | |
}); | 79 | 49 | }); | |
80 | 50 | |||
$scope.pushCard = function() { | 81 | 51 | $scope.pushCard = function() { | |
var myCard = { | 82 | 52 | var myCard = { | |
// we can't trim this string because it'd mess up the blanks. Something to fix. | 83 | 53 | // we can't trim this string because it'd mess up the blanks. Something to fix. | |
'text': $('#new-card-input').text(), | 84 | 54 | 'text': $('#new-card-input').text(), | |
'mask': $scope.newCardBlanks, | 85 | 55 | 'mask': $scope.newCardBlanks, | |
section: $scope.section.id | 86 | 56 | section: $scope.section.id | |
}; | 87 | 57 | }; | |
if (myCard.text == '') { | 88 | 58 | if (myCard.text == '') { | |
console.log('blank flashcard not pushed:' + myCard.text); | 89 | 59 | console.log('blank flashcard not pushed:' + myCard.text); | |
return closeNewCard(); | 90 | 60 | return closeNewCard(); | |
} | 91 | 61 | } | |
$http.post('/api/flashcards/', myCard). | 92 | 62 | $http.post('/api/flashcards/', myCard). | |
success(function(data) { | 93 | 63 | success(function(data) { | |
console.log('flashcard pushed: ' + myCard.text); | 94 | 64 | console.log('flashcard pushed: ' + myCard.text); | |
if (!UserService.hasVerifiedEmail()) { | 95 | 65 | if (!UserService.hasVerifiedEmail()) { | |
Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); | 96 | 66 | Materialize.toast("<p>Thanks for contributing! However, others won't see your card until you verify your email address<p>", 4000); | |
} | 97 | 67 | } | |
}). | 98 | 68 | }). | |
error(function(error) { | 99 | 69 | error(function(error) { | |
console.log('something went wrong pushing a card!'); | 100 | 70 | console.log('something went wrong pushing a card!'); | |
}); | 101 | 71 | }); | |
return $scope.closeNewCardModal(); | 102 | 72 | return $scope.closeNewCardModal(); | |
}; | 103 | 73 | }; | |
104 | 74 | |||
/* Key bindings for the whole feed window. Hotkey it up! */ | 105 | 75 | /* Key bindings for the whole feed window. Hotkey it up! */ | |
var listenForC = true; | 106 | 76 | var listenForC = true; | |
107 | 77 | |||
// Need to pass these options into openmodal and leanmodal, | 108 | 78 | // Need to pass these options into openmodal and leanmodal, | |
// otherwise the ready handler doesn't get called | 109 | 79 | // otherwise the ready handler doesn't get called | |
110 | 80 | |||
modal_options = { | 111 | 81 | modal_options = { | |
dismissible: true, // Modal can be dismissed by clicking outside of the modal | 112 | 82 | dismissible: true, // Modal can be dismissed by clicking outside of the modal | |
opacity: 0, // Opacity of modal background | 113 | 83 | opacity: 0, // Opacity of modal background | |
in_duration: 300, // Transition in duration | 114 | 84 | in_duration: 300, // Transition in duration | |
out_duration: 200, // Transition out duration | 115 | 85 | out_duration: 200, // Transition out duration | |
ready: function() { | 116 | 86 | ready: function() { | |
$('#new-card-input').focus(); | 117 | 87 | $('#new-card-input').focus(); | |
document.execCommand('selectAll', false, null); | 118 | 88 | document.execCommand('selectAll', false, null); | |
} | 119 | 89 | } | |
}; | 120 | 90 | }; | |
121 | 91 | |||
$(document).keydown(function(e) { | 122 | 92 | $(document).keydown(function(e) { | |
var keyed = e.which; | 123 | 93 | var keyed = e.which; | |
if (keyed == 67 && listenForC) { // "c" for compose | 124 | 94 | if (keyed == 67 && listenForC) { // "c" for compose | |
$scope.openNewCardModal(); | 125 | 95 | $scope.openNewCardModal(); | |
e.preventDefault(); | 126 | 96 | e.preventDefault(); | |
return false; | 127 | 97 | return false; | |
} else if (keyed == 27) { // clear on ESC | 128 | 98 | } else if (keyed == 27) { // clear on ESC | |
$scope.closeNewCardModal(); | 129 | 99 | $scope.closeNewCardModal(); | |
} | 130 | 100 | } | |
}); | 131 | 101 | }); | |
132 | 102 | |||
$scope.openNewCardModal = function() { | 133 | 103 | $scope.openNewCardModal = function() { | |
$('#newCard').openModal(modal_options); | 134 | 104 | $('#newCard').openModal(modal_options); | |
listenForC = false; | 135 | 105 | listenForC = false; | |
$('#new-card-input').html('Write a flashcard!'); | 136 | 106 | $('#new-card-input').html('Write a flashcard!'); | |
}; | 137 | 107 | }; | |
138 | 108 | |||
$scope.closeNewCardModal = function() { | 139 | 109 | $scope.closeNewCardModal = function() { | |
listenForC = true; | 140 | 110 | listenForC = true; | |
$('#new-card-input').html('').blur(); | 141 | 111 | $('#new-card-input').html('').blur(); | |
$('#newCard').closeModal(modal_options); | 142 | 112 | $('#newCard').closeModal(modal_options); | |
}; | 143 | 113 | }; | |
144 | 114 | |||
$('.tooltipped').tooltip({delay: 50}); | 145 | 115 | $('.tooltipped').tooltip({delay: 50}); | |
// the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | 146 | 116 | // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered | |
$('.modal-trigger').leanModal(modal_options); | 147 | 117 | $('.modal-trigger').leanModal(modal_options); | |
$('#new-card-input').on('keydown', function(e) { | 148 | 118 | $('#new-card-input').on('keydown', function(e) { | |
if (e.which == 13) { | 149 | 119 | if (e.which == 13) { | |
e.preventDefault(); | 150 | 120 | e.preventDefault(); | |
if ($scope.submit_enabled) { | 151 | 121 | if ($scope.submit_enabled) { | |
$scope.pushCard(); | 152 | 122 | $scope.pushCard(); | |
listenForC = true; | 153 | 123 | listenForC = true; | |
} | 154 | 124 | } | |
return false; | 155 | 125 | return false; | |
} else { | 156 | 126 | } else { | |
157 | 127 | |||
} | 158 | 128 | } | |
}); | 159 | 129 | }); | |
$('button#blank-selected').click(function() { | 160 | 130 | $('button#blank-selected').click(function() { | |
console.log(window.getSelection()); | 161 | 131 | console.log(window.getSelection()); | |
document.execCommand('bold'); | 162 | 132 | document.execCommand('bold'); | |
}); | 163 | 133 | }); | |
$scope.refreshCards(); | 164 | 134 | $scope.refreshCards(); | |
$scope.newCardBlanks = []; | 165 | 135 | $scope.newCardBlanks = []; | |
$scope.refreshNewCardInput = function() { | 166 | 136 | $scope.refreshNewCardInput = function() { | |
$scope.newCardText = $('#new-card-input').text(); | 167 | 137 | $scope.newCardText = $('#new-card-input').text(); | |
$scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; | 168 | 138 | $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; | |
var i = 0; | 169 | 139 | var i = 0; | |
$scope.newCardBlanks = []; | 170 | 140 | $scope.newCardBlanks = []; | |
$('#new-card-input')[0].childNodes.forEach(function(node) { | 171 | 141 | $('#new-card-input')[0].childNodes.forEach(function(node) { | |
if (typeof node.data == 'undefined') { | 172 | 142 | if (typeof node.data == 'undefined') { | |
console.log('undefined node'); | 173 | 143 | console.log('undefined node'); | |
} | 174 | 144 | } | |
node = $(node)[0]; | 175 | 145 | node = $(node)[0]; | |
if (node.tagName == 'B') { | 176 | 146 | if (node.tagName == 'B') { | |
var text = $(node).text(); | 177 | 147 | var text = $(node).text(); | |
var leftspaces = 0, rightspaces = 0; | 178 | 148 | var leftspaces = 0, rightspaces = 0; | |
// awful way to find the first non-space character from the left or the right. thanks.js | 179 | 149 | // awful way to find the first non-space character from the left or the right. thanks.js | |
while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++; | 180 | 150 | while (text[leftspaces] == ' ' || text[leftspaces] == '\xa0') leftspaces++; | |
while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++; | 181 | 151 | while (text[text.length - 1 - rightspaces] == ' ' || text[text.length - 1 - rightspaces] == '\xa0') rightspaces++; | |
console.log(leftspaces, text.length); | 182 | 152 | console.log(leftspaces, text.length); | |
if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]); | 183 | 153 | if (leftspaces != text.length) $scope.newCardBlanks.push([i + leftspaces, i + text.length - rightspaces]); | |
i += text.length; | 184 | 154 | i += text.length; | |
} else if (!node.data) { | 185 | 155 | } else if (!node.data) { | |
console.log('weird node', node); | 186 | 156 | console.log('weird node', node); | |
i += $(node).text().length; | 187 | 157 | i += $(node).text().length; | |
} else { | 188 | 158 | } else { | |
i += node.data.length; | 189 | 159 | i += node.data.length; | |
} | 190 | 160 | } | |
}); | 191 | 161 | }); | |
$scope.newCardBlanks.sort(function(a, b) { | 192 | 162 | $scope.newCardBlanks.sort(function(a, b) { | |
return a[0] - b[0]; | 193 | 163 | return a[0] - b[0]; | |
}); | 194 | 164 | }); | |
i = 0; | 195 | 165 | i = 0; | |
newtext = ''; | 196 | 166 | newtext = ''; | |
console.log($scope.newCardBlanks); | 197 | 167 | console.log($scope.newCardBlanks); | |
$scope.newCardBlanks.forEach(function(blank) { | 198 | 168 | $scope.newCardBlanks.forEach(function(blank) { | |
newtext += $scope.newCardText.slice(i, blank[0]); | 199 | 169 | newtext += $scope.newCardText.slice(i, blank[0]); | |
newtext += '<b>' + $scope.newCardText.slice(blank[0], blank[1]) + '</b>'; | 200 | 170 | newtext += '<b>' + $scope.newCardText.slice(blank[0], blank[1]) + '</b>'; | |
i = blank[1]; | 201 | 171 | i = blank[1]; | |
}); | 202 | 172 | }); | |
newtext += $scope.newCardText.slice(i); | 203 | 173 | newtext += $scope.newCardText.slice(i); | |
//$scope.newCardFormattedText = newtext; | 204 | 174 | //$scope.newCardFormattedText = newtext; | |
}; | 205 | 175 | }; | |
$scope.shuffleCards = function() { | 206 | 176 | $scope.shuffleCards = function() { | |
$timeout(function() { | 207 | 177 | $timeout(function() { | |
(function(o) { | 208 | 178 | (function(o) { | |
for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | 209 | 179 | for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | |
return o; | 210 | 180 | return o; | |
})($scope.cardCols[0]); | 211 | 181 | })($scope.cardCols[0]); | |
}); | 212 | 182 | }); | |
}; | 213 | 183 | }; | |
$scope.$on('$destroy', function() { | 214 | 184 | $scope.$on('$destroy', function() { |
scripts/FlashcardDirective.js
View file @
bf0941f
angular.module('flashy.FlashcardDirective', []). | 1 | 1 | angular.module('flashy.FlashcardDirective', []). | |
2 | 2 | |||
directive('flashcard', | 3 | 3 | directive('flashcard', | |
function($http, $state, $window) { | 4 | 4 | function($http, $state, $window) { | |
return { | 5 | 5 | return { | |
templateUrl: '/app/templates/flashcard.html', | 6 | 6 | templateUrl: '/app/templates/flashcard.html', | |
restrict: 'E', | 7 | 7 | restrict: 'E', | |
scope: { | 8 | 8 | scope: { | |
flashcard: '=flashcardObj', // flashcard-obj in parent html | 9 | 9 | flashcard: '=flashcardObj', // flashcard-obj in parent html | |
refresh: '&' // eval refresh in parent html | 10 | 10 | refresh: '&' // eval refresh in parent html | |
}, | 11 | 11 | }, | |
link: function(scope, element) { | 12 | 12 | link: function(scope, element) { | |
/* Handles width of the card */ | 13 | 13 | /* Handles width of the card */ | |
14 | ||||
} | 15 | 14 | } | |
}; | 16 | 15 | }; |
scripts/FlashcardFactory.js
View file @
bf0941f
angular.module('flashy.FlashcardFactory', ['ui.router']). | 1 | 1 | angular.module('flashy.FlashcardFactory', ['ui.router']). | |
factory('Flashcard', function ($http) { | 2 | 2 | factory('Flashcard', function ($http) { | |
3 | var FlashcardCache = []; | |||
var Flashcard = function (data, deck) { | 3 | 4 | var Flashcard = function (data, deck) { | |
5 | if (typeof data == 'number') return FlashcardCache[data]; | |||
for (var k in data) this[k] = data[k]; | 4 | 6 | for (var k in data) this[k] = data[k]; | |
this.deck = deck; | 5 | 7 | this.deck = deck; | |
this.textPieces = []; | 6 | 8 | this.textPieces = []; | |
this.mask.sort(function (a, b) { | 7 | 9 | this.mask.sort(function (a, b) { | |
return a[0] - b[0]; | 8 | 10 | return a[0] - b[0]; | |
}); | 9 | 11 | }); | |
10 | 12 | |||
var i = 0; | 11 | 13 | var i = 0; | |
this.mask.forEach(function (blank) { | 12 | 14 | this.mask.forEach(function (blank) { | |
this.textPieces.push({text: this.text.slice(i, blank[0])}); | 13 | 15 | this.textPieces.push({text: this.text.slice(i, blank[0])}); | |
this.textPieces.push({text: this.text.slice(blank[0], blank[1]), blank: true}); | 14 | 16 | this.textPieces.push({text: this.text.slice(blank[0], blank[1]), blank: true}); | |
i = blank[1]; | 15 | 17 | i = blank[1]; | |
}, this); | 16 | 18 | }, this); | |
this.textPieces.push({text: this.text.slice(i)}); | 17 | 19 | this.textPieces.push({text: this.text.slice(i)}); | |
20 | FlashcardCache[this.id] = this; | |||
}; | 18 | 21 | }; | |
19 | ||||
Flashcard.prototype.isInDeck = function () { | 20 | 22 | Flashcard.prototype.isInDeck = function () { | |
return this.deck[this.id]; | 21 | 23 | return this.deck[this.id]; | |
}; | 22 | 24 | }; | |
Flashcard.prototype.pull = function () { | 23 | 25 | Flashcard.prototype.pull = function () { | |
if (this.deck[this.id]) return console.log('Not pulling', this.id, "because it's already in deck"); | 24 | 26 | if (this.deck[this.id]) return console.log('Not pulling', this.id, "because it's already in deck"); | |
return $http.post('/api/flashcards/' + this.id + '/pull/'); | 25 | 27 | return $http.post('/api/flashcards/' + this.id + '/pull/'); | |
}; | 26 | 28 | }; | |
Flashcard.prototype.unpull = function () { | 27 | 29 | Flashcard.prototype.unpull = function () { | |
if (!this.deck[this.id]) return console.log('Not unpulling', this.id, "because it's not in deck"); | 28 | 30 | if (!this.deck[this.id]) return console.log('Not unpulling', this.id, "because it's not in deck"); | |
return $http.post('/api/flashcards/' + this.id + '/unpull/'); | 29 | 31 | return $http.post('/api/flashcards/' + this.id + '/unpull/'); | |
}; | 30 | 32 | }; | |
Flashcard.prototype.hide = function () { | 31 | 33 | Flashcard.prototype.hide = function () { | |
return $http.post('/api/flashcards/' + this.id + '/hide/'); | 32 | 34 | return $http.post('/api/flashcards/' + this.id + '/hide/'); | |
}; | 33 | 35 | }; |
scripts/SettingsController.js
View file @
bf0941f
angular.module('flashy.SettingsController', ['ui.router']). | 1 | 1 | angular.module('flashy.SettingsController', ['ui.router']). | |
2 | 2 | |||
controller('SettingsController', function($scope, $http) { | 3 | 3 | controller('SettingsController', function($scope, $http) { | |
$scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { | 4 | 4 | $scope.changePassword = function(oldPassword, newPassword, confirmedNewPassword) { | |
5 | 5 | |||
}; | 6 | 6 | }; | |
console.log("checking to see if chrome"); | 7 | 7 | console.log('checking to see if chrome'); | |
if (!chrome) { return; } | 8 | 8 | if (!chrome) { return; } | |
console.log("chrome"); | 9 | 9 | console.log('chrome'); | |
$scope.registerCallback = function(registrationId) { | 10 | 10 | $scope.registerCallback = function(registrationId) { | |
if (chrome.runtime.lastError) { | 11 | 11 | if (chrome.runtime.lastError) { | |
console.log("Registration failed") | 12 | 12 | console.log('Registration failed'); | |
} | 13 | 13 | } | |
14 | 14 | |||
sendRegistrationId(registrationId, function(succeed) { | 15 | 15 | sendRegistrationId(registrationId, function(succeed) { | |
if (succeed) { | 16 | 16 | if (succeed) { | |
chrome.storage.local.set({registered: true}); | 17 | 17 | chrome.storage.local.set({registered: true}); | |
} | 18 | 18 | } | |
}); | 19 | 19 | }); | |
}; | 20 | 20 | }; | |
21 | 21 | |||
function sendRegistrationId(registrationId, callback) { | 22 | 22 | function sendRegistrationId(registrationId, callback) { | |
console.log("registration id: "+registrationId); | 23 | 23 | console.log('registration id: '+ registrationId); | |
$http.post('/api/subscribe/', JSON.stringify({ | 24 | 24 | $http.post('/api/subscribe/', JSON.stringify({ | |
'registration_id': registrationId | 25 | 25 | 'registration_id': registrationId | |
})); | 26 | 26 | })); | |
callback(true); | 27 | 27 | callback(true); | |
} | 28 | 28 | } | |
29 | 29 | |||
console.log(chrome.runtime.onStartup); | 30 | 30 | console.log(chrome.runtime.onStartup); | |
chrome.runtime.onStartup.addListener(function() { | 31 | 31 | chrome.runtime.onStartup.addListener(function() { | |
chrome.storage.local.get("registered", function(result) { | 32 | 32 | chrome.storage.local.get('registered', function(result) { | |
// If already registered, bail out. | 33 | 33 | // If already registered, bail out. | |
if (result["registered"]) | 34 | 34 | if (result['registered']) | |
return; | 35 | 35 | return; | |
36 | 36 | |||
// Up to 100 senders are allowed. | 37 | 37 | // Up to 100 senders are allowed. | |
var senderIds = ["45066531702"]; | 38 | 38 | var senderIds = ['45066531702']; | |
chrome.gcm.register(senderIds, registerCallback); | 39 | 39 | chrome.gcm.register(senderIds, registerCallback); | |
}); | 40 | 40 | }); | |
}); | 41 | 41 | }); | |
}); | 42 | 42 | }); | |
43 | 43 | |||