1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
|
<?xml version="1.0"?>
<!-- ***** BEGIN LICENSE BLOCK *****
- Version: MPL 1.1/GPL 2.0/LGPL 2.1
-
- The contents of this file are subject to the Mozilla Public License Version
- 1.1 (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.mozilla.org/MPL/
-
- Software distributed under the License is distributed on an "AS IS" basis,
- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- for the specific language governing rights and limitations under the
- License.
-
- The Original Code is Richlistbox code.
-
- The Initial Developer of the Original Code is
- IBM Corporation.
- Portions created by the Initial Developer are Copyright (C) 2005
- IBM Corporation. All Rights Reserved.
-
- Contributor(s):
- Doron Rosenberg <doronr@us.ibm.com> (Original Author)
-
- Alternatively, the contents of this file may be used under the terms of
- either the GNU General Public License Version 2 or later (the "GPL"), or
- the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- in which case the provisions of the GPL or the LGPL are applicable instead
- of those above. If you wish to allow use of your version of this file only
- under the terms of either the GPL or the LGPL, and not to allow others to
- use your version of this file under the terms of the MPL, indicate your
- decision by deleting the provisions above and replace them with the notice
- and other provisions required by the GPL or the LGPL. If you do not delete
- the provisions above, a recipient may use your version of this file under
- the terms of any one of the MPL, the GPL or the LGPL.
-
- ***** END LICENSE BLOCK ***** -->
<bindings id="richlistboxBindings"
xmlns="http://www.mozilla.org/xbl"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:xbl="http://www.mozilla.org/xbl">
<binding id="richlistbox">
<content>
<xul:scrollbox allowevents="true" orient="vertical" anonid="main-box"
flex="1" style="overflow: auto;">
<children />
</xul:scrollbox>
</content>
<resources>
<stylesheet src="chrome://global/skin/richlistbox.css"/>
</resources>
<implementation implements="nsIAccessibleProvider, nsIDOMXULSelectControlElement">
<field name="scrollBoxObject">null</field>
<constructor>
<![CDATA[
var x = document.getAnonymousElementByAttribute(this, "anonid", "main-box");
this.scrollBoxObject = x.boxObject.QueryInterface(Components.interfaces.nsIScrollBoxObject);
// add a template build listener
if (this.builder)
this.builder.addListener(this._builderListener);
else
this._refreshSelection();
]]>
</constructor>
<destructor>
<![CDATA[
// remove the template build listener
if (this.builder)
this.builder.removeListener(this._builderListener);
this._selectedItem = null;
]]>
</destructor>
<property name="accessible">
<getter>
<![CDATA[
var accService = Components.classes["@mozilla.org/accessibilityService;1"].getService(Components.interfaces.nsIAccessibilityService);
return accService.createXULListboxAccessible(this);
]]>
</getter>
</property>
<property name="children">
<getter>
<![CDATA[
var childNodes = [];
for (var i = 0; i < this.childNodes.length; ++i) {
if (this.childNodes[i] instanceof Components.interfaces.nsIDOMXULSelectControlItemElement)
childNodes.push(this.childNodes[i]);
}
return childNodes;
]]>
</getter>
</property>
<field name="_builderListener">
<![CDATA[
({
mOuter: this,
item: null,
willRebuild : function(builder) {},
didRebuild : function(builder) {
this.mOuter._refreshSelection();
}
});
]]>
</field>
<method name="_refreshSelection">
<body>
<![CDATA[
// when this method is called, we know that either the selectedItem
// we have is null (ctor) or a reference to an element no longer in
// the DOM (template).
// fist look for a last-selected attribute
var lastSelected = this.getAttribute("last-selected");
if (lastSelected != "") {
var element = document.getElementById(lastSelected);
if (element) {
this.selectedItem = element;
if (!this._isItemVisible(this.selectedItem))
this.scrollBoxObject.scrollToElement(this.selectedItem);
return;
}
}
// cache the selected index
var selectedIndex = this._selectedIndex;
// refreshes selection. Called for example when a template rebuild
// happens.
if (this.selectedItem) {
if (this.selectedItem.hasAttribute("id")) {
var id = this.selectedItem.getAttribute("id");
var item = document.getElementById(id);
// if we find no item, clear selection so that the code at the bottom
// takes over
if (item) {
this.selectedItem = item;
} else {
this.clearSelection();
}
} else {
// if no id, we clear selection so that the below code will select
// based on the current index
this.clearSelection();
}
}
// if we have no previously selected item or the above if check fails to
// find the previous node (which causes it to clear selection)
if (!this.selectedItem) {
// if the selectedIndex is larger than the row count, select the last
// item.
if (selectedIndex >= this.getRowCount())
this.selectedIndex = this.getRowCount() - 1;
else
this.selectedIndex = selectedIndex;
// XXX: downloadmanager needs the following line, else we scroll to
// the middle on inital load.
this.ensureSelectedElementIsVisible();
}
]]>
</body>
</method>
<method name="fireActiveItemEvent">
<body>
<![CDATA[
if (this.selectedItem) {
var event = document.createEvent("Events");
event.initEvent("DOMMenuItemActive", true, true);
this.selectedItem.dispatchEvent(event);
}
return false;
]]>
</body>
</method>
<field name="_selectedIndex">0</field>
<property name="selectedIndex">
<getter>
<![CDATA[
return this.getIndexOf(this.selectedItem);
]]>
</getter>
<setter>
<![CDATA[
if (val == -1) {
// clear selection
this.clearSelection();
} else if (val >= 0) {
// only set if we get an item returned
var item = this.getItemAtIndex(val);
if (item)
this.selectedItem = item;
}
]]>
</setter>
</property>
<field name="_selectedItem">null</field>
<property name="selectedItem">
<getter>
return this._selectedItem;
</getter>
<setter>
<![CDATA[
if (this._selectedItem == val)
return;
this._setItemSelection(val);
if (val)
this.fireActiveItemEvent();
this._fireOnSelect();
]]>
</setter>
</property>
<!-- sets selection but doesn't cause any events -->
<method name="_setItemSelection">
<parameter name="aItem"/>
<body>
<![CDATA[
// unselect current item
if (this._selectedItem)
this._selectedItem.selected = false
this._selectedItem = aItem;
this._selectedIndex = this.getIndexOf(aItem);
this.ensureSelectedElementIsVisible();
if (aItem) {
aItem.selected = true;
aItem.focus();
}
]]>
</body>
</method>
<method name="clearSelection">
<body>
<![CDATA[
this.selectedItem = null;
]]>
</body>
</method>
<method name="getRowCount">
<body>
<![CDATA[
return this.children.length;
]]>
</body>
</method>
<method name="goUp">
<body>
<![CDATA[
// if nothing selected, we go from the bottom
for (var i = this.selectedItem ? this.selectedItem.previousSibling : this.lastChild; i; i = i.previousSibling) {
// could have a template element, which would be a sibling
if (i instanceof Components.interfaces.nsIDOMXULSelectControlItemElement) {
this.selectedItem = i;
return true;
}
}
return false;
]]>
</body>
</method>
<method name="goDown">
<body>
<![CDATA[
// if nothing selected, we go from the top
for (var i = this.selectedItem ? this.selectedItem.nextSibling : this.firstChild; i; i = i.nextSibling) {
// could have a template element, which would be a sibling
if (i instanceof Components.interfaces.nsIDOMXULSelectControlItemElement) {
this.selectedItem = i;
return true;
}
}
return false;
]]>
</body>
</method>
<method name="_isItemVisible">
<parameter name="aItem"/>
<body>
<![CDATA[
if (!aItem)
return false;
var y = {};
this.scrollBoxObject.getPosition({}, y);
y.value += this.scrollBoxObject.y;
// Partially visible items are also considered visible
return (aItem.boxObject.y + aItem.boxObject.height > y.value) &&
(aItem.boxObject.y < y.value + this.scrollBoxObject.height);
]]>
</body>
</method>
<method name="scrollOnePage">
<parameter name="aDirection"/> <!-- Must be -1 or 1 -->
<body>
<![CDATA[
var children = this.children;
if (children.length == 0)
return false;
var index = children.indexOf(this.selectedItem);
// If nothing is selected, we just select the first element
// at the extreme we're moving away from
if (index == -1) {
index = aDirection == -1 ? children.length - 1 : 0;
this.selectedItem = children[index];
return true;
}
// If the selected item is visible, we scroll by one page so that
// the newly selected item is at approximately the same position as
// the currently selected one
var currentItem = children[index];
if (this._isItemVisible(currentItem))
this.scrollBoxObject.scrollBy(0, this.scrollBoxObject.height * aDirection);
// Figure out, how many items fully fit into the view port
// (including the currently selected one), and determine
// the index of the first one lying (partially) outside
var height = this.scrollBoxObject.height;
var border = currentItem.boxObject.y;
if (aDirection == -1)
border += currentItem.boxObject.height;
while (index >= 0 && index < children.length) {
var border2 = children[index].boxObject.y;
if (aDirection == -1)
border2 += children[index].boxObject.height;
if ((border2 - border) * aDirection > height)
break;
index += aDirection;
}
index -= aDirection;
if (this.selectedItem != children[index]) {
this.selectedItem = children[index];
return true;
}
// Move by at least one item if the view port is too small
if (aDirection == -1)
return this.goUp();
return this.goDown();
]]>
</body>
</method>
<method name="getItemAtIndex">
<parameter name="aIndex"/>
<body>
<![CDATA[
return this.children[aIndex];
]]>
</body>
</method>
<method name="getIndexOf">
<parameter name="aElement"/>
<body>
<![CDATA[
// don't search the children, if we're looking for none of them
if (aElement == null)
return -1;
return this.children.indexOf(aElement);
]]>
</body>
</method>
<method name="ensureElementIsVisible">
<parameter name="aElement"/>
<body>
<![CDATA[
if (aElement)
this.scrollBoxObject.ensureElementIsVisible(aElement);
]]>
</body>
</method>
<method name="ensureSelectedElementIsVisible">
<body>
<![CDATA[
if (this.selectedItem) {
this.ensureElementIsVisible(this.selectedItem);
}
]]>
</body>
</method>
<property name="suppressOnSelect">
<getter>
<![CDATA[
return this.getAttribute("suppressonselect") == "true";
]]>
</getter>
</property>
<method name="_fireOnSelect">
<body>
<![CDATA[
if (this.selectedItem)
this.setAttribute("last-selected", this.selectedItem.getAttribute("id"));
else
this.removeAttribute("last-selected");
if (!this.suppressOnSelect) {
var event = document.createEvent("Events");
event.initEvent("select", true, true);
this.dispatchEvent(event);
// if we have controllers, notify the command dispatcher
if (this.controllers.getControllerCount() > 0)
document.commandDispatcher.updateCommands("richlistbox-select");
}
]]>
</body>
</method>
</implementation>
<handlers>
<handler event="keypress" keycode="VK_UP" action="goUp(); event.preventDefault();"/>
<handler event="keypress" keycode="VK_DOWN" action="goDown(); event.preventDefault();"/>
<handler event="keypress" keycode="VK_PAGE_UP" action="scrollOnePage(-1); event.preventDefault();"/>
<handler event="keypress" keycode="VK_PAGE_DOWN" action="scrollOnePage(1); event.preventDefault();"/>
<handler event="keypress" keycode="VK_HOME" action="clearSelection(); goDown(); event.preventDefault();"/>
<handler event="keypress" keycode="VK_END" action="clearSelection(); goUp(); event.preventDefault();"/>
<handler event="click">
<![CDATA[
// clicking into nothing should unselect
if (event.originalTarget.getAttribute("anonid") == "main-box")
this.clearSelection();
]]>
</handler>
<handler event="contextmenu">
<![CDATA[
// if the context menu was opened via the keyboard, display it in the
// right location.
if (event.button != 2) {
var popup = document.getElementById(this.getAttribute("context"));
if (popup)
popup.showPopup(this.selectedItem, -1, -1, "context", "bottomleft", "topleft");
}
]]>
</handler>
<handler event="focus">
<![CDATA[
if (event.target == this)
this.fireActiveItemEvent();
]]>
</handler>
</handlers>
</binding>
<binding id="richlistitem"
extends="chrome://global/content/bindings/general.xml#basecontrol">
<content>
<children />
</content>
<resources>
<stylesheet src="chrome://global/skin/richlistbox.css"/>
</resources>
<implementation implements="nsIAccessibleProvider, nsIDOMXULSelectControlItemElement">
<destructor>
<![CDATA[
// When we are destructed and we are selected, unselect ourselves so
// that richlistbox's selection doesn't point to something not in the DOM.
// We don't want to reset last-selected, so we don't call clearSelection().
if (this.selected) {
this.control._setItemSelection(null);
}
]]>
</destructor>
<!-- ///////////////// nsIAccessibleProvider ///////////////// -->
<property name="accessible">
<getter>
<![CDATA[
var accService = Components.classes["@mozilla.org/accessibilityService;1"].getService(Components.interfaces.nsIAccessibilityService);
return accService.createXULListitemAccessible(this);
]]>
</getter>
</property>
<!-- ///////////////// nsIDOMXULSelectControlItemElement ///////////////// -->
<property name="value" onget="return this.getAttribute('value');"
onset="this.setAttribute('value', val); return val;"/>
<property name="label">
<!-- Setter purposely not implemented; the getter returns a
concatentation of label text to expose via accessibility APIs-->
<getter>
<![CDATA[
var labelText = "";
var startEl = document.getAnonymousNodes(this)[0];
if (startEl) {
var labels =
startEl.getElementsByTagNameNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',
'label');
var numLabels = labels.length;
for (count = 0; count < numLabels; count ++) {
if (labels[count].className != 'text-link') {
labelText += labels[count].value + ' ';
}
}
}
return labelText;
]]>
</getter>
</property>
<property name="selected"
onget="return this.getAttribute('selected') == 'true';"
onset="return this.setAttribute('selected',val);"/>
<property name="control">
<getter>
<![CDATA[
var parent = this.parentNode;
while (parent) {
if (parent instanceof Components.interfaces.nsIDOMXULSelectControlElement)
return parent;
parent = parent.parentNode;
}
return null;
]]>
</getter>
</property>
</implementation>
<handlers>
<handler event="click">
<![CDATA[
var listbox = this.control;
if ((event.target == this) && event.ctrlKey && (listbox.selectedItem == this)) {
listbox.clearSelection();
} else {
listbox.selectedItem = this;
}
]]>
</handler>
<handler event="contextmenu" phase="capturing">
<![CDATA[
// This needed to be called before the contextmenu gets shown to handle
// someone rightclicking on an unselected item
if (event.target == this) {
var listbox = this.control;
if (listbox) {
listbox.selectedItem = this;
}
}
]]>
</handler>
</handlers>
</binding>
</bindings>
|