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