~loco-django-dev/loco-django/trunk

« back to all changes in this revision

Viewing changes to locodjango/templates/default/media/js/admin/CollapsedFieldsets.js

  • Committer: NerdyNick
  • Date: 2007-10-26 04:52:01 UTC
  • Revision ID: nerdynick@gmail.com-20071026045201-4hrnxmnpddf46s63
Improved the Navigation system
        Now supports categories
        PageCategory 1 is the default menu system
        The first page in the default menu is the default page for the website
        A RequestContext variable has been added containing a dictionary of all categories and there pages
                Check locodjango.navigation.views.siteMenu for an example of how to push it into your template
                Check /template/default/sitemenu.htm for example usage of the links dictionary in a template
                Note: You will need to follow the same render_to_string shortcut for all views in order to keep you navigation on all pages
OpenID support has been started
Default template media has been included into the /template/default/media folder.
        A Symbolic link or apache virtual directory will need to be added to your apache root directory for the site

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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(/\berror\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);