From 7a53b3daec6b6b0206a21ec520f3f5752cd2a77e Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Mon, 1 Jun 2015 04:17:23 -0700 Subject: [PATCH] fix blanks as you type except not really --- scripts/FeedController.js | 65 ++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/scripts/FeedController.js b/scripts/FeedController.js index bb9c843..cc2035b 100644 --- a/scripts/FeedController.js +++ b/scripts/FeedController.js @@ -1,26 +1,26 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket', 'contenteditable']). - controller('FeedController', function($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { + controller('FeedController', function ($scope, $rootScope, $state, $http, $window, $timeout, $stateParams, $websocket, UserService) { angular.module('flashy.CardGridController').controller.apply(this, arguments); console.log('Hello from feed'); - $scope.refreshCards = function() { + $scope.refreshCards = function () { $http.get('/api/sections/' + $scope.sectionId + '/feed/'). - success(function(data) { + success(function (data) { console.log(data); $scope.cards = data; $scope.refreshLayout(); console.log('success in refresh cards...'); }). - error(function(err) { + error(function (err) { console.log('refresh fail'); console.log(err); }); }; - $scope.sortAdd = function(card, array) { + $scope.sortAdd = function (card, array) { console.log('sort add'); - array.forEach(function(ele, i, ary) { + array.forEach(function (ele, i, ary) { if (ele.score <= card.score) { ary.splice(i, 0, card); return; @@ -28,7 +28,7 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' }); }; - $scope.hide = function(card) { + $scope.hide = function (card) { console.log('hiding card'); var found = -1; col = $scope.cardTable[card.id].colNum; @@ -43,7 +43,7 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' return -1; }; - $scope.update = function(id, new_score) { + $scope.update = function (id, new_score) { card = $scope.cardTable[id].obj; if (Math.abs(new_score - card.score) < .0001) { console.log('score same, no update required'); @@ -65,11 +65,11 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' }; $scope.feed_ws = $websocket($scope.ws_host + '/ws/feed/' + $scope.sectionId + '?subscribe-broadcast'); - $scope.feed_ws.onOpen(function() { + $scope.feed_ws.onOpen(function () { console.log('deck ws open'); }); - $scope.feed_ws.onMessage(function(e) { + $scope.feed_ws.onMessage(function (e) { data = JSON.parse(e.data); console.log('got websocket message ' + e.data); console.log(data); @@ -80,7 +80,7 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' } }); - $scope.pushCard = function() { + $scope.pushCard = function () { var myCard = { // we can't trim this string because it'd mess up the blanks. Something to fix. 'text': $('#new-card-input').text(), @@ -92,13 +92,13 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' return closeNewCard(); } $http.post('/api/flashcards/', myCard). - success(function(data) { + success(function (data) { console.log('flashcard pushed: ' + myCard.text); if (!UserService.hasVerifiedEmail()) { Materialize.toast("

Thanks for contributing! However, others won't see your card until you verify your email address

", 4000); } }). - error(function(error) { + error(function (error) { console.log('something went wrong pushing a card!'); }); return $scope.closeNewCardModal(); @@ -115,13 +115,13 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' opacity: 0, // Opacity of modal background in_duration: 300, // Transition in duration out_duration: 200, // Transition out duration - ready: function() { + ready: function () { $('#new-card-input').focus(); document.execCommand('selectAll', false, null); } }; - $(document).keydown(function(e) { + $(document).keydown(function (e) { var keyed = e.which; if (keyed == 67 && listenForC) { // "c" for compose $scope.openNewCardModal(); @@ -132,13 +132,13 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' } }); - $scope.openNewCardModal = function() { + $scope.openNewCardModal = function () { $('#newCard').openModal(modal_options); listenForC = false; $('#new-card-input').html('Write a flashcard!'); }; - $scope.closeNewCardModal = function() { + $scope.closeNewCardModal = function () { listenForC = true; $('#new-card-input').html('').blur(); $('#newCard').closeModal(modal_options); @@ -147,7 +147,7 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' $('.tooltipped').tooltip({delay: 50}); // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered $('.modal-trigger').leanModal(modal_options); - $('#new-card-input').on('keydown', function(e) { + $('#new-card-input').on('keydown', function (e) { if (e.which == 13) { e.preventDefault(); $scope.pushCard(); @@ -157,20 +157,22 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' } }); - $('button#blank-selected').click(function() { + $('button#blank-selected').click(function () { console.log(window.getSelection()); document.execCommand('bold'); }); $scope.refreshCards(); $scope.newCardBlanks = []; - $scope.refreshNewCardInput = function() { + $scope.refreshNewCardInput = function () { $scope.newCardText = $('#new-card-input').text(); $scope.submit_enabled = $scope.newCardText.length >= 5 && $scope.newCardText.length <= 160; - $('#new-card-input')[0].childNodes.forEach(function(node) { + var i = 0; + $('#new-card-input')[0].childNodes.forEach(function (node) { if (typeof node.data == 'undefined') { console.log('undefined node'); } node = $(node)[0]; + $scope.newCardBlanks = []; if (node.tagName == 'B') { var text = $(node).text(); console.log(text.length, text); @@ -187,17 +189,28 @@ angular.module('flashy.FeedController', ['ui.router', 'ngAnimate', 'ngWebSocket' i += node.data.length; } }); - + $scope.newCardBlanks.sort(function (a, b) { + return a[0] - b[0]; + }); + var i = 0; + newtext = ''; + $scope.newCardBlanks.forEach(function (blank) { + newtext += $scope.newCardText.slice(i, blank[0]); + newtext += '' + $scope.newCardText.slice(blank[0], blank[1]) + ''; + i = blank[1]; + }); + newtext += $scope.newCardText.slice(i); + //$scope.newCardFormattedText = newtext; }; - $scope.shuffleCards = function() { - $timeout(function() { - (function(o) { + $scope.shuffleCards = function () { + $timeout(function () { + (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); return o; })($scope.cardCols[0]); }); }; - $scope.$on('$destroy', function() { + $scope.$on('$destroy', function () { $scope.feed_ws.close(); }); }); -- 1.9.1