1 YUI.add('moodle-tool_capability-search', function (Y, NAME) {
4 * This file contains the capability overview search functionality.
6 * @module moodle-tool_capability-search
10 * Constructs a new capability search manager.
12 * @namespace M.tool_capability
17 var SEARCH = function() {
18 SEARCH.superclass.constructor.apply(this, arguments);
29 * The capability select node.
36 * An associative array of search option. Populated from the select node above during initialisation.
37 * @property selectoptions
43 * The search input field.
50 * The submit button for the form.
57 * The last search node if there is one.
58 * If there is a last search node then the last search term will be persisted between requests.
59 * @property lastsearch
65 * Constructs the search manager.
68 initializer : function() {
69 this.form = Y.one('#capability-overview-form');
70 this.select = this.form.one('select[data-search=capability]');
71 this.select.setStyle('minWidth', this.select.get('offsetWidth'));
72 this.select.get('options').each(function(option) {
73 var capability = option.get('value');
74 this.selectoptions[capability] = option;
76 this.button = this.form.all('input[type=submit]');
77 this.lastsearch = this.form.one('input[name=search]');
79 var div = Y.Node.create('<div id="capabilitysearchui"></div>'),
80 label = Y.Node.create('<label for="capabilitysearch">'+this.get('strsearch')+'</label>');
81 this.input = Y.Node.create('<input type="text" id="capabilitysearch" />');
83 div.append(label).append(this.input);
85 this.select.insert(div, 'before');
86 this.select.one('option').setStyle('display', 'none');
88 this.input.on('keyup', this.typed, this);
89 this.select.on('change', this.validate, this);
91 if (this.lastsearch) {
92 this.input.set('value', this.lastsearch.get('value'));
94 if (this.select.one('option[selected]')) {
95 this.select.set('scrollTop', this.select.one('option[selected]').get('getX'));
102 * Disables the submit button if there are no capabilities selected.
105 validate : function() {
106 this.button.set('disabled', (this.select.get('value') === ''));
109 * Called when ever the user types into the search field.
110 * This method hides any capabilities that don't match the search term.
114 var search = this.input.get('value'),
118 if (this.lastsearch) {
119 this.lastsearch.set('value', search);
121 this.select.all('option').remove();
122 for (capability in this.selectoptions) {
123 if (capability.indexOf(search) >= 0) {
125 last = this.selectoptions[capability];
126 this.select.append(this.selectoptions[capability]);
129 if (matching === 0) {
130 this.input.addClass("error");
132 this.input.removeClass("error");
133 if (matching === 1) {
134 last.set('selected', true);
140 Y.extend(SEARCH, Y.Base, SEARCH.prototype, {
141 NAME : 'tool_capability-search',
147 M.tool_capability = M.tool_capability || {};
150 * Initialises capability search functionality.
152 * @method M.tool_capability.init_capability_search
153 * @param {Object} options
155 M.tool_capability.init_capability_search = function(options) {
160 }, '@VERSION@', {"requires": ["base", "node"]});