summaryrefslogtreecommitdiff
path: root/assets/js/src/Markdown.js
blob: 50293729751b1c592edc1f34cfa91f9373d0dbb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function Markdown() {
}

Markdown.prototype.showPreview = function(e) {
    e.preventDefault();

    var write = $(".write-area");
    var preview = $(".preview-area");
    var textarea = $("textarea");

    $("#markdown-write").parent().removeClass("form-tab-selected");
    $("#markdown-preview").parent().addClass("form-tab-selected");

    var request = $.ajax({
        url: $("body").data("markdown-preview-url"),
        contentType: "application/json",
        type: "POST",
        processData: false,
        dataType: "html",
        data: JSON.stringify({
            "text": textarea.val()
        })
    });

    request.done(function(data) {
        preview.find(".markdown").html(data)
        preview.css("height", textarea.css("height"));
        preview.css("width", textarea.css("width"));

        write.hide();
        preview.show();
    });
};

Markdown.prototype.showWriter = function(e) {
    e.preventDefault();

    $("#markdown-write").parent().addClass("form-tab-selected");
    $("#markdown-preview").parent().removeClass("form-tab-selected");

    $(".write-area").show();
    $(".preview-area").hide();
};

Markdown.prototype.listen = function() {
    $(document).on("click", "#markdown-preview", this.showPreview.bind(this));
    $(document).on("click", "#markdown-write", this.showWriter.bind(this));
};