/
pert.js
executable file
·288 lines (238 loc) · 9.72 KB
/
pert.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Youama Pert Chrome App
*
* @version 1.0
* @link www.youama.com
* @live pert.youama.com
* @documentation www.youama.com/pert-chrome-app
* @author David Belicza
* @package jQuery/pertEvaluate
*/
(function($) {
$.fn.pertEvaluate = function(options) {
var lines = new Array();
var lineHtml = '';
var results = new Array();
var sequences = new Array();
var errors = false;
var stop = false;
var savedUrl = '';
return start();
function start() {
lineHtml = $('.base').html();
eventListeners();
if (getUrlVar("load") != null) {
autoLoader();
}
}
function reset() {
lines = new Array();
results = new Array();
sequences = new Array();
errors = false;
}
function eventListeners() {
$('.ok').click(function(){
if (stop != true) {
stop = true;
validate();
if (errors != true) {
reset();
hookValues();
calculate();
refreshLayout();
}
stop = false;
}
return false;
});
$('.pert').on('click', '.plus', function(){
addPlusLine();
});
$('.pert').on('click', '.minus', function(){
removePlusLine($(this));
});
$('.pert').on('keypress', '.float', function(event){
if ((event.which != 46 || $(this).val().indexOf(',') != -1)
&& (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
$('.pert').on('keypress', '.item-title', function(event){
if (event.which == 36 || //'$'
event.which == 38 || //'&'
event.which == 43 || //'+'
event.which == 46) { //'.'
event.preventDefault();
}
});
}
function validate() {
errors = false;
$('.float').each(function(){
if (errors == true) return;
currentValue = $(this).children('input').val();
if (currentValue == '') {
errors = true;
alert('Please fill out all fields or remove unused lines');
return;
}
if (currentValue == '0' || currentValue == 0) {
errors = true;
alert('Evaluate can not be zero');
return;
}
});
}
function hookValues() {
$('.pert .line').each(function(i) {
var currentValues = new Array();
currentValues['optimistic'] = parseFloat($(this)
.children('.optimistic').children('input').val()
.replace(/,/g, '.'));
currentValues['normal'] = parseFloat($(this)
.children('.normal').children('input').val()
.replace(/,/g, '.'));
currentValues['pessimistic'] = parseFloat($(this)
.children('.pessimistic').children('input').val()
.replace(/,/g, '.'));
lines[i] = currentValues;
});
}
function calculate() {
for (var i = 0; i < lines.length; i++) {
var resultsTemp = new Array();
resultsTemp['u'] = (lines[i]['optimistic']
+ (4 * lines[i]['normal']) + lines[i]['pessimistic']) / 6;
resultsTemp['o'] = (lines[i]['pessimistic']
- lines[i]['optimistic']) / 6;
results[i] = resultsTemp;
}
var uSequence = 0;
var oSequenceTemp = 0;
for (var i = 0; i < results.length; i++) {
uSequence = uSequence + results[i]['u'];
oSequenceTemp = oSequenceTemp + (results[i]['o']
* results[i]['o']);
}
var oSequence = Math.pow(oSequenceTemp, 0.5);
sequences['u'] = uSequence;
sequences['o'] = oSequence;
sequences['result'] = uSequence + oSequence;
autoSave();
}
function refreshLayout() {
$('.pert .line').each(function(i) {
$(this).children('.u').children('p').text(results[i]['u'].toFixed(1));
$(this).children('.o').children('p').text(results[i]['o'].toFixed(1));
});
$('.result .evaluation span').text(sequences['u'].toFixed(1));
$('.result .uncertainty span').text(sequences['o'].toFixed(1));
$('.result .finalresult .value').text(sequences['result'].toFixed(1) + ' hours');
$('body').animate({
scrollTop: $('.result').offset().top
}, {
queue: false,
duration: 2000,
easing: 'easeInOutCirc',
complete: function () {
$('.finalresult .value')
.effect('shake', {times:4}, 1000);
}
}
);
}
function addPlusLine() {
$('.line').last().after('<div class="line">' + lineHtml + '</div>');
$('body').delay(100).stop(true, true).animate({
scrollTop: $('.line').last().offset().top
}, {
queue: false,
duration: 1000,
easing: 'easeOutCirc'
}
);
}
function removePlusLine(removeDiv) {
removeDiv.parent('.fields').parent('.line').remove();
}
function autoSave() {
var title = '';
var optimistic = '';
var normal = '';
var pessimistic = '';
$('.pert .line').each(function() {
if (title.length != 0) title += '$';
var currentTitle = $(this).children('.item-title').children('input').val();
if (currentTitle == '' || currentTitle == null) currentTitle = '_';
title += encodeURIComponent(currentTitle);
if (optimistic.length != 0) optimistic += '$';
optimistic += $(this).children('.optimistic').children('input').val();
if (normal.length != 0) normal += '$';
normal += $(this).children('.normal').children('input').val();
if (pessimistic.length != 0) pessimistic += '$';
pessimistic += $(this).children('.pessimistic').children('input').val();
});
savedUrl = '?load=1&title=' + title + '&opt=' + optimistic + '&nor='
+ normal + '&pes=' + pessimistic;
window.history.pushState(null, "", savedUrl);
$('.pert .description p a').attr('href', 'http://polaris-evaluation-calculator.youama.com/' + savedUrl);
$('.pert .description p a').fadeIn();
}
//http://localhost/cloud/pert/?load=1&title=Els%C5%91$M%C3%A1sodik&opt=5.5$1&pes=2$3&nor=9$4
function autoLoader() {
var items = new Array();
var title = getUrlVar('title');
if (title != null) {
var titles = new Array();
titles = title.split('$');
items['titles'] = titles;
titles = null;
}
var pes = getUrlVar('pes');
var opt = getUrlVar('opt');
var nor = getUrlVar('nor');
if (pes != null && opt != null &&
nor != null) {
var pess = new Array();
var opts = new Array();
var nors = new Array();
pess = pes.split('$');
items['pess'] = pess;
opts = opt.split('$');
items['opts'] = opts;
nors = nor.split('$');
items['nors'] = nors;
var pess = null;
var opts = null;
var nors = null;
}
for (var i in items['nors']) {
if (i != 0) $('.base .plus').trigger('click');
$('.pert .line').last().children('.item-title')
.children('input').val(decodeURIComponent(items['titles'][i]));
$('.pert .line').last().children('.optimistic')
.children('input').val(items['opts'][i]);
$('.pert .line').last().children('.normal')
.children('input').val(items['nors'][i]);
$('.pert .line').last().children('.pessimistic')
.children('input').val(items['pess'][i]);
}
$('.ok-wrap .ok').trigger('click');
}
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function getUrlVar(name){
return getUrlVars()[name];
}
}
})(jQuery);