1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * JavaScript required by the question engine.
20 * @subpackage questionengine
21 * @copyright 2008 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * Scroll manager is a class that help with saving the scroll positing when you
28 * click on an action icon, and then when the page is reloaded after processing
29 * the action, it scrolls you to exactly where you were. This is much nicer for
32 * To use this in your code, you need to ensure that:
33 * 1. The button that triggers the action has to have a click event handler that
34 * calls M.core_scroll_manager.save_scroll_pos
35 * 2. The script that process the action has to grab the scrollpos parameter
36 * using $scrollpos = optional_param('scrollpos', 0, PARAM_INT);
37 * 3. After doing the processing, it must add ->param('scrollpos', $scrollpos)
38 * to the URL that it redirects to.
39 * 4. Finally, on the page that is reloaded (which should be the same as the one
40 * the user started on) you need to call M.core_scroll_manager.scroll_to_saved_pos
43 M.core_scroll_manager = M.core_scroll_manager || {};
46 * In the form that contains the element, set the value of the form field with
47 * name scrollpos to the current scroll position. If there is no element with
48 * that name, it creates a hidden form field wiht that name within the form.
49 * @param element the element in the form. Should be something that can be
52 M.core_scroll_manager.save_scroll_pos = function(Y, element) {
53 if (typeof(element) == 'string') {
54 // Have to use getElementById here because element id can contain :.
55 element = Y.one(document.getElementById(element));
57 var form = element.ancestor('form');
61 var scrollpos = form.one('input[name=scrollpos]');
63 scrollpos = form.appendChild(form.create('<input type="hidden" name="scrollpos" />'));
65 scrollpos.set('value', form.get('docScrollY'));
69 * Event handler that can be used on a link. Assumes that the link already
70 * contains at least one URL parameter.
72 M.core_scroll_manager.save_scroll_action = function(e) {
73 var link = e.target.ancestor('a[href]');
75 M.core_scroll_manager.save_scroll_pos({}, e.target);
78 link.set('href', link.get('href') + '&scrollpos=' + link.get('docScrollY'));
82 * If there is a parameter like scrollpos=123 in the URL, scroll to that saved position.
84 M.core_scroll_manager.scroll_to_saved_pos = function(Y) {
85 var matches = window.location.href.match(/^.*[?&]scrollpos=(\d*)(?:&|$|#).*$/, '$1');
87 // onDOMReady is the effective one here. I am leaving the immediate call to
88 // window.scrollTo in case it reduces flicker.
89 window.scrollTo(0, matches[1]);
90 Y.on('domready', function() { window.scrollTo(0, matches[1]); });
92 // And the following horror is necessary to make it work in IE 8.
93 // Note that the class ie8 on body is only there in Moodle 2.0 and OU Moodle.
94 if (Y.one('body').hasClass('ie')) {
95 M.core_scroll_manager.force_ie_to_scroll(Y, matches[1])
101 * Beat IE into submission.
102 * @param targetpos the target scroll position.
104 M.core_scroll_manager.force_ie_to_scroll = function(Y, targetpos) {
106 function do_scroll() {
107 window.scrollTo(0, targetpos);
110 setTimeout(do_scroll, 10);
113 Y.on('load', do_scroll, window);
116 M.core_question_engine = M.core_question_engine || {};
119 * Flag used by M.core_question_engine.prevent_repeat_submission.
121 M.core_question_engine.questionformalreadysubmitted = false;
124 * Initialise a question submit button. This saves the scroll position and
125 * sets the fragment on the form submit URL so the page reloads in the right place.
126 * @param id the id of the button in the HTML.
127 * @param slot the number of the question_attempt within the usage.
129 M.core_question_engine.init_submit_button = function(Y, button, slot) {
130 var buttonel = document.getElementById(button);
131 Y.on('click', function(e) {
132 M.core_scroll_manager.save_scroll_pos(Y, button);
133 buttonel.form.action = buttonel.form.action + '#q' + slot;
138 * Initialise a form that contains questions printed using print_question.
139 * This has the effect of:
140 * 1. Turning off browser autocomlete.
141 * 2. Stopping enter from submitting the form (or toggling the next flag) unless
142 * keyboard focus is on the submit button or the flag.
143 * 3. Removes any '.questionflagsavebutton's, since we have JavaScript to toggle
144 * the flags using ajax.
145 * 4. Scroll to the position indicated by scrollpos= in the URL, if it is there.
146 * 5. Prevent the user from repeatedly submitting the form.
147 * @param Y the Yahoo object. Needs to have the DOM and Event modules loaded.
148 * @param form something that can be passed to Y.one, to find the form element.
150 M.core_question_engine.init_form = function(Y, form) {
151 Y.one(form).setAttribute('autocomplete', 'off');
153 Y.on('submit', M.core_question_engine.prevent_repeat_submission, form, form, Y);
155 Y.on('key', function (e) {
156 if (!e.target.test('a') && !e.target.test('input[type=submit]') &&
157 !e.target.test('input[type=img]') && !e.target.test('textarea') && !e.target.test('[contenteditable=true]')) {
160 }, form, 'press:13');
162 Y.one(form).all('.questionflagsavebutton').remove();
164 M.core_scroll_manager.scroll_to_saved_pos(Y);
168 * Event handler to stop a question form being submitted more than once.
169 * @param e the form submit event.
170 * @param form the form element.
172 M.core_question_engine.prevent_repeat_submission = function(e, Y) {
173 if (M.core_question_engine.questionformalreadysubmitted) {
178 setTimeout(function() {
179 Y.all('input[type=submit]').set('disabled', true);
181 M.core_question_engine.questionformalreadysubmitted = true;