root/public/javascripts/comboBox.js

Revision 1, 2.9 kB (checked in by falcon, 17 years ago)

Version one -> initial work from the laptop.

Line 
1var ComboBox = Class.create();
2
3ComboBox.Autocompleter = Autocompleter.Local;
4
5ComboBox.Autocompleter.prototype.onBlur = function(event) {
6        if (Element.getStyle(this.update, 'display') == 'none') { return; }
7        setTimeout(this.hide.bind(this), 250);
8        this.hasFocus = false;
9        this.active = false;
10}
11
12
13ComboBox.prototype = {
14        initialize: function(textElement, resultsElement, array, options) {
15                this.textElement = $(textElement);
16       
17        // the first text box inside the container
18        this.textBox = $A( this.textElement.getElementsByTagName('INPUT') ).findAll( function(input) {
19            return (input.getAttribute('type') == 'text');
20        })[0];
21       
22        this.results = $(resultsElement);
23        this.textBox.paddingRight = '20px';
24        this.textElement.style.width = (this.textBox.offsetWidth) + 'px';
25        this.textElement.style.position = 'relative';
26
27                // we dynamically insert a SPAN that will serve as the drop-down 'arrow'
28        this.arrow = Element.extend(Builder.node('span'));
29        Object.extend(this.arrow.style, {
30            cursor: 'default',
31            backgroundColor: '#ddd',
32            color: '#000',
33            width: '20px',
34            position: 'absolute',
35            top: '0',
36                        right: '0px',
37                        textAlign: 'center',
38                        fontSize: 'xx-small',
39                        height: (this.textElement.offsetHeight - 2) + 'px'
40        });
41
42                if (document.all) {
43                        this.arrow.setStyle({ padding: '2px 0 0 3px', width: '18px', height: '17px'});
44
45                }
46        this.arrow.innerHTML = '↓';
47        this.textElement.appendChild(this.arrow);
48            this.array = array;
49
50                this.results.style.display      = 'none';
51               
52                this.events = {
53                        showChoices:        this.showChoices.bindAsEventListener(this),
54                        hideChoices:        this.hideChoices.bindAsEventListener(this),
55                        click:                          this.click.bindAsEventListener(this),
56                        keyDown:                        this.keyDown.bindAsEventListener(this)
57                }
58               
59                this.autocompleter = new ComboBox.Autocompleter(this.textBox, this.results, this.array, options);
60                               
61                Event.observe(this.arrow, 'click', this.events.click);
62                Event.observe(this.textBox, 'keydown', this.events.keyDown);
63        },
64       
65        getAllChoices: function(e) {
66                var choices = this.array.collect( function(choice) { return '<li>' + choice + '</li>'; } );
67                var html = '<ul>' + choices.join('') + '</ul>';
68                this.autocompleter.updateChoices(html);
69        },
70       
71        keyDown: function(e) {
72                if (e.keyCode == Event.KEY_DOWN && this.choicesVisible() ) {
73                        this.showChoices();
74                }
75        },
76       
77        // returns boolean indicating whether the choices are displayed
78        choicesVisible: function() { return (Element.getStyle(this.autocompleter.update, 'display') == 'none'); },
79       
80        click: function() {
81                if (this.choicesVisible() ) {
82                        this.showChoices();
83                } else {
84                        this.hideChoices();
85                }
86        },
87               
88        showChoices: function() {
89                this.textBox.focus();
90    this.autocompleter.changed = false;
91    this.autocompleter.hasFocus = true;
92    this.getAllChoices();
93        },
94       
95        hideChoices: function() {
96                this.autocompleter.onBlur();
97        }
98}
Note: See TracBrowser for help on using the browser.