summaryrefslogtreecommitdiff
path: root/layout/xul/grid/examples/dynamicgrid.xul
blob: d718df5f917f1b0da2062e3d63fa277f8a12b5d0 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?xml version="1.0"?> 
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->


<?xml-stylesheet href="chrome://global/skin/" type="text/css"?> 
<?xml-stylesheet href="gridsample.css" type="text/css"?> 

<!DOCTYPE window> 


<window orient="vertical" 
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
		onload="start()"> 

<script>

   var selected;
   var count = 0;

   function isCell(box)
   {
     if (box.localName == "row" || 
	     box.localName == "column" ||
		 box.localName == "rows" ||
		 box.localName == "columns" ||
		 box.localName == "grid")
		 return false;

	 return true;
   }

   function start()
   {
      selectIt(window.document.getElementById("rows"));
   }

   function selectIt(box)
   {
     if (!box)
	   return;

     var a = box.getAttribute("selected");
	 if (a != "true") {
	   box.setAttribute("selected","true");
  	   if (selected)
	      selected.setAttribute("selected","false");

	   selected = box;
     }
   }

   function addCellSelectionHandle(box)
   {
	 box.setAttribute("oncommand", "selectIt(this);");
   }

   function addRowColumnSelectionHandle(box)
   {
	 box.setAttribute("onclick", "selectIt(this);");
   }

   function createButton(str)
   {
	 var b = document.createElement("button");
	 b.setAttribute("label", str+count);
	 count++;
	 addCellSelectionHandle(b);
	 return b;
   }

   function createRow()
   {
	 var b = document.createElement("row");
 	 b.setAttribute("dynamic","true");

	 addRowColumnSelectionHandle(b);
	 return b;
   }

   function createColumn()
   {
	 var b = document.createElement("column");
	 b.setAttribute("dynamic","true");
	 addRowColumnSelectionHandle(b);
	 return b;
   }

   function createText(str)
   {
	 var text = document.createElement("text");
	 text.setAttribute("value", str+count);
	 count++;
	 text.setAttribute("style", "font-size: 40pt");
	 addCellSelectionHandle(text);
	 return text;
   }

   function appendElement(element, prepend)
   {
     if (!selected)
	    return;

     setUserAttribute(element);

	 if (selected.localName == "rows") 
	   appendRow(false);
     else if (selected.localName == "columns")
	   appendColumn(false);

	 if (selected.localName == "row" || selected.localName == "column" ) { // is row or column
		 selected.appendChild(element); 
	 } else {
		  var parent = selected.parentNode;
		  if (prepend)
		    parent.insertBefore(element, selected);	 
		  else {
		    var next = selected.nextSibling;
			if (next)
		      parent.insertBefore(element,next);
			else
			  parent.appendChild(element);
		  }
	 }
     
	 selectIt(element);
   }

   function getRows(box)
   {
      return window.document.getElementById("rows");
   }

   function getColumns(box)
   {
      return window.document.getElementById("columns");
   }

   function setUserAttribute(element)
   {
   	 var attributeBox = document.getElementById("attributebox");
	 var valueBox = document.getElementById("valuebox");
	 var attribute = attributeBox.value;
	 var value = valueBox.value;
	 if (attribute != "")
	   element.setAttribute(attribute,value);
   }

   function appendRowColumn(rowColumn, prepend)
   {
     if (!selected)
	    return;

     setUserAttribute(rowColumn); 

     var row = rowColumn;

	 // first see what we are adding.

	 if (isCell(selected)) { // if cell then select row/column
	   selectIt(selected.parentNode);
	 }

	 if (selected.localName == "row" || selected.localName == "rows")
	   if (row.localName == "column") {
	     selectIt(getColumns(selected)); 
		 dump("Selecting the column")
		 dump("Selected="+selected.localName);
		 }

	 if (selected.localName == "column" || selected.localName == "columns")
	   if (row.localName == "row")
	     selectIt(getRows(selected));

	 if (selected.localName == "rows" || selected.localName == "columns" ) 
	 { // if rows its easy
		 selected.appendChild(row); 
	 } else {
		  var parent = selected.parentNode;
		  if (prepend)
		    parent.insertBefore(row, selected);	 
		  else {
		    var next = selected.nextSibling;
			if (next)
		      parent.insertBefore(row,next);
			else
			  parent.appendChild(row);
		  }
	 }

     selectIt(row);
   }

 function appendRow(prepend)
 {
   var row = createRow();
   appendRowColumn(row,prepend);
 }


 function appendColumn(prepend)
 {
   var column = createColumn();
   appendRowColumn(column,prepend);
 }


 function selectRows()
 {
    var rows = getRows();
	if (rows.firstChild)
      selectIt(rows.firstChild);
	else
	  selectIt(rows);
 }


 function selectColumns()
 {
    var columns = getColumns();
	if (columns.firstChild)
      selectIt(columns.firstChild);
	else
	  selectIt(columns);
 }

 function nextElement()
 {
   if (!selected)
     return;

   selectIt(selected.nextSibling);
 }

 function previousElement()
 {
   if (!selected)
     return;

   selectIt(selected.previousSibling);
 }

 function selectRow()
 {
   if (!selected)
     return;

   if (selected.localName == "row")
     return;

   if (isCell(selected)) {
     if (selected.parentNode.localName == "row")
       selectIt(selected.parentNode);
   }
 }

 function selectColumn()
 {
   if (!selected)
     return;

   if (selected.localName == "column")
     return;

   if (isCell(selected)) {
     if (selected.parentNode.localName == "column")
       selectIt(selected.parentNode);
   }
 }

 function collapseGrid()
 {
	 var grid = document.getElementById("grid");
	 var collapsed = grid.getAttribute("collapsed");

	 if (collapsed == "")
	   grid.setAttribute("collapsed","true");
	 else
   	   grid.setAttribute("collapsed","");

 }

 function collapseElement()
 {
   if (selected) {
	 var collapsed = selected.getAttribute("collapsed");

	 if (collapsed == "")
	   selected.setAttribute("collapsed","true");
	 else
   	   selected.setAttribute("collapsed","");
	}
 }

