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

js/temp/view.js   A

Complexity

Total Complexity 27
Complexity/F 1.8

Size

Lines of Code 190
Function Count 15

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 27
dl 0
loc 190
rs 10
c 0
b 0
f 0
cc 0
nc 8
mnd 2
bc 27
fnc 15
bpm 1.8
cpm 1.8
noi 17

2 Functions

Rating   Name   Duplication   Size   Complexity  
B view.js ➔ View 0 128 1
B View.constructor 0 163 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
(function (OC, OCA, window, $, Handlebars, _) {
0 ignored issues
show
Unused Code introduced by
The parameter _ is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
11
    'use strict';
12
    /**
13
     * The view.
14
     * @public
15
     * @class Ocr.View
16
     */
17
    var View = (function () {
18
        /**
19
         * @private Handlebars.template
20
         * @static
21
         */
22
        var TEMPLATE_OCR_DROPDOWN = '<div id="ocrDropdown" class="ocrUserInterface">' +
23
            '<select id="ocrLanguage" class="multiselect" multiple="multiple">' +
24
            '{{#each languages}}' +
25
            '<option value="{{this}}">{{this}}</option>' +
26
            '{{/each}}' +
27
            '</select>' +
28
            '<input type="button" id="processOCR" value="' +
29
            t('ocr', 'Process') +
30
            '" />' +
31
            '</div>';
32
33
        /**
34
         * @private Handlebars.template
35
         * @static
36
         */
37
        var TEMPLATE_OCR_SELECTED_FILE_ACTION = '<span class="selectedActionsOCR hidden">' +
38
            '<a id="selectedFilesOCR" href="" class="ocr">' +
39
            '<span class="icon icon-ocr"></span>' +
40
            '<span class="pad-for-icon">' + t('ocr', 'OCR') + '</span>' +
41
            '</a>' +
42
            '</span>';
43
44
        /**
45
		 * Inner class
46
		 * @private
47
		 * @returns class
48
		 */
49
        var View = function () {
50
            /** The row of the notification for the pending state. */
51
            var notificationRow = undefined;
0 ignored issues
show
Unused Code Comprehensibility introduced by
The assignment of undefined is not necessary as notificationRow is implicitly marked as undefined by the declaration.
Loading history...
Unused Code introduced by
It's not necessary to initialize 'notificationRow' to 'undefined'.
Loading history...
52
53
            /**
54
             * Renders the select2 select filed inside the OCR dropdown.
55
             * @private
56
             */
57
            var renderSelectTwo = function () {
58
                $("#ocrLanguage").select2({
59
                    width: 'element',
60
                    placeholder: t('ocr', 'Select language'),
61
                    formatNoMatches: function () {
62
                        return t('ocr', 'No matches found.');
63
                    }
64
                });
65
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
66
67
            /**
68
             * Destroys the dropdown if existing.
69
             * @public
70
             */
71
            this.destroyDropdown = function () {
72
                if ($('#ocrDropdown').length) {
73
                    $('#ocrDropdown').detach();
74
                }
75
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
76
77
            /**
78
             * Renders the dropdown with the given languages.
79
             * @public
80
             * @param {Array<String>} languages 
81
             */
82
            this.renderDropdown = function (languages) {
83
                this.destroyDropdown();
84
                var template = Handlebars.compile(TEMPLATE_OCR_DROPDOWN);
85
                return template({ languages: languages });
86
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
87
88
            /**
89
             * Toggles the pending notification on the top of the window.
90
             * @public
91
             * @param {boolean} force - If new files in queue or already in the regular loop.
92
             * @param {number} count - How much files.
93
             */
94
            this.togglePendingNotification = function (force, count) {
95
                var html;
96
                if (force) {
97
                    html = '<span class="icon icon-loading-small ocr-row-adjustment"></span>&nbsp;<span>' + n('ocr', 'OCR started: %n new file in queue.', 'OCR started: %n new files in queue.', count) + '</span>';
98
                } else {
99
                    html = '<span class="icon icon-loading-small ocr-row-adjustment"></span>&nbsp;<span>' + ' ' + n('ocr', 'OCR: %n currently pending file in queue.', 'OCR: %n currently pending files in queue.', count) + '</span>';
100
                }
101
                if (count > 0 || force) {
102
                    if (notificationRow !== undefined) { OC.Notification.hide(notificationRow); }
103
                    notificationRow = OC.Notification.showHtml(html);
104
                } else {
105
                    if (notificationRow !== undefined) {
106
                        OC.Notification.hide(notificationRow);
107
                        notificationRow = undefined;
108
                    }
109
                }
110
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
111
112
            /** 
113
             * Displays an error for a given message.
114
             * @public
115
             * @param {String} message
116
             */
117
            this.displayError = function (message) {
118
                OC.Notification.showHtml('<div>' + message + '</div>', { timeout: 10, type: 'error' });
119
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
120
121
            /**
122
             * Hides or shows the selected files action button in the top bar.
123
             * @public
124
             */
125
            this.toggleSelectedFilesActionButton = function (show) {
126
                show ? $('.selectedActionsOCR').removeClass('hidden') : $('.selectedActionsOCR').addClass('hidden');
0 ignored issues
show
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
127
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
128
129
            /** 
130
             * Renders the OCR dropdown for the FileActionMenu option 
131
             * OR TopBarSelectedFilesAction button depending on a input file.
132
             * @public
133
             * @param file
134
             * @param {Array<String>} languages
135
             */
136
            this.renderFileAction = function (file, languages) {
137
                var html = this.renderDropdown(languages);
138
                file !== undefined ? $(html).appendTo($('tr').filterAttr('data-file', file).find('td.filename')) : $(html).appendTo($('tr').find('th.column-name'));
0 ignored issues
show
Bug introduced by
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
139
                renderSelectTwo();
140
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
141
142
            /** 
143
             * Checks if the ocrDropdown was not clicked.
144
             * @public
145
             * @param {Event} event
146
             */
147
            this.checkClickOther = function (event) {
148
                if (!$(event.target).closest('#ocrDropdown').length) {
149
                    this.destroyDropdown();
150
                    return true;
151
                } else {
152
                    return false;
153
                }
154
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
155
156
            /**
157
             * Renders the selected files action button.
158
             * @public
159
             */
160
            this.renderSelectedFilesActionButton = function () {
161
                $(TEMPLATE_OCR_SELECTED_FILE_ACTION).appendTo($('#headerName-container'));
162
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
163
164
            /**
165
             * Destroys the selected files action button.
166
             * @public
167
             */
168
            this.destroySelectedFilesActionButton = function () {
169
                $('.selectedActionsOCR').remove();
170
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
171
172
            this.destroy = function() {
173
                this.destroySelectedFilesActionButton();
174
                this.destroyDropdown();
175
            }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
176
        }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
177
178
        return View;
179
    })();
180
181
	/**
182
	 * Init OCR View
183
	 */
184
    /** We have to be in the Files App! */
185
    if (!OCA.Files) {
186
        return;
187
    }
188
    /** Escape when the requested file is public.php */
189
    if (/(public)\.php/i.exec(window.location.href) !== null) {
190
        return;
191
    }
192
    /** Create namespace Ocr */
193
    if (!OCA.Ocr) {
194
        OCA.Ocr = {};
195
    }
196
    OCA.Ocr.View = View;
197
198
    /** global: OC, OCA, Handlebars, _ */
199
})(OC, OCA, window, jQuery, Handlebars, _);
200