MDL-41987 Javascript: Tidy up YUI documentation
[moodle.git] / admin / tool / capability / yui / build / moodle-tool_capability-search / moodle-tool_capability-search-debug.js
blob3beb4f77fe88ad4e061b6037781c6c109b88fb4b
1 YUI.add('moodle-tool_capability-search', function (Y, NAME) {
3 /**
4  * This file contains the capability overview search functionality.
5  *
6  * @module moodle-tool_capability-search
7  */
9 /**
10  * Constructs a new capability search manager.
11  *
12  * @namespace M.tool_capability
13  * @class Search
14  * @constructor
15  * @extends Base
16  */
17 var SEARCH = function() {
18     SEARCH.superclass.constructor.apply(this, arguments);
20 SEARCH.prototype = {
21     /**
22      * The search form.
23      * @property form
24      * @type Node
25      * @protected
26      */
27     form : null,
28     /**
29      * The capability select node.
30      * @property select
31      * @type Node
32      * @protected
33      */
34     select: null,
35     /**
36      * An associative array of search option. Populated from the select node above during initialisation.
37      * @property selectoptions
38      * @type Object
39      * @protected
40      */
41     selectoptions : {},
42     /**
43      * The search input field.
44      * @property input
45      * @type Node
46      * @protected
47      */
48     input: null,
49     /**
50      * The submit button for the form.
51      * @property button
52      * @type Node
53      * @protected
54      */
55     button: null,
56     /**
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
60      * @type Node
61      * @protected
62      */
63     lastsearch : null,
64     /**
65      * Constructs the search manager.
66      * @method initializer
67      */
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;
75         }, this);
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'));
93             this.typed();
94             if (this.select.one('option[selected]')) {
95                 this.select.set('scrollTop', this.select.one('option[selected]').get('getX'));
96             }
97         }
99         this.validate();
100     },
101     /**
102      * Disables the submit button if there are no capabilities selected.
103      * @method validate
104      */
105     validate : function() {
106         this.button.set('disabled', (this.select.get('value') === ''));
107     },
108     /**
109      * Called when ever the user types into the search field.
110      * This method hides any capabilities that don't match the search term.
111      * @method typed
112      */
113     typed : function() {
114         var search = this.input.get('value'),
115             matching = 0,
116             last = null,
117             capability;
118         if (this.lastsearch) {
119             this.lastsearch.set('value', search);
120         }
121         this.select.all('option').remove();
122         for (capability in this.selectoptions) {
123             if (capability.indexOf(search) >= 0) {
124                 matching++;
125                 last = this.selectoptions[capability];
126                 this.select.append(this.selectoptions[capability]);
127             }
128         }
129         if (matching === 0) {
130             this.input.addClass("error");
131         } else {
132             this.input.removeClass("error");
133             if (matching === 1) {
134                 last.set('selected', true);
135             }
136         }
137         this.validate();
138     }
140 Y.extend(SEARCH, Y.Base, SEARCH.prototype, {
141     NAME : 'tool_capability-search',
142     ATTRS : {
143         strsearch : {}
144     }
147 M.tool_capability = M.tool_capability || {};
150  * Initialises capability search functionality.
151  * @static
152  * @method M.tool_capability.init_capability_search
153  * @param {Object} options
154  */
155 M.tool_capability.init_capability_search = function(options) {
156     new SEARCH(options);
160 }, '@VERSION@', {"requires": ["base", "node"]});