forked from can-luckyshot/IMSpoorViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gen.html
146 lines (132 loc) · 4.41 KB
/
test_gen.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<title>IMX Testfile Generator</title>
<script type="text/javascript" src="//code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<style>
#droptarget { position: absolute; top: 10px; bottom: 0px; left: 5px; right: 5px; border: 1px solid rgba(0, 0, 0, 0.9);}
#testOutput { position: absolute; top: 50px; bottom: 0px; left: 5px; right: 5px; border: 1px solid rgba(1, 0, 0, 0.9); }
</style>
<body>
<div id="droptarget">Drag input here</div>
<div id="testOutput">Output will be displayed here</div>
</body>
<script>
initDragAndDrop();
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files; // FileList object.
// files is a FileList of File objects. List some properties.
var output = [];
$.each(files, function (index, f) {
var reader = new FileReader();
reader.onload =
(function (file) {
var fileName = file.name;
console.log('file hallo: ' + fileName);
return function (event) {
var text = event.target.result;
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(text, "text/xml");
generateTestFile(xmlDoc,fileName);
};
})(f);
reader.onerror = function (event) {
console.log('file-error: ' + event.target.error.code);
};
reader.readAsText(f);
});
}
function getTypes(xmlDoc){
var objectsWithGeom = $(xmlDoc).find('GeographicLocation').parent().parent();
var typeMap = new Object();
$(objectsWithGeom).each(function (index, objectWithGeom) {
var nodeName = objectWithGeom.nodeName;
var entry = typeMap[nodeName];
if (entry == undefined) {
typeMap[nodeName] = new Object();
}
});
return typeMap;
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
function initDragAndDrop() {
// Setup the dnd listeners.
var dropZone = document.getElementById('droptarget');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
// setup projectie
}
function createChangeLog(xmlDoc){
var xmlData = '<SituationChanges>\r\n<Created></Created>\r\n<Updated></Updated>\r\n<Deleted></Deleted>\r\n</SituationChanges>';
var sitDoc = $.parseXML(xmlData);
var sc = $(sitDoc).find('SituationChanges');
var root = $(xmlDoc).find('IMSpoor').get(0);
$(root).append(sc);
}
function generateTestFile(xmlDoc,fileName){
createChangeLog(xmlDoc);
$('#droptarget').text('file: '+fileName);
var types = getTypes(xmlDoc);
$.each(types,function(type){
createTestSet(type,xmlDoc);
});
cleanupChangeLog(xmlDoc);
var xmlText = new XMLSerializer().serializeToString(xmlDoc);
xmlText = xmlText.replace(' xmlns=""','');
var em = $('<pre></pre>');
$(em).text(xmlText);
$('#testOutput').empty();
$('#testOutput').append(em);
}
function cleanupChangeLog(xmlDoc){
var elem = $(xmlDoc).find('Created');
elem.text(elem.text().trim());
var elem = $(xmlDoc).find('Updated');
elem.text(elem.text().trim());
var elem = $(xmlDoc).find('Deleted');
elem.text(elem.text().trim());
}
function uuidv4() {
var uuid = ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
return uuid;
}
function createTestSet(type,xmlDoc){
var imxItems = $(xmlDoc).find(type)
if(imxItems.length > 1){
var imxItemToDelete = imxItems.get(0);
var imxItemToUpdate = imxItems.get(1);
var group = $(imxItemToDelete).parent();
// create
var newImxItem = $(imxItemToDelete).clone();
var new_puic = uuidv4();
console.log('new '+type+': '+new_puic);
$(newImxItem).attr('puic',new_puic);
$(newImxItem).attr('name','nieuw_object');
$(group).append(newImxItem);
$(xmlDoc).find('Created').append(' '+new_puic);
// delete
$(xmlDoc).find('Deleted').append(' '+$(imxItemToDelete).attr('puic'));
$(imxItemToDelete).remove();
// update
$(imxItemToUpdate).attr('name','veranderd_object');
if($(imxItemToUpdate).attr('puic')==undefined){
console.log('undefined puic: '+type);
}
$(xmlDoc).find('Updated').append(' '+$(imxItemToUpdate).attr('puic'));
}
}
</script>
</html>