Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('moodle-form-shortforms', function (Y, NAME) {
2
 
3
/**
4
 * Provides the form shortforms class.
5
 *
6
 * @module moodle-form-shortforms
7
 */
8
 
9
/**
10
 * A class for a shortforms.
11
 *
12
 * @class M.form.shortforms
13
 * @constructor
14
 * @extends Base
15
 */
16
function SHORTFORMS() {
17
    SHORTFORMS.superclass.constructor.apply(this, arguments);
18
}
19
 
20
var SELECTORS = {
21
        COLLAPSED: '.collapsed',
22
        FIELDSETCOLLAPSIBLE: 'fieldset.collapsible',
23
        FIELDSETLEGENDLINK: 'fieldset.collapsible .fheader',
24
        FHEADER: '.fheader',
25
        LEGENDFTOGGLER: 'legend.ftoggler'
26
    },
27
    CSS = {
28
        COLLAPSEALL: 'collapse-all',
29
        COLLAPSED: 'collapsed',
30
        FHEADER: 'fheader'
31
    },
32
    ATTRS = {};
33
 
34
/**
35
 * The form ID attribute definition.
36
 *
37
 * @attribute formid
38
 * @type String
39
 * @default ''
40
 * @writeOnce
41
 */
42
ATTRS.formid = {
43
    value: null
44
};
45
 
46
Y.extend(SHORTFORMS, Y.Base, {
47
    /**
48
     * A reference to the form.
49
     *
50
     * @property form
51
     * @protected
52
     * @type Node
53
     * @default null
54
     */
55
    form: null,
56
 
57
    /**
58
     * The initializer for the shortforms instance.
59
     *
60
     * @method initializer
61
     * @protected
62
     */
63
    initializer: function() {
64
        var form = Y.one('#' + this.get('formid'));
65
        if (!form) {
66
            return;
67
        }
68
        // Stores the form in the object.
69
        this.form = form;
70
 
71
        // Subscribe collapsible fieldsets and buttons to click events.
72
        form.delegate('click', this.switch_state, SELECTORS.FIELDSETLEGENDLINK, this);
73
 
74
        // Handle event, when there's an error in collapsed section.
75
        Y.Global.on(M.core.globalEvents.FORM_ERROR, this.expand_fieldset, this);
76
    },
77
 
78
    /**
79
     * Set the collapsed state for the specified fieldset.
80
     *
81
     * @method set_state
82
     * @param {Node} fieldset The Node relating to the fieldset to set state on.
83
     * @param {Boolean} [collapsed] Whether the fieldset is collapsed.
84
     * @chainable
85
     */
86
    set_state: function(fieldset, collapsed) {
87
        var headerlink = fieldset.one(SELECTORS.FHEADER);
88
        if (collapsed) {
89
            fieldset.addClass(CSS.COLLAPSED);
90
            if (headerlink) {
91
                headerlink.setAttribute('aria-expanded', 'false');
92
            }
93
        } else {
94
            fieldset.removeClass(CSS.COLLAPSED);
95
            if (headerlink) {
96
                headerlink.setAttribute('aria-expanded', 'true');
97
            }
98
        }
99
        var statuselement = this.form.one('input[name=mform_isexpanded_' + fieldset.get('id') + ']');
100
        if (!statuselement) {
101
            return this;
102
        }
103
        statuselement.set('value', collapsed ? 0 : 1);
104
 
105
        return this;
106
    },
107
 
108
    /**
109
     * Toggle the state for the fieldset that was clicked.
110
     *
111
     * @method switch_state
112
     * @param {EventFacade} e
113
     */
114
    switch_state: function(e) {
115
        e.preventDefault();
116
        var fieldset = e.target.ancestor(SELECTORS.FIELDSETCOLLAPSIBLE);
117
        this.set_state(fieldset, !fieldset.hasClass(CSS.COLLAPSED));
118
    },
119
 
120
    /**
121
     * Expand the fieldset, which contains an error.
122
     *
123
     * @method expand_fieldset
124
     * @param {EventFacade} e
125
     */
126
    expand_fieldset: function(e) {
127
        e.stopPropagation();
128
        var formid = e.formid;
129
        if (formid === this.form.getAttribute('id')) {
130
            var errorfieldset = Y.one('#' + e.elementid).ancestor('fieldset');
131
            if (errorfieldset) {
132
                this.set_state(errorfieldset, false);
133
            }
134
 
135
        }
136
   }
137
}, {
138
    NAME: 'moodle-form-shortforms',
139
    ATTRS: ATTRS
140
});
141
 
142
M.form = M.form || {};
143
M.form.shortforms = M.form.shortforms || function(params) {
144
    return new SHORTFORMS(params);
145
};
146
 
147
 
148
}, '@VERSION@', {"requires": ["node", "base", "selector-css3", "moodle-core-event"]});