Skip to content
This repository has been archived by the owner on Jun 16, 2018. It is now read-only.

Commit

Permalink
scores: Allow editing score entry when team number is invalid, perfor…
Browse files Browse the repository at this point in the history
…m better sanitization in $scores.update(), fixes #105.
  • Loading branch information
poelstra committed Nov 26, 2014
1 parent 8ad6064 commit 671b6fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
1 change: 1 addition & 0 deletions spec/services/ng-scoresSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ describe('ng-scores',function() {
$scores.update(0, mockScore);
expect($scores.scores[0].score).toEqual(151);
expect($scores.scores[0].modified).toBeTruthy();
expect($scores.scores[0].edited).toBeTruthy();
});
});

Expand Down
41 changes: 23 additions & 18 deletions src/js/services/ng-scores.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,26 @@ define('services/ng-scores',[
this._update();
};

function sanitize(score) {
// Convert 'dirty' input score to a representation that we can store
// on a filesystem. This means e.g. not storing denormalized version of
// team and stage, but only their ID's. Additionally, forces values to be
// of the right type where possible.
return {
file: (score.file !== undefined && score.file !== null) ? String(score.file) : "",
teamNumber: parseInt((score.teamNumber !== undefined) ? score.teamNumber : score.team.number, 10),
stageId: String((score.stageId !== undefined) ? score.stageId : score.stage.id),
round: parseInt(score.round, 10),
score: score.score, // can be Number, null, "dnc", etc.
originalScore: parseInt(score.originalScore !== undefined ? score.originalScore : score.score, 10),
edited: score.edited !== undefined ? String(score.edited) : undefined // timestamp, e.g. "Wed Nov 26 2014 21:11:43 GMT+0100 (CET)"
};
}

Scores.prototype.add = function(score) {
// Create a copy of the score, in case the
// original score is being modified...
this._rawScores.push({
file: score.file,
teamNumber: parseInt((score.teamNumber !== undefined) ? score.teamNumber : score.team.number, 10),
stageId: (score.stageId !== undefined) ? score.stageId : score.stage.id,
round: score.round,
score: score.score,
originalScore: score.originalScore !== undefined ? score.originalScore : score.score
});
this._rawScores.push(sanitize(score));
this._update();
};

Expand All @@ -250,18 +259,12 @@ define('services/ng-scores',[
* the score as modified.
*/
Scores.prototype.update = function(index, score) {
var old = this._rawScores[index];
if (!old) {
if (index < 0 || index >= this._rawScores.length) {
throw new RangeError("unknown score index: " + index);
}
// Note: we leave eg. originalScore intact, so _update() will
// mark it as modified.
old.file = score.file;
old.teamNumber = parseInt((score.teamNumber !== undefined) ? score.teamNumber : score.team.number, 10);
old.stageId = (score.stageId !== undefined) ? score.stageId : score.stage.id;
old.round = score.round;
old.score = score.score;
old.edited = (new Date()).toString();
var newScore = sanitize(score);
newScore.edited = (new Date()).toString();
this._rawScores.splice(index, 1, newScore);
this._update();
};

Expand Down Expand Up @@ -380,11 +383,13 @@ define('services/ng-scores',[
// additional info
var s = {
file: _score.file,
teamNumber: _score.teamNumber,
team: $teams.get(_score.teamNumber),
stage: $stages.get(_score.stageId),
round: _score.round,
score: _score.score,
originalScore: _score.originalScore,
edited: _score.edited,
modified: false,
error: null
};
Expand Down
19 changes: 9 additions & 10 deletions src/js/views/scores.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,21 @@ define('views/scores',[
};
$scope.editScore = function(index) {
var score = $scores.scores[index];
score.teamNumber = score.team.number;
score.$editing = true;
};

$scope.finishEditScore = function(index) {
// The score entry is edited 'inline', then used to
// replace the entry in the scores list and its storage.
// Because scores are always 'sanitized' before storing,
// the $editing flag is automatically discarded.
var score = $scores.scores[index];
score.team = $teams.get(score.teamNumber);
if (!score.team) {
alert('Team number not found');
return;
try {
$scores.update(score.index, score);
$scores.save();
} catch(e) {
alert("Error updating score: " + e);
}
score.round = parseInt(score.round,10);
score.score = parseInt(score.score,10);
delete score.$editing;
$scores.update(score.index,score);
$scores.save();
};

$scope.cancelEditScore = function() {
Expand Down

0 comments on commit 671b6fb

Please sign in to comment.