Commit | Line | Data |
---|---|---|
3756320f ARN |
1 | YUI.add('moodle-core-formautosubmit', function (Y, NAME) { |
2 | ||
3 | var CSS, | |
4 | FORMAUTOSUBMITNAME = 'core-formautosubmit', | |
5 | FORMAUTOSUBMIT, | |
6 | INITIALIZED = false; | |
7 | ||
8 | // The CSS selectors we use | |
9 | CSS = { | |
10 | AUTOSUBMIT : 'autosubmit' | |
11 | }; | |
12 | ||
13 | FORMAUTOSUBMIT = function() { | |
14 | FORMAUTOSUBMIT.superclass.constructor.apply(this, arguments); | |
15 | }; | |
16 | ||
17 | Y.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 | } | |
39 | if (Y.UA.ios) { | |
40 | // IOS doesn't trigger click events because it's touch-based. | |
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 | Y.log("Warning: A single_select element was renderered, but the output is not displayed on the page."); | |
55 | } | |
56 | } | |
57 | }, | |
58 | ||
59 | /** | |
60 | * Check whether the select element was changed | |
61 | */ | |
62 | check_changed : function(e) { | |
63 | var select, | |
64 | nothing, | |
65 | startindex, | |
66 | currentindex, | |
67 | previousindex; | |
68 | select = e.target.ancestor('select.' + CSS.AUTOSUBMIT, true); | |
69 | if (!select) { | |
70 | return false; | |
71 | } | |
72 | ||
73 | nothing = select.getData('nothing'); | |
74 | startindex = select.getData('startindex'); | |
75 | currentindex = select.get('selectedIndex'); | |
76 | ||
77 | previousindex = select.getAttribute('data-previousindex'); | |
78 | select.setAttribute('data-previousindex', currentindex); | |
79 | if (!previousindex) { | |
80 | previousindex = startindex; | |
81 | } | |
82 | ||
83 | // Check whether the field has changed, and is not the 'nothing' value | |
84 | if ((nothing===false || select.get('value') !== nothing) | |
85 | && startindex !== select.get('selectedIndex') && currentindex !== previousindex) { | |
86 | return select; | |
87 | } | |
88 | return false; | |
89 | }, | |
90 | ||
91 | /** | |
92 | * Process any changes | |
93 | */ | |
94 | process_changes : function(e) { | |
95 | var select = this.check_changed(e), | |
96 | form; | |
97 | if (select) { | |
98 | form = select.ancestor('form', true); | |
99 | form.submit(); | |
100 | } | |
101 | } | |
102 | }, | |
103 | { | |
104 | NAME : FORMAUTOSUBMITNAME, | |
105 | ATTRS : { | |
106 | selectid : { | |
107 | 'value' : '' | |
108 | }, | |
109 | nothing : { | |
110 | 'value' : '' | |
111 | }, | |
112 | ignorechangeevent : { | |
113 | 'value' : false | |
114 | } | |
115 | } | |
116 | }); | |
117 | ||
118 | M.core = M.core || {}; | |
119 | M.core.init_formautosubmit = M.core.init_formautosubmit || function(config) { | |
120 | return new FORMAUTOSUBMIT(config); | |
121 | }; | |
122 | ||
123 | ||
124 | }, '@VERSION@', {"requires": ["base", "event-key"]}); |