PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/toolkit/content/widgets/autocomplete.xml

http://github.com/zpao/v8monkey
XML | 1739 lines | 1442 code | 257 blank | 40 comment | 0 complexity | 3b18134aa42af92aae058b387b993807 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. <?xml version="1.0"?>
  2. # -*- Mode: HTML -*-
  3. # ***** BEGIN LICENSE BLOCK *****
  4. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. #
  6. # The contents of this file are subject to the Mozilla Public License Version
  7. # 1.1 (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. # http://www.mozilla.org/MPL/
  10. #
  11. # Software distributed under the License is distributed on an "AS IS" basis,
  12. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. # for the specific language governing rights and limitations under the
  14. # License.
  15. #
  16. # The Original Code is mozilla.org browser.
  17. #
  18. # The Initial Developer of the Original Code is
  19. # Joe Hewitt.
  20. # Portions created by the Initial Developer are Copyright (C) 2003
  21. # the Initial Developer. All Rights Reserved.
  22. #
  23. # Contributor(s):
  24. # Pierre Chanial (p_ch@verizon.net)
  25. # Dean Tessman (dean_tessman@hotmail.com)
  26. # Masayuki Nakano (masayuki@d-toybox.com)
  27. # Pamela Greene (pamg.bugs@gmail.com)
  28. # Edward Lee (edward.lee@engineering.uiuc.edu)
  29. # Drew Willcoxon (adw@mozilla.com)
  30. #
  31. # Alternatively, the contents of this file may be used under the terms of
  32. # either the GNU General Public License Version 2 or later (the "GPL"), or
  33. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  34. # in which case the provisions of the GPL or the LGPL are applicable instead
  35. # of those above. If you wish to allow use of your version of this file only
  36. # under the terms of either the GPL or the LGPL, and not to allow others to
  37. # use your version of this file under the terms of the MPL, indicate your
  38. # decision by deleting the provisions above and replace them with the notice
  39. # and other provisions required by the GPL or the LGPL. If you do not delete
  40. # the provisions above, a recipient may use your version of this file under
  41. # the terms of any one of the MPL, the GPL or the LGPL.
  42. #
  43. # ***** END LICENSE BLOCK *****
  44. <!DOCTYPE bindings [
  45. <!ENTITY % actionsDTD SYSTEM "chrome://global/locale/actions.dtd">
  46. %actionsDTD;
  47. ]>
  48. <bindings id="autocompleteBindings"
  49. xmlns="http://www.mozilla.org/xbl"
  50. xmlns:html="http://www.w3.org/1999/xhtml"
  51. xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  52. xmlns:xbl="http://www.mozilla.org/xbl">
  53. <binding id="autocomplete"
  54. extends="chrome://global/content/bindings/textbox.xml#textbox">
  55. <resources>
  56. <stylesheet src="chrome://global/skin/autocomplete.css"/>
  57. </resources>
  58. <content sizetopopup="pref">
  59. <xul:hbox class="autocomplete-textbox-container" flex="1" xbl:inherits="focused">
  60. <children includes="image|deck|stack|box">
  61. <xul:image class="autocomplete-icon" allowevents="true"/>
  62. </children>
  63. <xul:hbox anonid="textbox-input-box" class="textbox-input-box" flex="1" xbl:inherits="tooltiptext=inputtooltiptext">
  64. <children/>
  65. <html:input anonid="input" class="autocomplete-textbox textbox-input"
  66. allowevents="true"
  67. xbl:inherits="tooltiptext=inputtooltiptext,onfocus,onblur,value,type,maxlength,disabled,size,readonly,placeholder,tabindex,accesskey,mozactionhint"/>
  68. </xul:hbox>
  69. <children includes="hbox"/>
  70. </xul:hbox>
  71. <xul:dropmarker anonid="historydropmarker" class="autocomplete-history-dropmarker"
  72. allowevents="true"
  73. xbl:inherits="open,enablehistory,parentfocused=focused"/>
  74. <xul:popupset anonid="popupset" class="autocomplete-result-popupset"/>
  75. <children includes="toolbarbutton"/>
  76. </content>
  77. <implementation implements="nsIAccessibleProvider, nsIAutoCompleteInput, nsIDOMXULMenuListElement">
  78. <field name="mController">null</field>
  79. <field name="mSearchNames">null</field>
  80. <field name="mIgnoreInput">false</field>
  81. <field name="mEnterEvent">null</field>
  82. <field name="mConsumeRollupEvent">false</field>
  83. <constructor><![CDATA[
  84. mController = Components.classes["@mozilla.org/autocomplete/controller;1"].
  85. getService(Components.interfaces.nsIAutoCompleteController);
  86. // For security reasons delay searches on pasted values.
  87. this.inputField.controllers.insertControllerAt(0, this._pasteController);
  88. ]]></constructor>
  89. <destructor><![CDATA[
  90. this.inputField.controllers.removeController(this._pasteController);
  91. ]]></destructor>
  92. <!-- =================== nsIAccessibleProvider =================== -->
  93. <property name="accessibleType" readonly="true">
  94. <getter>
  95. <![CDATA[
  96. // Will be exposed as nsIAccessible::ROLE_AUTOCOMPLETE
  97. return Components.interfaces.nsIAccessibleProvider.XULCombobox;
  98. ]]>
  99. </getter>
  100. </property>
  101. <!-- =================== nsIAutoCompleteInput =================== -->
  102. <field name="popup"><![CDATA[
  103. // Wrap in a block so that the let statements don't
  104. // create properties on 'this' (bug 635252).
  105. {
  106. let popup = null;
  107. let popupId = this.getAttribute("autocompletepopup");
  108. if (popupId)
  109. popup = document.getElementById(popupId);
  110. if (!popup) {
  111. popup = document.createElement("panel");
  112. popup.setAttribute("type", "autocomplete");
  113. popup.setAttribute("noautofocus", "true");
  114. let popupset = document.getAnonymousElementByAttribute(this, "anonid", "popupset");
  115. popupset.appendChild(popup);
  116. }
  117. popup.mInput = this;
  118. popup;
  119. }
  120. ]]></field>
  121. <property name="controller" onget="return this.mController;" readonly="true"/>
  122. <property name="popupOpen"
  123. onget="return this.popup.popupOpen;"
  124. onset="if (val) this.openPopup(); else this.closePopup();"/>
  125. <property name="disableAutoComplete"
  126. onset="this.setAttribute('disableautocomplete', val); return val;"
  127. onget="return this.getAttribute('disableautocomplete') == 'true';"/>
  128. <property name="completeDefaultIndex"
  129. onset="this.setAttribute('completedefaultindex', val); return val;"
  130. onget="return this.getAttribute('completedefaultindex') == 'true';"/>
  131. <property name="completeSelectedIndex"
  132. onset="this.setAttribute('completeselectedindex', val); return val;"
  133. onget="return this.getAttribute('completeselectedindex') == 'true';"/>
  134. <property name="forceComplete"
  135. onset="this.setAttribute('forcecomplete', val); return val;"
  136. onget="return this.getAttribute('forcecomplete') == 'true';"/>
  137. <property name="minResultsForPopup"
  138. onset="this.setAttribute('minresultsforpopup', val); return val;"
  139. onget="var m = parseInt(this.getAttribute('minresultsforpopup')); return isNaN(m) ? 1 : m;"/>
  140. <property name="showCommentColumn"
  141. onset="this.setAttribute('showcommentcolumn', val); return val;"
  142. onget="return this.getAttribute('showcommentcolumn') == 'true';"/>
  143. <property name="showImageColumn"
  144. onset="this.setAttribute('showimagecolumn', val); return val;"
  145. onget="return this.getAttribute('showimagecolumn') == 'true';"/>
  146. <property name="timeout"
  147. onset="this.setAttribute('timeout', val); return val;">
  148. <getter><![CDATA[
  149. // For security reasons delay searches on pasted values.
  150. if (this._valueIsPasted) {
  151. let t = parseInt(this.getAttribute('pastetimeout'));
  152. return isNaN(t) ? 1000 : t;
  153. }
  154. let t = parseInt(this.getAttribute('timeout'));
  155. return isNaN(t) ? 50 : t;
  156. ]]></getter>
  157. </property>
  158. <property name="searchParam"
  159. onget="return this.getAttribute('autocompletesearchparam') || '';"
  160. onset="this.setAttribute('autocompletesearchparam', val); return val;"/>
  161. <property name="searchCount" readonly="true"
  162. onget="this.initSearchNames(); return this.mSearchNames.length;"/>
  163. <property name="consumeRollupEvent" readonly="true"
  164. onget="return this.mConsumeRollupEvent;"/>
  165. <!-- This is the maximum number of drop-down rows we get when we
  166. hit the drop marker beside fields that have it (like the URLbar).-->
  167. <field name="maxDropMarkerRows" readonly="true">14</field>
  168. <method name="getSearchAt">
  169. <parameter name="aIndex"/>
  170. <body><![CDATA[
  171. this.initSearchNames();
  172. return this.mSearchNames[aIndex];
  173. ]]></body>
  174. </method>
  175. <property name="textValue"
  176. onget="return this.value;">
  177. <setter><![CDATA[
  178. // Completing a result should simulate the user typing the result,
  179. // so fire an input event.
  180. // Trim popup selected values, but never trim results coming from
  181. // autofill.
  182. if (this.popup.selectedIndex == -1)
  183. this._disableTrim = true;
  184. this.value = val;
  185. this._disableTrim = false;
  186. var evt = document.createEvent("UIEvents");
  187. evt.initUIEvent("input", true, false, window, 0);
  188. this.mIgnoreInput = true;
  189. this.dispatchEvent(evt);
  190. this.mIgnoreInput = false;
  191. return this.value;
  192. ]]></setter>
  193. </property>
  194. <method name="selectTextRange">
  195. <parameter name="aStartIndex"/>
  196. <parameter name="aEndIndex"/>
  197. <body><![CDATA[
  198. this.inputField.setSelectionRange(aStartIndex, aEndIndex);
  199. ]]></body>
  200. </method>
  201. <method name="onSearchBegin">
  202. <body><![CDATA[
  203. this.fireEvent("searchbegin");
  204. ]]></body>
  205. </method>
  206. <method name="onSearchComplete">
  207. <body><![CDATA[
  208. if (this.mController.matchCount == 0)
  209. this.setAttribute("nomatch", "true");
  210. else
  211. this.removeAttribute("nomatch");
  212. this.fireEvent("searchcomplete");
  213. ]]></body>
  214. </method>
  215. <method name="onTextEntered">
  216. <body><![CDATA[
  217. var rv = this.fireEvent("textentered", this.mEnterEvent);
  218. this.mEnterEvent = null;
  219. return rv;
  220. ]]></body>
  221. </method>
  222. <method name="onTextReverted">
  223. <body><![CDATA[
  224. return this.fireEvent("textreverted");
  225. ]]></body>
  226. </method>
  227. <!-- =================== nsIDOMXULMenuListElement =================== -->
  228. <property name="editable" readonly="true"
  229. onget="return true;" />
  230. <property name="crop"
  231. onset="this.setAttribute('crop',val); return val;"
  232. onget="return this.getAttribute('crop');"/>
  233. <property name="open"
  234. onget="return this.getAttribute('open') == 'true';">
  235. <setter><![CDATA[
  236. if (val)
  237. this.showHistoryPopup();
  238. else
  239. this.closePopup();
  240. ]]></setter>
  241. </property>
  242. <!-- =================== PUBLIC MEMBERS =================== -->
  243. <field name="valueIsTyped">false</field>
  244. <field name="_disableTrim">false</field>
  245. <property name="value">
  246. <getter><![CDATA[
  247. if (typeof this.onBeforeValueGet == "function") {
  248. var result = this.onBeforeValueGet();
  249. if (result)
  250. return result.value;
  251. }
  252. return this.inputField.value;
  253. ]]></getter>
  254. <setter><![CDATA[
  255. this.mIgnoreInput = true;
  256. if (typeof this.onBeforeValueSet == "function")
  257. val = this.onBeforeValueSet(val);
  258. if (typeof this.trimValue == "function" && !this._disableTrim)
  259. val = this.trimValue(val);
  260. this.valueIsTyped = false;
  261. this.inputField.value = val;
  262. if (typeof this.formatValue == "function")
  263. this.formatValue();
  264. this.mIgnoreInput = false;
  265. var event = document.createEvent('Events');
  266. event.initEvent('ValueChange', true, true);
  267. this.inputField.dispatchEvent(event);
  268. return val;
  269. ]]></setter>
  270. </property>
  271. <property name="focused" readonly="true"
  272. onget="return this.getAttribute('focused') == 'true';"/>
  273. <!-- maximum number of rows to display at a time -->
  274. <property name="maxRows"
  275. onset="this.setAttribute('maxrows', val); return val;"
  276. onget="return parseInt(this.getAttribute('maxrows')) || 0;"/>
  277. <!-- option to allow scrolling through the list via the tab key, rather than
  278. tab moving focus out of the textbox -->
  279. <property name="tabScrolling"
  280. onset="return this.setAttribute('tabscrolling', val); return val;"
  281. onget="return this.getAttribute('tabscrolling') == 'true';"/>
  282. <!-- disable key navigation handling in the popup results -->
  283. <property name="disableKeyNavigation"
  284. onset="this.setAttribute('disablekeynavigation', val); return val;"
  285. onget="return this.getAttribute('disablekeynavigation') == 'true';"/>
  286. <!-- option to completely ignore any blur events while
  287. searches are still going on. This is useful so that nothing
  288. gets autopicked if the window is required to lose focus for
  289. some reason (eg in LDAP autocomplete, another window may be
  290. brought up so that the user can enter a password to authenticate
  291. to an LDAP server). -->
  292. <property name="ignoreBlurWhileSearching"
  293. onset="this.setAttribute('ignoreblurwhilesearching', val); return val;"
  294. onget="return this.getAttribute('ignoreblurwhilesearching') == 'true';"/>
  295. <!-- option to highlight entries that don't have any matches -->
  296. <property name="highlightNonMatches"
  297. onset="this.setAttribute('highlightnonmatches', val); return val;"
  298. onget="return this.getAttribute('highlightnonmatches') == 'true';"/>
  299. <!-- =================== PRIVATE MEMBERS =================== -->
  300. <!-- ::::::::::::: autocomplete controller ::::::::::::: -->
  301. <method name="attachController">
  302. <body><![CDATA[
  303. this.mController.input = this;
  304. ]]></body>
  305. </method>
  306. <method name="detachController">
  307. <body><![CDATA[
  308. try {
  309. if (this.mController.input == this)
  310. this.mController.input = null;
  311. } catch (ex) {
  312. // nothing really to do.
  313. }
  314. ]]></body>
  315. </method>
  316. <!-- ::::::::::::: popup opening ::::::::::::: -->
  317. <method name="openPopup">
  318. <body><![CDATA[
  319. this.popup.openAutocompletePopup(this, this);
  320. ]]></body>
  321. </method>
  322. <method name="closePopup">
  323. <body><![CDATA[
  324. this.mConsumeRollupEvent = false;
  325. this.popup.closePopup();
  326. ]]></body>
  327. </method>
  328. <method name="showHistoryPopup">
  329. <body><![CDATA[
  330. // history dropmarker pushed state
  331. function cleanup(popup) {
  332. popup.removeEventListener("popupshowing", onShow, false);
  333. }
  334. function onShow(event) {
  335. var popup = event.target, input = popup.input;
  336. cleanup(popup);
  337. input.setAttribute("open", "true");
  338. function onHide() {
  339. input.removeAttribute("open");
  340. input.mConsumeRollupEvent = false;
  341. popup.removeEventListener("popuphiding", onHide, false);
  342. }
  343. popup.addEventListener("popuphiding", onHide, false);
  344. }
  345. this.popup.addEventListener("popupshowing", onShow, false);
  346. setTimeout(cleanup, 1000, this.popup);
  347. // Store our "normal" maxRows on the popup, so that it can reset the
  348. // value when the popup is hidden.
  349. this.popup._normalMaxRows = this.maxRows;
  350. // Increase our maxRows temporarily, since we want the dropdown to
  351. // be bigger in this case. The popup's popupshowing/popuphiding
  352. // handlers will take care of resetting this.
  353. this.maxRows = this.maxDropMarkerRows;
  354. // Ensure that we have focus.
  355. if (!this.focused)
  356. this.focus();
  357. this.mConsumeRollupEvent = true;
  358. this.attachController();
  359. this.mController.startSearch("");
  360. ]]></body>
  361. </method>
  362. <method name="toggleHistoryPopup">
  363. <body><![CDATA[
  364. if (!this.popup.mPopupOpen)
  365. this.showHistoryPopup();
  366. else
  367. this.closePopup();
  368. ]]></body>
  369. </method>
  370. <!-- ::::::::::::: event dispatching ::::::::::::: -->
  371. <method name="fireEvent">
  372. <parameter name="aEventType"/>
  373. <body><![CDATA[
  374. var cancel = false;
  375. // handle any xml attribute event handlers
  376. var handler = this.getAttribute("on"+aEventType);
  377. if (handler) {
  378. var fn = new Function("eventType", "param", handler);
  379. cancel = fn.apply(this, arguments);
  380. }
  381. return cancel;
  382. ]]></body>
  383. </method>
  384. <!-- ::::::::::::: key handling ::::::::::::: -->
  385. <method name="onKeyPress">
  386. <parameter name="aEvent"/>
  387. <body><![CDATA[
  388. if (aEvent.target.localName != "textbox")
  389. return; // Let child buttons of autocomplete take input
  390. //XXXpch this is so bogus...
  391. if (aEvent.defaultPrevented)
  392. return false;
  393. var cancel = false;
  394. // Catch any keys that could potentially move the caret. Ctrl can be
  395. // used in combination with these keys on Windows and Linux; and Alt
  396. // can be used on OS X, so make sure the unused one isn't used.
  397. if (!this.disableKeyNavigation &&
  398. #ifdef XP_MACOSX
  399. !aEvent.ctrlKey) {
  400. #else
  401. !aEvent.altKey) {
  402. #endif
  403. switch (aEvent.keyCode) {
  404. case KeyEvent.DOM_VK_LEFT:
  405. case KeyEvent.DOM_VK_RIGHT:
  406. case KeyEvent.DOM_VK_HOME:
  407. cancel = this.mController.handleKeyNavigation(aEvent.keyCode);
  408. break;
  409. }
  410. }
  411. // Handle keys that are not part of a keyboard shortcut (no Ctrl or Alt)
  412. if (!this.disableKeyNavigation && !aEvent.ctrlKey && !aEvent.altKey) {
  413. switch (aEvent.keyCode) {
  414. case KeyEvent.DOM_VK_TAB:
  415. if (this.tabScrolling && this.popup.mPopupOpen)
  416. cancel = this.mController.handleKeyNavigation(aEvent.shiftKey ?
  417. KeyEvent.DOM_VK_UP :
  418. KeyEvent.DOM_VK_DOWN);
  419. break;
  420. case KeyEvent.DOM_VK_UP:
  421. case KeyEvent.DOM_VK_DOWN:
  422. case KeyEvent.DOM_VK_PAGE_UP:
  423. case KeyEvent.DOM_VK_PAGE_DOWN:
  424. cancel = this.mController.handleKeyNavigation(aEvent.keyCode);
  425. break;
  426. }
  427. }
  428. // Handle keys we know aren't part of a shortcut, even with Alt or
  429. // Ctrl.
  430. switch (aEvent.keyCode) {
  431. case KeyEvent.DOM_VK_ESCAPE:
  432. cancel = this.mController.handleEscape();
  433. break;
  434. case KeyEvent.DOM_VK_RETURN:
  435. #ifdef XP_MACOSX
  436. // Prevent the default action, since it will beep on Mac
  437. if (aEvent.metaKey)
  438. aEvent.preventDefault();
  439. #endif
  440. this.mEnterEvent = aEvent;
  441. cancel = this.mController.handleEnter(false);
  442. break;
  443. case KeyEvent.DOM_VK_DELETE:
  444. #ifdef XP_MACOSX
  445. case KeyEvent.DOM_VK_BACK_SPACE:
  446. if (aEvent.shiftKey)
  447. #endif
  448. cancel = this.mController.handleDelete();
  449. break;
  450. case KeyEvent.DOM_VK_DOWN:
  451. case KeyEvent.DOM_VK_UP:
  452. if (aEvent.altKey)
  453. this.toggleHistoryPopup();
  454. break;
  455. #ifndef XP_MACOSX
  456. case KeyEvent.DOM_VK_F4:
  457. this.toggleHistoryPopup();
  458. break;
  459. #endif
  460. }
  461. if (cancel) {
  462. aEvent.stopPropagation();
  463. aEvent.preventDefault();
  464. }
  465. return true;
  466. ]]></body>
  467. </method>
  468. <!-- ::::::::::::: miscellaneous ::::::::::::: -->
  469. <method name="initSearchNames">
  470. <body><![CDATA[
  471. if (!this.mSearchNames) {
  472. var names = this.getAttribute("autocompletesearch");
  473. if (!names)
  474. this.mSearchNames = [];
  475. else
  476. this.mSearchNames = names.split(" ");
  477. }
  478. ]]></body>
  479. </method>
  480. <method name="_focus">
  481. <!-- doesn't reset this.mController -->
  482. <body><![CDATA[
  483. this._dontBlur = true;
  484. this.focus();
  485. this._dontBlur = false;
  486. ]]></body>
  487. </method>
  488. <method name="resetActionType">
  489. <body><![CDATA[
  490. if (this.mIgnoreInput)
  491. return;
  492. this.removeAttribute("actiontype");
  493. ]]></body>
  494. </method>
  495. <field name="_valueIsPasted">false</field>
  496. <field name="_pasteController"><![CDATA[
  497. ({
  498. _autocomplete: this,
  499. _kGlobalClipboard: Components.interfaces.nsIClipboard.kGlobalClipboard,
  500. supportsCommand: function(aCommand) aCommand == "cmd_paste",
  501. doCommand: function(aCommand) {
  502. this._autocomplete._valueIsPasted = true;
  503. this._autocomplete.editor.paste(this._kGlobalClipboard);
  504. this._autocomplete._valueIsPasted = false;
  505. },
  506. isCommandEnabled: function(aCommand) {
  507. return this._autocomplete.editor.isSelectionEditable &&
  508. this._autocomplete.editor.canPaste(this._kGlobalClipboard);
  509. },
  510. onEvent: function() {}
  511. })
  512. ]]></field>
  513. </implementation>
  514. <handlers>
  515. <handler event="input"><![CDATA[
  516. if (!this.mIgnoreInput && this.mController.input == this) {
  517. this.valueIsTyped = true;
  518. this.mController.handleText();
  519. }
  520. this.resetActionType();
  521. ]]></handler>
  522. <handler event="keypress" phase="capturing"
  523. action="return this.onKeyPress(event);"/>
  524. <handler event="compositionstart" phase="capturing"
  525. action="if (this.mController.input == this) this.mController.handleStartComposition();"/>
  526. <handler event="compositionend" phase="capturing"
  527. action="if (this.mController.input == this) this.mController.handleEndComposition();"/>
  528. <handler event="focus" phase="capturing"
  529. action="this.attachController();"/>
  530. <handler event="blur" phase="capturing"
  531. action="if (!this._dontBlur) this.detachController();"/>
  532. </handlers>
  533. </binding>
  534. <binding id="autocomplete-result-popup" extends="chrome://global/content/bindings/autocomplete.xml#autocomplete-base-popup">
  535. <resources>
  536. <stylesheet src="chrome://global/skin/tree.css"/>
  537. <stylesheet src="chrome://global/skin/autocomplete.css"/>
  538. </resources>
  539. <content ignorekeys="true" level="top">
  540. <xul:tree anonid="tree" class="autocomplete-tree plain" hidecolumnpicker="true" flex="1" seltype="single">
  541. <xul:treecols anonid="treecols">
  542. <xul:treecol id="treecolAutoCompleteValue" class="autocomplete-treecol" flex="1" overflow="true"/>
  543. </xul:treecols>
  544. <xul:treechildren class="autocomplete-treebody"/>
  545. </xul:tree>
  546. </content>
  547. <implementation>
  548. <field name="mShowCommentColumn">false</field>
  549. <field name="mShowImageColumn">false</field>
  550. <property name="showCommentColumn"
  551. onget="return this.mShowCommentColumn;">
  552. <setter>
  553. <![CDATA[
  554. if (!val && this.mShowCommentColumn) {
  555. // reset the flex on the value column and remove the comment column
  556. document.getElementById("treecolAutoCompleteValue").setAttribute("flex", 1);
  557. this.removeColumn("treecolAutoCompleteComment");
  558. } else if (val && !this.mShowCommentColumn) {
  559. // reset the flex on the value column and add the comment column
  560. document.getElementById("treecolAutoCompleteValue").setAttribute("flex", 2);
  561. this.addColumn({id: "treecolAutoCompleteComment", flex: 1});
  562. }
  563. this.mShowCommentColumn = val;
  564. return val;
  565. ]]>
  566. </setter>
  567. </property>
  568. <property name="showImageColumn"
  569. onget="return this.mShowImageColumn;">
  570. <setter>
  571. <![CDATA[
  572. if (!val && this.mShowImageColumn) {
  573. // remove the image column
  574. this.removeColumn("treecolAutoCompleteImage");
  575. } else if (val && !this.mShowImageColumn) {
  576. // add the image column
  577. this.addColumn({id: "treecolAutoCompleteImage", flex: 1});
  578. }
  579. this.mShowImageColumn = val;
  580. return val;
  581. ]]>
  582. </setter>
  583. </property>
  584. <method name="addColumn">
  585. <parameter name="aAttrs"/>
  586. <body>
  587. <![CDATA[
  588. var col = document.createElement("treecol");
  589. col.setAttribute("class", "autocomplete-treecol");
  590. for (var name in aAttrs)
  591. col.setAttribute(name, aAttrs[name]);
  592. this.treecols.appendChild(col);
  593. return col;
  594. ]]>
  595. </body>
  596. </method>
  597. <method name="removeColumn">
  598. <parameter name="aColId"/>
  599. <body>
  600. <![CDATA[
  601. return this.treecols.removeChild(document.getElementById(aColId));
  602. ]]>
  603. </body>
  604. </method>
  605. <property name="selectedIndex"
  606. onget="return this.tree.currentIndex;">
  607. <setter>
  608. <![CDATA[
  609. this.tree.view.selection.select(val);
  610. if (this.tree.treeBoxObject.height > 0)
  611. this.tree.treeBoxObject.ensureRowIsVisible(val < 0 ? 0 : val);
  612. // Fire select event on xul:tree so that accessibility API
  613. // support layer can fire appropriate accessibility events.
  614. var event = document.createEvent('Events');
  615. event.initEvent("select", true, true);
  616. this.tree.dispatchEvent(event);
  617. return val;
  618. ]]></setter>
  619. </property>
  620. <method name="adjustHeight">
  621. <body>
  622. <![CDATA[
  623. // detect the desired height of the tree
  624. var bx = this.tree.treeBoxObject;
  625. var view = this.tree.view;
  626. if (!view)
  627. return;
  628. var rows = this.maxRows;
  629. if (!view.rowCount || (rows && view.rowCount < rows))
  630. rows = view.rowCount;
  631. var height = rows * bx.rowHeight;
  632. if (height == 0)
  633. this.tree.setAttribute("collapsed", "true");
  634. else {
  635. if (this.tree.hasAttribute("collapsed"))
  636. this.tree.removeAttribute("collapsed");
  637. this.tree.setAttribute("height", height);
  638. }
  639. this.tree.setAttribute("hidescrollbar", view.rowCount <= rows);
  640. ]]>
  641. </body>
  642. </method>
  643. <method name="openAutocompletePopup">
  644. <parameter name="aInput"/>
  645. <parameter name="aElement"/>
  646. <body><![CDATA[
  647. // until we have "baseBinding", (see bug #373652) this allows
  648. // us to override openAutocompletePopup(), but still call
  649. // the method on the base class
  650. this._openAutocompletePopup(aInput, aElement);
  651. ]]></body>
  652. </method>
  653. <method name="_openAutocompletePopup">
  654. <parameter name="aInput"/>
  655. <parameter name="aElement"/>
  656. <body><![CDATA[
  657. if (!this.mPopupOpen) {
  658. this.mInput = aInput;
  659. this.view = aInput.controller.QueryInterface(Components.interfaces.nsITreeView);
  660. this.invalidate();
  661. this.showCommentColumn = this.mInput.showCommentColumn;
  662. this.showImageColumn = this.mInput.showImageColumn;
  663. var rect = aElement.getBoundingClientRect();
  664. var nav = aElement.ownerDocument.defaultView.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  665. .getInterface(Components.interfaces.nsIWebNavigation);
  666. var docShell = nav.QueryInterface(Components.interfaces.nsIDocShell);
  667. var docViewer = docShell.contentViewer.QueryInterface(Components.interfaces.nsIMarkupDocumentViewer);
  668. var width = (rect.right - rect.left) * docViewer.fullZoom;
  669. this.setAttribute("width", width > 100 ? width : 100);
  670. // Adjust the direction of the autocomplete popup list based on the textbox direction, bug 649840
  671. var popupDirection = aElement.ownerDocument.defaultView.getComputedStyle(aElement).direction;
  672. this.style.direction = popupDirection;
  673. // setConsumeRollupEvent() before we call openPopup()
  674. var nsIPopupBO = Components.interfaces.nsIPopupBoxObject;
  675. this.popupBoxObject.setConsumeRollupEvent(
  676. this.mInput.consumeRollupEvent ?
  677. nsIPopupBO.ROLLUP_CONSUME :
  678. nsIPopupBO.ROLLUP_NO_CONSUME);
  679. this.openPopup(aElement, "after_start", 0, 0, false, false);
  680. }
  681. ]]></body>
  682. </method>
  683. <method name="invalidate">
  684. <body><![CDATA[
  685. this.adjustHeight();
  686. this.tree.treeBoxObject.invalidate();
  687. ]]></body>
  688. </method>
  689. <method name="selectBy">
  690. <parameter name="aReverse"/>
  691. <parameter name="aPage"/>
  692. <body><![CDATA[
  693. try {
  694. var amount = aPage ? 5 : 1;
  695. this.selectedIndex = this.getNextIndex(aReverse, amount, this.selectedIndex, this.tree.view.rowCount-1);
  696. if (this.selectedIndex == -1) {
  697. this.input._focus();
  698. }
  699. } catch (ex) {
  700. // do nothing - occasionally timer-related js errors happen here
  701. // e.g. "this.selectedIndex has no properties", when you type fast and hit a
  702. // navigation key before this popup has opened
  703. }
  704. ]]></body>
  705. </method>
  706. <!-- =================== PUBLIC MEMBERS =================== -->
  707. <field name="tree">
  708. document.getAnonymousElementByAttribute(this, "anonid", "tree");
  709. </field>
  710. <field name="treecols">
  711. document.getAnonymousElementByAttribute(this, "anonid", "treecols");
  712. </field>
  713. <property name="view"
  714. onget="return this.mView;">
  715. <setter><![CDATA[
  716. // We must do this by hand because the tree binding may not be ready yet
  717. this.mView = val;
  718. var bx = this.tree.boxObject;
  719. bx = bx.QueryInterface(Components.interfaces.nsITreeBoxObject);
  720. bx.view = val;
  721. ]]></setter>
  722. </property>
  723. </implementation>
  724. </binding>
  725. <binding id="autocomplete-base-popup" extends="chrome://global/content/bindings/popup.xml#popup">
  726. <implementation implements="nsIAutoCompletePopup">
  727. <!-- nsIAccessible from #popup -->
  728. <property name="accessibleType" readonly="true"
  729. onget="return Components.interfaces.nsIAccessibleProvider.NoAccessible;"/>
  730. <field name="mInput">null</field>
  731. <field name="mPopupOpen">false</field>
  732. <!-- =================== nsIAutoCompletePopup =================== -->
  733. <property name="input" readonly="true"
  734. onget="return this.mInput"/>
  735. <property name="overrideValue" readonly="true"
  736. onget="return null;"/>
  737. <property name="popupOpen" readonly="true"
  738. onget="return this.mPopupOpen;"/>
  739. <method name="closePopup">
  740. <body>
  741. <![CDATA[
  742. if (this.mPopupOpen) {
  743. this.hidePopup();
  744. this.removeAttribute("width");
  745. }
  746. ]]>
  747. </body>
  748. </method>
  749. <!-- This is the default number of rows that we give the autocomplete
  750. popup when the textbox doesn't have a "maxrows" attribute
  751. for us to use. -->
  752. <field name="defaultMaxRows" readonly="true">6</field>
  753. <!-- In some cases (e.g. when the input's dropmarker button is clicked),
  754. the input wants to display a popup with more rows. In that case, it
  755. should increase its maxRows property and store the "normal" maxRows
  756. in this field. When the popup is hidden, we restore the input's
  757. maxRows to the value stored in this field.
  758. This field is set to -1 between uses so that we can tell when it's
  759. been set by the input and when we need to set it in the popupshowing
  760. handler. -->
  761. <field name="_normalMaxRows">-1</field>
  762. <property name="maxRows" readonly="true">
  763. <getter>
  764. <![CDATA[
  765. return (this.mInput && this.mInput.maxRows) || this.defaultMaxRows;
  766. ]]>
  767. </getter>
  768. </property>
  769. <method name="getNextIndex">
  770. <parameter name="aReverse"/>
  771. <parameter name="aAmount"/>
  772. <parameter name="aIndex"/>
  773. <parameter name="aMaxRow"/>
  774. <body><![CDATA[
  775. if (aMaxRow < 0)
  776. return -1;
  777. var newIdx = aIndex + (aReverse?-1:1)*aAmount;
  778. if (aReverse && aIndex == -1 || newIdx > aMaxRow && aIndex != aMaxRow)
  779. newIdx = aMaxRow;
  780. else if (!aReverse && aIndex == -1 || newIdx < 0 && aIndex != 0)
  781. newIdx = 0;
  782. if (newIdx < 0 && aIndex == 0 || newIdx > aMaxRow && aIndex == aMaxRow)
  783. aIndex = -1;
  784. else
  785. aIndex = newIdx;
  786. return aIndex;
  787. ]]></body>
  788. </method>
  789. <method name="onPopupClick">
  790. <parameter name="aEvent"/>
  791. <body><![CDATA[
  792. var controller = this.view.QueryInterface(Components.interfaces.nsIAutoCompleteController);
  793. controller.handleEnter(true);
  794. ]]></body>
  795. </method>
  796. </implementation>
  797. <handlers>
  798. <handler event="popupshowing"><![CDATA[
  799. // If normalMaxRows wasn't already set by the input, then set it here
  800. // so that we restore the correct number when the popup is hidden.
  801. if (this._normalMaxRows < 0)
  802. this._normalMaxRows = this.mInput.maxRows;
  803. this.mPopupOpen = true;
  804. ]]></handler>
  805. <handler event="popuphiding"><![CDATA[
  806. var isListActive = true;
  807. if (this.selectedIndex == -1)
  808. isListActive = false;
  809. var controller = this.view.QueryInterface(Components.interfaces.nsIAutoCompleteController);
  810. controller.stopSearch();
  811. this.mPopupOpen = false;
  812. // Reset the maxRows property to the cached "normal" value, and reset
  813. // _normalMaxRows so that we can detect whether it was set by the input
  814. // when the popupshowing handler runs.
  815. this.mInput.maxRows = this._normalMaxRows;
  816. this._normalMaxRows = -1;
  817. // If the list was being navigated and then closed, make sure
  818. // we fire accessible focus event back to textbox
  819. if (isListActive) {
  820. this.mInput.mIgnoreFocus = true;
  821. this.mInput._focus();
  822. this.mInput.mIgnoreFocus = false;
  823. }
  824. ]]></handler>
  825. </handlers>
  826. </binding>
  827. <binding id="autocomplete-rich-result-popup" extends="chrome://global/content/bindings/autocomplete.xml#autocomplete-base-popup">
  828. <resources>
  829. <stylesheet src="chrome://global/skin/autocomplete.css"/>
  830. </resources>
  831. <content ignorekeys="true" level="top">
  832. <xul:richlistbox anonid="richlistbox" class="autocomplete-richlistbox" flex="1"/>
  833. <xul:hbox>
  834. <children/>
  835. </xul:hbox>
  836. </content>
  837. <implementation implements="nsIAutoCompletePopup">
  838. <field name="_currentIndex">0</field>
  839. <!-- =================== nsIAutoCompletePopup =================== -->
  840. <property name="selectedIndex"
  841. onget="return this.richlistbox.selectedIndex;">
  842. <setter>
  843. <![CDATA[
  844. this.richlistbox.selectedIndex = val;
  845. // when clearing the selection (val == -1, so selectedItem will be
  846. // null), we want to scroll back to the top. see bug #406194
  847. this.richlistbox.ensureElementIsVisible(
  848. this.richlistbox.selectedItem || this.richlistbox.firstChild);
  849. return val;
  850. ]]>
  851. </setter>
  852. </property>
  853. <method name="openAutocompletePopup">
  854. <parameter name="aInput"/>
  855. <parameter name="aElement"/>
  856. <body>
  857. <![CDATA[
  858. // until we have "baseBinding", (see bug #373652) this allows
  859. // us to override openAutocompletePopup(), but still call
  860. // the method on the base class
  861. this._openAutocompletePopup(aInput, aElement);
  862. ]]>
  863. </body>
  864. </method>
  865. <method name="_openAutocompletePopup">
  866. <parameter name="aInput"/>
  867. <parameter name="aElement"/>
  868. <body>
  869. <![CDATA[
  870. if (!this.mPopupOpen) {
  871. this.mInput = aInput;
  872. // clear any previous selection, see bugs 400671 and 488357
  873. this.selectedIndex = -1;
  874. var width = aElement.getBoundingClientRect().width;
  875. this.setAttribute("width", width > 100 ? width : 100);
  876. // invalidate() depends on the width attribute
  877. this._invalidate();
  878. // setConsumeRollupEvent() before we call openPopup()
  879. var nsIPopupBO = Components.interfaces.nsIPopupBoxObject;
  880. this.popupBoxObject.setConsumeRollupEvent(
  881. this.mInput.consumeRollupEvent ?
  882. nsIPopupBO.ROLLUP_CONSUME :
  883. nsIPopupBO.ROLLUP_NO_CONSUME);
  884. this.openPopup(aElement, "after_start", 0, 0, false, false);
  885. }
  886. ]]>
  887. </body>
  888. </method>
  889. <method name="invalidate">
  890. <body>
  891. <![CDATA[
  892. // Don't bother doing work if we're not even showing
  893. if (!this.mPopupOpen)
  894. return;
  895. this._invalidate();
  896. ]]>
  897. </body>
  898. </method>
  899. <method name="_invalidate">
  900. <body>
  901. <![CDATA[
  902. if (!this.hasAttribute("height")) {
  903. // collapsed if no matches
  904. this.richlistbox.collapsed = (this._matchCount == 0);
  905. // Dynamically update height until richlistbox.rows works (bug 401939)
  906. // Adjust the height immediately and after the row contents update
  907. this.adjustHeight();
  908. setTimeout(function(self) self.adjustHeight(), 0, this);
  909. }
  910. // make sure to collapse any existing richlistitems
  911. // that aren't going to be used
  912. var existingItemsCount = this.richlistbox.childNodes.length;
  913. for (var i = this._matchCount; i < existingItemsCount; i++)
  914. this.richlistbox.childNodes[i].collapsed = true;
  915. this._currentIndex = 0;
  916. this._appendCurrentResult();
  917. ]]>
  918. </body>
  919. </method>
  920. <property name="maxResults" readonly="true">
  921. <getter>
  922. <![CDATA[
  923. // this is how many richlistitems will be kept around
  924. // (note, this getter may be overridden)
  925. return 20;
  926. ]]>
  927. </getter>
  928. </property>
  929. <property name="_matchCount" readonly="true">
  930. <getter>
  931. <![CDATA[
  932. return Math.min(this.mInput.controller.matchCount, this.maxResults);
  933. ]]>
  934. </getter>
  935. </property>
  936. <method name="adjustHeight">
  937. <body>
  938. <![CDATA[
  939. // Figure out how many rows to show
  940. let rows = this.richlistbox.childNodes;
  941. let numRows = Math.min(this._matchCount, this.maxRows, rows.length);
  942. // Default the height to 0 if we have no rows to show
  943. let height = 0;
  944. if (numRows) {
  945. let firstRowRect = rows[0].getBoundingClientRect();
  946. let lastRowRect = rows[numRows - 1].getBoundingClientRect();
  947. // Calculate the height to have the first row to last row shown
  948. height = lastRowRect.bottom - firstRowRect.top;
  949. }
  950. // Only update the height if we have a non-zero height and if it
  951. // changed (the richlistbox is collapsed if there are no results)
  952. if (height && height != this.richlistbox.height)
  953. this.richlistbox.height = height;
  954. ]]>
  955. </body>
  956. </method>
  957. <method name="_appendCurrentResult">
  958. <body>
  959. <![CDATA[
  960. var controller = this.mInput.controller;
  961. var matchCount = this._matchCount;
  962. var existingItemsCount = this.richlistbox.childNodes.length;
  963. // Process maxRows per chunk to improve performance and user experience
  964. for (let i = 0; i < this.maxRows; i++) {
  965. if (this._currentIndex >= matchCount)
  966. return;
  967. var item;
  968. // trim the leading/trailing whitespace
  969. var trimmedSearchString = controller.searchString.replace(/^\s+/, "").replace(/\s+$/, "");
  970. // Unescape the URI spec for showing as an entry in the popup
  971. let url = Components.classes["@mozilla.org/intl/texttosuburi;1"].
  972. getService(Components.interfaces.nsITextToSubURI).
  973. unEscapeURIForUI("UTF-8", controller.getValueAt(this._currentIndex));
  974. if (typeof this.input.trimValue == "function")
  975. url = this.input.trimValue(url);
  976. if (this._currentIndex < existingItemsCount) {
  977. // re-use the existing item
  978. item = this.richlistbox.childNodes[this._currentIndex];
  979. // Completely re-use the existing richlistitem if it's the same
  980. if (item.getAttribute("text") == trimmedSearchString &&
  981. item.getAttribute("url") == url) {
  982. item.collapsed = false;
  983. this._currentIndex++;
  984. continue;
  985. }
  986. }
  987. else {
  988. // need to create a new item
  989. item = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "richlistitem");
  990. }
  991. // set these attributes before we set the class
  992. // so that we can use them from the constructor
  993. item.setAttribute("image", controller.getImageAt(this._currentIndex));
  994. item.setAttribute("url", url);
  995. item.setAttribute("title", controller.getCommentAt(this._currentIndex));
  996. item.setAttribute("type", controller.getStyleAt(this._currentIndex));
  997. item.setAttribute("text", trimmedSearchString);
  998. if (this._currentIndex < existingItemsCount) {
  999. // re-use the existing item
  1000. item._adjustAcItem();
  1001. item.collapsed = false;
  1002. }
  1003. else {
  1004. // set the class at the end so we can use the attributes
  1005. // in the xbl constructor
  1006. item.className = "autocomplete-richlistitem";
  1007. this.richlistbox.appendChild(item);
  1008. }
  1009. this._currentIndex++;
  1010. }
  1011. // yield after each batch of items so that typing the url bar is responsive
  1012. setTimeout(function (self) { self._appendCurrentResult(); }, 0, this);
  1013. ]]>
  1014. </body>
  1015. </method>
  1016. <method name="selectBy">
  1017. <parameter name="aReverse"/>
  1018. <parameter name="aPage"/>
  1019. <body>
  1020. <![CDATA[
  1021. try {
  1022. var amount = aPage ? 5 : 1;
  1023. // because we collapsed unused items, we can't use this.richlistbox.getRowCount(), we need to use the matchCount
  1024. this.selectedIndex = this.getNextIndex(aReverse, amount, this.selectedIndex, this._matchCount - 1);
  1025. if (this.selectedIndex == -1) {
  1026. this.input._focus();
  1027. }
  1028. } catch (ex) {
  1029. // do nothing - occasionally timer-related js errors happen here
  1030. // e.g. "this.selectedIndex has no properties", when you type fast and hit a
  1031. // navigation key before this popup has opened
  1032. }
  1033. ]]>
  1034. </body>
  1035. </method>
  1036. <field name="richlistbox">
  1037. document.getAnonymousElementByAttribute(this, "anonid", "richlistbox");
  1038. </field>
  1039. <property name="view"
  1040. onget="return this.mInput.controller;"
  1041. onset="return val;"/>
  1042. </implementation>
  1043. </binding>
  1044. <binding id="autocomplete-richlistitem" extends="chrome://global/content/bindings/richlistbox.xml#richlistitem">
  1045. <content>
  1046. <xul:hbox align="center" class="ac-title-box">
  1047. <xul:image xbl:inherits="src=image" class="ac-site-icon"/>
  1048. <xul:hbox anonid="title-box" class="ac-title" flex="1"
  1049. onunderflow="_doUnderflow('_title');"
  1050. onoverflow="_doOverflow('_title');">
  1051. <xul:description anonid="title" class="ac-normal-text ac-comment" xbl:inherits="selected"/>
  1052. </xul:hbox>
  1053. <xul:label anonid="title-overflow-ellipsis" xbl:inherits="selected"
  1054. class="ac-ellipsis-after ac-comment"/>
  1055. <xul:hbox anonid="extra-box" class="ac-extra" align="center" hidden="true">
  1056. <xul:image class="ac-result-type-tag"/>
  1057. <xul:label class="ac-normal-text ac-comment" xbl:inherits="selected" value=":"/>
  1058. <xul:description anonid="extra" class="ac-normal-text ac-comment" xbl:inherits="selected"/>
  1059. </xul:hbox>
  1060. <xul:image anonid="type-image" class="ac-type-icon"/>
  1061. </xul:hbox>
  1062. <xul:hbox align="center" class="ac-url-box">
  1063. <xul:spacer class="ac-site-icon"/>
  1064. <xul:image class="ac-action-icon"/>
  1065. <xul:hbox anonid="url-box" class="ac-url" flex="1"
  1066. onunderflow="_doUnderflow('_url');"
  1067. onoverflow="_doOverflow('_url');">
  1068. <xul:description anonid="url" class="ac-normal-text ac-url-text"
  1069. xbl:inherits="selected type"/>
  1070. <xul:description anonid="action" class="ac-normal-text ac-action-text"
  1071. xbl:inherits="selected type"/>
  1072. </xul:hbox>
  1073. <xul:label anonid="url-overflow-ellipsis" xbl:inherits="selected"
  1074. class="ac-ellipsis-after ac-url-text"/>
  1075. <xul:spacer class="ac-type-icon"/>
  1076. </xul:hbox>
  1077. </content>
  1078. <implementation implements="nsIDOMXULSelectControlItemElement">
  1079. <constructor>
  1080. <![CDATA[
  1081. let ellipsis = "\u2026";
  1082. try {
  1083. ellipsis = Components.classes["@mozilla.org/preferences-service;1"].
  1084. getService(Components.interfaces.nsIPrefBranch).
  1085. getComplexValue("intl.ellipsis",
  1086. Components.interfaces.nsIPrefLocalizedString).data;
  1087. } catch (ex) {
  1088. // Do nothing.. we already have a default
  1089. }
  1090. this._urlOverflowEllipsis = document.getAnonymousElementByAttribute(this, "anonid", "url-overflow-ellipsis");
  1091. this._titleOverflowEllipsis = document.getAnonymousElementByAttribute(this, "anonid", "title-overflow-ellipsis");
  1092. this._urlOverflowEllipsis.value = ellipsis;
  1093. this._titleOverflowEllipsis.value = ellipsis;
  1094. this._typeImage = document.getAnonymousElementByAttribute(this, "anonid", "type-image");
  1095. this._urlBox = document.getAnonymousElementByAttribute(this, "anonid", "url-box");
  1096. this._url = document.getAnonymousElementByAttribute(this, "anonid", "url");
  1097. this._action = document.getAnonymousElementByAttribute(this, "anonid", "action");
  1098. this._titleBox = document.getAnonymousElementByAttribute(this, "anonid", "title-box");
  1099. this._title = document.getAnonymousElementByAttribute(this, "anonid", "title");
  1100. this._extraBox = document.getAnonymousElementByAttribute(this, "anonid", "extra-box");
  1101. this._extra = document.getAnonymousElementByAttribute(this, "anonid", "extra");
  1102. this._adjustAcItem();
  1103. ]]>
  1104. </constructor>
  1105. <property name="label" readonly="true">
  1106. <getter>
  1107. <![CDATA[
  1108. var title = this.getAttribute("title");
  1109. var url = this.getAttribute("url");
  1110. var panel = this.parentNode.parentNode;
  1111. // allow consumers that have extended popups to override
  1112. // the label values for the richlistitems
  1113. if (panel.createResultLabel)
  1114. return panel.createResultLabel(title, url, this.getAttribute("type"));
  1115. // aType (ex: "ac-result-type-<aType>") is related to the class of the image,
  1116. // and is not "visible" text so don't use it for the label (for accessibility).
  1117. return title + " " + url;
  1118. ]]>
  1119. </getter>
  1120. </property>
  1121. <field name="_boundaryCutoff">null</field>
  1122. <property name="boundaryCutoff" readonly="true">
  1123. <getter>
  1124. <![CDATA[
  1125. if (!this._boundaryCutoff) {
  1126. this._boundaryCutoff =
  1127. Components.classes["@mozilla.org/preferences-service;1"].
  1128. getService(Components.interfaces.nsIPrefBranch).
  1129. getIntPref("toolkit.autocomplete.richBoundaryCutoff");
  1130. }
  1131. return this._boundaryCutoff;
  1132. ]]>
  1133. </getter>
  1134. </property>
  1135. <method name="_getBoundaryIndices">
  1136. <parameter name="aText"/>
  1137. <parameter name="aSearchTokens"/>
  1138. <body>
  1139. <![CDATA[
  1140. // Short circuit for empty search ([""] == "")
  1141. if (aSearchTokens == "")
  1142. return [0, aText.length];
  1143. // Find which regions of text match the search terms
  1144. let regions = [];
  1145. for each (let search in aSearchTokens) {
  1146. let matchIndex;
  1147. let startIndex = 0;
  1148. let searchLen = search.length;
  1149. // Find all matches of the search terms, but stop early for perf
  1150. let lowerText = aText.toLowerCase().substr(0, this.boundaryCutoff);
  1151. while ((matchIndex = lowerText.indexOf(search, startIndex)) >= 0) {
  1152. // Start the next search from where this one finished
  1153. startIndex = matchIndex + searchLen;
  1154. regions.push([matchIndex, startIndex]);
  1155. }
  1156. }
  1157. // Sort the regions by start position then end position
  1158. regions = regions.sort(function(a, b) let (start = a[0] - b[0])
  1159. start == 0 ? a[1] - b[1] : start);
  1160. // Generate the boundary indices from each region
  1161. let start = 0;
  1162. let end = 0;
  1163. let boundaries = [];
  1164. let len = regions.length;
  1165. for (let i = 0; i < len; i++) {
  1166. // We have a new boundary if the start of the next is past the end
  1167. let region = regions[i];
  1168. if (region[0] > end) {
  1169. // First index is the beginning of match
  1170. boundaries.push(start);
  1171. // Second index is the beginning of non-match
  1172. boundaries.push(end);
  1173. // Track the new region now that we've stored the previous one
  1174. start = region[0];
  1175. }
  1176. // Push back the end index for the current or new region
  1177. end = Math.max(end, region[1]);
  1178. }
  1179. // Add the last region
  1180. boundaries.push(start);
  1181. boundaries.push(end);
  1182. // Put on the end boundary if necessary
  1183. if (end < aText.length)
  1184. boundaries.push(aText.length);
  1185. // Skip the first item because it's always 0
  1186. return boundaries.slice(1);
  1187. ]]>
  1188. </body>
  1189. </method>
  1190. <method name="_getSearchTokens">
  1191. <parameter name="aSearch"/>
  1192. <body>
  1193. <![CDATA[
  1194. let search = aSearch.toLowerCase();
  1195. return search.split(/\s+/);
  1196. ]]>
  1197. </body>
  1198. </method>
  1199. <method name="_needsAlternateEmphasis">
  1200. <parameter name="aText"/>
  1201. <body>
  1202. <![CDATA[
  1203. for (let i = aText.length; --i >= 0; ) {
  1204. let charCode = aText.charCodeAt(i);
  1205. // Arabic, Syriac, Indic languages are likely to have ligatures
  1206. // that are broken when using the main emphasis styling
  1207. if (0x0600 <= charCode && charCode <= 0x109F)
  1208. return true;
  1209. }
  1210. return false;
  1211. ]]>
  1212. </body>
  1213. </method>
  1214. <method name="_setUpDescription">
  1215. <parameter name="aDescriptionElement"/>
  1216. <parameter name="aText"/>
  1217. <parameter name="aNoEmphasis"/>
  1218. <body>
  1219. <![CDATA[
  1220. // Get rid of all previous text
  1221. while (aDescriptionElement.hasChildNodes())
  1222. aDescriptionElement.removeChild(aDescriptionElement.firstChild);
  1223. // If aNoEmphasis is specified, don't add any emphasis
  1224. if (aNoEmphasis) {
  1225. aDescriptionElement.appendChild(document.createTextNode(aText));
  1226. return;
  1227. }
  1228. // Get the indices that separate match and non-match text
  1229. let search = this.getAttribute("text");
  1230. let tokens = this._getSearchTokens(search);
  1231. let indices = this._getBoundaryIndices(aText, tokens);
  1232. // If we're searching for something that needs alternate emphasis,
  1233. // we'll need to check the text that we match
  1234. let checkAlt = this._needsAlternateEmphasis(search);
  1235. let next;
  1236. let start = 0;
  1237. let len = indices.length;
  1238. // Even indexed boundaries are matches, so skip the 0th if it's empty
  1239. for (let i = indices[0] == 0 ? 1 : 0; i < len; i++) {
  1240. next = indices[i];
  1241. let text = aText.substr(start, next - start);
  1242. start = next;
  1243. if (i % 2 == 0) {
  1244. // Emphasize the text for even indices
  1245. let span = aDescriptionElement.appendChild(
  1246. document.createElementNS("http://www.w3.org/1999/xhtml", "span"));
  1247. span.className = checkAlt && this._needsAlternateEmphasis(text) ?
  1248. "ac-emphasize-alt" : "ac-emphasize-text";
  1249. span.textContent = text;
  1250. } else {
  1251. // Otherwise, it's plain text
  1252. aDescriptionElement.appendChild(document.createTextNode(text));
  1253. }
  1254. }
  1255. ]]>
  1256. </body>
  1257. </method>
  1258. <method name="_adjustAcItem">
  1259. <body>
  1260. <![CDATA[
  1261. var url = this.getAttribute("url");
  1262. var title = this.getAttribute("title");
  1263. var type = this.getAttribute("type");
  1264. this.removeAttribute("actiontype");
  1265. // The ellipses are hidden via their visibility so that they always
  1266. // take up space and don't pop in on top of text when shown. For
  1267. // keyword searches, however, the title ellipsis should not take up
  1268. // space when hidden. Setting the hidden property accomplishes that.
  1269. this._titleOverflowEllipsis.hidden = false;
  1270. // If the type includes an action, set up the item appropriately.
  1271. var types = type.split(/\s+/);
  1272. var actionIndex = types.indexOf("action");
  1273. if (actionIndex >= 0) {
  1274. let [,action, param] = url.match(/^moz-action:([^,]+),(.*)$/);
  1275. this.setAttribute("actiontype", action);
  1276. url = param;
  1277. let desc = "]]>&action.switchToTab.label;<![CDATA[";
  1278. this._setUpDescription(this._action, desc, true);
  1279. // Remove the "action" substring so that the correct style, if any,
  1280. // is applied below.
  1281. types.splice(actionIndex, 1);
  1282. type = types.join(" ");
  1283. }
  1284. // If we have a tag match, show the tags and icon
  1285. if (type == "tag") {
  1286. // Configure the extra box for tags display
  1287. this._extraBox.hidden = false;
  1288. this._extraBox.childNodes[0].hidden = false;
  1289. this._extraBox.childNodes[1].hidden = true;
  1290. this._extraBox.pack = "end";
  1291. this._titleBox.flex = 1;
  1292. // The title is separated from the tags by an endash
  1293. let tags;
  1294. [, title, tags] = title.match(/^(.+) \u2013 (.+)$/);
  1295. // Each tag is split by a comma in an undefined order, so sort it
  1296. let sortedTags = tags.split(",").sort().join(", ");
  1297. // Emphasize the matching text in the tags
  1298. this._setUpDescription(this._extra, sortedTags);
  1299. // Treat tagged matches as bookmarks for the star
  1300. type = "bookmark";
  1301. } else if (type == "keyword") {
  1302. // Configure the extra box for keyword display
  1303. this._extraBox.hidden = false;
  1304. this._extraBox.childNodes[0].hidden = true;
  1305. this._extraBox.childNodes[1].hidden = false;
  1306. this._extraBox.pack = "start";
  1307. this._titleBox.flex = 0;
  1308. // Hide the ellipsis so it doesn't take up space.
  1309. this._titleOverflowEllipsis.hidden = true;
  1310. // Put the parameters next to the title if we have any
  1311. let search = this.getAttribute("text");
  1312. let params = "";
  1313. let paramsIndex = search.indexOf(' ');
  1314. if (paramsIndex != -1)
  1315. params = search.substr(paramsIndex + 1);
  1316. // Emphasize the keyword parameters
  1317. this._setUpDescription(this._extra, params);
  1318. // Don't emphasize keyword searches in the title or url
  1319. this.setAttribute("text", "");
  1320. } else {
  1321. // Hide the title's extra box if we don't need extra stuff
  1322. this._extraBox.hidden = true;
  1323. this._titleBox.flex = 1;
  1324. }
  1325. // Give the image the icon style and a special one for the type
  1326. this._typeImage.className = "ac-type-icon" +
  1327. (type ? " ac-result-type-" + type : "");
  1328. // Show the url as the title if we don't have a title
  1329. if (title == "")
  1330. title = url;
  1331. // Emphasize the matching search terms for the description
  1332. this._setUpDescription(this._title, title);
  1333. this._setUpDescription(this._url, url);
  1334. // Set up overflow on a timeout because the contents of the box
  1335. // might not have a width yet even though we just changed them
  1336. setTimeout(this._setUpOverflow, 0, this._titleBox, this._titleOverflowEllipsis);
  1337. setTimeout(this._setUpOverflow, 0, this._urlBox, this._urlOverflowEllipsis);
  1338. ]]>
  1339. </body>
  1340. </method>
  1341. <method name="_setUpOverflow">
  1342. <parameter name="aParentBox"/>
  1343. <parameter name="aEllipsis"/>
  1344. <body>
  1345. <![CDATA[
  1346. // Hide the ellipsis incase there's just enough to not underflow
  1347. aEllipsis.style.visibility = "hidden";
  1348. // Start with the parent's width and subtract off its children
  1349. let tooltip = [];
  1350. let children = aParentBox.childNodes;
  1351. let widthDiff = aParentBox.boxObject.width;
  1352. for (let i = 0; i < children.length; i++) {
  1353. // Only consider a child if it actually takes up space
  1354. let childWidth = children[i].boxObject.width;
  1355. if (childWidth > 0) {
  1356. // Subtract a little less to account for subpixel rounding
  1357. widthDiff -= childWidth - .5;
  1358. // Add to the tooltip if it's not hidden and has text
  1359. let childText = children[i].textContent;
  1360. if (childText)
  1361. tooltip.push(childText);
  1362. }
  1363. }
  1364. // If the children take up more space than the parent.. overflow!
  1365. if (widthDiff < 0) {
  1366. // Re-show the ellipsis now that we know it's needed
  1367. aEllipsis.style.visibility = "visible";
  1368. // Separate text components with a ndash --
  1369. aParentBox.tooltipText = tooltip.join(" \u2013 ");
  1370. }
  1371. ]]>
  1372. </body>
  1373. </method>
  1374. <method name="_doUnderflow">
  1375. <parameter name="aName"/>
  1376. <body>
  1377. <![CDATA[
  1378. // Hide the ellipsis right when we know we're underflowing instead of
  1379. // waiting for the timeout to trigger the _setUpOverflow calculations
  1380. this[aName + "Box"].tooltipText = "";
  1381. this[aName + "OverflowEllipsis"].style.visibility = "hidden";
  1382. ]]>
  1383. </body>
  1384. </method>
  1385. <method name="_doOverflow">
  1386. <parameter name="aName"/>
  1387. <body>
  1388. <![CDATA[
  1389. this._setUpOverflow(this[aName + "Box"],
  1390. this[aName + "OverflowEllipsis"]);
  1391. ]]>
  1392. </body>
  1393. </method>
  1394. </implementation>
  1395. </binding>
  1396. <binding id="autocomplete-tree" extends="chrome://global/content/bindings/tree.xml#tree">
  1397. <content>
  1398. <children includes="treecols"/>
  1399. <xul:treerows class="autocomplete-treerows tree-rows" xbl:inherits="hidescrollbar" flex="1">
  1400. <children/>
  1401. </xul:treerows>
  1402. </content>
  1403. </binding>
  1404. <binding id="autocomplete-richlistbox" extends="chrome://global/content/bindings/richlistbox.xml#richlistbox">
  1405. <implementation>
  1406. <field name="mLastMoveTime">Date.now()</field>
  1407. </implementation>
  1408. <handlers>
  1409. <handler event="mouseup">
  1410. <![CDATA[
  1411. // don't call onPopupClick for the scrollbar buttons, thumb, slider, etc.
  1412. var item = event.originalTarget;
  1413. while (item && item.localName != "richlistitem")
  1414. item = item.parentNode;
  1415. if (!item)
  1416. return;
  1417. this.parentNode.onPopupClick(event);
  1418. ]]>
  1419. </handler>
  1420. <handler event="mousemove">
  1421. <![CDATA[
  1422. if (Date.now() - this.mLastMoveTime > 30) {
  1423. var item = event.target;
  1424. while (item && item.localName != "richlistitem")
  1425. item = item.parentNode;
  1426. if (!item)
  1427. return;
  1428. var rc = this.getIndexOfItem(item);
  1429. if (rc != this.selectedIndex)
  1430. this.selectedIndex = rc;
  1431. this.mLastMoveTime = Date.now();
  1432. }
  1433. ]]>
  1434. </handler>
  1435. </handlers>
  1436. </binding>
  1437. <binding id="autocomplete-treebody">
  1438. <implementation>
  1439. <field name="mLastMoveTime">Date.now()</field>
  1440. </implementation>
  1441. <handlers>
  1442. <handler event="mouseup" action="this.parentNode.parentNode.onPopupClick(event);"/>
  1443. <handler event="mousedown"><![CDATA[
  1444. var rc = this.parentNode.treeBoxObject.getRowAt(event.clientX, event.clientY);
  1445. if (rc != this.parentNode.currentIndex)
  1446. this.parentNode.view.selection.select(rc);
  1447. ]]></handler>
  1448. <handler event="mousemove"><![CDATA[
  1449. if (Date.now() - this.mLastMoveTime > 30) {
  1450. var rc = this.parentNode.treeBoxObject.getRowAt(event.clientX, event.clientY);
  1451. if (rc != this.parentNode.currentIndex)
  1452. this.parentNode.view.selection.select(rc);
  1453. this.mLastMoveTime = Date.now();
  1454. }
  1455. ]]></handler>
  1456. </handlers>
  1457. </binding>
  1458. <binding id="autocomplete-treerows">
  1459. <content>
  1460. <xul:hbox flex="1" class="tree-bodybox">
  1461. <children/>
  1462. </xul:hbox>
  1463. <xul:scrollbar xbl:inherits="collapsed=hidescrollbar" orient="vertical" class="tree-scrollbar"/>
  1464. </content>
  1465. </binding>
  1466. <binding id="history-dropmarker" extends="chrome://global/content/bindings/general.xml#dropmarker">
  1467. <implementation>
  1468. <method name="showPopup">
  1469. <body><![CDATA[
  1470. var textbox = document.getBindingParent(this);
  1471. textbox.showHistoryPopup();
  1472. ]]></body>
  1473. </method>
  1474. </implementation>
  1475. <handlers>
  1476. <handler event="mousedown" button="0"><![CDATA[
  1477. this.showPopup();
  1478. ]]></handler>
  1479. </handlers>
  1480. </binding>
  1481. </bindings>