~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/Resources/blackberry/selectControlBlackBerry.js

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) Research In Motion Limited, 2012. All rights reserved.
 
3
 */
 
4
 
 
5
// Upon the user making a selection, I will call window.setValueAndClosePopup with a binary string where
 
6
// the character at index i being '1' means that the option at index i is selected.
 
7
(function (){
 
8
 
 
9
    var selectOption = function (event) {
 
10
        for (var option = document.getElementById('select-area').firstChild; option; option = option.nextSibling) {
 
11
            if (option === event.target) {
 
12
                if (option.className.indexOf('selected') === -1) {
 
13
                    option.className += ' selected';
 
14
                }
 
15
            } else {
 
16
                option.className = option.className.replace('selected', '');
 
17
            }
 
18
        }
 
19
        done();
 
20
    };
 
21
 
 
22
    var toggleOption = function (event) {
 
23
        if (event.target.className.indexOf('selected') === -1) {
 
24
            event.target.className += ' selected';
 
25
        } else {
 
26
            event.target.className = event.target.className.replace('selected', '');
 
27
        }
 
28
    };
 
29
 
 
30
    var done = function () {
 
31
        var result = '';
 
32
        for (var option = document.getElementById('select-area').firstChild; option; option = option.nextSibling) {
 
33
            if (option.className.indexOf('selected') === -1) {
 
34
                result += '0';
 
35
            } else {
 
36
                result += '1';
 
37
            }
 
38
        }
 
39
        window.setValueAndClosePopup(result, window.popUp);
 
40
    };
 
41
 
 
42
    /* multiple - a boolean
 
43
     * labels - an array of strings
 
44
     * enableds - an array of booleans.
 
45
     *   -I will assume that the HTML "disabled optgroups disable all options in the optgroup" hasn't been applied,
 
46
     *    so if the index corresponds to an optgroup, I will render all of its options as disabled
 
47
     * itemTypes - an array of integers, 0 === option, 1 === optgroup, 2 === option in optgroup
 
48
     * selecteds - an array of booleans
 
49
     * buttonText - a string to use for the button presented when multiple is true. Like "OK" or "Done" or something.
 
50
     */
 
51
    var show = function (multiple, labels, enableds, itemTypes, selecteds, buttonText) {
 
52
        var i,
 
53
            size = labels.length,
 
54
            popup = document.createElement('div'),
 
55
            select = document.createElement('div');
 
56
 
 
57
        popup.className = 'popup-area';
 
58
        select.className = 'select-area';
 
59
        select.id = 'select-area';
 
60
        popup.appendChild(select);
 
61
 
 
62
        for (i = 0; i < size; i++) {
 
63
            // TODO: handle itemTypes
 
64
            var option = document.createElement('div');
 
65
            option.className = 'option' + (enableds[i] ? '' : ' disabled') + (selecteds[i] ? ' selected' : '');
 
66
            option.appendChild(document.createTextNode(labels[i]));
 
67
            if (!multiple) {
 
68
                option.addEventListener('click', selectOption);
 
69
            } else if (enableds[i]) {
 
70
                option.addEventListener('click', toggleOption);
 
71
            }
 
72
 
 
73
            select.appendChild(option);
 
74
        }
 
75
 
 
76
        if (multiple) {
 
77
            var okButton = document.createElement('button'),
 
78
                buttons = document.createElement('div');
 
79
            buttons.className = 'popup-buttons';
 
80
            okButton.className = 'popup-button';
 
81
            okButton.addEventListener('click', done);
 
82
            okButton.appendChild(document.createTextNode(buttonText));
 
83
            buttons.appendChild(okButton);
 
84
            popup.appendChild(buttons);
 
85
        }
 
86
 
 
87
        document.body.appendChild(popup);
 
88
    };
 
89
 
 
90
    window.select = window.select || {};
 
91
    window.select.show = show;
 
92
}());