topic.html 8.57 KB
<!DOCTYPE html>
<html>
<head>
    <title>GotIT</title>
    <link rel="icon" href="/static/favicon.ico">

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="author" content="Miguel Barão">

<!-- MathJax -->
    <script type="text/x-mathjax-config">
        MathJax.Hub.Config({
            tex2jax: {
                inlineMath: [["$$$","$$$"], ["$","$"], ["\\(","\\)"]]
            }
        });
    </script>
    <script type="text/javascript" src="/static/mathjax/MathJax.js?delayStartupUntil=onload&config=TeX-AMS_CHTML-full"></script>

<!-- Bootstrap -->
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/css/animate.min.css">
    <link rel="stylesheet" href="/static/css/github.css">

<!-- Other -->
    <link rel="stylesheet" href="/static/css/learn.css">

</head>
<!-- ===================================================================== -->
<body>

<!-- Navbar -->
<nav class="navbar navbar-expand-sm fixed-top navbar-dark bg-dark">
  <a class="navbar-brand" href="#">
    <i class="fa fa-clock-o" aria-hidden="true"></i>
  </a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarText">
    <ul class="navbar-nav mr-auto">
    </ul>

    <span class="navbar-text">
      <i class="fa fa-user" aria-hidden="true"></i>
      <span id="name">{{ escape(name) }}</span>
      (<span id="number">{{ escape(uid) }}</span>)
      <span class="caret"></span>
    </span>
  </div>
</nav>

<!-- ===================================================================== -->
<!-- main panel with questions -->
<div id="main">
    <div id="body">
        <div class="progress">
            <div class="progress-bar progress-bar-success" id="topic_progress" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="min-width: 0em;width: 0%"></div>
        </div>

        <!-- <div class="col-md-12"> -->

            <div id="notifications">
                <!-- notifications go here -->
            </div>

            <!-- Question body -->
            <form action="/question" method="post" id="question_form" autocomplete="off">
                {% module xsrf_form_html() %}

                <div id="question_div">
                    <!-- questions go here -->
                </div>

            </form>
            <button class="btn btn-primary" id="submit" data-toggle="tooltip" data-placement="right" title="Shift-Enter">Continuar</button>
        <!-- </div> -->
    </div> <!-- body -->
</div> <!-- main -->

<!-- === Change Password Modal =========================================== -->
<div id="password_modal" class="modal fade" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
<!-- header -->
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Alterar Password</h4>
      </div>
<!-- body -->
      <div class="modal-body">
        <div class="control-group">
            <label for="new_password" class="control-label">Nova Password</label>
            <div class="controls">
                <input type="password" id="new_password" name="new_password" autocomplete="new-password">
            </div>
        </div>
      </div>
<!-- footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
        <button id="change_password" type="button" class="btn btn-danger" data-dismiss="modal">Alterar</button>
      </div>

    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<!-- ===================================================================== -->
<!--                          JAVASCRIPT                                   -->
<!-- ===================================================================== -->
<script>
$.fn.extend({
    animateCSS: function (animation) {
        var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
        this.addClass('animated ' + animation).one(animationEnd, function() {
            $(this).removeClass('animated ' + animation);
        });
    }
});

// Process the response given by the server
function updateQuestion(response){
    switch (response["method"]) {
    case "new_question":
        $("#question_div").html(response["params"]["question"]);
        $("#topics").html(response["params"]["state"]);
        $('#topic_progress').css('width', (100*response["params"]["progress"])+'%').attr('aria-valuenow', 100*response["params"]["progress"]);

        MathJax.Hub.Queue(["Typeset",MathJax.Hub,"question_div"]);

        // enable shift+enter to submit and tab to spaces conversion
        $("input:text, input:radio, input:checkbox").keydown(function (e) {
            if (e.keyCode == 13) {
                e.preventDefault();
                if (e.shiftKey) postQuestion();
                return false;
            }
        });
        $("textarea").keydown(function (e) {
            if (e.keyCode == 13 && e.shiftKey) { // shift enter
                e.preventDefault();
                postQuestion();
            }
            else if (e.keyCode === 9) { // tab
                e.preventDefault();     // prevent loosing focus
                // get caret position/selection
                var start = this.selectionStart;
                var end = this.selectionEnd;
                var value = $(this).val();

                // set textarea value to: text before caret + tab + text after caret
                $(this).val(value.substring(0, start) + "    " + value.substring(end));

                // put caret at right position again (add one for the tab)
                this.selectionStart = this.selectionEnd = start + 4;
            }
        });

        // var audio = new Audio('/static/sounds/correct.mp3');
        // audio.play();
        $('#question_div').animateCSS('zoomIn');
        break;

    case "shake":
        // var audio = new Audio('/static/sounds/wrong.mp3');
        // audio.play();
        $('#topic_progress').css('width', (100*response["params"]["progress"])+'%').attr('aria-valuenow', 100*response["params"]["progress"]);
        $('#question_div').animateCSS('shake');
        break;

    case "finished_topic":
        $('#topic_progress').css('width', '100%').attr('aria-valuenow', 100);
        $("#topics").html(response["params"]["state"]);
        $("#question_div").html('<img src="/static/trophy.png" alt="trophy" class="img-rounded img-responsive center-block">');  // FIXME size
        break;
    }
}

// Send answer and receive a response.
// The response can be a new_question or a shake if the answer is wrong.
function postQuestion() {
    $.ajax({
        type: "POST",
        url: "/question",
        // headers: {"X-XSRFToken": token},
        data: $("#question_form").serialize(), // {'a':10,'b':20},
        dataType: "json", // expected from server
        success: updateQuestion,
        error: function() {alert("O servidor não responde.");}
    });
}

// Get current question
function getQuestion() {
    $.ajax({
        url: "/question",
        // headers: {"X-XSRFToken": token},
        dataType: "json", // expected from server
        success: updateQuestion,
        error: function() {alert("O servidor não responde.");}
    });
}

function change_password() {
    $.ajax({
        type: "POST",
        url: "/change_password",
        data: {
            "new_password": $("#new_password").val(),
        },
        dataType: "json",
        success: function(r) {
            $("#notifications").html(r["msg"]);
            $("#notification").fadeIn(250).delay(5000).fadeOut(500);
        },
        error: function(r) {
            $("#notifications").html(r["msg"]);
            $("#notification").fadeIn(250).delay(5000).fadeOut(500);
        },
    });
}

$(document).ready(function() {
    getQuestion();
    $("#submit").click(postQuestion);
    $('[data-toggle=offcanvas]').click(function() {
        $('.row-offcanvas').toggleClass('active');
    });
    $("#change_password").click(change_password);
});
</script>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/bootstrap/js/bootstrap.min.js"></script>

</body>
</html>