-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimate.js
184 lines (160 loc) · 6.4 KB
/
estimate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* PERT project estimation.
*
* https://github.com/linuxswords/pert-estimation
*
*/
var Estimate = {
totsignature : 'μ<sub>tot</sub>/σ<sub>tot</sub>',
signature : ' (μ/σ)',
taskcounter : 1,
localStorageKey : 'estimate-data',
addTaskTo : function(row) {
var $task = $('.cloner > li.task').clone(true);
var number = Estimate.taskcounter++;
$task.find('input').each(function (i, elem) {
var name = $(elem).attr('name');
$(elem).attr('name', name.replace('-x', '-' + number));
});
row = row || $('form').find('li:last');
$task.insertAfter(row);
$task.children('input:text').val('');
Estimate.storeTaskCounter();
},
storeTaskCounter : function() {
// console.log(Estimate.taskcounter);
localStorage.setItem(Estimate.localStorageKey, '' + Estimate.taskcounter);
},
addTask : function(event) {
var source = $(event.target).closest('li');
Estimate.addTaskTo(source);
source.next('li.task').find('input.description').select();
},
isNumber : function(candidate) {
return !isNaN(parseInt(candidate));
},
popOutCSV : function() {
var title = $('input.title').val() || 'no title';
var data = title + '\ndescription,O,N,P,duration,deviation\n';
$('.tasks ul li.task').each(
function () {
$(this).find('input').each(function () {
data += $(this).val() + ',';
});
var mu = $(this).find('span.mu').html();
data += (mu || '') + ',';
var sigma = $(this).find('span.sigma').html();
data += sigma || '';
data += '\n';
});
window.location.href = 'data:text/csv;charset=utf8,' + encodeURIComponent(data);
},
updateNumbers : function(){
var mu_tot = 0;
var sigma_tot = 0;
$('.tasks input[type=number]').each(function () {
Estimate.validate($(this));
});
$('.tasks ul li.task').each(
function () {
var opt = $(this).children('input.optimus').val();
var norm = $(this).children('input.nominal').val();
var worst = $(this).children('input.worst').val();
if (Estimate.isNumber(opt) && Estimate.isNumber(norm) && Estimate.isNumber(worst)) {
var mu = (parseFloat(opt) + 4 * parseFloat(norm) + parseFloat(worst)) / 6.0;
var sigma = (parseFloat(worst) - parseFloat(opt)) / 6.0;
$(this).find('.sum').remove();
$('<span class="sum">' + Estimate.signature +
' <span class="mu">' + mu.toFixed(2) +
'</span>/<span class="sigma">' + sigma.toFixed(2) +
'</span></span>').insertAfter($(this).find('input.worst'));
mu_tot += parseFloat(mu);
sigma_tot += sigma * sigma;
}
});
$('.summary').empty().append($('<span class="totsum">' +
Estimate.totsignature + '<br />' + mu_tot.toFixed(2) +
'/' + Math.sqrt(sigma_tot).toFixed(2) + '</span>'));
},
validate : function(input) {
if (Estimate.isNumber(input.val())) {
input.removeClass('error');
}
else {
input.addClass('error');
}
},
init : function() {
$('input[type=number]').live('keyup', function () {
Estimate.updateNumbers();
});
$('img.add').live('click', function (event) {
event.stopImmediatePropagation();
Estimate.addTaskTo($(this).closest('li'));
});
$('img.delete').live('click', function () {
if ($('.tasks ul li.task').size() > 1) {
$(this).closest('li').remove();
Estimate.taskcounter--;
Estimate.storeTaskCounter();
Estimate.updateNumbers();
}
});
$('img.clear').click(
function () {
$('.tasks ul li.task').slice(1).remove();
$('.tasks ul li.task input.optimus').removeClass('error').val('0');
$('.tasks ul li.task input.nominal').removeClass('error').val('0');
$('.tasks ul li.task input.worst').removeClass('error').val('0');
$('.tasks ul li.task input.description').val('description');
$('input:first').select();
Estimate.taskcounter = 1;
Estimate.storeTaskCounter();
});
$('img.download').click(function() { Estimate.popOutCSV(); });
$('input').live('click', function () {
$(this).select();
}).bind('keydown', function (event) {
if (event.keyCode == 13 && event.target == this && event.shiftKey) {
var source = $(this).closest('li');
Estimate.addTaskTo(source);
source.find('+ li').find('input:first').select();
}
var inputIndex = $(this).closest('li.task').find('input').index($(this));
// arrow down
if (event.keyCode == 40) {
// if we are on the last row
if ($(this).closest('li').index() == $("div.tasks li.task:last").index()) {
Estimate.addTask(event);
}
// else navigate down
else {
$(this).closest('li').next('li.task').find('input').eq(inputIndex).select();
}
}
// arrow up
if (event.keyCode == 38) {
// case new row
// case navigate down
$(this).closest('li').prev('li.task').find('input').eq(inputIndex).select();
}
});
$('.catcher').click(
function () {
$('.explanation').slideToggle('slow');
});
$('.tasks ul li.task input:first').select();
$('[title]').tooltip();
$('form').sisyphus({
onBeforeRestore: Estimate.restoreDynamicRows
});
},// end init
restoreDynamicRows : function() {
var numberOfRows = localStorage.getItem(Estimate.localStorageKey);
if (numberOfRows > 1) {
for (var i = 2; i <= numberOfRows; i++) {
Estimate.addTaskTo();
}
}
}
};