-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
438 lines (380 loc) · 14.8 KB
/
index.html
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ortasks</title>
</head>
<style>
*{
margin: 0;
padding: 0;
color: #343434;
}
body{
background: #f1f1f1;
}
a{
cursor: default;
text-decoration: none;
}
#container{
border-radius: 1em;
background: #f1f1f1;
box-shadow: 20px 20px 60px #cdcdcd, -20px -20px 60px #ffffff;
padding: 1em;
margin: 2em;
}
.btn{
cursor: default;
border-radius: 0.4em;
background: #f1f1f1;
box-shadow: 4px 2px 5px #cdcdcd, 0px -4px 6px #ffffff;
margin: 0.5em;
padding: 0.5em;
text-align: center;
}
h1{
border-radius: 0.2em;
background: #f1f1f1;
box-shadow: inset 3px 2px 5px #cdcdcd, inset -3px -1px 15px #ffffff;
margin: 0.2em;
padding: 0.3em;
color: #343434;
}
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 0px;
border-radius: 4px;
box-sizing: border-box;
box-shadow: inset 3px 2px 5px #cdcdcd, inset -3px -1px 15px #ffffff;
}
#add-tasks-buttons{
display: grid;
grid-template-columns: 1fr 1fr;
}
#decide-weights-range{
display: grid;
grid-template-columns: 1fr 2fr 1fr;
text-align: center;
}
#ordered-tasks-list{
text-align: center;
}
.add-tasks-list-item{
display: grid;
grid-template-columns: 4fr 1fr;
align-items: center;
box-shadow: inset 3px 2px 5px #cdcdcd, inset -3px -1px 15px #ffffff;
background-color: #f7f7f7;
padding: 0.5em;
border-radius: 0.5em;
margin: 0.7em;
}
.add-tasks-list-item .btn{
background-color: #f7f7f7;
}
</style>
<body>
<!-- https://codepen.io/tirsolecointere/details/BayJqzG -->
<div id="container">
<div id="add-tasks" class="screen">
<h1>Add Tasks</h1>
<input id="task-description" type="text" value="Next task">
<div id="add-tasks-buttons">
<span id="next-task" class="btn">Add task</span>
<span id="finished-adding-task" class="btn">Sort tasks</span>
</div>
<div id="add-tasks-list">
</div>
</div>
<div id="sort-tasks-by-value" class="screen">
<h1>Which task is more valuable?</h1>
<span id="task-a-value" class="btn">Task A</span>
<span id="task-b-value" class="btn">Task B</span>
</div>
<div id="sort-tasks-by-effort" class="screen">
<h1>Which task takes more effort?</h1>
<span id="task-a-effort" class="btn">Task A</span>
<span id="task-b-effort" class="btn">Task B</span>
</div>
<div id="decide-weights" class="screen">
<h1>What do you prefer?</h1>
<div id="decide-weights-range">
More Value
<input type="range" id="weights" name="weights" min="0" max="10">
LessEffort
</div>
<span id="show-results" class="btn">Show results!</span>
</div>
<div id="results" class="screen">
<h1>This is the order you should do the tasks:</h1>
<div id="ordered-tasks-list">foo
</div>
</div>
</div>
<script>
const show = (screenId) => {
console.log("Show screen: " + screenId)
for(s of Array.from(document.getElementsByClassName("screen"))){
s.style.display = "none"
}
document.getElementById(screenId).style.display = "grid"
}
const getRandomItem = (arr) => arr[Math.floor(Math.random() * arr.length)]
let tasks = []
let tasksOrderedByValue = []//0 is more valuable
let tasksOrderedByEffort = []//0 is less effort
let valueWeigth = 1
let effortWeigth = 1
let contexts = []
var currentContext
const reRenderTasks = () => {
const taskList = document.getElementById("add-tasks-list")
taskList.innerHTML = ''
for(task of tasks){
const div = document.createElement("div")
div.classList.add("add-tasks-list-item")
const span = document.createElement("span")
span.textContent = task.description
div.appendChild(span)
const button = document.createElement("a")
button.textContent = "Delete"
button.classList.add("btn")
let description = task.description
button.addEventListener("click", ()=>{
console.log("Removed task: " + JSON.stringify(task))
tasks = tasks.filter(t => t.description != description)
reRenderTasks()
})
div.appendChild(button)
taskList.appendChild(div)
}
}
const compareByValue = (taskA, taskB) => {
console.log("compare value of tasksOrderedByValue["+taskA+"] and tasksOrderedByValue["+taskB+"]")
console.log("CompareValue: '"+tasksOrderedByValue[taskA].description+"' vs '"+tasksOrderedByValue[taskB].description+"'")
document.getElementById("task-a-value").textContent = tasksOrderedByValue[taskA].description
document.getElementById("task-b-value").textContent = tasksOrderedByValue[taskB].description
show("sort-tasks-by-value")
}
const compareByEffort = (taskA, taskB) => {
console.log("compare effort of tasksOrderedByValue["+taskA+"] and tasksOrderedByValue["+taskB+"]")
console.log("CompareEffort: '"+tasksOrderedByEffort[taskA].description+"' vs '"+tasksOrderedByEffort[taskB].description+"'")
document.getElementById("task-a-effort").textContent = tasksOrderedByEffort[taskA].description
document.getElementById("task-b-effort").textContent = tasksOrderedByEffort[taskB].description
show("sort-tasks-by-effort")
}
const compareTest = (taskA, taskB) => {
console.log("Comparing tasks["+taskA+"] = "+tasksOrderedByValue[taskA].description+" with pivot tasks["+taskB+"] = "+tasksOrderedByValue[taskB].description)
if(tasksOrderedByValue[taskA].description > tasksOrderedByValue[taskB].description){
pivotIsSmaller()
}else{
pivotIsBigger()
}
}
const nextStep = () => {
document.getElementById("task-a-effort").textContent = "EMPTY"
document.getElementById("task-b-effort").textContent = "EMPTY"
document.getElementById("task-a-value").textContent = "EMPTY"
document.getElementById("task-b-value").textContent = "EMPTY"
if(contexts.length == 0){
console.log("No more contexts to process")
console.log("tasksOrderedByValue is: [" + (tasksOrderedByValue.map(t=>t.description).join(", ")) + "]")
console.log("tasksOrderedByEffort is: [" + (tasksOrderedByEffort.map(t=>t.description).join(", ")) + "]")
show("decide-weights")
return
}
currentContext = getRandomItem(contexts)
console.log("Next step ====")
console.log("Current context: "+JSON.stringify(currentContext))
if(currentContext.pivotAtEndIndex <= currentContext.start) return
let notYetComparedToPivotIndex = currentContext.notYetComparedToPivotIndex
let pivotAtEndIndex = currentContext.pivotAtEndIndex
let firstBiggerThanPivotIndex = currentContext.firstBiggerThanPivotIndex
let tasksOrdered
let compare
if(currentContext.sorting == "value"){
tasksOrdered = tasksOrderedByValue
compare = compareByValue
}else if (currentContext.sorting == "effort"){
tasksOrdered = tasksOrderedByEffort
compare = compareByEffort
}else{
tasksOrdered = tasksOrderedByValue
compare = compareTest
}
console.log("Tasks are: [" + (tasksOrdered.map(t=>t.description).join(", ")) + "]")
if(notYetComparedToPivotIndex < pivotAtEndIndex){
compare(notYetComparedToPivotIndex, pivotAtEndIndex)
}else{
console.log("=========Finishing context")
let pivotFinalPosition = pivotAtEndIndex
if(currentContext.firstBiggerThanPivotIndex != pivotAtEndIndex){
console.log("Swapping "+tasksOrdered[pivotAtEndIndex].description+" at "+ pivotAtEndIndex + " with "+ tasksOrdered[firstBiggerThanPivotIndex].description +" at "+firstBiggerThanPivotIndex )
let tmp = tasksOrdered[pivotAtEndIndex]
tasksOrdered[pivotAtEndIndex] = tasksOrdered[firstBiggerThanPivotIndex]
tasksOrdered[firstBiggerThanPivotIndex] = tmp
pivotFinalPosition = firstBiggerThanPivotIndex
console.log("Pivot final position is "+pivotFinalPosition)
}
let contextSolved = currentContext
contexts = contexts.filter(c => c != currentContext)
console.log("=========Context Finished")
console.log("Result = [" + (tasksOrdered.map(t=>t.description).join(", ")) + "]")
if(pivotAtEndIndex > 0 && contextSolved.start < pivotFinalPosition - 1){
let newContextBeforePivot = {
"sorting": contextSolved.sorting,
"start": contextSolved.start,
"pivotAtEndIndex": pivotFinalPosition - 1,
"firstBiggerThanPivotIndex": contextSolved.start,
"notYetComparedToPivotIndex": contextSolved.start
}
contexts.push(newContextBeforePivot)
console.log("Added context before pivot "+JSON.stringify(newContextBeforePivot))
}
if(pivotAtEndIndex < tasks.length - 1 && pivotFinalPosition + 1 < contextSolved.pivotAtEndIndex){
let newContextAfterPivot = {
"sorting": contextSolved.sorting,
"start": pivotFinalPosition + 1,
"pivotAtEndIndex": contextSolved.pivotAtEndIndex,
"firstBiggerThanPivotIndex": pivotFinalPosition + 1,
"notYetComparedToPivotIndex": pivotFinalPosition + 1
}
contexts.push(newContextAfterPivot)
console.log("Added context after pivot "+JSON.stringify(newContextAfterPivot))
}
nextStep()
}
}
const pivotIsSmaller = ()=>{
console.log("Pivot (" + currentContext.pivotAtEndIndex + ") is smaller")
currentContext.notYetComparedToPivotIndex += 1
console.log("Increased currentContext.notYetComparedToPivotIndex to " + currentContext.notYetComparedToPivotIndex)
let tasksOrdered
if(currentContext.sorting == "effort"){
tasksOrdered = tasksOrderedByEffort
}else{
tasksOrdered = tasksOrderedByValue
}
console.log("Tasks are: [" + (tasksOrdered.map(t=>t.description).join(", ")) + "]")
nextStep()
}
const pivotIsBigger = ()=>{
console.log("Pivot (" + currentContext.pivotAtEndIndex + ") is bigger")
let currentComparingWith = currentContext.notYetComparedToPivotIndex
let tasksOrdered
if(currentContext.sorting == "effort"){
tasksOrdered = tasksOrderedByEffort
}else{
tasksOrdered = tasksOrderedByValue
}
if(currentComparingWith != currentContext.firstBiggerThanPivotIndex){
console.log("Swapping "+tasksOrdered[currentComparingWith].description+" at "+ currentComparingWith + " with "+ tasksOrdered[currentContext.firstBiggerThanPivotIndex].description +" at "+currentContext.firstBiggerThanPivotIndex )
let tmp = tasksOrdered[currentComparingWith]
tasksOrdered[currentComparingWith] = tasksOrdered[currentContext.firstBiggerThanPivotIndex]
tasksOrdered[currentContext.firstBiggerThanPivotIndex] = tmp
}
currentContext.notYetComparedToPivotIndex += 1
currentContext.firstBiggerThanPivotIndex += 1
console.log("Increased currentContext.notYetComparedToPivotIndex to " + currentContext.notYetComparedToPivotIndex)
console.log("Increased currentContext.firstBiggerThanPivotIndex to " + currentContext.firstBiggerThanPivotIndex)
console.log("Tasks are: [" + (tasksOrdered.map(t=>t.description).join(", ")) + "]")
nextStep()
}
document.getElementById("task-a-value").addEventListener("click", pivotIsBigger)
document.getElementById("task-a-effort").addEventListener("click", pivotIsBigger)
document.getElementById("task-b-value").addEventListener("click", pivotIsSmaller)
document.getElementById("task-b-effort").addEventListener("click", pivotIsSmaller)
const newTask = (description) => ({
"description": description,
"effort": 0,
"value": 0,
"random": Math.random()
})
const addTask = () => {
const taskDescription = document.getElementById("task-description")
const value = taskDescription.value
taskDescription.value = ""
let task = newTask(value)
tasks.push(task)
console.log("Added task: "+JSON.stringify(task))
reRenderTasks()
}
document.getElementById("next-task").addEventListener("click", addTask)
document.getElementById("task-description").addEventListener("keyup", (event) => {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("next-task").click();
}
});
document.getElementById("finished-adding-task").addEventListener("click", ()=>{
tasks.sort((a, b) => a.random - b.random)
tasksOrderedByValue = [...tasks]
tasksOrderedByEffort = [...tasks]
console.log("Tasks are: [" + (tasks.map(t=>t.description).join(", ")) + "]")
contexts = [
{"sorting":"value", "start":0, "pivotAtEndIndex":tasks.length -1, "firstBiggerThanPivotIndex": 0, "notYetComparedToPivotIndex": 0},
{"sorting":"effort", "start":0, "pivotAtEndIndex":tasks.length -1, "firstBiggerThanPivotIndex": 0, "notYetComparedToPivotIndex": 0}
]
nextStep()
})
document.getElementById("show-results").addEventListener("click", ()=>{
for(let i = 0; i < tasksOrderedByValue.length; i++){
tasksOrderedByValue[i].value = i
}
for(let i = 0; i < tasksOrderedByEffort.length; i++){
tasksOrderedByEffort[i].effort = (tasksOrderedByEffort.length -1) - i
}
const range = parseInt(document.getElementById("weights").value) // 10 less effort ,0 more value
const valueWeigth = 1 - (range/10)
const effortWeigth = (range/10)
console.log("Score is value * valueWeigth + effort * effortWeigth")
console.log("Priority goes from smaller to biggest")
console.log("valueWeigth is " + valueWeigth)
console.log("effortWeigth is " + effortWeigth)
tasks.forEach(t => t.score = t.value * valueWeigth + t.effort * effortWeigth)
tasks.sort((a, b) => a.score - b.score)
console.log("Final tasks ordered is: [" + (tasks.map(t=> JSON.stringify(t)).join(", ")) + "]")
const taskList = document.getElementById("ordered-tasks-list")
taskList.innerHTML = ''
for(task of tasks){
const div = document.createElement("div")
const span = document.createElement("span")
span.textContent = task.description
div.appendChild(span)
let description = task.description
taskList.appendChild(div)
}
console.log(tasks)
show("results")
})
let runTests = false
if(runTests){ +(() => {
console.log("test")
tasks = [
newTask(1),
newTask(4),
newTask(5),
newTask(3),
newTask(2),
newTask(6)
]
tasksOrderedByValue = [...tasks]
tasksOrderedByEffort = [...tasks]
contexts = [
{"sorting":"test", "start":0, "pivotAtEndIndex":tasks.length -1, "firstBiggerThanPivotIndex": 0, "notYetComparedToPivotIndex": 0}
]
nextStep()
tasks = []
tasksOrderedByValue = []
tasksOrderedByEffort = []
})()
}
show("add-tasks")
</script>
</body>
</html>