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