Completed
Pull Request — master (#90)
by Janis
15:10
created

js/temp/ocrpersonalview.js   A

Complexity

Total Complexity 15
Complexity/F 1.25

Size

Lines of Code 114
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 114
rs 10
c 0
b 0
f 0
cc 0
nc 4
mnd 1
bc 16
fnc 12
bpm 1.3333
cpm 1.25
noi 23

6 Functions

Rating   Name   Duplication   Size   Complexity  
A Backbone.View.extend.initialize 0 3 1
A Backbone.View.extend.render 0 3 1
B Backbone.View.extend._load 0 26 2
A Backbone.View.extend._showTable 0 7 1
B Backbone.View.extend._delete 0 27 2
A Backbone.View.extend._showMsg 0 4 1
1
/**
2
 * nextCloud - ocr
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Janis Koehr <[email protected]>
8
 * @copyright Janis Koehr 2017
9
 */
10
11
/* global Backbone, Handlebars */
12
(function (OC, Backbone, Handlebars, $) {
13
    'use strict';
14
15
    OC.Settings = OC.Settings || {};
16
    OC.Settings.Ocr = OC.Settings.Ocr || {};
17
18
    var TEMPLATE = '{{#if enabled}}'
19
        + '         <table class="grid ocrsettings">'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
20
        + '             <thead>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
21
        + '                 <tr>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
22
        + '                     <th>' + t('ocr', 'Name') + '</th>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
23
        + '                     <th>' + t('ocr', 'Status') + '</th>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
24
        + '                     <th>' + t('ocr', 'Delete from queue') + '</th>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
25
        + '                 </tr>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
26
        + '             </thead>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
27
        + '             <tbody>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
28
        + '                 {{#each status}}'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
29
        + '                     <tr data-id="{{ id }}">'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
30
        + '                         <td>{{ target }}</td>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
31
        + '                         <td>{{ status }}</td>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
32
        + '                         <td class="ocr-action-delete"><div id="ocr-delete"><span>' + t('ocr', 'Delete') + '</span><span class="icon icon-delete"></span></div></td>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
33
        + '                     </tr>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
34
        + '                 {{/each}}'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
35
        + '             </tbody>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
36
        + '         </table>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
37
        + '     {{else}}'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
38
        + '         <p>' + t('ocr' , 'No pending or failed OCR items found.') +'</p>'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
39
        + '     {{/if}}'
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
40
        + '<button id="ocr-search">' + t('ocr', 'Refresh') + '</button>';
0 ignored issues
show
Bug introduced by
There seems to be a bad line break before +.
Loading history...
41
42
    var View;
43
    View = Backbone.View.extend({
44
        template: Handlebars.compile(TEMPLATE),
45
        _loading: undefined,
46
        _enabled: undefined,
47
        events: {
48
            'click #ocr-search': '_load',
49
            'click #ocr-delete': '_delete'
50
        },
51
        initialize: function () {
52
            this._load();
53
        },
54
        render: function (data) {
55
            this.$el.html(this.template(data));
56
        },
57
        _load: function () {
58
            if (this._loading) {
59
                //ignore when loading
60
                return;
61
            }
62
            this._loading = true;
63
64
            var url = OC.generateUrl('/apps/ocr/settings/personal');
65
            var loading = $.ajax(url, {
66
                method: 'GET'
67
            });
68
69
            var _this = this;
70
            $.when(loading).done(function (status) {
71
                if (status.length > 0) {
72
                    _this._enabled = true;
73
                    _this._showTable(status);
74
                } else {
75
                    _this._enabled = false;
76
                    _this._showTable();
77
                }
78
            });
79
            $.when(loading).always(function () {
80
                _this._loading = false;
81
            });
82
        },
83
        _delete: function(e) {
84
            if (this._loading) {
85
                //ignore when loading
86
                return;
87
            }
88
            this._loading = true;
89
            var _this = this;
90
            var url = OC.generateUrl('/apps/ocr/settings/personal');
91
            var deleting = $.ajax(url, {
92
                method: 'DELETE',
93
                data: {
94
                    id: $(e.target).closest('tr').data('id')
95
                }
96
            });
97
98
            $.when(deleting).done(function(data) {
99
                _this._showMsg(t('ocr', 'Following file has been successfully deleted from the queue:') + ' "' + data.target + '"');
100
                _this._loading = false;
101
                _this._load();
102
            });
103
104
            $.when(deleting).fail(function(jqXHR) {
105
                _this._showMsg(t('ocr', 'Error during deletion: ') + jqXHR.responseText);
106
                _this._loading = false;
107
            });
108
109
        },
110
        _showTable: function(data) {
111
            var _this = this;
112
            this.render({
113
                enabled: _this._enabled,
114
                status: data
115
            });
116
        },
117
        _showMsg: function (msg) {
118
            $('#ocr-msg').text(msg);
119
            setTimeout(function() { $('#ocr-msg').text(''); }, 5000);
120
        }
121
    });
122
123
    OC.Settings.Ocr.View = View;
124
125
})(OC, Backbone, Handlebars, $);
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...