summaryrefslogtreecommitdiff
path: root/js/scilab_fixer.js
blob: ef056112865511f7d437dee7cde254ec9c8267ee (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
$(document).ready(function() {
    var basePath = Drupal.settings.basePath;
    var modPath = basePath + "fix/";

    $category = $("#fix-caption-form #edit-category");
    $book = $("#fix-caption-form #edit-book");
    $chapter = $("#fix-caption-form #edit-chapter");
    $example = $("#fix-caption-form #edit-example");
    $caption = $("#fix-caption-form #edit-caption");
    $code = $("#fix-caption-form #fix-caption-code");
    $form = $("#scilab-fixer-caption-form");
    $updating = $("#fix-caption-page #updating");
    $done = $("#fix-caption-page #done");

    function reset() {
        for (var i = 0, l = arguments.length; i < l; i ++) {
            switch(arguments[i]) {
                case "book":
                    $book.html("<option value='0'>Please select a book</option>");
                    break;
                
                case "chapter":
                    $chapter.html("<option value='0'>Please select a chapter</option>");
                    break;
                
                case "example":
                    $example.html("<option value='0'>Please select a example</option>");
                    break;
                
                case "caption":
                    $caption.val("");
                    break;
                
            }
        }
    }

    $category.change(function() {
        reset("book", "chapter", "example", "caption");
        var category_id = $(this).val();
        
        $.ajax({
            url: modPath + "ajax/category/" + category_id,
            type: "POST",
            dataType: "html",
            success: function(data) {
                $book.html(data);
            }
        });
    });

    $book.change(function() {
        reset("chapter", "example", "caption");
        var book_id = $(this).val();
        
        $.ajax({
            url: modPath + "ajax/book/" + book_id,
            type: "POST",
            dataType: "html",
            success: function(data) {
                $chapter.html(data);
            }
        });
    });

    $chapter.change(function() {
        reset("example", "caption");
        var chapter_id = $(this).val();
        
        $.ajax({
            url: modPath + "ajax/chapter/" + chapter_id,
            type: "POST",
            dataType: "html",
            success: function(data) {
                $example.html(data);
            }
        });
    });

    $example.change(function() {
        var example_id = $(this).val();
        reset("caption");
        
        $.ajax({
            url: modPath + "ajax/example/" + example_id,
            type: "POST",
            dataType: "html",
            success: function(data) {
                var code = $(data).filter("#code").html();
                $code.html(code);
                var caption = $(data).filter("#caption").html();
                $caption.val(caption);
            }
        });
    });

    $form.submit(function(e) {
        var example_id = $example.val();
        if(example_id != "0") {
            var caption = $caption.val();
            $updating.show();
            $.ajax({
                url: modPath + "ajax/update/",
                type: "POST",
                data: {
                    example_id: example_id,
                caption: caption
                },
                dataType: "html",
                success: function(data) {
                    $chapter.trigger("change");
                    $updating.hide();
                    $done.show();
                    $done.fadeOut("slow");
                }
            });
        } else {
            alert("No example selected.")
        }
        e.preventDefault();
    });
});