</script>

    <hbox flex="1" style="border: 2px inset gray; overflow: auto">
	 <vbox flex="1">
	  <hbox>
		<grid id="grid" style="border: 2px solid red;">
		   <columns id="columns">
		   </columns>

		   <rows start="true" id="rows">
		   </rows>
		</grid>
	    <spacer flex="1"/>
      </hbox>
 	    <spacer flex="1"/>
	 </vbox>
    </hbox>

    <grid style="background-color: blue">
	   <columns>
             <column flex="1"/> 
             <column flex="1"/>
             <column flex="1"/>             
             <column flex="1"/>
 	   </columns>
	 <rows>
     
         <row>
		  <button label="append row" oncommand="appendRow(false);"/>
		  <button label="prepend row" oncommand="appendRow(true);"/>

		  <button label="append column" oncommand="appendColumn(false);"/>
		  <button label="prepend column" oncommand="appendColumn(true);"/>
		 </row>

        <row>

		 <button label="append button" oncommand="appendElement(createButton('button'),false);"/>
		 <button label="prepend button" oncommand="appendElement(createButton('button'),true);"/>

		 <button label="append text" oncommand="appendElement(createText('text'),false);"/>
		 <button label="prepend text" oncommand="appendElement(createText('text'),true);"/>

        </row>

		 <row>

 	     <button label="select rows" oncommand="selectRows()"/>
	     <button label="select columns" oncommand="selectColumns()"/>

		 <button label="next" oncommand="nextElement()"/>
		 <button label="previous" oncommand="previousElement()"/>

        </row>

	<hbox align="center">
 	     <button label="collapse/uncollapse grid" flex="1" oncommand="collapseGrid()"/>
 	     <button label="collapse/uncollapse element" flex="1" oncommand="collapseElement()"/>
        </hbox>



	<hbox>

         <text value="attribute"/>
		 <textbox id="attributebox" value="" flex="1"/>
         <text value="value"/>
		 <textbox id="valuebox" value="" flex="2"/>
        </hbox>


       </rows>
	</grid>

</window>