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
            Y.log('Could not locate the form', 'warn', 'moodle-form-shortforms');
67
            return;
68
        }
69
        // Stores the form in the object.
70
        this.form = form;
71
 
72
        // Subscribe collapsible fieldsets and buttons to click events.
73
        form.delegate('click', this.switch_state, SELECTORS.FIELDSETLEGENDLINK, this);
74
 
75
        // Handle event, when there's an error in collapsed section.
76
        Y.Global.on(M.core.globalEvents.FORM_ERROR, this.expand_fieldset, this);
77
    },
78
 
79
    /**
80
     * Set the collapsed state for the specified fieldset.
81
     *
82
     * @method set_state
83
     * @param {Node} fieldset The Node relating to the fieldset to set state on.
84
     * @param {Boolean} [collapsed] Whether the fieldset is collapsed.
85
     * @chainable
86
     */
87
    set_state: function(fieldset, collapsed) {
88
        var headerlink = fieldset.one(SELECTORS.FHEADER);
89
        if (collapsed) {
90
            fieldset.addClass(CSS.COLLAPSED);
91
            if (headerlink) {
92
                headerlink.setAttribute('aria-expanded', 'false');
93
            }
94
        } else {
95
            fieldset.removeClass(CSS.COLLAPSED);
96
            if (headerlink) {
97
                headerlink.setAttribute('aria-expanded', 'true');
98
            }
99
        }
100
        var statuselement = this.form.one('input[name=mform_isexpanded_' + fieldset.get('id') + ']');
101
        if (!statuselement) {
102
            Y.log("M.form.shortforms::switch_state was called on an fieldset without a status field: '" +
103
                fieldset.get('id') + "'", 'debug', 'moodle-form-shortforms');
104
            return this;
105
        }
106
        statuselement.set('value', collapsed ? 0 : 1);
107
 
108
        return this;
109
    },
110
 
111
    /**
112
     * Toggle the state for the fieldset that was clicked.
113
     *
114
     * @method switch_state
115
     * @param {EventFacade} e
116
     */
117
    switch_state: function(e) {
118
        e.preventDefault();
119
        var fieldset = e.target.ancestor(SELECTORS.FIELDSETCOLLAPSIBLE);
120
        this.set_state(fieldset, !fieldset.hasClass(CSS.COLLAPSED));
121
    },
122
 
123
    /**
124
     * Expand the fieldset, which contains an error.
125
     *
126
     * @method expand_fieldset
127
     * @param {EventFacade} e
128
     */
129
    expand_fieldset: function(e) {
130
        e.stopPropagation();
131
        var formid = e.formid;
132
        if (formid === this.form.getAttribute('id')) {
133
            var errorfieldset = Y.one('#' + e.elementid).ancestor('fieldset');
134
            if (errorfieldset) {
135
                this.set_state(errorfieldset, false);
136
            }
137
 
138
        }
139
   }
140
}, {
141
    NAME: 'moodle-form-shortforms',
142
    ATTRS: ATTRS
143
});
144
 
145
M.form = M.form || {};
146
M.form.shortforms = M.form.shortforms || function(params) {
147
    return new SHORTFORMS(params);
148
};
149
 
150
 
151
}, '@VERSION@', {"requires": ["node", "base", "selector-css3", "moodle-core-event"]});