Merge branch 'MDL-39776-master' of git://github.com/phalacee/moodle
[moodle.git] / lib / yui / build / moodle-core-formautosubmit / moodle-core-formautosubmit.js
CommitLineData
3756320f
ARN
1YUI.add('moodle-core-formautosubmit', function (Y, NAME) {
2
3var CSS,
4 FORMAUTOSUBMITNAME = 'core-formautosubmit',
5 FORMAUTOSUBMIT,
6 INITIALIZED = false;
7
8// The CSS selectors we use
9CSS = {
10 AUTOSUBMIT : 'autosubmit'
11};
12
13FORMAUTOSUBMIT = function() {
14 FORMAUTOSUBMIT.superclass.constructor.apply(this, arguments);
15};
16
17Y.extend(FORMAUTOSUBMIT, Y.Base, {
18
19 /**
20 * Initialize the module
21 */
22 initializer : function() {
23 // Set up local variables
24 var applyto,
25 thisselect;
26 // We only apply the delegation once
27 if (!INITIALIZED) {
28 INITIALIZED = true;
29 applyto = Y.one('body');
30
31 // We don't listen for change events by default as using the keyboard triggers these too.
32 applyto.delegate('key', this.process_changes, 'press:13', 'select.' + CSS.AUTOSUBMIT, this);
33 applyto.delegate('click', this.process_changes, 'select.' + CSS.AUTOSUBMIT, this);
34
35 if (Y.UA.os === 'macintosh' && Y.UA.webkit) {
36 // Macintosh webkit browsers like change events, but non-macintosh webkit browsers don't.
37 applyto.delegate('change', this.process_changes, 'select.' + CSS.AUTOSUBMIT, this);
38 }
61411916
JF
39 if (Y.UA.touchEnabled) {
40 // IOS and Android trigger touch events.
3756320f
ARN
41 applyto.delegate('change', this.process_changes, 'select.' + CSS.AUTOSUBMIT, this);
42 }
43 }
44
45 // Assign this select items 'nothing' value and lastindex (current value)
46 if (this.get('selectid')) {
47 thisselect = Y.one('select#' + this.get('selectid'));
48 if (thisselect) {
49 if (this.get('nothing')) {
50 thisselect.setData('nothing', this.get('nothing'));
51 }
52 thisselect.setData('startindex', thisselect.get('selectedIndex'));
53 } else {
54 }
55 }
56 },
57
58 /**
59 * Check whether the select element was changed
60 */
61 check_changed : function(e) {
62 var select,
63 nothing,
64 startindex,
65 currentindex,
66 previousindex;
67 select = e.target.ancestor('select.' + CSS.AUTOSUBMIT, true);
68 if (!select) {
69 return false;
70 }
71
72 nothing = select.getData('nothing');
73 startindex = select.getData('startindex');
74 currentindex = select.get('selectedIndex');
75
76 previousindex = select.getAttribute('data-previousindex');
77 select.setAttribute('data-previousindex', currentindex);
78 if (!previousindex) {
79 previousindex = startindex;
80 }
81
82 // Check whether the field has changed, and is not the 'nothing' value
83 if ((nothing===false || select.get('value') !== nothing)
84 && startindex !== select.get('selectedIndex') && currentindex !== previousindex) {
85 return select;
86 }
87 return false;
88 },
89
90 /**
91 * Process any changes
92 */
93 process_changes : function(e) {
94 var select = this.check_changed(e),
95 form;
96 if (select) {
97 form = select.ancestor('form', true);
98 form.submit();
99 }
100 }
101},
102{
103 NAME : FORMAUTOSUBMITNAME,
104 ATTRS : {
105 selectid : {
106 'value' : ''
107 },
108 nothing : {
109 'value' : ''
110 },
111 ignorechangeevent : {
112 'value' : false
113 }
114 }
115});
116
117M.core = M.core || {};
118M.core.init_formautosubmit = M.core.init_formautosubmit || function(config) {
119 return new FORMAUTOSUBMIT(config);
120};
121
122
123}, '@VERSION@', {"requires": ["base", "event-key"]});