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/>.
19 * @module core/search-input
22 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define(['jquery'], function($) {
29 * This search box div node.
36 * Toggles the form visibility.
42 var toggleForm = function(ev) {
44 if (wrapper.hasClass('expanded')) {
52 * Shows the form or submits it depending on the window size.
58 var showForm = function(ev) {
60 var windowWidth = $(document).width();
62 // We are only interested in enter and space keys (accessibility).
63 if (ev.type === 'keydown' && ev.keyCode !== 13 && ev.keyCode !== 32) {
67 if (windowWidth <= 767 && (ev.type === 'click' || ev.type === 'keydown')) {
68 // Move to the search page when using small window sizes as the input requires too much space.
71 } else if (windowWidth <= 767) {
72 // Ignore mousedown events in while using small window sizes.
76 if (ev.type === 'keydown') {
77 // We don't want to submit the form unless the user hits enter.
81 wrapper.addClass('expanded');
82 wrapper.find('form').addClass('expanded');
83 wrapper.find('input').focus();
92 var hideForm = function() {
93 wrapper.removeClass('expanded');
94 wrapper.find('form').removeClass('expanded');
104 var submitForm = function() {
105 wrapper.find('form').submit();
108 return /** @alias module:core/search-input */ {
109 // Public variables and functions.
112 * Assigns listeners to the requested select box.
115 * @param {Number} id The search wrapper div id
118 wrapper = $('#' + id);
119 wrapper.on('click mouseover keydown', 'div', toggleForm);