Jacek Kowalski
2012-07-07 4670c76fe96e1ff78ff094e6fae8469d2d10d747
commit | author | age
175a52 1 var xml;
JK 2 var ajax;
3 var loc = false;
4
5 function ajax() {
6     try {
7         ajax = new XMLHttpRequest(); 
8         return;
9     }
10     catch(e) {
11         var activex = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
12         for(var i=0; i<activex.length; i++) {
13             try {
14                 ajax = new ActiveXObject(activex[i]);
15                 return;
16             }
17             catch(e) {
18             }
19         }
20     }
21     
22     fatalError('Przeglądarka nie obsługuje XMLHttpRequest');
23 }
24
25 function status(msg) {
26     document.getElementById('result1').innerHTML = document.getElementById('result2').innerHTML
27     document.getElementById('result2').innerHTML = document.getElementById('result3').innerHTML;
28     document.getElementById('result3').innerHTML = msg;
29 }
30
31 function error(msg) {
32     status('<span class="error">'+msg+'</span>');
33     document.getElementById('audio').play();
34 }
35
36 function fatalError(msg) {
37     status('<span class="error">'+msg+'</span>');
38     throw new Exception('Błąd krytyczny: '+msg);
39 }
40
41 function changeLocation(element) {
42     while(element && element.parentNode!=document && element.tagName != 'TABLE') {
43         element = element.parentNode
44     }
45     
46     if(!element || element.tagName != 'TABLE') {
47         error('Podana lokalizacja nie istnieje lub jest nieznana.');
48         return;
49     }
50     
51     if(loc) {
52         loc.className = '';
53     }
54     
55     loc = element;
56     loc.className = 'current';
57     loc.scrollIntoView();
58 }
59
60 function makeCaption(regal, polka, rzad) {
61     var caption = document.createElement('caption');
62     caption.appendChild(document.createTextNode('Półka: '+regal+'/'+polka+'/'+rzad));
63     caption.onclick = 'changeLocation(this)';
64     return caption;
65 }
66
67 function textValue(element) {
68     var text = '';
69     
70     for(var i=0; i<element.childNodes.length; i++) {
71         if(element.childNodes.item(i) instanceof String) {
72             text += element.childNodes.item(i);
73         }
74         else if(element.childNodes.item(i) instanceof Text) {
75             text += element.childNodes.item(i).nodeValue;
76         }
77         else
78         {
79             text += textValue(element.childNodes.item(i));
80         }
81     }
82     
83     return text;
84 }
85
86 function makeHeader(caption) {
87     var tr = document.createElement('tr');
88     var th = document.createElement('th');
89     th.appendChild(document.createTextNode('ID'));
90     th.appendChild(document.createElement('br'));
91     th.appendChild(document.createTextNode('status'));
92     tr.appendChild(th);
93     var th = document.createElement('th');
94     th.appendChild(document.createTextNode('Autor'));
95     th.appendChild(document.createElement('br'));
96     th.appendChild(document.createTextNode('Tytuł'));
97     tr.appendChild(th);
98     var th = document.createElement('th');
99     th.appendChild(document.createTextNode('Miejsce, rok'));
100     th.appendChild(document.createElement('br'));
101     th.appendChild(document.createTextNode('Wydawnictwo'));
102     tr.appendChild(th);
103     
104     return tr;
105 }
106
107 function processBook(book) {
108     var tr = document.createElement('tr');
109     tr.id = book.attributes['id'].nodeValue;
110     tr.onclick = 'changeBook(this)';
111     
112     var th = document.createElement('td');
113     th.appendChild(document.createTextNode(book.attributes['id'].nodeValue.substr(1)));
114     th.appendChild(document.createElement('br'));
115     if(!book.hasAttribute('status')) {
116         th.appendChild(document.createTextNode('Nieznany'));
117     }
118     else if(book.getAttribute('status') == 'ok') {
119         tr.className = 'ok';
120         th.appendChild(document.createTextNode('Na miejscu'));
121     }
122     else
123     {
124         tr.className = 'ok';
125         th.appendChild(document.createTextNode('Przeniesiona'));
126     }
127     tr.appendChild(th);
128     var th = document.createElement('td');
129     th.appendChild(document.createTextNode(textValue(book.getElementsByTagName('autor').item(0))));
130     th.appendChild(document.createElement('br'));
131     th.appendChild(document.createTextNode(textValue(book.getElementsByTagName('tytul').item(0))));
132     tr.appendChild(th);
133     var th = document.createElement('td');
134     th.appendChild(document.createTextNode(textValue(book.getElementsByTagName('miejsce').item(0))+' '+textValue(book.getElementsByTagName('rok').item(0))));
135     th.appendChild(document.createElement('br'));
136     th.appendChild(document.createTextNode(textValue(book.getElementsByTagName('wydawnictwo').item(0))));
137     tr.appendChild(th);
138     
139     return tr;
140 }
141
142 function changeBook(book) {
4670c7 143     if(document.getElementById('input').style.display != 'block') {
JK 144         return false;
145     }
146     
175a52 147     if(book.parentNode == loc) {
JK 148         xml.getElementById(book.id).setAttribute('status', 'ok');
149         
150         book.className = 'ok';
151         book.childNodes[0].childNodes[2].data = 'Na miejscu';
152         book.scrollIntoView();
153     }
154     else
155     {
156         xml.getElementById(loc.id).appendChild(xml.getElementById(book.id));
157         xml.getElementById(book.id).setAttribute('status', 'moved');
158         
159         var book2 = book.cloneNode(true);
160         book2.className = 'ok';
161         book2.childNodes[0].childNodes[2].data = 'Przeniesiona';
162         
163         if(document.getElementById('e'+book.id)) {
164             document.getElementById('e'+book.id).parentNode.removeChild(document.getElementById('e'+book.id));
165         }
166         
167         book.className = 'err';
168         book.id = 'e'+book.id;
169         book.childNodes[0].childNodes[2].data = 'Przeniesiona';
170         
171         loc.appendChild(book2);
172         book2.scrollIntoView();
173     }
174 }
175
176 function clearInput() {
177     document.getElementById('i1').value = document.getElementById('i2').value = document.getElementById('i3').value = '';
178     document.getElementById('i1').focus();
179 }
180
181 function processInput() {
4670c7 182     if(document.getElementById('input').style.display != 'block') {
JK 183         return false;
184     }
185     
175a52 186     var reg = /^([0-9]{1,8})$/;
JK 187     var i1 = document.getElementById('i1').value;
188     var i2 = document.getElementById('i2').value;
189     var i3 = document.getElementById('i3').value;
190     
191     if(i2 != '' || i3 != '') {
192         if(!document.getElementById('m_'+i1+'_'+i2+'_'+i3)) {
193             error('Podane regał/półka/rząd nie istnieją!');
194             clearInput();
195             return false;
196         }
197         
198         changeLocation(document.getElementById('m_'+i1+'_'+i2+'_'+i3));
199         status('Wybrano '+i1+'/'+i2+'/'+i3);
200         clearInput();
201         return true;
202     }
203     
204     if(reg.test(i1)) {
205         i1 = parseInt(i1, 10);
206         if(!document.getElementById('k'+i1)) {
207             error('Wybrana książka nie istnieje!');
208             clearInput();
209             return false;
210         }
211         
212         changeBook(document.getElementById('k'+i1));
213         status('OK - książka '+i1);
214         clearInput();
215         return true;
216     }
217     
218     error('Nieznany typ (książka - 8 cyfr; regał - tekst; półka/rząd - liczby)!');
219     clearInput();
220     return false;
221 }
222
223 function keyEvent(e) {
224     if(!e) e = window.event;
225     
226     if(e.keyCode == 13) {
227         processInput();
228         clearInput();
229         return false;
230     }
4670c7 231 }
JK 232
233 function saveWithCallback(callback) {
234     ajax.open('POST', 'save.php', true);
235     ajax.onreadystatechange = callback;
236     ajax.send(xml);
237 }
238
239 function save() {
240     document.getElementById('input').style.display = 'none';
241     status('Zapisywanie. Proszę czekać...');
242     
243     saveWithCallback(function() {
244         if(ajax.readyState == 3) {
245             status('Wysyłanie danych...');
246         }
247         else if(ajax.readyState == 4) {
248             if(ajax.status == 200) {
249                 status('Dane zostały zapisane. '+ajax.status);
250             }
251             else
252             {
253                 error('Zapis nie powiódł się. Błąd HTTP '+ajax.status);
254             }
255             
256             document.getElementById('input').style.display = 'block';
257         }
258     });
259 }
260
261 function end() {
262     document.getElementById('input').style.display = 'none';
263     status('Zapisywanie. Proszę czekać...');
264     
265     saveWithCallback(function() {
266         if(ajax.readyState == 3) {
267             status('Wysyłanie danych...');
268         }
269         else if(ajax.readyState == 4) {
270             if(ajax.status == 200) {
271                 status('Dane zostały zapisane. '+ajax.status);
272                 status('Przekierowywanie... Proszę czekać...');
273                 window.location.replace('end.php');
274             }
275             else
276             {
277                 error('Zapis nie powiódł się. Błąd HTTP '+ajax.status);
278                 document.getElementById('input').style.display = 'block';
279             }
280         }
281     });
175a52 282 }
JK 283
284 function process() {
285     var number = 0;
286     var header = makeHeader();
287     
288     xml = ajax.responseXML;
289     if(!(xml instanceof XMLDocument)) {
290         fatalError('Pobrany dokument nie jest poprawnym arkuszem XML');
291     }
292     
293     var total = xml.getElementsByTagName('ksiazka').length;
294     
295     if(!xml.getElementById) {
296         fatalError('Przeglądarka nie wspiera XMLDocument.getElementById');
297     }
298     
299     status('Książek do przetworzenia: '+total);
300     var miejsca = xml.getElementsByTagName('lokalizacja');
301     for(var i=0; i<miejsca.length; i++) {
302         var table = document.createElement('table');
303         if(loc == false) {
304             loc = table;
305         }
306         table.id = 'm_'+miejsca[i].attributes['regal'].nodeValue+'_'+miejsca[i].attributes['polka'].nodeValue+'_'+miejsca[i].attributes['rzad'].nodeValue;
307         table.appendChild(makeCaption(miejsca[i].attributes['regal'].nodeValue, miejsca[i].attributes['polka'].nodeValue, miejsca[i].attributes['rzad'].nodeValue));
308         table.appendChild(header.cloneNode(true));
309         
310         for(var j=0; j<miejsca[i].childNodes.length; j++) {
311             if(!miejsca[i].childNodes[j].tagName) continue;
312             table.appendChild(processBook(miejsca[i].childNodes[j]));
313             
314             if((++number % 100) == 0) {
315                 status('Przetworzono: '+Math.floor(number*100/total)+'% ('+number+' z '+total+')');
316             }
317         }
318         
319         document.getElementById('data').appendChild(table);
320     }
321     
322     status('Przetworzono: 100% ('+total+' z '+total+')');
323     
324     changeLocation(loc);
325     
326     document.getElementById('input').style.display = 'block';
327     
328     document.getElementById('i1').onkeydown = document.getElementById('i2').onkeydown = document.getElementById('i3').onkeydown = keyEvent;
329     document.getElementById('i4').onclick = processInput;
330     document.getElementById('i5').onclick = save;
4670c7 331     document.getElementById('i6').onclick = end;
175a52 332     document.getElementById('i1').focus();
JK 333     
334     status('Gotowy do pracy.');
335 }
336
337 function getData() {
338     status('Inicjowanie transferu...');
339     ajax();
340     ajax.open('GET', 'list.xml?time='+((new Date()).getTime())+'&rand='+Math.random(), true);
341     ajax.onreadystatechange = function() {
342         if(ajax.readyState == 3) {
343             status('Pobieranie danych...');
344         }
345         else if(ajax.readyState == 4) {
346             if(ajax.status == 200 || ajax.status == 304) {
347                 status('Przetwarzanie danych...');
348                 process();
349             }
350             else
351             {
352                 fatalError('Kod HTTP '+ajax.status+'. Nie udało się pobrać danych. Spróbuj przeładować stronę.');
353             }
354         }
355     };
356     ajax.send();
357 }
358
359 window.onload = getData;