mirror of
https://github.com/moodle/moodle.git
synced 2025-07-21 14:21:42 +02:00
MDL-31989 search: Search API and search engine API
Introducing both APIs in moodle along with: - search_box widget to add a tiny search box - admin settings with setup steps helper - cache for search results - template for a search result - php unit stuff Many thanks to Tomasz Muras, Prateek Sachan and Daniel Neis for their contributions, for starting this development and for pushing for it to be completed. Also thanks to other contributors: Jonathan Harker and eugeneventer.
This commit is contained in:
committed by
Dan Poltawski
parent
95c6aeaf1c
commit
db48207e1a
117
lib/amd/src/search-input.js
Normal file
117
lib/amd/src/search-input.js
Normal file
@@ -0,0 +1,117 @@
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Search box.
|
||||
*
|
||||
* @module core/search-input
|
||||
* @class search-input
|
||||
* @package core
|
||||
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.1
|
||||
*/
|
||||
define(['jquery'], function($) {
|
||||
|
||||
/**
|
||||
* This search box div node.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
var wrapper = null;
|
||||
|
||||
/**
|
||||
* Toggles the form visibility.
|
||||
*
|
||||
* @param {Event} ev
|
||||
* @method toggleForm
|
||||
* @private
|
||||
*/
|
||||
var toggleForm = function(ev) {
|
||||
|
||||
if (wrapper.hasClass('expanded')) {
|
||||
hideForm();
|
||||
} else {
|
||||
showForm(ev);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Shows the form or submits it depending on the window size.
|
||||
*
|
||||
* @param {Event} ev
|
||||
* @method showForm
|
||||
* @private
|
||||
*/
|
||||
var showForm = function(ev) {
|
||||
|
||||
var windowWidth = $(document).width();
|
||||
|
||||
if (windowWidth <= 767 && (ev.type === 'click' || ev.type === 'keydown')) {
|
||||
// Move to the search page when using small window sizes as the input requires too much space.
|
||||
submitForm();
|
||||
return;
|
||||
} else if (windowWidth <= 767) {
|
||||
// Ignore mousedown events in while using small window sizes.
|
||||
return;
|
||||
}
|
||||
|
||||
if (ev.type === 'keydown') {
|
||||
// We don't want to submit the form unless the user hits enter.
|
||||
ev.preventDefault();
|
||||
}
|
||||
|
||||
wrapper.addClass('expanded');
|
||||
wrapper.find('form').addClass('expanded');
|
||||
wrapper.find('input').focus();
|
||||
};
|
||||
|
||||
/**
|
||||
* Hides the form.
|
||||
*
|
||||
* @method hideForm
|
||||
* @private
|
||||
*/
|
||||
var hideForm = function() {
|
||||
wrapper.removeClass('expanded');
|
||||
wrapper.find('form').removeClass('expanded');
|
||||
};
|
||||
|
||||
/**
|
||||
* Submits the form.
|
||||
*
|
||||
* @param {Event} ev
|
||||
* @method submitForm
|
||||
* @private
|
||||
*/
|
||||
var submitForm = function() {
|
||||
wrapper.find('form').submit();
|
||||
};
|
||||
|
||||
return /** @alias module:core/search-input */ {
|
||||
// Public variables and functions.
|
||||
|
||||
/**
|
||||
* Assigns listeners to the requested select box.
|
||||
*
|
||||
* @method init
|
||||
* @param {Number} id The search wrapper div id
|
||||
*/
|
||||
init: function(id) {
|
||||
wrapper = $('#' + id);
|
||||
wrapper.on('click mouseover keydown', 'div', toggleForm);
|
||||
}
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user