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, _) { |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
} |
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Destroys the dropdown if existing. |
69
|
|
|
* @public |
70
|
|
|
*/ |
71
|
|
|
this.destroyDropdown = function () { |
72
|
|
|
if ($('#ocrDropdown').length) { |
73
|
|
|
$('#ocrDropdown').detach(); |
74
|
|
|
} |
75
|
|
|
} |
|
|
|
|
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
|
|
|
} |
|
|
|
|
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> <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> <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
|
|
|
} |
|
|
|
|
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
|
|
|
} |
|
|
|
|
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'); |
|
|
|
|
127
|
|
|
} |
|
|
|
|
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')); |
|
|
|
|
139
|
|
|
renderSelectTwo(); |
140
|
|
|
} |
|
|
|
|
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
|
|
|
} |
|
|
|
|
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
|
|
|
} |
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Destroys the selected files action button. |
166
|
|
|
* @public |
167
|
|
|
*/ |
168
|
|
|
this.destroySelectedFilesActionButton = function () { |
169
|
|
|
$('.selectedActionsOCR').remove(); |
170
|
|
|
} |
|
|
|
|
171
|
|
|
|
172
|
|
|
this.destroy = function() { |
173
|
|
|
this.destroySelectedFilesActionButton(); |
174
|
|
|
this.destroyDropdown(); |
175
|
|
|
} |
|
|
|
|
176
|
|
|
} |
|
|
|
|
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
|
|
|
|
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.