summaryrefslogtreecommitdiff
path: root/http/dealconvert.js
blob: e038b58f10b104293640a868ad2e682f63ca6139 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
$(document).ready(function() {
    $('a.faq-btn').popover();
    $('input[name="output"]').change(function() {
        if ($('input[name="output"]:checked').length > 0) {
            $('#submit-btn').removeAttr('disabled');
        } else {
            $('#submit-btn').attr('disabled', 'disabled');
        }
    });
    $('#converter-input').submit(function() {
        var that = $(this);
        var output = [];
        that.find('input[name="output"]:checked').each(function() {
            output.push(this.value);
        });
        var display = that.find('input[name="display"]').is(':checked');
        var files = {};
        var formFiles = this['input-files'].files;
        if (formFiles.length) {
            $('.output-group').remove();
            that.css('opacity', 0.3);
            that.find('input, button').attr('disabled', 'disabled');
        }
        var completed = 0;
        for (var i = 0; i < formFiles.length; i++) {
            var currentFile = formFiles[i];
            files[currentFile.name] = null;
            var reader = new FileReader();
            reader.file = currentFile;
            reader.onload = function(e) {
                files[e.target.file.name] = btoa(e.target.result);
                for (var file in files) {
                    if (files[file] == null) {
                        return;
                    }
                }
                for (var file in files) {
                    var paramObj = {
                        'name': file,
                        'content': files[file],
                        'output': output,
                        'display_deals': display
                    };
                    $.ajax(
                        'api/upload/',
                        {
                            data: JSON.stringify(paramObj),
                            dataType: 'json',
                            method: 'POST',
                            success: function(data, status, xhr) {
                                var outputGroup = $('template#file-output-group').clone().contents().unwrap();
                                var warningTemplate = $('template#file-output-warning');
                                var errorTemplate = $('template#file-output-error');
                                var fileTemplate = $('template#file-output');
                                var inputHeader = outputGroup.find('.file-header');
                                inputHeader.text(data.name);
                                var groupBody = outputGroup.find('.file-body');
                                if (data.error) {
                                    inputHeader.addClass('bg-danger');
                                    groupBody.append(errorTemplate.clone().contents().unwrap().text(data.error));
                                } else {
                                    inputHeader.addClass('bg-success');
                                    if (data.warnings.length) {
                                        inputHeader.removeClass('bg-success');
                                        inputHeader.addClass('bg-warning');
                                        for (var w = 0; w < data.warnings.length; w++) {
                                            groupBody.append(warningTemplate.clone().contents().unwrap().text(data.warnings[w]));
                                        }
                                    }
                                    for (var f = 0; f < data.files.length; f++) {
                                        var fileContent = fileTemplate.clone().contents().unwrap();
                                        groupBody.append(fileContent);
                                        fileContent.find('.file-name').text(data.files[f].name);
                                        fileContent.find('.file-status').popover();
                                        if (data.files[f].error) {
                                            fileContent.find('.file-link').remove();
                                            fileContent.find('.file-status').addClass('btn-danger').text('⚠️').attr('data-content', data.files[f].error);
                                            fileContent.find('.file-name').addClass('btn-danger');
                                            inputHeader.removeClass('bg-success');
                                            inputHeader.addClass('bg-warning');
                                        } else {
                                            if (data.files[f].warnings.length) {
                                                fileContent.find('.file-status').addClass('btn-warning').text('⚠️').attr(
                                                    'data-content', data.files[f].warnings.join("<br />"));
                                                fileContent.find('.file-name').addClass('btn-warning');
                                                inputHeader.removeClass('bg-success');
                                                inputHeader.addClass('bg-warning');
                                            } else {
                                                fileContent.find('.file-status').addClass('btn-success').text('✔️');
                                                fileContent.find('.file-name').addClass('btn-success');
                                            }
                                            fileContent.find('.file-link').attr(
                                                'href', 'api/' + data.files[f].link
                                            );
                                        }
                                    }
                                    if (data.preview) {
                                        var boardTemplate = $('#board-preview');
                                        var hands = ['north', 'east', 'south', 'west'];
                                        var suits = ['spades', 'hearts', 'diamonds', 'clubs'];
                                        for (var b = 0; b < data.preview.length; b++) {
                                            var board = boardTemplate.clone().contents().unwrap();
                                            board.find('.board-number').text(data.preview[b].number);
                                            board.find('.board-conditions').attr('src', 'img/' + data.preview[b].conditions + '.png');
                                            for (var h = 0; h < hands.length; h++) {
                                                for (var s = 0; s < suits.length; s++) {
                                                    board.find('.board-' + hands[h] + '-' + suits[s]).text(data.preview[b].hands[h][s].join(''));
                                                }
                                            }
                                            outputGroup.find('.file-boards-panel .board-body').append(board);
                                        }
                                    } else {
                                        outputGroup.find('.file-boards-panel').remove();
                                    }
                                }
                                $('body').append(outputGroup);
                                completed += 1;
                                if (completed >= formFiles.length) {
                                    that.css('opacity', '');
                                    that.find('input, button').removeAttr('disabled');
                                }
                            },
                            error: function(xhr, status, error) {
                                var errorBox = $('<div class="container output-group"></div>');
                                errorBox.append($('template#file-output-error').clone().contents().unwrap().text(
                                    'Nie udało się wykonać konwersji: ' + xhr.responseText
                                ));
                                $('body').append(errorBox);
                                that.css('opacity', '');
                                that.find('input, button').removeAttr('disabled');
                            }
                        }
                    );
                }
            }
            reader.readAsBinaryString(currentFile);
        }
        return false;
    });
});