[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/tool/capability/yui/src/search/js/ -> search.js (source)

   1  /**
   2   * This file contains the capability overview search functionality.
   3   *
   4   * @module moodle-tool_capability-search
   5   */
   6  
   7  /**
   8   * Constructs a new capability search manager.
   9   *
  10   * @namespace M.tool_capability
  11   * @class Search
  12   * @constructor
  13   * @extends Base
  14   */
  15  var SEARCH = function() {
  16      SEARCH.superclass.constructor.apply(this, arguments);
  17  };
  18  SEARCH.prototype = {
  19      /**
  20       * The search form.
  21       * @property form
  22       * @type Node
  23       * @protected
  24       */
  25      form : null,
  26      /**
  27       * The capability select node.
  28       * @property select
  29       * @type Node
  30       * @protected
  31       */
  32      select: null,
  33      /**
  34       * An associative array of search option. Populated from the select node above during initialisation.
  35       * @property selectoptions
  36       * @type Object
  37       * @protected
  38       */
  39      selectoptions : {},
  40      /**
  41       * The search input field.
  42       * @property input
  43       * @type Node
  44       * @protected
  45       */
  46      input: null,
  47      /**
  48       * The submit button for the form.
  49       * @property button
  50       * @type Node
  51       * @protected
  52       */
  53      button: null,
  54      /**
  55       * The last search node if there is one.
  56       * If there is a last search node then the last search term will be persisted between requests.
  57       * @property lastsearch
  58       * @type Node
  59       * @protected
  60       */
  61      lastsearch : null,
  62      /**
  63       * Constructs the search manager.
  64       * @method initializer
  65       */
  66      initializer : function() {
  67          this.form = Y.one('#capability-overview-form');
  68          this.select = this.form.one('select[data-search=capability]');
  69          this.select.setStyle('minWidth', this.select.get('offsetWidth'));
  70          this.select.get('options').each(function(option) {
  71              var capability = option.get('value');
  72              this.selectoptions[capability] = option;
  73          }, this);
  74          this.button = this.form.all('input[type=submit]');
  75          this.lastsearch = this.form.one('input[name=search]');
  76  
  77          var div = Y.Node.create('<div id="capabilitysearchui"></div>'),
  78              label = Y.Node.create('<label for="capabilitysearch">'+this.get('strsearch')+'</label>');
  79          this.input = Y.Node.create('<input type="text" id="capabilitysearch" />');
  80  
  81          div.append(label).append(this.input);
  82  
  83          this.select.insert(div, 'before');
  84          this.select.one('option').setStyle('display', 'none');
  85  
  86          this.input.on('keyup', this.typed, this);
  87          this.select.on('change', this.validate, this);
  88  
  89          if (this.lastsearch) {
  90              this.input.set('value', this.lastsearch.get('value'));
  91              this.typed();
  92              if (this.select.one('option[selected]')) {
  93                  this.select.set('scrollTop', this.select.one('option[selected]').get('getX'));
  94              }
  95          }
  96  
  97          this.validate();
  98      },
  99      /**
 100       * Disables the submit button if there are no capabilities selected.
 101       * @method validate
 102       */
 103      validate : function() {
 104          this.button.set('disabled', (this.select.get('value') === ''));
 105      },
 106      /**
 107       * Called when ever the user types into the search field.
 108       * This method hides any capabilities that don't match the search term.
 109       * @method typed
 110       */
 111      typed : function() {
 112          var search = this.input.get('value'),
 113              matching = 0,
 114              last = null,
 115              capability;
 116          if (this.lastsearch) {
 117              this.lastsearch.set('value', search);
 118          }
 119          this.select.all('option').remove();
 120          for (capability in this.selectoptions) {
 121              if (capability.indexOf(search) >= 0) {
 122                  matching++;
 123                  last = this.selectoptions[capability];
 124                  this.select.append(this.selectoptions[capability]);
 125              }
 126          }
 127          if (matching === 0) {
 128              this.input.addClass("error");
 129          } else {
 130              this.input.removeClass("error");
 131              if (matching === 1) {
 132                  last.set('selected', true);
 133              }
 134          }
 135          this.validate();
 136      }
 137  };
 138  Y.extend(SEARCH, Y.Base, SEARCH.prototype, {
 139      NAME : 'tool_capability-search',
 140      ATTRS : {
 141          strsearch : {}
 142      }
 143  });
 144  
 145  M.tool_capability = M.tool_capability || {};
 146  
 147  /**
 148   * Initialises capability search functionality.
 149   * @static
 150   * @method M.tool_capability.init_capability_search
 151   * @param {Object} options
 152   */
 153  M.tool_capability.init_capability_search = function(options) {
 154      new SEARCH(options);
 155  };


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1