1/**
2 * DokuWiki Plugin rowmove (JavaScript Component)
3 *
4 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 * @author  lisps
6 */
7/* when ajax is working set working to true*/
8var working = false;
9
10
11function rowup(el,pageid)
12{
13	var elTr = getParentTr(el);
14	var elTbody = elTr.parentNode;
15	var r_elRows = elTbody.rows;
16	var iTr = elTr.rowIndex;
17	var count = countRows(elTr);
18	var offset_thead = r_elRows[0].rowIndex;
19
20	iTr=iTr-offset_thead;
21
22	if(iTr==0) return; // erste Zeile
23	if(!checkRow(r_elRows[iTr-1])) return; //vorherige Zeile tauschbar
24
25	if(working)return;
26	working =true;
27
28	elTbody.insertBefore(r_elRows[iTr],r_elRows[iTr-1]);
29
30	var tnr = numberTable(elTbody);
31	rowmovesend(pageid,iTr+offset_thead,iTr-1+offset_thead,tnr);
32}
33
34/*
35* Verschiebt eine Zeile um eins nach unten
36* el: Element unterhalb oder gleich tr Element
37* pageid: ID
38*/
39function rowdown(el,pageid)
40{
41
42	var elTr = getParentTr(el);
43	var elTbody = elTr.parentNode;
44	var r_elRows = elTbody.rows;
45	var iTr = elTr.rowIndex;
46	var count = countRows(elTr);
47	var offset_thead = r_elRows[0].rowIndex;
48
49	iTr=iTr-offset_thead;
50
51	if(iTr+1 == count) return; //letzte Zeile
52	if(!checkRow(r_elRows[iTr+1])) return; //nächste Zeile tauschbar
53
54	if(working) return;
55	var working =true;
56
57	if(iTr+2 == count)
58		elTbody.appendChild(r_elRows[iTr]);
59	else
60		elTbody.insertBefore(r_elRows[iTr],r_elRows[iTr+2]);
61	var tnr = numberTable(elTbody);
62	rowmovesend(pageid,iTr+offset_thead,iTr+1+offset_thead,tnr);
63}
64
65//send ajax data
66function rowmovesend(pageid,idx_row,idx_row2,tablenr)
67{
68	ajaxedit_send2(
69		'rowmove',
70		tablenr,
71		rowmovedone,
72		{
73			idx_row:idx_row,
74			idx_row2:idx_row2,
75		}
76	);
77}
78
79function rowmovedone(data)
80{
81	working = false;
82	var ret = ajaxedit_parse(data);
83	ajaxedit_checkResponse(ret);
84}
85
86function numberTable(elTbody) {
87	var tables = jQuery('#dokuwiki__content table');
88	for(ii=0;ii<tables.length;ii++) {
89		if(elTbody.parentNode == tables[ii]) return ii;
90	}
91	return -1;
92}
93
94/*
95* zaehlt die zeilen
96* benätigt das TBody Element
97*/
98function countRows(el)
99{
100	var count = 0;
101
102	var elTr = getParentTr(el);
103	var elTable = elTr.parentNode;
104	var count = elTable.rows.length;
105	return count;
106}
107
108/*
109* checkt ob Zeile zum tauschen makiert,
110* d.h. ob ein rowmove tag in der Zeile vorhanden ist
111*/
112function checkRow(elTr)
113{
114	var elCells = elTr.cells;
115
116	for(ii = 0; ii<elCells.length; ii++) {
117		for(kk=0; kk<elCells[ii].childNodes.length; kk++)
118			if(elCells[ii].childNodes[kk].className=="rowmove") return true;
119	}
120	return false;
121
122}
123
124/*
125* gibt das Tr Element eines darunterliegenden Elementes zurück
126*/
127function getParentTr(el)
128{
129	while ( el.nodeName.toLowerCase() != "tr")
130		el = el.parentNode;
131	return el;
132}
133