/
pdf_view.js
1345 lines (1162 loc) · 54.2 KB
/
pdf_view.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Tabula = Tabula || {};
var clip = null;
PDF_ID = window.location.pathname.split('/')[2];
Tabula.LazyLoad = 10; // max number of pages around the cursor to show (2x Tabula.LazyLoad pages are shown)
Tabula.HideOnLazyLoad = false; // ideally, set to true, but this requires differently positioned selections, see https://github.com/tabulapdf/tabula/issues/245#issuecomment-75182061
ZeroClipboard.config( { swfPath: "/swf/ZeroClipboard.swf" } );
Tabula.entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'`': '`',
'=': '='
};
Tabula.escapeHtml = function(string) {
return String(string).replace(/[&<>"'`=\/]/g, function (s) {
return Tabula.entityMap[s];
});
}
Tabula.Page = Backbone.Model.extend({
// number: null, //set on initialize
// width: null, //set on initialize
// height: null, //set on initialize
// rotation: null, //set on initialize
initialize: function(){
this.set('number_zero_indexed', this.get('number') - 1);
},
imageUrl: function(){
var resolution = Math.max(Tabula.pdf_view.pdf_document.get('thumbnail_sizes')) || 560;
this.set('image_url', '/pdfs/' + PDF_ID + '/document_'+resolution+'_' + this.get('number') + '.png');
return this.get('image_url');
}
});
Tabula.Pages = Backbone.Collection.extend({
model: Tabula.Page,
url: null, //set on initialize
comparator: 'number',
initialize: function(){
this.url = '/pdfs/' + PDF_ID + '/pages.json?_=' + Math.round(+new Date()).toString();
}
});
Tabula.Document = Backbone.Model.extend({
page_collection: null, //set on initialize
selections: null, //set on initialize
pdf_id: PDF_ID, //set on initialize
url: null, //set on initialize
initialize: function(options){
this.page_collection = new Tabula.Pages([], {pdf_document: this});
this.selections = new Tabula.Selections([], {pdf_document: this});
this.autodetected_selections = new Tabula.AutodetectedSelections([], {pdf_document: this});
this.url = '/pdf/' + this.pdf_id + '/metadata.json';
this.set('original_filename', '');
this.set('new_filename', false);
},
});
Tabula.Selection = Backbone.Model.extend({
pdf_id: PDF_ID,
initialize: function(){
_.bindAll(this, 'repeatLassos', 'toCoords');
},
updateCoords: function(){
var page = Tabula.pdf_view.pdf_document.page_collection.at(this.get('page_number') - 1);
var imageWidth = this.get('imageWidth');
var original_pdf_width = page.get('width');
var original_pdf_height = page.get('height');
// var pdf_rotation = page.get('rotation');
var scale = original_pdf_width / imageWidth;
var rp = this.attributes.getDims().relativePos;
this.set({
x1: rp.left * scale,
x2: (rp.left + rp.width) * scale,
y1: rp.top * scale,
y2: (rp.top + rp.height) * scale,
width: rp.width * scale, // not used by Tabula right now, but used in the UI elsewhere
height: rp.height * scale, // not used by Tabula right now, but used in the UI elsewhere
});
},
toCoords: function(){
if (this.attributes.getDims){
this.updateCoords();
}
var selection_coords = {
page: this.get('page_number'),
extraction_method: this.get('extractionMethod') || 'guess',
selection_id: this.id,
x1: this.get('x1'),
x2: this.get('x2'),
y1: this.get('y1'),
y2: this.get('y2'),
width: this.get('width'),
height: this.get('height')
};
return selection_coords;
},
repeatLassos: function() {
Tabula.pdf_view.pdf_document.page_collection.each(_.bind(function(page){
if(this.get('page_number') < page.get('number')){ // for each page after this one,
new_selection = this.clone(); // and create a new Selection.
new_selection.set('page_number', page.get('number'));
this.collection.add(Tabula.pdf_view.renderSelection(new_selection.toCoords()));
/* which causes thumbnails to be created, Download All button to know about these selections. */
}
}, this));
},
});
// Not currently used at all.
Tabula.Options = Backbone.Model.extend({
initialize: function(){
_.bindAll(this, 'write');
// this.set('multiselect_mode', localStorage.getItem("tabula-multiselect-mode") !== "false");
},
write: function(){
// localStorage.setItem("tabula-multiselect-mode", this.get('multiselect_mode'));
}
});
/* What the hell are you doing here, Jeremy?
The canonical store of selections now needs to be in Backbone, not in imgareaselect.
The UI can listen to the Selections; imgAreaselect creates adds to the collection,
causing the thumbnail to be drawn.
Clearing or repeating is much easier, because we don't have to mess around with the UI.
Querying all is likewise easy.
We could also store extraction option info on the selections, if we want.
On imgareaselect's _onSelectEnd, add the selection to Selections
On Selections's remove (or change), find the right imgAreaSelect
*/
Tabula.Selections = Backbone.Collection.extend({
// model: Tabula.Selection,
comparator: 'page_number',
updateOrCreateByVendorSelectorId: function(vendorSelection, pageNumber, imageWidth){
var selection = this.get(vendorSelection.id);
if (selection) { // if it already exists
selection.set(_.omit(vendorSelection, 'id'));
}
else {
new_selection_args = _.extend({'page_number': pageNumber,
'imageWidth': imageWidth,
'extraction_method': Tabula.pdf_view.options.extraction_method,
'hidden': false,
'pdf_document': this.pdf_document},
vendorSelection);
selection = new Tabula.Selection(new_selection_args);
this.add(selection);
}
return selection;
},
createHiddenSelection: function(sel){
new_selection_args = _.extend({'page_number': sel.page,
'extraction_method': 'spreadsheet',
'id': String.fromCharCode(65 + Math.floor(Math.random() * 26)) + Date.now(),
'hidden': true,
'pdf_document': this.pdf_document},
sel);
selection = new Tabula.Selection(new_selection_args);
this.add(selection);
return selection;
}
});
Tabula.AutodetectedSelections = Tabula.Selections.extend({
url: null, //set on init
initialize: function(){
this.url = '/pdfs/' + PDF_ID + '/tables.json?_=' + Math.round(+new Date()).toString();
_.bindAll(this, 'updateOrCreateByVendorSelectorId');
},
parse: function(response){
// a JSON list of pages, which are each just a list of coords
var tables = [];
var selections = _(response).map(_.bind(function(page_tables, listIndex){
var pageIndex = listIndex + 1;
return _(page_tables).map(_.bind(function(tableCoords){
if(tableCoords[2] * tableCoords[3] < 400){ //exclude tiny autodetected selections
return null;
}
return {
x1: tableCoords[0],
y1: tableCoords[1],
x2: tableCoords[0] + tableCoords[2],
y2: tableCoords[1] + tableCoords[3],
width: tableCoords[2],
height: tableCoords[3],
page: pageIndex,
extraction_method: 'spreadsheet',
selection_id: null
};
}, this));
}, this));
return _.select(_.flatten(selections), function(i){ return i; });
}
});
Tabula.Query = Backbone.Model.extend({
//has selections, data
//pertains to DataView
// on modal exit, destroy this.pdf_view.query
// on selection end or download all button, create this.pdf_view.query
// in the modal, modify and requery.
initialize: function(){
// should be inited with list_of_coords
_.bindAll(this, 'doQuery', 'setExtractionMethod', 'convertTo', 'convertToCSV',
'convertToTabulaExtractorScript', 'convertToBoundingBoxJSON');
},
convertTo: function(format){
if(!this.get('data')){
throw Error("You need to query for data before converting it locally.");
}else{
switch(format.toLowerCase()){
// case "XML":
// return this.convertToXML();
// break;
case "bbox":
return this.convertToBoundingBoxJSON();
case "tsv":
return this.convertToCSV("\t");
case "csv":
return this.convertToCSV();
case "\uD83D\uDCA9SV":
return this.convertToCSV("\uD83D\uDCA9SV");
case "script":
return this.convertToTabulaExtractorScript();
default:
throw Error("Unknown format: " + format);
}
}
},
convertToCSV: function(delimiter_maybe_undef){
var delimiter = typeof delimiter_maybe_undef == "undefined" ? ',' : delimiter_maybe_undef
var csv = _(this.get('data')).chain().pluck('data').map(function(table){
return _(table).chain().map(function(row){
return _.map(row, function(cell){
var text = cell.text;
text = text.replace("\"", "\\\""); //escape quotes
text = text.indexOf(delimiter) > -1 ? "\"" + text + "\"" : text; //enquote cells containing the delimiter.
return text;
}).join(delimiter);
}).value();
}).flatten(true).value().join("\n");
return csv;
},
convertToBoundingBoxJSON: function(){
return JSON.stringify(this.get('list_of_coords'), null, 4);
},
convertToTabulaExtractorScript: function(){
return _(this.get('list_of_coords')).map(function(c){
var extraction_method_switch = "";
if(c.extraction_method == "original"){
var extraction_method_switch = "--no-spreadsheet";
}else if(c.extraction_method == "spreadsheet"){
var extraction_method_switch = "--spreadsheet";
}
return "tabula "+extraction_method_switch+" -a "+roundTo(c.y1, 3)+","+roundTo(c.x1, 3)+","+roundTo(c.y2, 3)+","+roundTo(c.x2, 3)+" -p "+c.page+" \"$1\"";
}).join("\n");
},
doQuery: function(options) {
this.query_data = {
'coords': JSON.stringify(this.get('list_of_coords')),
'new_filename': null,
};
// print selection coordinates to the console
// way easier FOR NOW than downloading the script/JSON
console.log(_.map(this.get('list_of_coords'), function(l){ return [l.y1, l.x1, l.y2, l.x2].join(', '); }).join("\n") );
// shallow copy the selections collection
// so if hte user somehow changes the selections between starting the query and it finishing,
// there isn't an error
var stashed_selections = new Tabula.Selections(Tabula.pdf_view.pdf_document.selections.models.slice());
this.trigger("tabula:query-start");
window.tabula_router.navigate('pdf/' + PDF_ID + '/extract'); // TODO: this should probably go in a view!! -JBM
$.ajax({
type: 'POST',
url: '/pdf/' + PDF_ID + '/data',
data: this.query_data,
success: _.bind(function(resp) {
this.set('data', resp);
// this only needs to happen on the first select, when we don't know what the extraction method is yet
// (because it's set by the heuristic on the server-side).
// TODO: only execute it when one of the list_of_coords has guess or undefined as its extraction_method
_(resp).each(_.bind(function(resp_item, i){
var coord_set = this.get('list_of_coords')[resp_item['spec_index']];
// _(_.zip(this.get('list_of_coords'), resp)).each(function(stuff, i){
// var coord_set = stuff[0];
// var resp_item = stuff[1];
// if(!coord_set) return; // DIRTY HACK, see https://github.com/tabulapdf/tabula/issues/497
// // if one set of coords returns 2+ tables,
// // then this zip won't work.
if (stashed_selections.get(coord_set.selection_id)){
stashed_selections.get(coord_set.selection_id).
set('extraction_method', resp_item["extraction_method"]);
}
coord_set["extraction_method"] = resp_item["extraction_method"];
},this));
this.trigger("tabula:query-success");
if (options !== undefined && _.isFunction(options.success)){
Tabula.pdf_view.options.success(resp);
}
}, this),
error: _.bind(function(xhr, status, error) {
console.log("error!", xhr, status);
var error_text = xhr.responseText;
window.raw_xhr_responseText = xhr.responseText; // for consoles, etc.
if(error_text.indexOf("DOCTYPE") != -1){ // we're in Jar/Jetty/whatever land, not rackup land
var error_html = $('<div></div>').html( error_text );
var summary = error_html.find('#summary').text().trim();
var meta = error_html.find('#meta').text().trim();
var info = error_html.find('#info').text().trim();
error_text = [summary, meta, info].join("<br />");
}
var debugging_text = "Tabula API version: " + Tabula.api_version + "\nFilename: " + Tabula.pdf_view.pdf_document.get('original_filename') + "\n" + error_text
this.set('error_message', debugging_text);
this.trigger("tabula:query-error");
if (options !== undefined && _.isFunction(options.error))
options.error(resp);
}, this),
});
},
setExtractionMethod: function(extractionMethod){
_(this.get('list_of_coords')).each(function(coord_set){ coord_set['extraction_method'] = extractionMethod; });
},
getDataArray: function(){
// this.data is a list of responses (because we sent a list of coordinate sets)
// $.each( _.pluck(this.model.get('data'), 'data'), function(i, rows) {
// $.each(rows, function(j, row) {
// tableHTML += '<tr><td>' + _.pluck(row, 'text').join('</td><td>') + '</td></tr>';
// });
// });
/* via https://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery
* if a PDF contains the string "<iframe>" we want to display that, not an actual iframe!
*/
if (!this.get('data')){ return []; }
var data = _(this.get('data')).chain().pluck('data').map(function(table){
return _(table).chain().map(function(row){
return (_.pluck(row, 'text')).map(Tabula.escapeHtml);
}).value();
})/*.flatten(true)*/.value();
return data.length == 1 && data[0].length === 0 ? [] : data; // checking whether there's no data, i.e. data == [[]]
}
});
Tabula.DataView = Backbone.View.extend({ // one per query object.
el: '#tabula-dataview',
template: _.template($('#templates #export-page-template').html().replace(/nestedscript/g, 'script')),
events: {
'click .extraction-method-btn:not(.active)': 'queryWithToggledExtractionMethod',
'click #download-data': 'setFormAction',
'keyup .filename': 'updateFilename',
//N.B.: Download button (and format-specific download buttons) are an HTML form, so not handled here.
//TODO: handle flash clipboard thingy here.
// 'click #copy-csv-to-clipboard':
'click #revise-selections': 'closeAndRenderSelectionView'
},
pdf_view: null, //added on create
extractionMethod: "guess",
initialize: function(stuff){
_.bindAll(this, 'render', 'renderFlashClipboardNonsense', 'updateFilename', 'queryWithToggledExtractionMethod', 'closeAndRenderSelectionView', 'setFormAction');
this.pdf_view = stuff.pdf_view;
this.listenTo(this.model, 'tabula:query-start', this.render);
this.listenTo(this.model, 'tabula:query-success', this.render);
this.listenTo(this.model, 'tabula:query-error', this.render);
},
disableDownloadButton: function(){
$('#download-data').addClass('download-in-progress');
$('#download-data').prop('disabled', 'disabled');
window.setTimeout( function(){
$('#download-data').removeClass('download-in-progress');
$('#download-data').removeProp('disabled');
}, 2000);
},
updateFilename: function(){
var new_filename = this.$el.find('#control-panel input.filename').val();
if(new_filename.length){
this.pdf_view.pdf_document.set('new_filename', new_filename);
}
},
closeAndRenderSelectionView: function(){
window.tabula_router.navigate('pdf/' + PDF_ID);
this.$el.empty();
this.undelegateEvents();
this.pdf_view.$el.show();
this.pdf_view.render();
$('body').removeClass('page-export');
$('body').addClass('page-selections');
var oldSelections = this.pdf_view.pdf_document.selections.models.map(function(sel){
var selection = Tabula.pdf_view.renderSelection(sel.toCoords());
// selection.attributes.rebind(); // o. m. g.
return selection;
});
this.pdf_view.pdf_document.selections.reset(oldSelections);
},
setFormAction: function(e){
var formActionUrl = $(e.currentTarget).data('action');
this.$el.find('#download-form').attr('action', formActionUrl);
this.$el.find('#download-form').submit();
this.disableDownloadButton();
},
render: function(e){
document.title="Export Data | Tabula";
var uniq_extraction_methods = _.uniq(_(this.model.get('list_of_coords')).pluck('extraction_method'));
// save the current scroll position (if unset), then scroll to the top
if(!Tabula.pdf_view.selectionScrollTop){
Tabula.pdf_view.selectionScrollTop = $(document).scrollTop();
}
$(document).scrollTop(0);
//TODO: move flash_borked to this object (dataview) away from pdf_view
$('body').removeClass('page-selections');
$('body').addClass('page-export');
this.delegateEvents();
this.pdf_view.$el.hide();
$('.selection-box').css('visibility', 'hidden');
$('.table-region').remove();
$('.selection-show').remove(); // nuke thumbs, we'll put 'em back in a second
this.$el.html(this.template({
pdf_id: PDF_ID,
data: this.model.getDataArray(),
loading: !(this.model.get('data') || this.model.get('error_message')),
error_message: this.model.get('error_message')
}));
this.$el.find('#control-panel').html(
_.template($('#templates #export-control-panel-template').html().replace(/nestedscript/g, 'script'))(
_(this.pdf_view.pdf_document.attributes).extend({
pdf_id: PDF_ID,
list_of_coords: JSON.stringify(this.model.get('list_of_coords')),
copyDisabled: Tabula.pdf_view.flash_borked ? 'disabled="disabled" data-toggle="tooltip" title="'+Tabula.pdf_view.flash_borken_message+'"' : '',
disableIfNoData: (_.isNull(this.model.get('data')) || typeof(this.model.get('data')) === "undefined") ? 'disabled="disabled"' : ''
})
));
this.$el.find('#sidebar').html(
_.template($('#templates #export-page-sidebar-template').html().replace(/nestedscript/g, 'script')) (
{
pdf_id: PDF_ID,
disableExtractionMethodButtons: _.isNull(this.model.data) || uniq_extraction_methods.length > 1 ? 'disabled="disabled"' : '',
}
));
this.renderFlashClipboardNonsense();
if(!this.model.get('data')) this.startSpinner();
if (uniq_extraction_methods.length == 1){
// prospectively support renaming the methods in tabula-extractor to "lattice"/"stream"
// and cope with "basic" extraction algorithm ID in tabula-java
// https://github.com/tabulapdf/tabula-extractor/issues/96
if (["basic", "original", "stream"].indexOf(uniq_extraction_methods[0]) > -1){
uniq_extraction_methods[0] = "original";
}else if(["spreadsheet", "lattice"].indexOf(uniq_extraction_methods[0]) > -1){
uniq_extraction_methods[0] = "spreadsheet";
}
this.$el.find('#' + uniq_extraction_methods[0] + '-method-btn').button('toggle');
}else{
console.log("A mix of unique extraction methods found, not selecting either extraction method radio button: " + uniq_extraction_methods.join(", "))
}
this.$el.find('.has-tooltip').tooltip();
return this;
},
startSpinner: function(){
var SPINNER_OPTS = {
lines: 10, // The number of lines to draw
length: 5, // The length of each line
width: 2, // The line thickness
radius: 5, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
color: '#000', // #rgb or #rrggbb
speed: 1.1, // Rounds per second
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: true, // Whether to use hardware acceleration
className: 'spinner', // The CSS class to assign to the spinner
zIndex: 2e9, // The z-index (defaults to 2000000000)
top: 'auto', // Top position relative to parent in px
left: 'auto' // Left position relative to parent in px
};
new Spinner(SPINNER_OPTS).spin($('#spinner').get(0));
},
renderFlashClipboardNonsense: function(){
if(!Tabula.pdf_view.client){
try{
Tabula.pdf_view.client = new ZeroClipboard();
}catch(e){
this.$el.find('#copy-csv-to-clipboard').hide();
}
}
if( !Tabula.pdf_view.flash_borked ){
Tabula.pdf_view.client.on( 'ready', _.bind(function(event) {
Tabula.pdf_view.client.clip( this.$el.find("#copy-csv-to-clipboard") );
Tabula.pdf_view.client.on( 'copy', _.bind(function(event) {
var clipboard = event.clipboardData;
console.log('clippy thingy clicked', $('#download-form select').val());
var button_text = this.$el.find("#copy-csv-to-clipboard .clipboard-text");
button_text.text("Copied!");
window.setTimeout(_.bind(function(){
button_text.text("Copy to Clipboard");
},this), 2000);
var tableData = Tabula.pdf_view.query.convertTo($('#download-form select').val());
clipboard.setData( 'text/plain', tableData );
}, this) );
this.$el.find('.modal-footer button').prop('disabled', '');
}, this) );
Tabula.pdf_view.client.on( 'error', _.bind(function(event) {
//disable all clipboard buttons, add tooltip, event.message
Tabula.pdf_view.flash_borked = true;
Tabula.pdf_view.flash_borken_message = event.message;
this.$el.find('#copy-csv-to-clipboard').addClass('has-tooltip').tooltip();
console.log( 'ZeroClipboard error of type "' + event.name + '": ' + event.message );
ZeroClipboard.destroy();
this.$el.find('.modal-footer button').prop('disabled', '');
},this) );
}
return this;
},
queryWithToggledExtractionMethod: function(e){
this.model.set('data', null);
var extractionMethod = $(e.currentTarget).data('method');
this.pdf_view.options.set('extraction_method', extractionMethod);
Tabula.pdf_view.query.setExtractionMethod(extractionMethod);
Tabula.pdf_view.query.doQuery();
},
});
Tabula.DocumentView = Backbone.View.extend({ // Singleton
events: {
},
pdf_view: null, //added on create
page_views: {},
rectangular_selector: null,
_selectionsGetter: function(target) {
return _(Tabula.pdf_view.pdf_document.selections.where({page_number: $(target).data('page')})).map(function(i){ return i.attributes; });
},
initialize: function(stuff){
_.bindAll(this, 'render', 'removePage', '_onRectangularSelectorEnd', '_selectionsGetter');
this.pdf_view = stuff.pdf_view;
this.listenTo(this.collection, 'remove', this.removePage);
// attach rectangularSelector to main page container
this.rectangular_selector = new RectangularSelector(
this.$el,
{
selector: '#pages-container .pdf-page img',
end: this._onRectangularSelectorEnd,
areas: this._selectionsGetter,
validSelection: function(selection) {
return (selection.absolutePos.width > $(selection.pageView).width() * 0.01) &&
(selection.absolutePos.height > $(selection.pageView).height() * 0.01);
}
}
);
},
// listens to mouseup of RectangularSelector
_onRectangularSelectorEnd: function(d) {
var page_number = $(d.pageView).data('page');
var pv = this.page_views[page_number];
var rs = new ResizableSelection({
position: d.absolutePos,
target: pv.$el.find('img'),
areas: this._selectionsGetter
});
rs.on({
resize: _.debounce(pv._onSelectChange, 100),
remove: pv._onSelectCancel
});
pv._onSelectEnd(rs);
this.$el.append(rs.el);
rs.$el.css('z-index', 100 - this._selectionsGetter($(d.pageView)).length);
},
removePage: function(pageModel){
var page_view = this.page_views[pageModel.get('number')];
page_view.$el.fadeOut(200, function(){
deleted_page_height = page_view.$el.height();
deleted_page_top = page_view.$el.offset()["top"];
page_view.remove();
});
},
render: function(){
if(!Tabula.LazyLoad){ // old-style, non-lazyload behavior
_(this.page_views).each(_.bind(function(page_view, index){
var already_on_page = $('#page-' + parseInt(index)+1).length;
if(!already_on_page) this.$el.append(page_view.render().el);
}, this));
}else{
//useful in the console for debugging:
// $('.pdf-page:visible').map(function(i, el){ return $(el).find('img').data('page') }).get();
// just so pages end up in the right order, we have to loop AWAY FROM the cursor in both directions
// so if the cursor is at 1.
for (var number=Tabula.pdf_view.lazyLoadCursor;number>0;number--){
var page_view = this.page_views[number];
if(!page_view) continue; // this is the first render, and there are no pages (probably!)
var page_el = $('#page-' + number);
var visible_on_page = page_el.filter(':visible').length;
if(visible_on_page && Tabula.HideOnLazyLoad){
if(Math.abs(Tabula.pdf_view.lazyLoadCursor - number) >= Tabula.LazyLoad ) {
$('#page-' + number).hide();
// console.log('hide', number)
}
}else{
if(Math.abs(Tabula.pdf_view.lazyLoadCursor - number) < Tabula.LazyLoad ) {
if(page_el.length){
page_view.$el.show();
// console.log('show ' + number);
}else{
this.$el.prepend(page_view.render().el);
}
}
}
}
for (var number=Tabula.pdf_view.lazyLoadCursor+1;number<=_(this.page_views).keys().length;number++){
var page_view = this.page_views[number];
if(!page_view) continue; // this is the first render, and there are no pages (probably!)
var page_el = $('#page-' + number);
var visible_on_page = page_el.filter(':visible').length;
if(visible_on_page && Tabula.HideOnLazyLoad){
if(Math.abs(Tabula.pdf_view.lazyLoadCursor - number) >= Tabula.LazyLoad ) {
$('#page-' + number).hide();
}
}else{
if(Math.abs(Tabula.pdf_view.lazyLoadCursor - number) < Tabula.LazyLoad ) {
if(page_el.length){
page_view.$el.show();
// console.log('show ' + number);
}else{
this.$el.append(page_view.render().el);
}
}
}
}
}
// should remove the "hidden" selections
// then should render the selections for this page from autodetectedSelections the "normal" way.
Tabula.pdf_view.pdf_document.selections.filter(function(sel){ return sel.get('hidden') && sel.get('page') <= number}).map(function(hidden_selection){
Tabula.pdf_view.pdf_document.selections.remove(hidden_selection);
var new_selection = Tabula.pdf_view.renderSelection(hidden_selection.attributes); // adds it to Tabula.pdf_view.pdf_document.selections
return new_selection;
});
return this;
}
});
Tabula.PageView = Backbone.View.extend({ // one per page of the PDF
document_view: null, //added on create
className: 'pdf-page',
iasAlreadyInited: false,
selections: null,
id: function(){
return 'page-' + this.model.get('number');
},
template: _.template($('#templates #page-template').html().replace(/nestedscript/g, 'script')) ,
'events': {
// 'click i.rotate-left i.rotate-right': 'rotate_page',
},
initialize: function(stuff){
this.pdf_view = stuff.pdf_view;
_.bindAll(this, 'rotate_page', 'createTables',
'_onSelectStart', '_onSelectChange', '_onSelectEnd', '_onSelectCancel', 'render');
this.listenTo(Tabula.pdf_view.pdf_document, 'change', function(){ this.render(); });
this.listenTo(Tabula.pdf_view.pdf_document, 'change', function(){ this.render(); });
},
render: function(){
this.$el.html(this.template({
'number': this.model.get('number'),
'image_url': this.model.imageUrl()
}));
this.$el.find('img').attr('data-page', this.model.get('number'))
.attr('data-original-width', this.model.get('width'))
.attr('data-original-height', this.model.get('height'));
// .attr('data-rotation', this.model.get('rotation'));
this.$image = this.$el.find('img');
return this;
},
createTables: function(asfd){
this.iasAlreadyInited = true;
},
_onSelectStart: function(selection) {
Tabula.pdf_view.pdf_document.selections.updateOrCreateByVendorSelectorId(selection,
this.model.get('number'),
this.$image.width());
},
_onSelectChange: function(selection) {
Tabula.pdf_view.pdf_document.selections.updateOrCreateByVendorSelectorId(selection,
this.model.get('number'),
this.$image.width());
},
_onSelectEnd: function(selection) {
var sel = Tabula.pdf_view.pdf_document.selections.updateOrCreateByVendorSelectorId(selection,
this.model.get('number'),
this.$image.width());
// deal with invalid/too-small selections somehow (including thumbnails)
if (selection.width === 0 && selection.height === 0) {
$('#thumb-' + this.$image.attr('id') + ' #vendorSelection-show-' + selection.id).css('display', 'none');
selection.remove();
}
// if this is not the last page
if(this.model != this.model.collection.last()) {
var but_id = this.model.get('number') + '-' + selection.id; //create a "Repeat this Selection" button
var button = $('<button class="btn btn-default repeat-lassos" id="'+but_id+'">Repeat this Selection</button>');
button.data("selectionId", selection.id);
selection.$el.append(button);
}
Tabula.pdf_view.components['control_panel'].render(); // deals with buttons that need blurred out if there's zero selections, etc.
},
// vendorSelection
_onSelectCancel: function(selection) {
// remove repeat lassos button
var but_id = this.$image.attr('id') + '-' + selection.id;
$('button#' + but_id).remove();
// find and remove the canceled selection from the collection of selections. (triggering remove events).
var sel = Tabula.pdf_view.pdf_document.selections.get(selection.id);
removed_selection = Tabula.pdf_view.pdf_document.selections.remove(sel);
Tabula.pdf_view.components['control_panel'].render(); // deal with buttons that need blurred out if there's zero selections, etc.
},
rotate_page: function(t) {
alert('not implemented');
}
});
Tabula.ControlPanelView = Backbone.View.extend({ // only one
events: {
'click #clear-all-selections': 'clearAllSelections',
'click #restore-detected-tables': 'restoreDetectedTables',
'click #all-data': 'queryAllData',
'click #repeat-lassos': 'repeatLassos',
},
template: _.template($('#templates #select-control-panel-template').html().replace(/nestedscript/g, 'script')),
/* in case there's a PDF with a complex format that's repeated on multiple pages */
repeatFirstPageLassos: function(){
alert('not yet implemented');
return;
/* TODO: write this */
},
clearAllSelections: function(){
_(Tabula.pdf_view.pdf_document.selections.models.slice()).each(function(i){ if(typeof i.attributes.remove !== "undefined") i.attributes.remove(); }); // call remove() on the vendorSelection of each seleciton; except for "hidden" selections that don't have one.
Tabula.pdf_view.pdf_document.selections.reset([]);
Tabula.pdf_view.components['control_panel'].render(); // deal with buttons that need blurred out if there's zero selections, etc.
// reset doesn't trigger the right events because we have to remove from the collection and from the page (with selection.remove())
// we can't use _.each because we're mutating the collection that we're iterating over
// ugh
},
restoreDetectedTables: function(){
var autodetected_selections = this.pdf_view.pdf_document.autodetected_selections.models.map(function(sel){
return Tabula.pdf_view.renderSelection(sel.attributes);
});
this.pdf_view.pdf_document.selections.reset(autodetected_selections);
},
initialize: function(stuff){
this.pdf_view = stuff.pdf_view;
_.bindAll(this, 'queryAllData', 'render');
this.listenTo(this.pdf_view.pdf_document, 'sync', this.render );
},
queryAllData : function(){
var list_of_all_coords = Tabula.pdf_view.pdf_document.selections.invoke("toCoords");
//TODO: figure out how to handle extraction methods when there are multiple selections
// should it be set globally, or per selection?
// actually, how to handle extraction method is a bit of an open question.
// should we support in the UI extraction methods per selection?
// if so, what does the modal show if its showing results from more than one selection?
// maybe it only shows them if they match?
// or not at all ever?
// but then we need to make it clearer in the UI that you are "editing" a selection.
// which will require different reactions with multiselect mode:
// when you finish a query, then still pop up its data.
// when you click or move an already-selected query, then you're "editing" it?
// hmm.
Tabula.pdf_view.query = new Tabula.Query({list_of_coords: list_of_all_coords, extraction_method: 'guess'});
Tabula.pdf_view.createDataView();
Tabula.pdf_view.query.doQuery();
},
render: function(){
var numOfSelectionsOnPage = this.pdf_view.totalSelections();
this.$el.html(this.template(
_(this.pdf_view.pdf_document.attributes).extend({
'disable_clear_all_selections': numOfSelectionsOnPage <= 0 ? 'disabled="disabled"' : '' ,
'disable_download_all': numOfSelectionsOnPage <= 0 ? 'disabled="disabled"' : '',
// three states: autodetection still incomplete, autodetection done but no tables found, autodetection done and tables found
'restore_detected_tables': this.pdf_view.hasAutodetectedTables ? "autodetect-finished" : "autodetect-in-progress",
'disable_detected_tables': numOfSelectionsOnPage > 0 || this.pdf_view.pdf_document.autodetected_selections.size() === 0 ? 'disabled="disabled"' : ''
})));
return this;
},
});
Tabula.SidebarView = Backbone.View.extend({ // only one
tagName: 'ul',
className: 'thumbnail-list',
thumbnail_views: {},
pdf_view: null, // defined on initialize
initialize: function(stuff){
this.pdf_view = stuff.pdf_view;
_.bindAll(this, 'addSelectionThumbnail', 'removeSelectionThumbnail', 'changeSelectionThumbnail',
'removeThumbnail', 'render');
this.listenTo(this.collection, 'remove', this.removeThumbnail);
this.listenTo(this.pdf_view.pdf_document.selections, 'sync', this.render);
this.listenTo(this.pdf_view.pdf_document.selections, 'add', this.addSelectionThumbnail); // render a thumbnail selection
this.listenTo(this.pdf_view.pdf_document.selections, 'change', this.changeSelectionThumbnail); // render a thumbnail selection
this.listenTo(this.pdf_view.pdf_document.selections, 'remove', this.removeSelectionThumbnail); // remove a thumbnail selection
},
addSelectionThumbnail: function (new_selection){
if(this.thumbnail_views[new_selection.get('page_number')].$el.find('img').length){
this.thumbnail_views[new_selection.get('page_number')].createSelectionThumbnail(new_selection);
}
},
changeSelectionThumbnail: function (selection){
this.thumbnail_views[selection.get('page_number')].changeSelectionThumbnail(selection);
},
removeSelectionThumbnail: function (selection){
this.thumbnail_views[selection.get('page_number')].removeSelectionThumbnail(selection);
},
removeThumbnail: function(pageModel){
var thumbnail_view = this.thumbnail_views[pageModel.get('number')];
thumbnail_view.$el.fadeOut(200, function(){ thumbnail_view.remove(); });
},
render: function(){
if(!Tabula.LazyLoad){ // old-style, un-lazyload behavior where all pages are shown at once.
_(this.thumbnail_views).each(_.bind(function(thumbnail_view, index){
var already_on_page = $('#page-' + parseInt(index)+1).length;
if(!already_on_page) this.$el.append(thumbnail_view.render().el);
}, this));
}else{
for (var number=Tabula.pdf_view.lazyLoadCursor;number>0;number--){
var thumbnail_view = this.thumbnail_views[number];
if(!thumbnail_view) continue; // this is the first render, and there are no pages (or there's a problem!)
var thumb_el = $('#thumb-page-' + number);
var visible_on_page = thumb_el.filter(':visible').length;
if(visible_on_page && Tabula.HideOnLazyLoad){
if(Math.abs(Tabula.pdf_view.lazyLoadCursor - number) >= Tabula.LazyLoad ) {
$('#thumb-page-' + number).hide();
// console.log('hide', number)
}
}else{
if(Math.abs(Tabula.pdf_view.lazyLoadCursor - number) < Tabula.LazyLoad ) {
if(thumb_el.length){
thumbnail_view.$el.show();
// console.log('show ' + number);
}else{
this.$el.prepend(thumbnail_view.render().el);
// console.log('append ' + number);
}
}
}
}
for (var number=Tabula.pdf_view.lazyLoadCursor+1;number<=_(this.thumbnail_views).keys().length;number++){
var thumbnail_view = this.thumbnail_views[number];
if(!thumbnail_view) continue; // this is the first render, and there are no pages (probably!)
var thumb_el = $('#thumb-page-' + number);
var visible_on_page = thumb_el.filter(':visible').length;
if(visible_on_page && Tabula.HideOnLazyLoad){
if(! (Math.abs(Tabula.pdf_view.lazyLoadCursor - number) < Tabula.LazyLoad )) {
$('#thumb-page-' + number).hide();
// console.log('hide', number)
}
}else{
if(Math.abs(Tabula.pdf_view.lazyLoadCursor - number) < Tabula.LazyLoad ) {
if(thumb_el.length){
thumbnail_view.$el.show();
// console.log('show ' + number);
}else{
this.$el.append(thumbnail_view.render().el);
// console.log('append ' + number);
}
}
}
}
}
return this;
}
});
Tabula.ThumbnailView = Backbone.View.extend({ // one per page
'events': {
// on load, create an empty div with class 'selection-show' to be the selection thumbnail.
'load .thumbnail-list li img': function() { $(this).after($('<div />', { class: 'selection-show'})); },
'click i.delete-page': 'deletePage',
},
tagName: 'li',
className: "page-thumbnail page",
id: function(){
return 'thumb-page-' + this.model.get('number');
},
// initialize: function(){
// },
template: _.template($('#templates #thumbnail-template').html().replace(/nestedscript/g, 'script')),
initialize: function(){
_.bindAll(this, 'render', 'createSelectionThumbnail', 'changeSelectionThumbnail', 'removeSelectionThumbnail');
this.listenTo(Tabula.pdf_view.pdf_document, 'change', function(){ this.render(); });
this.listenTo(Tabula.pdf_view.pdf_document, 'change', function(){ this.render(); });
},
deletePage: function(){
if (!confirm('Delete page ' + this.model.get('number') + '?')) return;
Tabula.pdf_view.pdf_document.page_collection.remove( this.model );
},
render: function(){
this.$el.html(this.template({
'number': this.model.get('number'),
'image_url': this.model.imageUrl()