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