From cb811641ab98f05d142238f2a39c6d0771d0edb2 Mon Sep 17 00:00:00 2001 From: hellofeiwu Date: Wed, 22 Jun 2016 16:50:00 +0800 Subject: [PATCH] add restore feature --- client/main.css | 10 ++++ client/main.less | 11 ++++ client/views/truth-view.js | 100 ++++++++++++++++++++-------------- client/views/truth.handlebars | 8 +-- server/index.js | 17 ++++-- 5 files changed, 94 insertions(+), 52 deletions(-) diff --git a/client/main.css b/client/main.css index 6873180..36e3788 100644 --- a/client/main.css +++ b/client/main.css @@ -60,6 +60,10 @@ display: table-cell; vertical-align: middle; } +.card h1 button { + display: none; + margin-top: 20px; +} .card-bottom { background: #DDD; border-radius: 5px; @@ -71,3 +75,9 @@ position: absolute; transform: translate(10px, 5px); } +.move-left { + transform: translate(-500px, 0) scale(0.8); + transition: .8s; + cursor: e-resize; + opacity: 0; +} diff --git a/client/main.less b/client/main.less index 1ed25fa..ecbc9c0 100644 --- a/client/main.less +++ b/client/main.less @@ -69,6 +69,10 @@ body { margin: 0; display: table-cell; vertical-align: middle; + button { + display: none; + margin-top: 20px; + } } } @@ -82,4 +86,11 @@ body { margin: auto; position: absolute; transform: translate(10px, 5px); +} + +.move-left { + transform: translate(-500px, 0) scale(0.8); + transition: .8s; + cursor: e-resize; + opacity: 0; } \ No newline at end of file diff --git a/client/views/truth-view.js b/client/views/truth-view.js index 909d27f..afc3a1c 100644 --- a/client/views/truth-view.js +++ b/client/views/truth-view.js @@ -9,53 +9,73 @@ var TruthView = function () { }; TruthView.prototype.render = function () { - var url = '/api/truth'; var self = this; + var truth = ''; + + var $truthTmpl = $(truthTmpl()); + self.$container.empty().append($truthTmpl); // make api call and response - var init = function() { + var getQuestion = function(restore) { + var url = '/api/truth/' + restore; $.getJSON(url, function (data) { - var truth = data; - - var $truthTmpl = $(truthTmpl({ - question: truth.question - })); - - $truthTmpl.find('#change').click(function() { - init(); - }); - self.$container.empty().append($truthTmpl); - - $truthTmpl.find("#submit").click(function() { - console.log('button works ' + $truthTmpl.find("#new-question").val()); - var url = 'api/truth'; - var newQuestion = { - question: $truthTmpl.find("#new-question").val(), - type: 'truth' - }; - HttpRequest.http_request_with_data_authentication( - 'POST', - url, - JSON.stringify(newQuestion), - function successCallback(data){ - $truthTmpl.find('#myModal').modal('hide'); - console.log('works'); - }, - function (data) { - console.log('fail'); - window.location.hash = 'login'; - } - ); - }); + truth = data; + cardInsert(); + }); + }; - $truthTmpl.find("#logout").click(function() { - window.localStorage.removeItem('username'); - window.location.hash = ''; + var cardInsert = function () { + $truthTmpl.find('.card-bottom').after('

' + + truth.question + + '
' + + '' + + '' + + '
' + + '

'); + if (truth.question == 'There is no new question.') { + $truthTmpl.find('#question').find('button').show(); + } + $truthTmpl.find('#question-restore').click(function() { + $truthTmpl.find('.card').addClass('move-left'); + getQuestion(1); }); - }); }; - - init(); + + getQuestion(); + + $truthTmpl.find('#change').click(function() { + $truthTmpl.find('.card').addClass('move-left'); + getQuestion(); + }); + + $truthTmpl.find("#submit").click(function() { + console.log('button works ' + $truthTmpl.find("#new-question").val()); + var url = 'api/truth'; + var newQuestion = { + question: $truthTmpl.find("#new-question").val(), + type: 'truth' + }; + HttpRequest.http_request_with_data_authentication( + 'POST', + url, + JSON.stringify(newQuestion), + function successCallback(data){ + $truthTmpl.find('#myModal').modal('hide'); + console.log('works'); + $truthTmpl.find('.card').addClass('move-left'); + getQuestion(); + }, + function errorCallback(data) { + console.log('fail'); + window.location.hash = 'login'; + } + ); + }); + + $truthTmpl.find("#logout").click(function() { + window.localStorage.removeItem('username'); + window.location.hash = ''; + }); }; var truthView = new TruthView(); diff --git a/client/views/truth.handlebars b/client/views/truth.handlebars index 569ae4f..7c153c5 100644 --- a/client/views/truth.handlebars +++ b/client/views/truth.handlebars @@ -5,17 +5,11 @@

This is a truth page

loading...
-
-

{{question}}?

-
- Home + Home Dare -