Skip to content

Commit

Permalink
Editing, validating and splitting up the time
Browse files Browse the repository at this point in the history
  • Loading branch information
benjimouse committed Dec 16, 2014
1 parent 90bcc5f commit c3474cd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 20 deletions.
20 changes: 16 additions & 4 deletions client/templates/steps/step_edit.html
@@ -1,11 +1,23 @@
<template name="stepEdit">
<form class="main form">
<div class="form-group" {{errorClass 'durationInSeconds'}}>
<label class="control-label" for="durationInSeconds">Duration in seconds</label>
<fieldset class="form-group {{errorClass 'durationInSeconds'}}">
<label class="control-label" for="hours">Hours</label>
<div class="controls">
<input name="durationInSeconds" id="durationInSeconds" type="number" value="{{durationInSeconds}}" placeholder="Duration in seconds" class="form-control" /><span class="help-block">{{errorMessage 'durationInSeconds'}}</span>
<input name="hours" id="hours" type="number" value="{{hours}}" placeholder="hours" class="form-control" />

</div>
</div>
<label class="control-label" for="minutes">Minutes</label>
<div class="controls">
<input name="minutes" id="minutes" type="number" value="{{minutes}}" placeholder="minutes" class="form-control" />

</div>
<label class="control-label" for="seconds">Seconds</label>
<div class="controls">
<input name="seconds" id="seconds" type="number" value="{{seconds}}" placeholder="seconds" class="form-control" />

</div>
<span class="help-block">{{errorMessage 'durationInSeconds'}}</span>
</fieldset>
<div class="form-group {{errorClass 'description'}}">
<label class="control-label" for="description">Description</label>
<div class="controls">
Expand Down
28 changes: 25 additions & 3 deletions client/templates/steps/step_edit.js
Expand Up @@ -3,25 +3,47 @@ Template.stepEdit.created = function () {
Session.set('stepEditErrors', {});
};


Template.stepEdit.helpers({

errorMessage: function (field) {
'use strict';
return Session.get('stepEditErrors')[field];
},
errorClass: function (field) {
'use strict';
return !!Session.get('stepEditErrors')[field] ? 'has-error' : '';
},
seconds: function () {
'use strict';
var time = new Time().createTimeFromSeconds(this.durationInSeconds);
return time.seconds;
},
minutes: function () {
'use strict';
var time = new Time().createTimeFromSeconds(this.durationInSeconds);
return time.minutes;
},
hours: function () {
'use strict';
var time = new Time().createTimeFromSeconds(this.durationInSeconds);
return time.hours;
}
});

Template.stepEdit.events({
'submit form': function (e) {
'use strict';
e.preventDefault();

var currentStepId = this._id,
var
seconds = parseInt($(e.target).find('[name=seconds]').val(), 10),
minutes = parseInt($(e.target).find('[name=minutes]').val(), 10),
hours = parseInt($(e.target).find('[name=hours]').val(), 10),
time = new Time().createTimeFromHoursMinutesSeconds(hours, minutes, seconds),
durationInSeconds = time.durationInSeconds,
currentStepId = this._id,
stepProperties = {
durationInSeconds: $(e.target).find('[name=durationInSeconds]').val(),
durationInSeconds: durationInSeconds,
description: $(e.target).find('[name=description]').val()
},
errors = validateStep(stepProperties);
Expand Down
21 changes: 16 additions & 5 deletions client/templates/steps/step_submit.html
@@ -1,12 +1,23 @@
<template name="stepSubmit">
<form class="main form">
<div class="form-group {{errorClass 'durationInSeconds'}}">
<label class="control-label" for="durationInSeconds">Duration in seconds</label>
<fieldset class="form-group {{errorClass 'durationInSeconds'}}">
<label class="control-label" for="hours">Hours</label>
<div class="controls">
<input name="durationInSeconds" id="durationInSeconds" type="number" value="" placeholder="Duration in seconds" class="form-control" />
<span class="help-block">{{errorMessage 'durationInSeconds'}}</span>
<input name="hours" id="hours" type="number" value="" placeholder="hours" class="form-control" />

</div>
</div>
<label class="control-label" for="minutes">Minutes</label>
<div class="controls">
<input name="minutes" id="minutes" type="number" value="" placeholder="minutes" class="form-control" />

</div>
<label class="control-label" for="seconds">Seconds</label>
<div class="controls">
<input name="seconds" id="seconds" type="number" value="" placeholder="seconds" class="form-control" />

</div>
<span class="help-block">{{errorMessage 'durationInSeconds'}}</span>
</fieldset>
<div class="form-group {{errorClass 'description'}}">
<label class="control-label" for="description">Description</label>
<div class="controls">
Expand Down
12 changes: 9 additions & 3 deletions client/templates/steps/step_submit.js
Expand Up @@ -2,9 +2,14 @@ Template.stepSubmit.events({
'submit form': function (e) {
'use strict';
e.preventDefault();

var step = {
durationInSeconds: parseInt($(e.target).find('[name=durationInSeconds]').val()),
var
seconds = parseInt($(e.target).find('[name=seconds]').val(), 10),
minutes = parseInt($(e.target).find('[name=minutes]').val(), 10),
hours = parseInt($(e.target).find('[name=hours]').val(), 10),
time = new Time().createTimeFromHoursMinutesSeconds(hours, minutes, seconds),
durationInSeconds = time.durationInSeconds,
step = {
durationInSeconds: durationInSeconds,
description: $(e.target).find('[name=description]').val()
},
errors = validateStep(step);
Expand Down Expand Up @@ -38,6 +43,7 @@ Template.stepSubmit.helpers({
return Session.get('stepSubmitErrors')[field];
},
errorClass: function (field) {
'use strict';
return !!Session.get('stepSubmitErrors')[field] ? 'has-error' : '';
}
});
17 changes: 12 additions & 5 deletions lib/helper/time.js
Expand Up @@ -5,6 +5,16 @@ Time = function () {
minutes = 0,
seconds = 0,
durationInSeconds = 0;
this.createTimeFromHoursMinutesSeconds = function (hours, minutes, seconds) {
var self = this,
totalSeconds;
hours = isNaN(hours) ? 0 : hours;
minutes = isNaN(minutes) ? 0 : minutes;
seconds = isNaN(seconds) ? 0 : seconds;
totalSeconds = (hours * 60 * 60) + (minutes * 60) + seconds;

return self.createTimeFromSeconds(totalSeconds);
};
this.createTimeFromSteps = function (durationSteps) {
var totalSeconds = 0,
self = this;
Expand All @@ -18,10 +28,7 @@ Time = function () {
self.durationInSeconds = durationInSeconds;
self.secondsToHoursMinutesSeconds();
return self;

};
// TODO: Make this a private method,
// tried but everything blew up...
this.secondsToHoursMinutesSeconds = function () {
var placeHolderSeconds;
placeHolderSeconds = this.durationInSeconds;
Expand All @@ -42,10 +49,10 @@ Time = function () {
return displayTime.trim();
};
this.addTimePartForDisplay = function (timePart, timeDescription) {
if (timePart > 1) {
if (timePart === 1) {
return timePart + " " + timeDescription;
}
if (timePart === 1) {
if (timePart > 1) {
return timePart + " " + timeDescription + 's';
}
return '';
Expand Down

0 comments on commit c3474cd

Please sign in to comment.