Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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