comparison media/django/js/admin/CollapsedFieldsets.js @ 1:0dcfcdf50c62

Initial import of Madeira project from the private repository.
author Brian Neal <bgneal@gmail.com>
date Mon, 06 Apr 2009 03:10:59 +0000
parents
children
comparison
equal deleted inserted replaced
0:df0370bfe3f0 1:0dcfcdf50c62
1 // Finds all fieldsets with class="collapse", collapses them, and gives each
2 // one a "Show" link that uncollapses it. The "Show" link becomes a "Hide"
3 // link when the fieldset is visible.
4
5 function findForm(node) {
6 // returns the node of the form containing the given node
7 if (node.tagName.toLowerCase() != 'form') {
8 return findForm(node.parentNode);
9 }
10 return node;
11 }
12
13 var CollapsedFieldsets = {
14 collapse_re: /\bcollapse\b/, // Class of fieldsets that should be dealt with.
15 collapsed_re: /\bcollapsed\b/, // Class that fieldsets get when they're hidden.
16 collapsed_class: 'collapsed',
17 init: function() {
18 var fieldsets = document.getElementsByTagName('fieldset');
19 var collapsed_seen = false;
20 for (var i = 0, fs; fs = fieldsets[i]; i++) {
21 // Collapse this fieldset if it has the correct class, and if it
22 // doesn't have any errors. (Collapsing shouldn't apply in the case
23 // of error messages.)
24 if (fs.className.match(CollapsedFieldsets.collapse_re) && !CollapsedFieldsets.fieldset_has_errors(fs)) {
25 collapsed_seen = true;
26 // Give it an additional class, used by CSS to hide it.
27 fs.className += ' ' + CollapsedFieldsets.collapsed_class;
28 // (<a id="fieldsetcollapser3" class="collapse-toggle" href="#">Show</a>)
29 var collapse_link = document.createElement('a');
30 collapse_link.className = 'collapse-toggle';
31 collapse_link.id = 'fieldsetcollapser' + i;
32 collapse_link.onclick = new Function('CollapsedFieldsets.show('+i+'); return false;');
33 collapse_link.href = '#';
34 collapse_link.innerHTML = gettext('Show');
35 var h2 = fs.getElementsByTagName('h2')[0];
36 h2.appendChild(document.createTextNode(' ('));
37 h2.appendChild(collapse_link);
38 h2.appendChild(document.createTextNode(')'));
39 }
40 }
41 if (collapsed_seen) {
42 // Expand all collapsed fieldsets when form is submitted.
43 addEvent(findForm(document.getElementsByTagName('fieldset')[0]), 'submit', function() { CollapsedFieldsets.uncollapse_all(); });
44 }
45 },
46 fieldset_has_errors: function(fs) {
47 // Returns true if any fields in the fieldset have validation errors.
48 var divs = fs.getElementsByTagName('div');
49 for (var i=0; i<divs.length; i++) {
50 if (divs[i].className.match(/\berrors\b/)) {
51 return true;
52 }
53 }
54 return false;
55 },
56 show: function(fieldset_index) {
57 var fs = document.getElementsByTagName('fieldset')[fieldset_index];
58 // Remove the class name that causes the "display: none".
59 fs.className = fs.className.replace(CollapsedFieldsets.collapsed_re, '');
60 // Toggle the "Show" link to a "Hide" link
61 var collapse_link = document.getElementById('fieldsetcollapser' + fieldset_index);
62 collapse_link.onclick = new Function('CollapsedFieldsets.hide('+fieldset_index+'); return false;');
63 collapse_link.innerHTML = gettext('Hide');
64 },
65 hide: function(fieldset_index) {
66 var fs = document.getElementsByTagName('fieldset')[fieldset_index];
67 // Add the class name that causes the "display: none".
68 fs.className += ' ' + CollapsedFieldsets.collapsed_class;
69 // Toggle the "Hide" link to a "Show" link
70 var collapse_link = document.getElementById('fieldsetcollapser' + fieldset_index);
71 collapse_link.onclick = new Function('CollapsedFieldsets.show('+fieldset_index+'); return false;');
72 collapse_link.innerHTML = gettext('Show');
73 },
74
75 uncollapse_all: function() {
76 var fieldsets = document.getElementsByTagName('fieldset');
77 for (var i=0; i<fieldsets.length; i++) {
78 if (fieldsets[i].className.match(CollapsedFieldsets.collapsed_re)) {
79 CollapsedFieldsets.show(i);
80 }
81 }
82 }
83 }
84
85 addEvent(window, 'load', CollapsedFieldsets.init);