DOCTYPE html html head lang en meta charset UTF-8 title title head bod

  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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="application/javascript" src="underscore.js"></script>
<script type="application/javascript">
function trackTime(method) {
var beginTime = _.now();
method();
console.log('Time: ' + (_.now() - beginTime) + ' ms');
}
var collection = [
{"id": 6, "text": "І — покажем, що ми, браття, козацького роду!", "code": "tj", "codeExt": "", "status": 1, "position": 4, "fromTime": "", "tillTime": "", "parentId": 1, "userId": 20121, "userFirstName": "Snijana", "userLastName": "Mis", "userEmpNo": ""},
{"id": 7, "text": "Гей-гей, браття милі,", "code": "", "codeExt": "", "status": 1, "position": 5, "fromTime": "", "tillTime": "", "parentId": 1, "userId": 10099, "userFirstName": "Test", "userLastName": "Edit", "userEmpNo": ""},
{"id": 9, "text": "Гей, гей, пора встати,", "code": "", "codeExt": "", "status": 1, "position": 6, "fromTime": "", "tillTime": "", "parentId": 6, "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 12, "text": "Нащо оддав Україну ворогам поганим?!", "code": "", "codeExt": "", "status": 1, "position": 9, "fromTime": "", "tillTime": "", "parentId": 11, "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 1, "text": "Ще не вмерла Україна, ні слава, ні воля,", "code": "qq", "codeExt": "", "status": 1, "position": 1, "fromTime": "", "tillTime": "", "parentId": "12", "userId": 10099, "userFirstName": "Test", "userLastName": "Edit", "userEmpNo": ""},
{"id": 5, "text": "Душу й тіло ми положим за нашу свободу", "code": "", "codeExt": "", "status": 1, "position": 2, "fromTime": "", "tillTime": "", "parentId": "", "userId": 20121, "userFirstName": "Snijana", "userLastName": "Mis", "userEmpNo": ""},
{"id": 8, "text": "Нумо братися за діло,", "code": "", "codeExt": "", "status": 1, "position": 7, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 10, "text": "Пора волю добувати!", "code": "", "codeExt": "", "status": 1, "position": 7, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 11, "text": "Ой, Богдане, Богдане, славний наш гетьмане,", "code": "", "codeExt": "", "status": 1, "position": 8, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 13, "text": "Щоб вернути її честь, ляжем головами,", "code": "", "codeExt": "", "status": 1, "position": 10, "fromTime": "", "tillTime": "", "parentId": "", "userId": 20382, "userFirstName": "User1", "userLastName": "User1", "userEmpNo": ""},
{"id": 14, "text": "Наречемось України славними синами.", "code": "", "codeExt": "", "status": 1, "position": 11, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 15, "text": "Душу й тіло ми положим за нашу свободу", "code": "", "codeExt": "", "status": 1, "position": 12, "fromTime": "", "tillTime": "", "parentId": "11", "userId": 20381, "userFirstName": "Light", "userLastName": "Version", "userEmpNo": ""},
{"id": 16, "text": "І — покажем, що ми, браття, козацького роду!", "code": "", "codeExt": "", "status": 1, "position": 13, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 17, "text": "Спогадаймо тяжкий час, лихую годину,", "code": "", "codeExt": "", "status": 1, "position": 14, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 18, "text": "тих, що вміли умирати за нашу Вкраїну,", "code": "", "codeExt": "", "status": 1, "position": 15, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 19, "text": "Спогадаймо славну смерть лицарства-козацтва!", "code": "", "codeExt": "", "status": 1, "position": 16, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 20, "text": "Щоб не стратить марно нам свого юнацтва!", "code": "", "codeExt": "", "status": 1, "position": 17, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 21, "text": "Душу й тіло ми положим за нашу свободу", "code": "", "codeExt": "", "status": 1, "position": 18, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""},
{"id": 22, "text": "І — покажем, що ми, браття, козацького роду!", "code": "", "codeExt": "", "status": 1, "position": 19, "fromTime": "", "tillTime": "", "parentId": "", "userId": 0, "userFirstName": "", "userLastName": "", "userEmpNo": ""}
];
function modifyResult() {
var stack = [];
var results = _.sortBy(collection, function (item) {
return item.id;
});
var data = [
{
id: 0,
children: []
}
];
function findNode(id, data) {
for (var index = 0; index < data.length; index++) {
if (data[index].id == id) {
return data[index].children;
} else {
var children = data[index].children;
var node = findNode(id, children);
if (node) {
return node;
}
}
}
}
_.forEach(results, function (item) {
stack.push(item);
});
while (stack.length > 0) {
var item = stack.shift();
var node = findNode(item.parentId || 0, data);
if (node) {
node.push({
id: item.id,
item: item,
children: []
});
} else {
stack.push(item);
}
}
function makeList(nodes, results, $level/*todo: temp*/) {
nodes = _.sortBy(nodes, function (node) {
return node['item'].position;
});
_.forEach(nodes, function (node) {
node.item.$level = $level;//todo: temp
results.push(node.item);
makeList(node.children, results, $level + 1/*todo: temp*/);
});
}
var list = [];
makeList(data[0].children, list, 0);
return list;
}
trackTime(function () {
var result = modifyResult();
//todo: temp
function getOffset(item) {
var result = '';
_.times(item.$level, function () {
result += ' ';
});
return result;
}
_.forEach(result, function (item) {
console.log(getOffset(item) + '|id: ' + item.id);
console.log(getOffset(item) + '|parentId: ' + (item.parentId || 0));
console.log(getOffset(item) + '|position: ' + item.position);
console.log(getOffset(item) + '|--------------------');
});
});
</script>
</body>
</html>