e35dbe09fe9da6004e5961fed30f97928162287b
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / jsfunc.tceforms_selectboxfilter.js
1 /**
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Class for JS handling of selectbox filter in TCEforms.
16 *
17 * @author Marc Bastian Heinrichs <mbh@mbh-software.de>
18 */
19
20 if (!TCEForms) {
21 var TCEForms = {};
22 }
23
24 TCEForms.SelectBoxFilter = Class.create({
25 selectBox: '',
26 selectBoxOriginal: '',
27 selectBoxOriginalOptionsLength: 0,
28 filterTextfield: false,
29 filterDropDown: false,
30 delayObject: '',
31
32 /**
33 * Assigns a new filter object to the available items select box object.
34 *
35 * @param selectBoxId The ID of the object to assign the filter to
36 */
37 initialize: function(selectBoxId) {
38
39 this.selectBox = $(selectBoxId);
40 this.selectBoxOriginal = this.selectBox.cloneNode(true);
41 this.selectBoxOriginalOptionsLength = this.selectBoxOriginal.options.length;
42
43 if ($(selectBoxId + '_filtertextfield') != undefined) {
44 this.filterTextfield = $(selectBoxId + '_filtertextfield');
45 }
46 if ($(selectBoxId + '_filterdropdown') != undefined) {
47 this.filterDropDown = $(selectBoxId + '_filterdropdown');
48 }
49
50 // setting
51 if (this.filterTextfield) {
52 this.filterTextfield.observe('keyup', function(event) {
53 this.delayObject = this.updateSelectOptions.bindAsEventListener(this).delay(0.5);
54 }.bindAsEventListener(this));
55
56 this.filterTextfield.observe('keydown', function(event) {
57 if (this.delayObject != undefined)
58 window.clearTimeout(this.delayId);
59 }.bindAsEventListener(this));
60 }
61
62 if (this.filterDropDown) {
63 this.filterDropDown.observe('change', function(event) {
64 this.updateSelectOptions();
65 }.bindAsEventListener(this));
66 }
67 },
68
69 /**
70 * Updates the available items select box based the filter textfield or filter drop-down
71 */
72 updateSelectOptions: function() {
73
74 var filterTextFromTextfield = '';
75 var filterTextFromDropDown = '';
76
77 if (this.filterTextfield) {
78 filterTextFromTextfield = this.filterTextfield.getValue();
79 }
80
81 if (this.filterDropDown) {
82 filterTextFromDropDown = this.filterDropDown.getValue();
83 }
84
85 this.selectBox.innerHTML = '';
86
87 if (filterTextFromTextfield.length > 0 || filterTextFromDropDown.length > 0) {
88 var matchStringTextfield = new RegExp(filterTextFromTextfield, 'i');
89 var matchStringDropDown = new RegExp(filterTextFromDropDown, 'i');
90 for (var i = 0; i < this.selectBoxOriginalOptionsLength; i++) {
91 if (this.selectBoxOriginal.options[i].firstChild.nodeValue.match(matchStringTextfield) != null &&
92 this.selectBoxOriginal.options[i].firstChild.nodeValue.match(matchStringDropDown) != null) {
93 var tempNode = this.selectBoxOriginal.options[i].cloneNode(true);
94 this.selectBox.appendChild(tempNode);
95 }
96 }
97 } else {
98 // recopy original list
99 for (var i = 0; i < this.selectBoxOriginalOptionsLength; i++) {
100 var tempNode = this.selectBoxOriginal.options[i].cloneNode(true);
101 this.selectBox.appendChild(tempNode);
102 }
103 }
104 }
105 });
106