mirrored from git://git.moodle.org/moodle.git
/
shortforms.js
96 lines (88 loc) · 3 KB
/
shortforms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
YUI.add('moodle-form-shortforms', function(Y) {
/**
* Provides the form shortforms class
*
* @module moodle-form-shortforms
*/
/**
* A class for a shortforms
*
* @param {Object} config Object literal specifying shortforms configuration properties.
* @class M.form.shortforms
* @constructor
* @extends Y.Base
*/
function SHORTFORMS(config) {
SHORTFORMS.superclass.constructor.apply(this, [config]);
}
var SELECTORS = {
FIELDSETCOLLAPSIBLE : 'fieldset.collapsible',
LEGENDFTOGGLER : 'legend.ftoggler'
},
CSS = {
COLLAPSED : 'collapsed',
FHEADER : 'fheader',
JSPROCESSED : 'jsprocessed'
},
ATTRS = {};
/**
* Static property provides a string to identify the JavaScript class.
*
* @property NAME
* @type String
* @static
*/
SHORTFORMS.NAME = 'moodle-form-shortforms';
/**
* Static property used to define the default attribute configuration for the Shortform.
*
* @property ATTRS
* @type String
* @static
*/
SHORTFORMS.ATTRS = ATTRS;
/**
* The form ID attribute definition
*
* @attribute formid
* @type String
* @default ''
* @writeOnce
*/
ATTRS.formid = {
value : null
};
Y.extend(SHORTFORMS, Y.Base, {
initializer : function() {
var fieldlist = Y.Node.all('#'+this.get('formid')+' '+SELECTORS.FIELDSETCOLLAPSIBLE);
// Look through collapsible fieldset divs
fieldlist.each(this.process_fieldset, this);
// Subscribe collapsible fieldsets to click event
Y.one('#'+this.get('formid')).delegate('click', this.switch_state, SELECTORS.FIELDSETCOLLAPSIBLE+' .'+CSS.FHEADER);
},
process_fieldset : function(fieldset) {
fieldset.addClass(CSS.JSPROCESSED);
// Get legend element
var legendelement = fieldset.one(SELECTORS.LEGENDFTOGGLER);
// Turn headers to links for accessibility
var headerlink = Y.Node.create('<a href="#"></a>');
headerlink.addClass(CSS.FHEADER);
headerlink.appendChild(legendelement.get('firstChild'));
legendelement.prepend(headerlink);
},
switch_state : function(e) {
e.preventDefault();
var fieldset = this.ancestor(SELECTORS.FIELDSETCOLLAPSIBLE);
// toggle collapsed class
fieldset.toggleClass(CSS.COLLAPSED);
// get corresponding hidden variable
var statuselement = new Y.one('input[name=mform_isexpanded_'+fieldset.get('id')+']');
// and invert it
statuselement.set('value', Math.abs(Number(statuselement.get('value'))-1));
}
});
M.form = M.form || {};
M.form.shortforms = M.form.shortforms || function(params) {
return new SHORTFORMS(params);
};
}, '@VERSION@', {requires:['base', 'node', 'selector-css3']});