Skip to content

Commit

Permalink
Merge pull request ome#188 from waxenegger/deletion_count
Browse files Browse the repository at this point in the history
bug: don't count new but instantly deleted
  • Loading branch information
jburel authored Mar 26, 2018
2 parents 6513d2a + da014b4 commit 4a4afec
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 20 deletions.
94 changes: 78 additions & 16 deletions src/model/regions_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export default class RegionsHistory {
*/
historyPointer = -1;

/**
* @see checkIfHistoryHasOnlyNewlyDeleted
* @memberof History
* @type {boolean}
*/
hasOnlyNewlyDeleted = true;

/**
* a reference to RegionsInfo
* @memberof History
Expand Down Expand Up @@ -161,23 +168,23 @@ export default class RegionsHistory {
this.history.length-this.historyPointer, opts);
this.historyPointer++;
}

this.checkIfHistoryHasOnlyNewlyDeleted();
}

/**
* Undoes the last action
* @param {boolean} remove delete history entry after action (default: false)
* @memberof History
*/
undoHistory(remove=false) {
undoHistory() {
if (!this.canUndo()) return;

let entry = this.history[this.historyPointer];
// converge
this.doHistory(entry, true);
if (typeof remove === 'boolean' && remove)
this.history.splice(this.historyPointer, 1);
//adjust pointer
this.historyPointer--;
this.checkIfHistoryHasOnlyNewlyDeleted();
}

/**
Expand All @@ -192,6 +199,7 @@ export default class RegionsHistory {
this.doHistory(entry, false);
//adjust pointer
this.historyPointer++;
this.checkIfHistoryHasOnlyNewlyDeleted();
}

/**
Expand Down Expand Up @@ -277,35 +285,89 @@ export default class RegionsHistory {
}

/**
* @return {boolean} true if we are not at the end of the history
* @memberof History
*/
* Sets the hasOnlyNewlyDeleted flag if we are at the beginning
* of the history or the history contains deletes of new shapes only
* @memberof History
*/
checkIfHistoryHasOnlyNewlyDeleted() {
this.hasOnlyNewlyDeleted = true;
if (this.historyPointer < 0) return;
let new_shapes = new Map();
for (let i=0;i<=this.historyPointer;i++) {
let entry = this.history[i];
if (!Misc.isArray(entry.records)) continue;
for (let r in entry.records) {
let rec = entry.records[r];
switch(entry.action) {
// ol3 actions constitute change unless for new shapes
case this.action.OL_ACTION:
for (let s in rec.shape_ids)
if (new_shapes.get(rec.shape_ids[s]) !== null) {
this.hasOnlyNewlyDeleted = false;
return;
}
break;
// property actions constitute change unless for new shapes
case this.action.PROPERTIES:
if (new_shapes.get(rec.shape_id) !== null) {
this.hasOnlyNewlyDeleted = false;
return;
}
break;
default:
if (!Misc.isArray(rec.diffs)) break;
// deletes are changes unless for new shapes
for (let d in rec.diffs) {
let shape_id = rec.diffs[d].shape_id;
let is_new = typeof rec.diffs[d]['is_new'] === 'boolean';
if (!rec.new_vals) {
if (is_new) new_shapes.delete(shape_id);
else {
this.hasOnlyNewlyDeleted = false;
return;
}
} else if (is_new) new_shapes.set(shape_id, null);
}
}
}
}
// if we have come that far, there were no changes
// other than to newly created shapes
// should they all have been deleted we set the flag accordingly
this.hasOnlyNewlyDeleted = new_shapes.size === 0;
}

/**
* @return {boolean} true if we are not at the end of the history
* @memberof History
*/
canRedo() {
return this.hasHistory() && this.historyPointer < this.history.length-1;
}

/**
* @return {boolean} true if we are not at the beginning of the history
* @memberof History
*/
* @return {boolean} true if we are not at the beginning of the history
* @memberof History
*/
canUndo() {
return this.hasHistory() && this.historyPointer >= 0;
}

/**
* @return {boolean} true if we have at least one record, false otherwise
* @memberof History
*/
* @return {boolean} true if we have at least one record, false otherwise
* @memberof History
*/
hasHistory() {
return this.history.length > 0;
}

/**
* Resets history
* @memberof History
*/
* Resets history
* @memberof History
*/
resetHistory() {
this.history = [];
this.hasOnlyNewlyDeleted = true;
this.historyPointer = -1;
}
}
7 changes: 5 additions & 2 deletions src/model/regions_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ export default class RegionsInfo {
* @return {boolean} true if shapes have been modified, otherwise false
*/
hasBeenModified() {
return this.history instanceof RegionsHistory && this.history.canUndo();
return this.history instanceof RegionsHistory &&
this.history.historyPointer >= 0 &&
!this.history.hasOnlyNewlyDeleted;
}

/**
Expand Down Expand Up @@ -559,7 +561,8 @@ export default class RegionsInfo {
*/
getNumberOfDeletedShapes() {
return this.unsophisticatedShapeFilter(
["deleted"], [true], ['canDelete']).length;
["deleted", "is_new"], [true, "false"],
['canDelete', 'canDelete']).length;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/regions/regions-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export default class RegionsList extends EventSubscriber {
* @memberof RegionsList
*/
hasPermissions(shape) {
return !shape.permissions ||
return !(shape && shape.permissions) ||
(shape.permissions &&
shape.permissions.canAnnotate &&
shape.permissions.canEdit &&
Expand Down
3 changes: 2 additions & 1 deletion src/regions/regions.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
<button type="button" class="btn btn-default btn-sm"
disabled.bind="!(regions_info.history &&
regions_info.history.history.length > 0 &&
regions_info.history.historyPointer >= 0)"
regions_info.history.historyPointer >= 0 &&
!regions_info.history.hasOnlyNewlyDeleted)"
click.delegate="saveShapes()">Save
</button>
<button type="button"
Expand Down

0 comments on commit 4a4afec

Please sign in to comment.