MDL-66332 tool_capability: adding acceptance tests
[moodle.git] / admin / tool / capability / yui / build / moodle-tool_capability-search / moodle-tool_capability-search.js
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);
19 };
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" data-fieldtype="text"></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');
87         this.input.on('keyup', this.typed, this);
88         this.select.on('change', this.validate, this);
90         if (this.lastsearch) {
91             this.input.set('value', this.lastsearch.get('value'));
92             this.typed();
93             if (this.select.one('option[selected]')) {
94                 this.select.set('scrollTop', this.select.one('option[selected]').get('getX'));
95             }
96         }
98         this.validate();
99     },
100     /**
101      * Disables the submit button if there are no capabilities selected.
102      * @method validate
103      */
104     validate: function() {
105         this.button.set('disabled', (this.select.get('value') === ''));
106     },
107     /**
108      * Called when ever the user types into the search field.
109      * This method hides any capabilities that don't match the search term.
110      * @method typed
111      */
112     typed: function() {
113         var search = this.input.get('value'),
114             matching = 0,
115             last = null,
116             capability;
117         if (this.lastsearch) {
118             this.lastsearch.set('value', search);
119         }
120         this.select.all('option').remove();
121         for (capability in this.selectoptions) {
122             if (capability.indexOf(search) >= 0) {
123                 matching++;
124                 last = this.selectoptions[capability];
125                 this.select.append(this.selectoptions[capability]);
126             }
127         }
128         if (matching === 0) {
129             this.input.addClass("error");
130         } else {
131             this.input.removeClass("error");
132             if (matching === 1) {
133                 last.set('selected', true);
134             }
135         }
136         this.validate();
137     }
138 };
139 Y.extend(SEARCH, Y.Base, SEARCH.prototype, {
140     NAME: 'tool_capability-search',
141     ATTRS: {
142         strsearch: {}
143     }
144 });
146 M.tool_capability = M.tool_capability || {};
148 /**
149  * Initialises capability search functionality.
150  * @static
151  * @method M.tool_capability.init_capability_search
152  * @param {Object} options
153  */
154 M.tool_capability.init_capability_search = function(options) {
155     new SEARCH(options);
156 };
159 }, '@VERSION@', {"requires": ["base", "node"]});