Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save and load templates, with tabula-java-1.0.0 #711

Merged
merged 24 commits into from
Aug 10, 2017
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
610233a
Add save and load
cheapsteak May 4, 2017
b7defcb
Merge branch 'master' of git://github.com/cheapsteak/tabula into chea…
jeremybmerrill Jul 8, 2017
fc4d5b2
you can upload templates and they're displayed on the front page
jeremybmerrill Jul 8, 2017
c569c59
Merge branch 'cheapsteak-master' into feature/templates
jeremybmerrill Jul 8, 2017
85d0704
can save selections as a template from selection page
jeremybmerrill Jul 10, 2017
e4efed3
you can delete templates
jeremybmerrill Jul 10, 2017
9a02427
you can load templates (but it's all ugly)
Jul 11, 2017
9925c43
you can rename templates on the front page
jeremybmerrill Jul 14, 2017
7f003bf
you can download templates
jeremybmerrill Jul 14, 2017
6e18a4c
templates are in the right format now; also they're validated
jeremybmerrill Jul 14, 2017
20746e3
fixes template upload
jeremybmerrill Jul 16, 2017
f36d3ba
moves template library to its own page
jeremybmerrill Aug 8, 2017
343acb2
Merge branch 'tabula-java-1.0.0' into feature/templates
jeremybmerrill Aug 8, 2017
2b4da47
Merge branch 'tabula-java-1.0.0' into feature/templates
jeremybmerrill Aug 9, 2017
954cc8c
refactors workspace to work with the new Tabula::Workspace thingy Man…
jeremybmerrill Aug 9, 2017
4756734
convert old workspaces to the the new kind; deactivate load template …
jeremybmerrill Aug 10, 2017
574272d
rename a variable for clarity
jeremybmerrill Aug 10, 2017
533f410
endpoint for getting just the list of documents
jeremybmerrill Aug 10, 2017
d9422e1
remove puts
jazzido Aug 10, 2017
2f44389
remove a puts
jeremybmerrill Aug 10, 2017
e9fd394
moves create_template method inline, since it's used only once
jeremybmerrill Aug 10, 2017
160651e
remove some console logs
jeremybmerrill Aug 10, 2017
7b83be4
try to convince Travis to work
jeremybmerrill Aug 10, 2017
e49ac4f
fiddle with Travis again
jeremybmerrill Aug 10, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add save and load
  • Loading branch information
cheapsteak committed May 4, 2017
commit 610233a116d63dd09489db6c0d787a98adbfff3b
94 changes: 89 additions & 5 deletions webapp/static/js/pdf_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ Tabula.DocumentView = Backbone.View.extend({ // Singleton
},

initialize: function(stuff){
_.bindAll(this, 'render', 'removePage', '_onRectangularSelectorEnd', '_selectionsGetter');
_.bindAll(this, 'render', 'removePage', 'addSelection', '_onRectangularSelectorEnd', '_selectionsGetter');
this.pdf_view = stuff.pdf_view;
this.listenTo(this.collection, 'remove', this.removePage);

Expand All @@ -590,9 +590,8 @@ Tabula.DocumentView = Backbone.View.extend({ // Singleton
);
},

// listens to mouseup of RectangularSelector
_onRectangularSelectorEnd: function(d) {
var page_number = $(d.pageView).data('page');
addSelection: function (d) {
var page_number = $(d.pageView).data('page') || d.pageNumber;
var pv = this.page_views[page_number];
var rs = new ResizableSelection({
position: d.absolutePos,
Expand All @@ -608,6 +607,11 @@ Tabula.DocumentView = Backbone.View.extend({ // Singleton
rs.$el.css('z-index', 100 - this._selectionsGetter($(d.pageView)).length);
},

// listens to mouseup of RectangularSelector
_onRectangularSelectorEnd: function(d) {
this.addSelection(d);
},

removePage: function(pageModel){
var page_view = this.page_views[pageModel.get('number')];

Expand Down Expand Up @@ -1036,7 +1040,9 @@ Tabula.PDFView = Backbone.View.extend(

initialize: function(){
_.bindAll(this, 'render', 'addOne', 'addAll', 'totalSelections', 'renderSelection',
'createDataView', 'checkForAutodetectedTables', 'getData', 'handleScroll');
'createDataView', 'checkForAutodetectedTables', 'getData', 'handleScroll',
'serializeSelections', 'loadSerializedSelections', 'getSavedStates',
'save', 'load', 'saveAs');

this.pdf_document = new Tabula.Document({
pdf_id: PDF_ID,
Expand Down Expand Up @@ -1241,6 +1247,84 @@ Tabula.PDFView = Backbone.View.extend(
console.log(this.pdf_document.selections);
},

serializeSelections: function () {
return this.pdf_document.selections.map(function (x) {
return _.extend(
{ pageNumber: x.attributes.page_number },
_.omit(x.attributes.getDims(), '$el'))
});
},

loadSerializedSelections: function (serializedSelections) {
serializedSelections.forEach(this.components.document_view.addSelection);
},

getSavedStates: function () {
return JSON.parse(localStorage.getItem('savedStates')) || [];
},

saveAs: function (state, options) {
var shouldOverwrite = options.shouldOverwrite;
var savedStates = this.getSavedStates();

var windowDimensions = {
width: window.innerWidth,
height: window.innerHeight,
};

var conflictingSavedState = _.findWhere(savedStates, {id: state.id});

if (conflictingSavedState && !shouldOverwrite) {
console.error('Failed to save selection due to conflicting id. "' + state.id + '" already exists. Pass in `save("' + state.id +'", "' + state.name + '", { shouldOverride: true })` to overwrite');
return;
}

var stateToSave = _.defaults(state, {
windowDimensions: windowDimensions,
dateSaved: new Date().toISOString(),
selections: this.serializeSelections(),
});

var statesToSave = _.without(savedStates, conflictingSavedState).concat(stateToSave);
localStorage.setItem('savedStates', JSON.stringify(statesToSave));
},

save: function () {
var stateToSave = {
id: (this.loadedSavedState && this.loadedSavedState.id) || _.max(_.pluck(this.getSavedStates(), 'id').concat(0)) + 1,
name: (this.loadedSavedState && this.loadedSavedState.name) || this.pdf_document.attributes.original_filename || this.pdf_document.attributes.original_filename,
selections: this.serializeSelections(),
};

this.saveAs(stateToSave, { shouldOverwrite: true });
},

load: function (id) {
if (!id) {
console.error('Missing id of stored save to load');
return;
}
var saveToLoad = _.findWhere(this.getSavedStates(), { id: id });
console.log('', saveToLoad);
if (window.innerWidth !== saveToLoad.windowDimensions.width || window.innerHeight !== saveToLoad.windowDimensions.height) {
console.warn('Warning: These selections were saved with window dimensions of width: '
+ saveToLoad.windowDimensions.width + ', height: ' + saveToLoad.windowDimensions.height
+ ', which are different from your current window dimensions of width: ' + window.innerWidth +' , height: ' + window.innerHeight);
}
this.loadSerializedSelections(saveToLoad.selections);
this.loadedSavedState = saveToLoad;
},

delete: function (id) {
if (!id) {
console.error('An id is required');
return;
}
var savedStates = this.getSavedStates();
var stateToDelete = _.findWhere(savedStates, {id: id});
localStorage.setItem('savedStates', JSON.stringify(_.without(savedStates, stateToDelete)));
},

render : function(){
document.title="Select Tables | Tabula";
this.components['document_view'].render();
Expand Down