|
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() { |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
// Handlebarsjs template |
|
14
|
|
|
var TEMPLATE_OCR_DROPDOWN = '<div id="ocrDropdown" class="ocrUserInterface">'+ |
|
15
|
|
|
'{{#if noMatches}}'+ |
|
16
|
|
|
t('ocr', 'No languages for tesseract available')+ |
|
17
|
|
|
'{{else}}'+ |
|
18
|
|
|
'<select id="ocrLanguage" class="multiselect" multiple="multiple">'+ |
|
19
|
|
|
'{{#each languages}}'+ |
|
20
|
|
|
'<option value="{{this}}">{{this}}</option>'+ |
|
21
|
|
|
'{{/each}}'+ |
|
22
|
|
|
'</select>'+ |
|
23
|
|
|
'<input type="button" id="processOCR" value="'+ |
|
24
|
|
|
t('ocr', 'Process')+ |
|
25
|
|
|
'" />'+ |
|
26
|
|
|
'{{/if}}'+ |
|
27
|
|
|
'</div>'; |
|
28
|
|
|
|
|
29
|
|
|
var TEMPLATE_OCR_SELECTED_FILE_ACTION = '<span class="selectedActionsOCR hidden">'+ |
|
30
|
|
|
'<a id="selectedFilesOCR" href="" class="ocr">'+ |
|
31
|
|
|
'<span class="icon icon-external"></span>'+ |
|
32
|
|
|
'<span>'+t('ocr', 'OCR')+'</span>'+ |
|
33
|
|
|
'</a>'+ |
|
34
|
|
|
'</span>'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Constructor of the View object. |
|
38
|
|
|
* This will update the different parts of the html. |
|
39
|
|
|
* @param ocr |
|
40
|
|
|
* @constructor |
|
41
|
|
|
*/ |
|
42
|
|
|
var View = function (ocr) { |
|
43
|
|
|
this._ocr = ocr; |
|
44
|
|
|
this._selectedFiles = []; |
|
45
|
|
|
this._row = undefined; |
|
46
|
|
|
}; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Class prototype for the View. Following functions are available: |
|
50
|
|
|
* |
|
51
|
|
|
*/ |
|
52
|
|
|
View.prototype = { |
|
53
|
|
|
initialize: function () { |
|
54
|
|
|
this.renderSelectedActionButton(); |
|
55
|
|
|
this.registerFileActions(); |
|
56
|
|
|
this.registerEvents(); |
|
57
|
|
|
this.loopForStatus(); |
|
58
|
|
|
}, |
|
59
|
|
|
destroy: function () { |
|
60
|
|
|
var self = this; |
|
61
|
|
|
self.destroyDropdown(); |
|
62
|
|
|
self.destroySelectedActionButton(); |
|
63
|
|
|
OCA.Files.fileActions.clear(); |
|
64
|
|
|
OCA.Files.fileActions.registerDefaultActions(); |
|
65
|
|
|
}, |
|
66
|
|
|
registerFileActions: function () { |
|
67
|
|
|
var self = this; |
|
68
|
|
|
/** |
|
69
|
|
|
* Register FileAction for mimetype pdf |
|
70
|
|
|
*/ |
|
71
|
|
|
OCA.Files.fileActions.registerAction({ |
|
72
|
|
|
name: 'Ocr', |
|
73
|
|
|
displayName: t('ocr', 'OCR'), |
|
74
|
|
|
order: 100, |
|
75
|
|
|
mime: 'application/pdf', |
|
76
|
|
|
permissions: OC.PERMISSION_UPDATE, |
|
77
|
|
|
altText: t('ocr', 'OCR'), |
|
78
|
|
|
iconClass: 'icon-external', |
|
79
|
|
|
actionHandler: function (filename, context) { |
|
80
|
|
|
var id = context.$file.attr('data-id'); |
|
81
|
|
|
var mimetype = context.fileActions.getCurrentMimeType(); |
|
82
|
|
|
self.renderFileAction(id, filename, mimetype); |
|
83
|
|
|
} |
|
84
|
|
|
}); |
|
85
|
|
|
/** |
|
86
|
|
|
* Register FileAction for mimetype image |
|
87
|
|
|
*/ |
|
88
|
|
|
OCA.Files.fileActions.registerAction({ |
|
89
|
|
|
name: 'Ocr', |
|
90
|
|
|
displayName: t('ocr', 'OCR'), |
|
91
|
|
|
order: 100, |
|
92
|
|
|
mime: 'image', |
|
93
|
|
|
permissions: OC.PERMISSION_UPDATE, |
|
94
|
|
|
altText: t('ocr', 'OCR'), |
|
95
|
|
|
iconClass: 'icon-external', |
|
96
|
|
|
actionHandler: function (filename, context) { |
|
97
|
|
|
var id = context.$file.attr('data-id'); |
|
98
|
|
|
var mimetype = context.fileActions.getCurrentMimeType(); |
|
99
|
|
|
self.renderFileAction(id, filename, mimetype); |
|
100
|
|
|
} |
|
101
|
|
|
}); |
|
102
|
|
|
}, |
|
103
|
|
|
setSelectedFiles: function (selectedFiles) { |
|
104
|
|
|
var self = this; |
|
105
|
|
|
self._selectedFiles = selectedFiles; |
|
106
|
|
|
}, |
|
107
|
|
|
getSelectedFiles: function () { |
|
108
|
|
|
var self = this; |
|
109
|
|
|
return self._selectedFiles; |
|
110
|
|
|
}, |
|
111
|
|
|
destroySelectedActionButton: function () { |
|
112
|
|
|
// remove the Template |
|
113
|
|
|
$('.selectedActionsOCR').remove(); |
|
114
|
|
|
}, |
|
115
|
|
|
renderSelectedActionButton: function () { |
|
116
|
|
|
// append the TEMPLATE to correct position |
|
117
|
|
|
$(TEMPLATE_OCR_SELECTED_FILE_ACTION).appendTo($('#headerName-container')); |
|
118
|
|
|
}, |
|
119
|
|
|
destroyDropdown: function () { |
|
120
|
|
|
if ($('#ocrDropdown').length){ |
|
121
|
|
|
$('#ocrDropdown').detach(); |
|
122
|
|
|
} |
|
123
|
|
|
}, |
|
124
|
|
|
renderDropdown: function(){ |
|
125
|
|
|
var self = this; |
|
126
|
|
|
self.destroyDropdown(); |
|
127
|
|
|
/** global: Handlebars */ |
|
128
|
|
|
var template = Handlebars.compile(TEMPLATE_OCR_DROPDOWN); |
|
129
|
|
|
var noMatches = true; |
|
130
|
|
|
var languages = self._ocr.getLanguages(); |
|
131
|
|
|
if(languages.length > 0 && typeof languages !== undefined){ noMatches = false; } |
|
132
|
|
|
return template({languages: languages, noMatches: noMatches}); |
|
133
|
|
|
}, |
|
134
|
|
|
renderFileAction: function (id, file, mimetype) { |
|
135
|
|
|
var self = this; |
|
136
|
|
|
var html = self.renderDropdown(); |
|
137
|
|
|
$(html).appendTo($('tr').filterAttr('data-file',file).find('td.filename')); |
|
138
|
|
|
$("#ocrLanguage").select2({ |
|
139
|
|
|
width: 'element', |
|
140
|
|
|
placeholder: t('ocr', 'Select language'), |
|
141
|
|
|
formatNoMatches: function(){ |
|
142
|
|
|
return t('ocr', 'No matches found.'); |
|
143
|
|
|
} |
|
144
|
|
|
}); |
|
145
|
|
|
var files = [{id: id, mimetype: mimetype}]; |
|
146
|
|
|
self.setSelectedFiles(files); |
|
147
|
|
|
}, |
|
148
|
|
|
toggleSelectedActionButton: function () { |
|
149
|
|
|
var self = this; |
|
150
|
|
|
var selectedActionButton = $('.selectedActionsOCR'); |
|
151
|
|
|
var selFiles = OCA.Files.App.fileList.getSelectedFiles(); |
|
152
|
|
|
if(selFiles.length > 0 && typeof selFiles !== undefined){ |
|
153
|
|
|
//show if all have correct mimetype and type = file |
|
154
|
|
|
if(self._ocr.checkMimeTypes(selFiles)){ |
|
155
|
|
|
// show if not already shown |
|
156
|
|
|
selectedActionButton.removeClass('hidden'); |
|
157
|
|
|
}else{ |
|
158
|
|
|
selectedActionButton.addClass('hidden'); |
|
159
|
|
|
} |
|
160
|
|
|
}else{ |
|
161
|
|
|
// hide if not already hidden |
|
162
|
|
|
selectedActionButton.addClass('hidden'); |
|
163
|
|
|
self.setSelectedFiles([]); |
|
164
|
|
|
} |
|
165
|
|
|
}, |
|
166
|
|
|
hideSelectedActionButton: function () { |
|
167
|
|
|
var self = this; |
|
168
|
|
|
var selectedActionButton = $('.selectedActionsOCR'); |
|
169
|
|
|
selectedActionButton.addClass('hidden'); |
|
170
|
|
|
self.setSelectedFiles([]); |
|
171
|
|
|
}, |
|
172
|
|
|
togglePendingState: function (force, initialcount) { |
|
173
|
|
|
var self = this; |
|
174
|
|
|
var html = ''; |
|
175
|
|
|
var pendingcount = self._ocr.getStatus().pending; |
|
176
|
|
|
if(force){ |
|
177
|
|
|
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.', initialcount) + '</span>'; |
|
178
|
|
|
}else{ |
|
179
|
|
|
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.', pendingcount) + '</span>'; |
|
180
|
|
|
} |
|
181
|
|
|
if(pendingcount > 0 || force){ |
|
182
|
|
|
if (self._row !== undefined) { OC.Notification.hide(self._row); } |
|
183
|
|
|
self._row = OC.Notification.showHtml(html); |
|
184
|
|
|
}else{ |
|
185
|
|
|
if (self._row !== undefined){ |
|
186
|
|
|
OC.Notification.hide(self._row); |
|
187
|
|
|
self._row = undefined; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
}, |
|
191
|
|
|
updateFileList: function () { |
|
192
|
|
|
var self = this; |
|
193
|
|
|
OCA.Files.App.fileList.reload(); |
|
194
|
|
|
self.toggleSelectedActionButton(); |
|
195
|
|
|
}, |
|
196
|
|
|
/** |
|
197
|
|
|
* Loops as long as there are pending objects |
|
198
|
|
|
*/ |
|
199
|
|
|
loopForStatus: function () { |
|
200
|
|
|
var self = this; |
|
201
|
|
|
$.when(self._ocr.checkStatus()).done(function(){ |
|
202
|
|
|
if(self._ocr.getStatus().failed > 0) { self.notifyError(n('ocr', 'OCR processing for %n file failed. For details please go to your personal settings.', 'OCR processing for %n files failed. For details please go to your personal settings.', self._ocr.getStatus().failed)); } |
|
203
|
|
|
if(self._ocr.getStatus().pending > 0){ |
|
204
|
|
|
if(self._ocr.getStatus().processed > 0) { self.updateFileList(); } |
|
205
|
|
|
self.togglePendingState(false); |
|
206
|
|
|
setTimeout($.proxy(self.loopForStatus,self), 4500); |
|
207
|
|
|
}else{ |
|
208
|
|
|
if(self._ocr.getStatus().processed > 0) { self.updateFileList(); } |
|
209
|
|
|
self.togglePendingState(false); |
|
210
|
|
|
} |
|
211
|
|
|
}).fail(function(message){ |
|
212
|
|
|
self.notifyError(message); |
|
213
|
|
|
setTimeout($.proxy(self.loopForStatus,self), 4500); |
|
214
|
|
|
}); |
|
215
|
|
|
}, |
|
216
|
|
|
notifyError: function (message) { |
|
217
|
|
|
/** global: OC */ |
|
218
|
|
|
OC.Notification.showHtml('<div>'+message+'</div>', {timeout: 10, type: 'error'}); |
|
219
|
|
|
}, |
|
220
|
|
|
registerEvents: function(){ |
|
221
|
|
|
var self = this; |
|
222
|
|
|
// Close on click on other element |
|
223
|
|
|
$(document).click(function(event) { |
|
224
|
|
|
if(!$(event.target).closest('#ocrDropdown').length) { |
|
225
|
|
|
self.destroyDropdown(); |
|
226
|
|
|
self.setSelectedFiles([]); |
|
227
|
|
|
} |
|
228
|
|
|
}); |
|
229
|
|
|
// Register submit action |
|
230
|
|
|
$(document).on('click', '#processOCR', function(){ |
|
231
|
|
|
var selectedLanguages = $('#ocrLanguage').select2('val'); |
|
232
|
|
|
$.when(self._ocr.process(self.getSelectedFiles(), selectedLanguages)).done(function(){ |
|
233
|
|
|
self.destroyDropdown(); |
|
234
|
|
|
|
|
235
|
|
|
// status monitoring init |
|
236
|
|
|
self.togglePendingState(true, self.getSelectedFiles().length); |
|
237
|
|
|
self.setSelectedFiles([]); |
|
238
|
|
|
setTimeout($.proxy(self.loopForStatus,self), 4500); |
|
239
|
|
|
}).fail(function(message){ |
|
240
|
|
|
self.notifyError(t('ocr', 'OCR processing failed:')+ ' ' + message); |
|
241
|
|
|
self.destroyDropdown(); |
|
242
|
|
|
}); |
|
243
|
|
|
}); |
|
244
|
|
|
// Register click selectedFilesAction |
|
245
|
|
|
$(document).on('click', '#selectedFilesOCR', function(){ |
|
246
|
|
|
var html = self.renderDropdown(); |
|
247
|
|
|
$(html).appendTo($('tr').find('th.column-name')); |
|
248
|
|
|
$("#ocrLanguage").select2({ |
|
249
|
|
|
width: 'element', |
|
250
|
|
|
placeholder: t('ocr', 'Select language'), |
|
251
|
|
|
formatNoMatches: function(){ |
|
252
|
|
|
return t('ocr', 'No matches found.'); |
|
253
|
|
|
} |
|
254
|
|
|
}); |
|
255
|
|
|
self.setSelectedFiles(OCA.Files.App.fileList.getSelectedFiles()); |
|
256
|
|
|
return false; |
|
257
|
|
|
}); |
|
258
|
|
|
// Register checkbox events |
|
259
|
|
|
/** global: _ */ |
|
260
|
|
|
OCA.Files.App.fileList.$fileList.on('change', 'td.filename>.selectCheckBox', _.bind(self.toggleSelectedActionButton, this)); |
|
261
|
|
|
OCA.Files.App.fileList.$el.find('.select-all').click(_.bind(self.toggleSelectedActionButton, this)); |
|
262
|
|
|
OCA.Files.App.fileList.$el.find('.delete-selected').click(_.bind(self.hideSelectedActionButton, this)); |
|
263
|
|
|
} |
|
264
|
|
|
}; |
|
265
|
|
|
/** global: OCA */ |
|
266
|
|
|
if (OCA.Ocr) { |
|
267
|
|
|
OCA.Ocr.View = View; |
|
268
|
|
|
} |
|
269
|
|
|
})(); |