Jacek Kowalski
2012-06-23 175a52e78df3f3462927885ce6d732cb1b36a818
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) {
143     if(book.parentNode == loc) {
144         xml.getElementById(book.id).setAttribute('status', 'ok');
145         
146         book.className = 'ok';
147         book.childNodes[0].childNodes[2].data = 'Na miejscu';
148         book.scrollIntoView();
149     }
150     else
151     {
152         xml.getElementById(loc.id).appendChild(xml.getElementById(book.id));
153         xml.getElementById(book.id).setAttribute('status', 'moved');
154         
155         var book2 = book.cloneNode(true);
156         book2.className = 'ok';
157         book2.childNodes[0].childNodes[2].data = 'Przeniesiona';
158         
159         if(document.getElementById('e'+book.id)) {
160             document.getElementById('e'+book.id).parentNode.removeChild(document.getElementById('e'+book.id));
161         }
162         
163         book.className = 'err';
164         book.id = 'e'+book.id;
165         book.childNodes[0].childNodes[2].data = 'Przeniesiona';
166         
167         loc.appendChild(book2);
168         book2.scrollIntoView();
169     }
170 }
171
172 function clearInput() {
173     document.getElementById('i1').value = document.getElementById('i2').value = document.getElementById('i3').value = '';
174     document.getElementById('i1').focus();
175 }
176
177 function processInput() {
178     var reg = /^([0-9]{1,8})$/;
179     var i1 = document.getElementById('i1').value;
180     var i2 = document.getElementById('i2').value;
181     var i3 = document.getElementById('i3').value;
182     
183     if(i2 != '' || i3 != '') {
184         if(!document.getElementById('m_'+i1+'_'+i2+'_'+i3)) {
185             error('Podane regał/półka/rząd nie istnieją!');
186             clearInput();
187             return false;
188         }
189         
190         changeLocation(document.getElementById('m_'+i1+'_'+i2+'_'+i3));
191         status('Wybrano '+i1+'/'+i2+'/'+i3);
192         clearInput();
193         return true;
194     }
195     
196     if(reg.test(i1)) {
197         i1 = parseInt(i1, 10);
198         if(!document.getElementById('k'+i1)) {
199             error('Wybrana książka nie istnieje!');
200             clearInput();
201             return false;
202         }
203         
204         changeBook(document.getElementById('k'+i1));
205         status('OK - książka '+i1);
206         clearInput();
207         return true;
208     }
209     
210     error('Nieznany typ (książka - 8 cyfr; regał - tekst; półka/rząd - liczby)!');
211     clearInput();
212     return false;
213 }
214
215 function keyEvent(e) {
216     if(!e) e = window.event;
217     
218     if(e.keyCode == 13) {
219         processInput();
220         clearInput();
221         return false;
222     }
223 }
224
225 function process() {
226     var number = 0;
227     var header = makeHeader();
228     
229     xml = ajax.responseXML;
230     if(!(xml instanceof XMLDocument)) {
231         fatalError('Pobrany dokument nie jest poprawnym arkuszem XML');
232     }
233     
234     var total = xml.getElementsByTagName('ksiazka').length;
235     
236     if(!xml.getElementById) {
237         fatalError('Przeglądarka nie wspiera XMLDocument.getElementById');
238     }
239     
240     status('Książek do przetworzenia: '+total);
241     var miejsca = xml.getElementsByTagName('lokalizacja');
242     for(var i=0; i<miejsca.length; i++) {
243         var table = document.createElement('table');
244         if(loc == false) {
245             loc = table;
246         }
247         table.id = 'm_'+miejsca[i].attributes['regal'].nodeValue+'_'+miejsca[i].attributes['polka'].nodeValue+'_'+miejsca[i].attributes['rzad'].nodeValue;
248         table.appendChild(makeCaption(miejsca[i].attributes['regal'].nodeValue, miejsca[i].attributes['polka'].nodeValue, miejsca[i].attributes['rzad'].nodeValue));
249         table.appendChild(header.cloneNode(true));
250         
251         for(var j=0; j<miejsca[i].childNodes.length; j++) {
252             if(!miejsca[i].childNodes[j].tagName) continue;
253             table.appendChild(processBook(miejsca[i].childNodes[j]));
254             
255             if((++number % 100) == 0) {
256                 status('Przetworzono: '+Math.floor(number*100/total)+'% ('+number+' z '+total+')');
257             }
258         }
259         
260         document.getElementById('data').appendChild(table);
261     }
262     
263     status('Przetworzono: 100% ('+total+' z '+total+')');
264     
265     changeLocation(loc);
266     
267     document.getElementById('input').style.display = 'block';
268     
269     document.getElementById('i1').onkeydown = document.getElementById('i2').onkeydown = document.getElementById('i3').onkeydown = keyEvent;
270     document.getElementById('i4').onclick = processInput;
271     document.getElementById('i5').onclick = save;
272     document.getElementById('i1').focus();
273     
274     status('Gotowy do pracy.');
275 }
276
277 function save() {
278     document.getElementById('input').style.display = 'none';
279     status('Zapisywanie. Proszę czekać...');
280     
281     ajax.open('POST', 'save.php', true);
282     ajax.onreadystatechange = function() {
283         if(ajax.readyState == 3) {
284             status('Wysyłanie danych...');
285         }
286         else if(ajax.readyState == 4) {
287             if(ajax.status == 200) {
288                 status('Dane zostały zapisane. '+ajax.status);
289             }
290             else
291             {
292                 error('Zapis nie powiódł się. Błąd HTTP '+ajax.status);
293             }
294             
295             document.getElementById('input').style.display = 'block';
296         }
297     };
298     ajax.send(xml);
299 }
300
301 function getData() {
302     status('Inicjowanie transferu...');
303     ajax();
304     ajax.open('GET', 'list.xml?time='+((new Date()).getTime())+'&rand='+Math.random(), true);
305     ajax.onreadystatechange = function() {
306         if(ajax.readyState == 3) {
307             status('Pobieranie danych...');
308         }
309         else if(ajax.readyState == 4) {
310             if(ajax.status == 200 || ajax.status == 304) {
311                 status('Przetwarzanie danych...');
312                 process();
313             }
314             else
315             {
316                 fatalError('Kod HTTP '+ajax.status+'. Nie udało się pobrać danych. Spróbuj przeładować stronę.');
317             }
318         }
319     };
320     ajax.send();
321 }
322
323 window.onload = getData;