2
* Copyright (C) Research In Motion Limited, 2012. All rights reserved.
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.
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';
16
option.className = option.className.replace('selected', '');
22
var toggleOption = function (event) {
23
if (event.target.className.indexOf('selected') === -1) {
24
event.target.className += ' selected';
26
event.target.className = event.target.className.replace('selected', '');
30
var done = function () {
32
for (var option = document.getElementById('select-area').firstChild; option; option = option.nextSibling) {
33
if (option.className.indexOf('selected') === -1) {
39
window.setValueAndClosePopup(result, window.popUp);
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.
51
var show = function (multiple, labels, enableds, itemTypes, selecteds, buttonText) {
54
popup = document.createElement('div'),
55
select = document.createElement('div');
57
popup.className = 'popup-area';
58
select.className = 'select-area';
59
select.id = 'select-area';
60
popup.appendChild(select);
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]));
68
option.addEventListener('click', selectOption);
69
} else if (enableds[i]) {
70
option.addEventListener('click', toggleOption);
73
select.appendChild(option);
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);
87
document.body.appendChild(popup);
90
window.select = window.select || {};
91
window.select.show = show;