MDL-66332 tool_capability: adding acceptance tests
[moodle.git] / admin / tool / capability / yui / src / search / js / search.js
CommitLineData
9935136f
SH
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 *
1f777e5c 10 * @namespace M.tool_capability
9935136f
SH
11 * @class Search
12 * @constructor
1f777e5c 13 * @extends Base
9935136f
SH
14 */
15var SEARCH = function() {
16 SEARCH.superclass.constructor.apply(this, arguments);
17};
18SEARCH.prototype = {
19 /**
20 * The search form.
21 * @property form
22 * @type Node
23 * @protected
24 */
5bb4f444 25 form: null,
9935136f
SH
26 /**
27 * The capability select node.
28 * @property select
29 * @type Node
30 * @protected
31 */
32 select: null,
46af2845
SH
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 */
5bb4f444 39 selectoptions: {},
9935136f
SH
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 */
5bb4f444 61 lastsearch: null,
9935136f
SH
62 /**
63 * Constructs the search manager.
64 * @method initializer
65 */
5bb4f444 66 initializer: function() {
9935136f
SH
67 this.form = Y.one('#capability-overview-form');
68 this.select = this.form.one('select[data-search=capability]');
46af2845
SH
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);
9935136f
SH
74 this.button = this.form.all('input[type=submit]');
75 this.lastsearch = this.form.one('input[name=search]');
76
178be88e 77 var div = Y.Node.create('<div id="capabilitysearchui" data-fieldtype="text"></div>'),
5bb4f444 78 label = Y.Node.create('<label for="capabilitysearch">' + this.get('strsearch') + '</label>');
9935136f
SH
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');
9935136f
SH
84
85 this.input.on('keyup', this.typed, this);
86 this.select.on('change', this.validate, this);
87
88 if (this.lastsearch) {
89 this.input.set('value', this.lastsearch.get('value'));
90 this.typed();
46af2845
SH
91 if (this.select.one('option[selected]')) {
92 this.select.set('scrollTop', this.select.one('option[selected]').get('getX'));
93 }
9935136f
SH
94 }
95
96 this.validate();
97 },
98 /**
99 * Disables the submit button if there are no capabilities selected.
100 * @method validate
101 */
5bb4f444 102 validate: function() {
9935136f
SH
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 */
5bb4f444 110 typed: function() {
9935136f 111 var search = this.input.get('value'),
9935136f 112 matching = 0,
46af2845
SH
113 last = null,
114 capability;
9935136f
SH
115 if (this.lastsearch) {
116 this.lastsearch.set('value', search);
117 }
46af2845
SH
118 this.select.all('option').remove();
119 for (capability in this.selectoptions) {
120 if (capability.indexOf(search) >= 0) {
9935136f 121 matching++;
858fc79d 122 last = this.selectoptions[capability];
46af2845 123 this.select.append(this.selectoptions[capability]);
9935136f 124 }
46af2845 125 }
9935136f
SH
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};
137Y.extend(SEARCH, Y.Base, SEARCH.prototype, {
5bb4f444
DP
138 NAME: 'tool_capability-search',
139 ATTRS: {
140 strsearch: {}
9935136f
SH
141 }
142});
143
9935136f
SH
144M.tool_capability = M.tool_capability || {};
145
146/**
147 * Initialises capability search functionality.
148 * @static
1f777e5c 149 * @method M.tool_capability.init_capability_search
9935136f
SH
150 * @param {Object} options
151 */
152M.tool_capability.init_capability_search = function(options) {
153 new SEARCH(options);
858fc79d 154};