[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/templatelibrary/amd/src/ -> search.js (source)

   1  // This file is part of Moodle - http://moodle.org/
   2  //
   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.
   7  //
   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.
  12  //
  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/>.
  15  
  16  /**
  17   * This module adds ajax search functions to the template library page.
  18   *
  19   * @module     tool_templatelibrary/search
  20   * @package    tool_templatelibrary
  21   * @copyright  2015 Damyon Wiese <damyon@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  define(['jquery', 'core/ajax', 'core/log', 'core/notification', 'core/templates'],
  25         function($, ajax, log, notification, templates) {
  26  
  27      /**
  28       * The ajax call has returned with a new list of templates.
  29       *
  30       * @method reloadListTemplate
  31       * @param {String[]} templateList List of template ids.
  32       */
  33      var reloadListTemplate = function(templateList) {
  34          templates.render('tool_templatelibrary/search_results', {templates: templateList})
  35              .done(function(result, js) {
  36                  templates.replaceNode($('[data-region="searchresults"]'), result, js);
  37              }).fail(notification.exception);
  38      };
  39  
  40      /**
  41       * Get the current values for the form inputs and refresh the list of matching templates.
  42       *
  43       * @method refreshSearch
  44       */
  45      var refreshSearch = function() {
  46          var componentStr = $('[data-field="component"]').val();
  47          var searchStr = $('[data-field="search"]').val();
  48  
  49          // Trigger the search.
  50  
  51          ajax.call([
  52              {methodname: 'tool_templatelibrary_list_templates',
  53                args: {component: componentStr, search: searchStr},
  54                done: reloadListTemplate,
  55                fail: notification.exception}
  56          ], true, false);
  57      };
  58  
  59      var throttle = null;
  60  
  61      /**
  62       * Call the specified function after a delay. If this function is called again before the function is executed,
  63       * the function will only be executed once.
  64       *
  65       * @method queueRefresh
  66       * @param {function} callback
  67       * @param {Number} delay The time in milliseconds to delay.
  68       */
  69      var queueRefresh = function(callback, delay) {
  70          if (throttle !== null) {
  71              window.clearTimeout(throttle);
  72          }
  73  
  74          throttle = window.setTimeout(function() {
  75              callback();
  76              throttle = null;
  77          }, delay);
  78      };
  79  
  80      var changeHandler = function() {
  81          queueRefresh(refreshSearch, 400);
  82      };
  83      // Add change handlers to refresh the list.
  84      $('[data-region="list-templates"]').on('change', '[data-field="component"]', changeHandler);
  85      $('[data-region="list-templates"]').on('input', '[data-field="search"]', changeHandler);
  86  
  87      refreshSearch();
  88      return {};
  89  });


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1