Skip to content

Commit

Permalink
[REFACTOR] Switching to OOP
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlutzenberger committed Dec 4, 2016
1 parent db7e143 commit 4db47e4
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 82 deletions.
204 changes: 124 additions & 80 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Time Estimation v0.3
* Time Estimation v0.4
* Calculate your tasks with PERT
*
* Copyright (c) 2016 - Tom Lutzenberger (lutzenbergerthomas at gmail dot com)
Expand All @@ -12,148 +12,192 @@

/*globals $,document,console*/

function isValidValue(value) {
function TimeEstimation() {

'use strict';

var isValid = false;
this.n = null;
this.o = null;
this.p = null;
this.title = null;
this.resultMu = null;
this.resultSigma = null;
}

if (value > 0) {
isValid = true;

} else {
console.warn('Given value "' + value + '" is not a valid number.');
isValid = false;
}

return isValid;
}
TimeEstimation.prototype = {

constructor: TimeEstimation,


function isFormComplete() {

'use strict';
isValidValue: function (value) {

if ($('.te-n').val() !== '' && $('.te-o').val() !== '' && $('.te-p').val() !== '') {
return true;
} else {
return false;
}
}
'use strict';

var isValid = false;

if (value !== '' && value > 0) {
isValid = true;
} else {
console.warn('Given value "' + value + '" is not a valid number.');
isValid = false;
}

function getValues() {
return isValid;
},

'use strict';

return {
title: $('.te-title').val(),
n: parseFloat($('.te-n').val()),
o: parseFloat($('.te-o').val()),
p: parseFloat($('.te-p').val())
};
}

isFormComplete: function () {

'use strict';

function updateUrlParams() {
if (this.isValidValue(this.o) && this.isValidValue(this.n) && this.isValidValue(this.p)) {
return true;
} else {
return false;
}
},

'use strict';

var
values = getValues(),
urlParams = '?title=' + encodeURIComponent(values.title) + '&o=' + values.o + '&n=' + values.n + '&p=' + values.p;

history.pushState(null, null, urlParams);
}
getValues: function () {

'use strict';

this.title = $('.te-title').val();
this.n = parseFloat($('.te-n').val());
this.o = parseFloat($('.te-o').val());
this.p = parseFloat($('.te-p').val());
},

function getUrlParams() {

'use strict';

var
regex = new RegExp('\\?(.*)', 'i'),
urlParams = regex.exec(location.search),
params = [],
p = null,
param = null;
setValues: function () {

'use strict';

$('.te-title').val(this.title);
$('.te-n').val(this.n);
$('.te-o').val(this.o);
$('.te-p').val(this.p);
},


if (urlParams !== null && urlParams.length > 1) {
params = urlParams[1].split('&');

for (p in params) {
if (params.hasOwnProperty(p)) {
param = params[p].split('=');
parseUrlParams: function () {

'use strict';

var
urlPattern = new RegExp('\\?(.*)', 'i'),
urlParamString = urlPattern.exec(location.search);

if (urlParamString !== null && urlParamString.length > 1) {
return urlParamString[1].split('&');
} else {
return false;
}
},

$('.te-' + param[0].toLowerCase()).val(decodeURIComponent(param[1]));


getUrlParams: function () {

'use strict';

var
params = this.parseUrlParams(),
p = null,
param = null;

if (params !== false) {
for (p in params) {
if (params.hasOwnProperty(p)) {

param = params[p].split('=');
this[param[0].toLowerCase()] = decodeURIComponent(param[1]);
}
}

this.setValues();

return true;

} else {
console.info('No URL parameters to read');
return false;
}
},

return true;

} else {

console.info('No URL parameters to read');
return false;
setUrlParams: function () {

}
}
'use strict';

var
paramString = '?title=' + encodeURIComponent(this.title) + '&o=' + this.o + '&n=' + this.n + '&p=' + this.p;

history.pushState(null, null, paramString);
},

function resetResult() {

'use strict';

$('.result-mu').text('');
$('.result-sigma').text('');
}
calculateResult: function () {

'use strict';

this.resultMu = (this.o + (4 * this.n) + this.p) / 6;
this.resultSigma = (this.p - this.o) / 6;
},

function calculateResult() {

'use strict';

var
values = getValues(),
resultMu = null,
resultSigma = null;
setResult: function () {

'use strict';

$('.result-mu').text(this.resultMu.toFixed(2));
$('.result-sigma').text(this.resultSigma.toFixed(2));
},

if (isValidValue(values.o) && isValidValue(values.n) && isValidValue(values.p)) {
resultMu = (values.o + (4 * values.n) + values.p) / 6;
resultSigma = (values.p - values.o) / 6;

$('.result-mu').text(resultMu.toFixed(2));
$('.result-sigma').text(resultSigma.toFixed(2));

} else {
resetResult();
unsetResult: function () {

'use strict';

$('.result-mu').text('');
$('.result-sigma').text('');
}
}
};



$(document).ready(function () {

'use strict';

if (getUrlParams()) {
calculateResult();
var te = new TimeEstimation();

if (te.getUrlParams()) {
te.calculateResult();
}

$('.te').on('change', function () {

if (isFormComplete()) {
calculateResult();
updateUrlParams();
te.getValues();

if (te.isFormComplete()) {
te.calculateResult();
te.setResult();
te.setUrlParams();

} else {
resetResult();
te.unsetResult();
}
});
});
4 changes: 2 additions & 2 deletions js/main.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4db47e4

Please sign in to comment.