2012-03-01 10:58:19 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var SelectHelper = {
|
|
|
|
_uiBusy: false,
|
|
|
|
|
2012-05-07 16:52:20 -07:00
|
|
|
handleEvent: function(aEvent) {
|
|
|
|
this.handleClick(aEvent.target);
|
|
|
|
},
|
|
|
|
|
2012-03-01 10:58:19 -08:00
|
|
|
handleClick: function(aTarget) {
|
|
|
|
// if we're busy looking at a select we want to eat any clicks that
|
|
|
|
// come to us, but not to process them
|
2013-02-21 08:41:41 -08:00
|
|
|
if (this._uiBusy || !this._isMenu(aTarget) || aTarget.disabled)
|
2012-05-07 16:52:20 -07:00
|
|
|
return;
|
2012-03-01 10:58:19 -08:00
|
|
|
|
2012-05-07 16:52:20 -07:00
|
|
|
this._uiBusy = true;
|
|
|
|
this.show(aTarget);
|
|
|
|
this._uiBusy = false;
|
2012-03-01 10:58:19 -08:00
|
|
|
},
|
|
|
|
|
2012-05-07 16:52:20 -07:00
|
|
|
show: function(aElement) {
|
|
|
|
let list = this.getListForElement(aElement);
|
2012-03-01 10:58:19 -08:00
|
|
|
|
2013-06-13 14:19:04 -07:00
|
|
|
let p = new Prompt({
|
|
|
|
window: aElement.contentDocument
|
|
|
|
});
|
|
|
|
|
|
|
|
if (aElement.multiple) {
|
|
|
|
p.addButton({
|
|
|
|
label: Strings.browser.GetStringFromName("selectHelper.closeMultipleSelectDialog")
|
|
|
|
}).setMultiChoiceItems(list);
|
|
|
|
} else {
|
|
|
|
p.setSingleChoiceItems(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
p.show((function(data) {
|
|
|
|
let selected = data.button;
|
|
|
|
if (selected == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (aElement instanceof Ci.nsIDOMXULMenuListElement) {
|
2013-07-31 10:31:46 -07:00
|
|
|
if (aElement.selectedIndex != selected) {
|
|
|
|
aElement.selectedIndex = selected;
|
|
|
|
this.fireOnCommand(aElement);
|
|
|
|
}
|
2013-06-13 14:19:04 -07:00
|
|
|
} else if (aElement instanceof HTMLSelectElement) {
|
2013-07-31 10:31:46 -07:00
|
|
|
let changed = false;
|
2013-06-13 14:19:04 -07:00
|
|
|
if (!Array.isArray(selected)) {
|
|
|
|
let temp = [];
|
|
|
|
for (let i = 0; i <= list.length; i++) {
|
|
|
|
temp[i] = (i == selected);
|
|
|
|
}
|
|
|
|
selected = temp;
|
2012-03-01 17:34:37 -08:00
|
|
|
}
|
2013-06-13 14:19:04 -07:00
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
this.forOptions(aElement, function(aNode) {
|
|
|
|
if (aNode.selected != selected[i]) {
|
|
|
|
changed = true;
|
|
|
|
aNode.selected = selected[i];
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
});
|
2012-05-30 13:23:13 -07:00
|
|
|
|
2013-07-31 10:31:46 -07:00
|
|
|
if (changed)
|
|
|
|
this.fireOnChange(aElement);
|
|
|
|
}
|
2013-06-13 14:19:04 -07:00
|
|
|
}).bind(this));
|
2012-03-01 10:58:19 -08:00
|
|
|
},
|
|
|
|
|
2012-03-01 17:34:37 -08:00
|
|
|
_isMenu: function(aElement) {
|
|
|
|
return (aElement instanceof HTMLSelectElement ||
|
|
|
|
aElement instanceof Ci.nsIDOMXULMenuListElement);
|
2012-03-01 10:58:19 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
getListForElement: function(aElement) {
|
2012-03-01 17:34:37 -08:00
|
|
|
let index = 0;
|
2013-06-13 14:19:04 -07:00
|
|
|
let items = [];
|
|
|
|
this.forOptions(aElement, function(aNode, aOptions, aParent) {
|
2012-03-01 10:58:19 -08:00
|
|
|
let item = {
|
|
|
|
label: aNode.text || aNode.label,
|
2013-06-13 14:19:04 -07:00
|
|
|
header: aOptions.isGroup,
|
2012-03-01 10:58:19 -08:00
|
|
|
disabled: aNode.disabled,
|
2013-06-13 14:19:04 -07:00
|
|
|
id: index,
|
|
|
|
selected: aNode.selected
|
2012-03-01 10:58:19 -08:00
|
|
|
}
|
|
|
|
|
2013-06-13 14:19:04 -07:00
|
|
|
if (aParent) {
|
|
|
|
item.child = true;
|
|
|
|
item.disabled = item.disabled || aParent.disabled;
|
|
|
|
}
|
|
|
|
items.push(item);
|
|
|
|
|
2012-03-01 17:34:37 -08:00
|
|
|
index++;
|
2012-03-01 10:58:19 -08:00
|
|
|
});
|
2013-06-13 14:19:04 -07:00
|
|
|
return items;
|
2012-03-01 10:58:19 -08:00
|
|
|
},
|
|
|
|
|
2013-06-13 14:19:04 -07:00
|
|
|
forOptions: function(aElement, aFunction, aParent = null) {
|
|
|
|
let element = aElement;
|
2012-03-01 17:34:37 -08:00
|
|
|
if (aElement instanceof Ci.nsIDOMXULMenuListElement)
|
2013-06-13 14:19:04 -07:00
|
|
|
element = aElement.menupopup;
|
|
|
|
let children = element.children;
|
2012-03-01 10:58:19 -08:00
|
|
|
let numChildren = children.length;
|
2012-03-01 17:34:37 -08:00
|
|
|
|
2012-03-01 10:58:19 -08:00
|
|
|
// if there are no children in this select, we add a dummy row so that at least something appears
|
|
|
|
if (numChildren == 0)
|
2013-06-13 14:19:04 -07:00
|
|
|
aFunction.call(this, { label: "" }, { isGroup: false }, aParent);
|
2012-03-01 17:34:37 -08:00
|
|
|
|
2012-03-01 10:58:19 -08:00
|
|
|
for (let i = 0; i < numChildren; i++) {
|
|
|
|
let child = children[i];
|
2012-03-01 17:34:37 -08:00
|
|
|
if (child instanceof HTMLOptionElement ||
|
|
|
|
child instanceof Ci.nsIDOMXULSelectControlItemElement) {
|
2013-06-13 14:19:04 -07:00
|
|
|
aFunction.call(this, child, { isGroup: false }, aParent);
|
2012-03-01 10:58:19 -08:00
|
|
|
} else if (child instanceof HTMLOptGroupElement) {
|
2013-06-13 14:19:04 -07:00
|
|
|
aFunction.call(this, child, { isGroup: true });
|
|
|
|
this.forOptions(child, aFunction, child);
|
2012-03-01 10:58:19 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
fireOnChange: function(aElement) {
|
|
|
|
let evt = aElement.ownerDocument.createEvent("Events");
|
|
|
|
evt.initEvent("change", true, true, aElement.defaultView, 0,
|
|
|
|
false, false,
|
|
|
|
false, false, null);
|
|
|
|
setTimeout(function() {
|
|
|
|
aElement.dispatchEvent(evt);
|
|
|
|
}, 0);
|
2013-07-31 10:31:46 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
fireOnCommand: function(aElement) {
|
|
|
|
let evt = aElement.ownerDocument.createEvent("XULCommandEvent");
|
|
|
|
evt.initCommandEvent("command", true, true, aElement.defaultView, 0,
|
|
|
|
false, false,
|
|
|
|
false, false, null);
|
|
|
|
setTimeout(function() {
|
|
|
|
aElement.dispatchEvent(evt);
|
|
|
|
}, 0);
|
2012-03-01 10:58:19 -08:00
|
|
|
}
|
|
|
|
};
|