DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (9e2e265534e7)

VCS Links

Line Code
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 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
<?xml version="1.0"?>

<!-- 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/. -->

<bindings id="glodaFacetBindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
          xmlns:html="http://www.w3.org/1999/xhtml"
          xmlns:xbl="http://www.mozilla.org/xbl"
          xmlns:svg="http://www.w3.org/2000/svg">

<!-- ===== Constraints ===== -->

<binding id="query-explanation">
  <content>
  </content>
  <implementation>
    <constructor><![CDATA[
      ChromeUtils.import("resource:///modules/mailServices.js");
    ]]></constructor>
    <!-- Indicate that we are based on a fulltext search-->
    <method name="setFulltext">
      <parameter name="aMsgSearcher" />
      <body><![CDATA[
        while (this.hasChildNodes())
          this.lastChild.remove();

        let dis = this;
        let spanify = function(aText, aClass) {
          let span = document.createElement("span");
          span.setAttribute("class", aClass);
          span.textContent = aText;
          dis.appendChild(span);
          return span;
        }

        let searchLabel = glodaFacetStrings.get(
          "glodaFacetView.search.label");
        spanify(searchLabel, "explanation-fulltext-label");

        let criteriaText = glodaFacetStrings.get(
          "glodaFacetView.constraints.query.fulltext." +
          (aMsgSearcher.andTerms ? "and" : "or") + "JoinWord");
        for (let [iTerm, term] of aMsgSearcher.fulltextTerms.entries()) {
          if (iTerm)
            spanify(criteriaText, "explanation-fulltext-criteria");
          spanify(term, "explanation-fulltext-term");
        }
      ]]></body>
    </method>
    <method name="setQuery">
      <parameter name="aMsgQuery" />
      <body><![CDATA[
      try {
        while (this.hasChildNodes())
          this.lastChild.remove();

        let dis = this;
        let spanify = function(aText, aClass) {
          let span = document.createElement("span");
          span.setAttribute("class", aClass);
          span.textContent = aText;
          dis.appendChild(span);
          return span;
        }

        let label = glodaFacetStrings.get(
          "glodaFacetView.search.label");
        spanify(label, "explanation-query-label");

        let constraintStrings = [];
        for (let constraint of aMsgQuery._constraints) {
          if (constraint[0] != 1) return; // no idea what this is about
          if (constraint[1].attributeName == 'involves') {
            let involvesLabel = glodaFacetStrings.get(
              "glodaFacetView.constraints.query.involves.label");
            involvesLabel = involvesLabel.replace("#1", constraint[2].value)
            spanify(involvesLabel, "explanation-query-involves");
          } else if (constraint[1].attributeName == 'tag') {
            let tagLabel = glodaFacetStrings.get(
              "glodaFacetView.constraints.query.tagged.label");
            let tag = constraint[2];
            let tagNode = document.createElement("span");
            let colorClass = "blc-" + MailServices.tags.getColorForKey(tag.key).substr(1);
            tagNode.setAttribute("class", "message-tag tag " + colorClass);
            tagNode.textContent = tag.tag;
            spanify(tagLabel, "explanation-query-tagged");
            this.appendChild(tagNode);
          }
        }
        label = label + constraintStrings.join(', '); // XXX l10n?
      } catch (e) {
        logException(e);
      }
      ]]></body>
    </method>
  </implementation>
</binding>

<!-- ===== Facets ===== -->

<binding id="facets">
  <content>
  </content>
  <implementation>
    <method name="clearFacets">
      <body><![CDATA[
        while (this.hasChildNodes())
          this.lastChild.remove();
      ]]></body>
    </method>
    <method name="addFacet">
      <parameter name="aType" />
      <parameter name="aAttrDef" />
      <parameter name="aArgs" />
      <body><![CDATA[
        let facets = this;

        let facet = document.createElement("div");
        facet.attrDef = aAttrDef;
        facet.nounDef = aAttrDef.objectNounDef;
        for (let key in aArgs) {
          facet[key] = aArgs[key];
        }
        facet.setAttribute("class", "facetious");
        facet.setAttribute("type", aType);
        facet.setAttribute("name", aAttrDef.attributeName);
        facets.appendChild(facet);

        return facet;
      ]]></body>
    </method>
  </implementation>
</binding>

<binding id="facet-base">
  <implementation>
    <method name="brushItems">
      <body><![CDATA[
      ]]></body>
    </method>
    <method name="clearBrushedItems">
      <body><![CDATA[
      ]]></body>
    </method>
  </implementation>
</binding>

<!--
  - A boolean facet; we presume orderedGroups contains at most two entries, and
  -  that their group values consist of true or false.
  -
  - The implication of this UI is that you only want to filter to true values
  -  and would never want to filter out the false values.  Consistent with this
  -  assumption we disable the UI if there are no true values.
  -
  - This depressingly
  -->
<binding id="facet-boolean"
         extends="chrome://messenger/content/glodaFacetBindings.xml#facet-base">
  <content>
    <html:span anonid="bubble" class="facet-checkbox-bubble">
      <html:input anonid="checkbox" type="checkbox" />
      <html:span anonid="label" class="facet-checkbox-label" />
      <html:span anonid="count" class="facet-checkbox-count" />
    </html:span>
  </content>
  <implementation>
    <constructor><![CDATA[
      this.bubble = document.getAnonymousElementByAttribute(this, "anonid",
                                                            "bubble");
      this.checkbox = document.getAnonymousElementByAttribute(this, "anonid",
                                                              "checkbox");
      this.labelNode = document.getAnonymousElementByAttribute(this, "anonid",
                                                               "label");
      this.countNode = document.getAnonymousElementByAttribute(this, "anonid",
                                                               "count");

      let dis = this;
      this.bubble.addEventListener("click", function (event) {
        return dis.bubbleClicked(event);
      }, true);

      this.extraSetup();

      if ("faceter" in this)
        this.build(true);
    ]]></constructor>
    <field name="canUpdate" readonly="true">true</field>
    <property name="disabled">
      <getter><![CDATA[
        return this.getAttribute("disabled") == "true";
      ]]></getter>
      <setter><![CDATA[
        if (val) {
          this.setAttribute("disabled", "true");
          this.checkbox.setAttribute("disabled", true);
        }
        else {
          this.removeAttribute("disabled");
          this.checkbox.removeAttribute("disabled");
        }
      ]]></setter>
    </property>
    <property name="checked">
      <getter><![CDATA[
        return this.getAttribute("checked") == "true";
      ]]></getter>
      <setter><![CDATA[
        if (this.checked == val)
          return;
        this.checkbox.checked = val;
        if (val) {
          // the XBL inherits magic appears to fail if we explicitly check the
          //  box itself rather than via our click handler, presumably because
          //  we unshadow something.  So manually apply changes ourselves.
          this.setAttribute("checked", "true");
          this.checkbox.setAttribute("checked", "true");
          if (!this.disabled)
            FacetContext.addFacetConstraint(this.faceter, true,
                                            this.trueGroups);
        }
        else {
          this.removeAttribute("checked");
          this.checkbox.removeAttribute("checked");
          if (!this.disabled)
            FacetContext.removeFacetConstraint(this.faceter, true,
                                               this.trueGroups);
        }
        this.checkStateChanged();
      ]]></setter>
    </property>
    <method name="extraSetup">
      <body><![CDATA[
      ]]></body>
    </method>
    <method name="checkStateChanged">
      <body><![CDATA[
      ]]></body>
    </method>
    <method name="build">
      <parameter name="aFirstTime" />
      <body><![CDATA[
        if (aFirstTime) {
          this.labelNode.textContent = this.facetDef.strings.facetNameLabel;
          this.checkbox.setAttribute("aria-label",
                                     this.facetDef.strings.facetNameLabel);
          this.trueValues = [];
        }

        // If we do not currently have a constraint applied and there is only
        //  one (or no) group, then: disable us, but reflect the underlying
        //  state of the data (checked or non-checked)
        if (!this.faceter.constraint && (this.orderedGroups.length <= 1)){
          this.disabled = true;
          let count = 0;
          if (this.orderedGroups.length) {
            // true case?
            if (this.orderedGroups[0][0]) {
              count = this.orderedGroups[0][1].length;
              this.checked = true;
            }
            else {
              this.checked = false;
            }
          }
          this.countNode.textContent = count.toLocaleString();
          return;
        }
        // if we were disabled checked before, clear ourselves out
        if (this.disabled && this.checked)
          this.checked = false;
        this.disabled = false;

        // if we are here, we have our 2 groups, find true...
        // (note: it is possible to get jerked around by null values
        //  currently, so leave a reasonable failure case)
        this.trueValues = [];
        this.trueGroups = [true];
        for (let groupPair of this.orderedGroups) {
          if (groupPair[0] == true)
            this.trueValues = groupPair[1];
        }

        this.countNode.textContent = this.trueValues.length.toLocaleString();
      ]]></body>
    </method>
    <method name="bubbleClicked">
      <parameter name="event" />
      <body><![CDATA[
      if (!this.disabled)
        this.checked = !this.checked;
      event.stopPropagation();
      ]]></body>
    </method>
  </implementation>
  <handlers>
    <handler event="mouseover"><![CDATA[
      FacetContext.hoverFacet(this.faceter, this.faceter.attrDef,
                              true, this.trueValues);
    ]]></handler>
    <handler event="mouseout"><![CDATA[
      FacetContext.unhoverFacet(this.faceter, this.faceter.attrDef,
                                true, this.trueValues);
    ]]></handler>
  </handlers>
</binding>

<!--
  - A check-box with filter-box front-end to a standard discrete faceter.  If
  -  there are no non-null values, we disable the UI.  If there are non-null
  -  values the checkbox is enabled and the filter is hidden.  Once you check
  -  the box we apply the facet and show the filtering mechanism.
  -->
<binding id="facet-boolean-filtered"
         extends="chrome://messenger/content/glodaFacetBindings.xml#facet-boolean">
  <content>
    <html:span anonid="bubble" class="facet-checkbox-bubble">
      <html:input anonid="checkbox" type="checkbox"
                  xbl:inherits="checked,disabled"/>
      <html:span anonid="label" class="facet-checkbox-label" />
      <html:span anonid="count" class="facet-checkbox-count" />
    </html:span>
    <html:select anonid="filter" class="facet-filter-list" />
  </content>
  <implementation>
    <method name="extraSetup">
      <body><![CDATA[
        this.filterNode = document.getAnonymousElementByAttribute(
                            this, "anonid", "filter");
        this.groupDisplayProperty = this.getAttribute("groupDisplayProperty");

        let dis = this;
        this.filterNode.addEventListener("change", function(event) {
          return dis.filterChanged(event);
        });

        this.selectedValue = "all";
      ]]></body>
    </method>
    <method name="build">
      <parameter name="aFirstTime" />
      <body><![CDATA[
        if (aFirstTime) {
          this.labelNode.textContent = this.facetDef.strings.facetNameLabel;
          this.checkbox.setAttribute("aria-label",
                                     this.facetDef.strings.facetNameLabel);
          this.trueValues = [];
        }

        // Only update count if anything other than "all" is selected.
        //  Otherwise we lose the set of attachment types in our select box,
        //  and that makes us sad.  We do want to update on "all" though
        //  because other facets may further reduce the number of attachments
        //  we see.  (Or if this is not just being used for attachments, it
        //  still holds.)
        if (this.selectedValue != "all") {
          let count = 0;
          for (let groupPair of this.orderedGroups) {
            if (groupPair[0] != null)
              count += groupPair[1].length;
          }
          this.countNode.textContent = count.toLocaleString();
          return;
        }

        while (this.filterNode.hasChildNodes())
          this.filterNode.lastChild.remove();
        let allNode = document.createElement("option");
        allNode.textContent =
          glodaFacetStrings.get("glodaFacetView.facets.filter." +
                                this.attrDef.attributeName + ".allLabel");
        allNode.setAttribute("value", "all");
        if (this.selectedValue == "all")
          allNode.setAttribute("selected", "selected");
        this.filterNode.appendChild(allNode);

        // if we are here, we have our 2 groups, find true...
        // (note: it is possible to get jerked around by null values
        //  currently, so leave a reasonable failure case)
        // empty true groups is for the checkbox
        this.trueGroups = [];
        // the real true groups is the actual true values for our explicit
        //  filtering
        this.realTrueGroups = [];
        this.trueValues = [];
        this.falseValues = [];
        let selectNodes = [];
        for (let groupPair of this.orderedGroups) {
          if (groupPair[0] == null)
            this.falseValues.push.apply(this.falseValues, groupPair[1]);
          else {
            this.trueValues.push.apply(this.trueValues, groupPair[1]);

            let groupValue = groupPair[0];
            let selNode = document.createElement("option");
            selNode.textContent = groupValue[this.groupDisplayProperty];
            selNode.setAttribute("value", this.realTrueGroups.length);
            if (this.selectedValue == groupValue.category)
              selNode.setAttribute("selected", "selected");
            selectNodes.push(selNode);

            this.realTrueGroups.push(groupValue);
          }
        }
        selectNodes.sort(function(a, b) {
          return a.textContent.localeCompare(b.textContent);
        });
        selectNodes.forEach(function(selNode) { this.filterNode.appendChild(selNode); }, this);

        this.disabled = !this.trueValues.length;

        this.countNode.textContent = this.trueValues.length.toLocaleString();
      ]]></body>
    </method>
    <method name="checkStateChanged">
      <body><![CDATA[
        // if they un-check us, revert our value to all.
        if (!this.checked)
          this.selectedValue = "all";
      ]]></body>
    </method>
    <method name="filterChanged">
      <parameter name="event" />
      <body><![CDATA[
        if (!this.checked)
          return;
        if (this.filterNode.value == "all") {
          this.selectedValue = "all";
          FacetContext.addFacetConstraint(this.faceter, true,
                                          this.trueGroups, false, true);
        }
        else {
          let groupValue = this.realTrueGroups[parseInt(this.filterNode.value)];
          this.selectedValue = groupValue.category;
          FacetContext.addFacetConstraint(this.faceter, true,
                                          [groupValue], false, true);
        }
      ]]></body>
    </method>
  </implementation>
</binding>

<binding id="popup-menu">
  <content>
    <html:div anonid="parent" class="parent"
              tabindex="0"
      ><html:div anonid="include-item" class="popup-menuitem top"
        tabindex="0"
        onmouseover="this.focus()"
        onkeypress="if (event.keyCode == event.DOM_VK_RETURN) this.parentNode.parentNode.doInclude()"
        onmouseup="this.parentNode.parentNode.doInclude()"></html:div
      ><html:div anonid="exclude-item" class="popup-menuitem bottom"
        tabindex="0"
        onmouseover="this.focus()"
        onkeypress="if (event.keyCode == event.DOM_VK_RETURN) this.parentNode.parentNode.doExclude()"
        onmouseup="this.parentNode.parentNode.doExclude()"></html:div
      ><html:div anonid="undo-item" class="popup-menuitem undo"
        tabindex="0"
        onmouseover="this.focus()"
        onkeypress="if (event.keyCode == event.DOM_VK_RETURN) this.parentNode.parentNode.doUndo()"
        onmouseup="this.parentNode.parentNode.doUndo()"></html:div
    ></html:div>
  </content>
  <handlers>
    <handler event="keypress" keycode="VK_ESCAPE"
             action="this.hide();"/>
    <handler event="keypress" keycode="VK_DOWN"
             action="this.moveFocus(event, 1);"/>
    <handler event="keypress" keycode="VK_TAB"
             action="this.moveFocus(event, 1);"/>
    <handler event="keypress" keycode="VK_TAB" modifiers="shift"
             action="this.moveFocus(event, -1);"/>
    <handler event="keypress" keycode="VK_UP"
             action="this.moveFocus(event, -1);"/>
  </handlers>
  <implementation>
    <constructor><![CDATA[
      this.includeNode = document.getAnonymousElementByAttribute(this, "anonid",
                                                        "include-item");
      this.excludeNode = document.getAnonymousElementByAttribute(this, "anonid",
                                                     "exclude-item");
      this.undoNode = document.getAnonymousElementByAttribute(this, "anonid",
                                                     "undo-item");
    ]]></constructor>
    <method name="_getLabel">
      <parameter name="facetDef"/>
      <parameter name="facetValue"/>
      <parameter name="groupValue"/>
      <parameter name="stringName"/>
      <body><![CDATA[
        let label, labelFormat;
        if (stringName in facetDef.strings)
          labelFormat = facetDef.strings[stringName];
        else
          labelFormat = glodaFacetStrings.get(
              "glodaFacetView.facets."+stringName+".fallbackLabel");
        if (!labelFormat.includes("#1"))
          return labelFormat;
        else
          return labelFormat.replace("#1", facetValue);
      ]]></body>
    </method>
    <method name="build">
      <parameter name="facetDef"/>
      <parameter name="facetValue"/>
      <parameter name="groupValue"/>
      <body><![CDATA[
      try {
        if (groupValue)
          this.includeNode.textContent = this._getLabel(facetDef, facetValue,
                                                        groupValue, "mustMatchLabel");
        else
          this.includeNode.textContent = this._getLabel(facetDef, facetValue,
                                                        groupValue, "mustMatchNoneLabel");
        if (groupValue)
          this.excludeNode.textContent = this._getLabel(facetDef, facetValue,
                                                        groupValue, "cantMatchLabel");
        else
          this.excludeNode.textContent = this._getLabel(facetDef, facetValue,
                                                        groupValue, "mustMatchSomeLabel");
        if (groupValue)
          this.undoNode.textContent = this._getLabel(facetDef, facetValue,
                                                     groupValue, "mayMatchLabel");
        else
          this.undoNode.textContent = this._getLabel(facetDef, facetValue,
                                                     groupValue, "mayMatchAnyLabel");
      } catch (e) {
        logException(e);
      }
      ]]>
      </body>
    </method>
    <method name="moveFocus">
      <parameter name="event"/>
      <parameter name="delta"/>
      <body><![CDATA[
      try {
        let parent = document.getAnonymousElementByAttribute(this,
          "anonid", "parent");
        // We probably want something quite generic in the long term, but that
        // is way too much for now (needs to skip over invisible items, etc)
        let focused = document.activeElement;
        if (focused == this.includeNode)
          this.excludeNode.focus();
        else if (focused == this.excludeNode)
          this.includeNode.focus();
        event.preventDefault();
        event.stopPropagation();
      } catch (e) {
        logException(e);
      }
      ]]>
      </body>
    </method>
    <method name="selectItem">
      <parameter name="event"/>
      <body><![CDATA[
      try {
        let focused = document.activeElement;
        if (focused == this.includeNode)
          this.doInclude();
        else if (focused == this.excludeNode)
          this.doExclude();
        else
          this.doUndo();
      } catch (e) {
        logException(e);
      }
      ]]>
      </body>
    </method>
    <method name="show">
      <parameter name="event"/>
      <parameter name="facetNode"/>
      <parameter name="barNode"/>
      <body><![CDATA[
        try {
          this.node = barNode;
          this.facetNode = facetNode;
          let facetDef = facetNode.facetDef;
          let groupValue = barNode.groupValue;
          let variety = barNode.getAttribute("variety");
          let label = barNode.querySelector(".bar-link").textContent;
          this.build(facetDef, label, groupValue);
          this.node.setAttribute("selected", "true");
          var rtl = window.getComputedStyle(this, null).direction == "rtl";
          /* We show different menus if we're on an "unselected" facet value,
             or if we're on a preselected facet value, whether included or
             excluded. The variety attribute handles that through CSS */
          this.setAttribute("variety", variety);
          let rect = barNode.getBoundingClientRect();
          let X, Y;
          if (event.type == "click") {
            // center the menu on the mouse click
            if (rtl)
              X = event.pageX + 10;
            else
              X = event.pageX - 10;
            Y = Math.max(20, event.pageY - 15);
          } else {
            if (rtl)
              X = rect.left + rect.width / 2 + 20;
            else
              X = rect.left + rect.width / 2 - 20;
            Y = rect.top - 10;
          }
          if (rtl)
            this.style.left = (X - this.getBoundingClientRect().width) + "px";
          else
            this.style.left = X + "px";
          this.style.top = Y + "px";

          if (variety == "remainder")
            // include
            document.getAnonymousElementByAttribute(this,
              "anonid", "parent").firstChild.focus();
          else
            // undo
            document.getAnonymousElementByAttribute(this,
              "anonid", "parent").lastChild.focus();
        } catch (e) {
          logException(e);
        }
      ]]>
      </body>
    </method>
    <method name="hide">
      <body><![CDATA[
      try {
        this.setAttribute("variety", "invisible");
        if (this.node) {
          this.node.removeAttribute("selected");
          this.node.focus();
        }
      } catch (e) {
        logException(e);
      }
      ]]>
      </body>
    </method>
    <method name="doInclude">
      <body><![CDATA[
      try {
        this.facetNode.includeFacet(this.node);
        this.hide();
      } catch (e) {
        logException(e);
      }
      ]]>
      </body>
    </method>
    <method name="doExclude">
      <body><![CDATA[
        this.facetNode.excludeFacet(this.node);
        this.hide();
      ]]>
      </body>
    </method>
    <method name="doUndo">
      <body><![CDATA[
        this.facetNode.undoFacet(this.node);
        this.hide();
      ]]>
      </body>
    </method>
  </implementation>
</binding>

<binding id="facet-discrete">
  <content>
  <!-- without this explicit div here, the sibling selectors used to span
       included-label/included and excluded-label/excluded fail to apply.
       so. magic! (this is why our binding node's class is facetious. -->
  <html:div class="facet">
    <html:h2 anonid="name"></html:h2>
    <html:div anonid="content-box" class="facet-content">
      <html:h3 anonid="included-label" class="facet-included-header"></html:h3>
      <html:ul anonid="included" class="facet-included barry"></html:ul>
      <html:h3 anonid="remainder-label" class="facet-remaindered-header"></html:h3>
      <html:ul anonid="remainder" class="facet-remaindered barry"></html:ul>
      <html:h3 anonid="excluded-label" class="facet-excluded-header"></html:h3>
      <html:ul anonid="excluded" class="facet-excluded barry"></html:ul>
      <html:div anonid="more" class="facet-more" needed="false" tabindex="0"
                role="button"/>
    </html:div>
  </html:div>
  </content>
  <implementation>
    <constructor><![CDATA[
      if ("faceter" in this)
        this.build(true);
    ]]></constructor>
    <field name="canUpdate" readonly="true">false</field>
    <method name="build">
      <parameter name="aFirstTime" />
      <body><![CDATA[
        // -- Header Building
        let nameNode = document.getAnonymousElementByAttribute(this, "anonid",
                                                               "name");
        nameNode.textContent = this.facetDef.strings.facetNameLabel;

        // - include
        // setup the include label
        this.includeLabel = document.getAnonymousElementByAttribute(
                           this, "anonid", "included-label");
        if ("includeLabel" in this.facetDef.strings)
          this.includeLabel.textContent = this.facetDef.strings.includeLabel;
        else
          this.includeLabel.textContent =
            glodaFacetStrings.get(
              "glodaFacetView.facets.included.fallbackLabel");
        this.includeLabel.setAttribute("state", "empty");

        // include list ref
        this.includeList = document.getAnonymousElementByAttribute(
                                      this, "anonid", "included");

        // - exclude
        // setup the exclude label
        this.excludeLabel = document.getAnonymousElementByAttribute(
                                       this, "anonid", "excluded-label");
        if ("excludeLabel" in this.facetDef.strings)
          this.excludeLabel.textContent = this.facetDef.strings.excludeLabel;
        else
          this.excludeLabel.textContent =
            glodaFacetStrings.get(
              "glodaFacetView.facets.excluded.fallbackLabel");
        this.excludeLabel.setAttribute("state", "empty");

        // exclude list ref
        this.excludeList = document.getAnonymousElementByAttribute(
                                      this, "anonid", "excluded");

        // - remainder
        // setup the remainder label
        this.remainderLabel = document.getAnonymousElementByAttribute(
                                this, "anonid", "remainder-label");
        if ("remainderLabel" in this.facetDef.strings)
          this.remainderLabel.textContent = this.facetDef.strings.remainderLabel;
        else
          this.remainderLabel.textContent =
            glodaFacetStrings.get(
              "glodaFacetView.facets.remainder.fallbackLabel");
        // remainder list ref
        this.remainderList = document.getAnonymousElementByAttribute(
                                        this, "anonid", "remainder");

        // - more button
        this.moreButton = document.getAnonymousElementByAttribute(
                            this, "anonid", "more");

        // we need to know who the content box is for flying fun
        this.contentBox = document.getAnonymousElementByAttribute(
                            this, "anonid", "content-box");


        // -- House-cleaning
        // -- All/Top mode decision
        this.modes = ["all"];
        if (this.maxDisplayRows >= this.orderedGroups.length) {
          this.mode = "all";
        }
        else {
          // top mode must be used
          this.modes.push("top");
          this.mode = "top";
          this.topGroups = FacetUtils.makeTopGroups(this.attrDef,
                                                    this.orderedGroups,
                                                    this.maxDisplayRows);
          // setup the more button string
          let groupCount = this.orderedGroups.length;
          this.moreButton.textContent =
            PluralForm.get(groupCount,
                           glodaFacetStrings.get(
                             "glodaFacetView.facets.mode.top.listAllLabel"))
                      .replace("#1", groupCount);
        }

        // -- Row Building
        this.buildRows();

      ]]></body>
    </method>
    <method name="changeMode">
      <parameter name="aNewMode" />
      <body><![CDATA[
        this.mode = aNewMode;
        this.setAttribute("mode", aNewMode);
        this.buildRows();
      ]]></body>
    </method>
    <method name="buildRows">
      <body><![CDATA[
        let nounDef = this.nounDef;
        let useGroups = (this.mode == "all") ? this.orderedGroups
                                             : this.topGroups;

        // should we just rely on automatic string coercion?
        this.moreButton.setAttribute("needed",
                                     (this.mode == "top") ? "true" : "false");

        let constraint = this.faceter.constraint;

        // -- empty all of our display buckets...
        let remainderList = this.remainderList;
        while (remainderList.hasChildNodes())
          remainderList.lastChild.remove();
        let includeList = this.includeList, excludeList = this.excludeList;
        while (includeList.hasChildNodes())
          includeList.lastChild.remove();
        while (excludeList.hasChildNodes())
          excludeList.lastChild.remove();

        // -- first pass, check for ambiguous labels
        // It's possible that multiple groups are identified by the same short
        //  string, in which case we want to use the longer string to
        //  disambiguate.  For example, un-merged contacts can result in
        //  multiple identities having contacts with the same name.  In that
        //  case we want to display both the contact name and the identity
        //  name.
        // This is generically addressed by using the userVisibleString function
        //  defined on the noun type if it is defined.  It takes an argument
        //  indicating whether it should be a short string or a long string.
        // Our algorithm is somewhat dumb.  We get the short strings, put them
        //  in a dictionary that maps to whether they are ambiguous or not.  We
        //  do not attempt to map based on their id, so then when it comes time
        //  to actually build the labels, we must build the short string and
        //  then re-call for the long name.  We could be smarter by building
        //  a list of the input values that resulted in the output string and
        //  then using that to back-update the id map, but it's more compelx and
        //  the performance difference is unlikely to be meaningful.
        let ambiguousKeyValues;
        if ("userVisibleString" in nounDef) {
          ambiguousKeyValues = {};
          for (let groupPair of useGroups) {
            let [groupValue, groupItems] = groupPair;

            // skip null values, they are handled by the none special-case
            if (groupValue == null)
              continue;

            let groupStr = nounDef.userVisibleString(groupValue, false);
            // We use hasOwnProperty because it is possible that groupStr could
            //  be the same as the name of one of the attributes on
            //  Object.prototype.
            if (ambiguousKeyValues.hasOwnProperty(groupStr))
              ambiguousKeyValues[groupStr] = true;
            else
              ambiguousKeyValues[groupStr] = false;
          }
        }

        // -- create the items, assigning them to the right list based on
        //  existing constraint values
        for (let groupPair of useGroups) {
          let [groupValue, groupItems] = groupPair;
          let li = document.createElement("li");
          li.setAttribute("class", "bar");
          li.setAttribute("tabindex", "0");
          li.setAttribute("role", "link");
          li.setAttribute("aria-haspopup", "true");
          li.groupValue = groupValue;
          li.setAttribute("groupValue", groupValue);
          li.groupItems = groupItems;

          let countSpan = document.createElement("span");
          countSpan.setAttribute("class", "bar-count");
          countSpan.textContent = groupItems.length.toLocaleString();
          li.appendChild(countSpan);

          let label = document.createElement("span");
          label.setAttribute("class", "bar-link");

          // Set a tooltip with this label's textContent on the binding when
          // the mouse enters the label. This lets us show the full contents of
          // the label even if we overflowed. We set it on the binding because
          // anonymous XBL nodes can't be the source of HTML tooltips. Shadow
          // DOMs are screwy! (We also clear out the tooltip when the mouse
          // leaves the label, just to be polite.)
          let rootBinding = this;
          label.addEventListener("mouseenter", function() {
            rootBinding.setAttribute("title", this.textContent);
          });
          label.addEventListener("mouseleave", function() {
            rootBinding.setAttribute("title", "");
          });

          // The null value is a special indicator for 'none'
          if (groupValue == null) {
            label.textContent =
              glodaFacetStrings.get("glodaFacetView.facets.noneLabel");
          }
          // Otherwise stringify the group object
          else {
            let labelStr;
            if (ambiguousKeyValues) {
              labelStr = nounDef.userVisibleString(groupValue, false);
              if (ambiguousKeyValues[labelStr])
                labelStr = nounDef.userVisibleString(groupValue, true);
            }
            else if ("labelFunc" in this.facetDef) {
              labelStr = this.facetDef.labelFunc(groupValue);
            }
            else {
              labelStr = groupValue.toLocaleString().substring(0, 80);
            }
            label.textContent = labelStr;
          }
          li.appendChild(label);

          // root it under the appropriate list
          if (constraint) {
            if (constraint.isIncludedGroup(groupValue)) {
              li.setAttribute("variety", "include");
              includeList.appendChild(li);
            }
            else if (constraint.isExcludedGroup(groupValue)) {
              li.setAttribute("variety", "exclude");
              excludeList.appendChild(li);
            }
            else {
              li.setAttribute("variety", "remainder");
              remainderList.appendChild(li);
            }
          }
          else {
            li.setAttribute("variety", "remainder");
            remainderList.appendChild(li);
          }
        }

        this.updateHeaderStates();
      ]]></body>
    </method>
    <!--
      - Mark the include/exclude headers as "some" if there is anything in their
      -  lists, mark the remainder header as "needed" if either of include /
      -  exclude exist so we need that label.
      -->
    <method name="updateHeaderStates">
      <parameter name="aItems" />
      <body><![CDATA[
        this.includeLabel.setAttribute("state",
          this.includeList.childElementCount ? "some" : "empty");
        this.excludeLabel.setAttribute("state",
          this.excludeList.childElementCount ? "some" : "empty");
        this.remainderLabel.setAttribute("needed",
          ((this.includeList.childElementCount ||
            this.excludeList.childElementCount) &&
           this.remainderList.childElementCount) ? "true" : "false");

        // nuke the style attributes.
        this.includeLabel.removeAttribute("style");
        this.excludeLabel.removeAttribute("style");
        this.remainderLabel.removeAttribute("style");
      ]]></body>
    </method>
    <method name="brushItems">
      <parameter name="aItems" />
      <body><![CDATA[
      ]]></body>
    </method>
    <method name="clearBrushedItems">
      <body><![CDATA[
      ]]></body>
    </method>
    <method name="afterListVisible">
      <parameter name="aVariety" />
      <parameter name="aCallback" />
      <body><![CDATA[
        let labelNode = this[aVariety + "Label"];
        let listNode = this[aVariety + "List"];

        // if there are already things displayed, no need
        if (listNode.childElementCount) {
          aCallback();
          return;
        }

        let remListVisible =
          this.remainderLabel.getAttribute("needed") == "true";
        let remListShouldBeVisible =
          this.remainderList.childElementCount > 1;

        labelNode.setAttribute("state", "some");

        // We used to use jQuery here, but it turns out that it has issues with
        // XML namespaces and stringification, so we now resort to poking the
        // nodes directly.
        let showNodes = [labelNode];
        if (remListVisible != remListShouldBeVisible)
          showNodes = [labelNode, this.remainderLabel];

        showNodes.forEach(node => node.style.display = "block");

        aCallback();
      ]]></body>
    </method>
    <method name="_flyBarAway">
      <parameter name="aBarNode" />
      <parameter name="aVariety" />
      <parameter name="aCallback" />
      <body><![CDATA[
        function getRect(aElement) {
          let box = aElement.getBoundingClientRect();
          let documentElement = aElement.ownerDocument.documentElement;
          return {
            top: box.top + window.pageYOffset - documentElement.clientTop,
            left: box.left + window.pageXOffset - documentElement.clientLeft,
            width: box.width,
            height: box.height
          };
        };
        // figure out our origin location prior to adding the target or it
        //  will shift us down.
        let origin = getRect(aBarNode);

        // clone the node into its target location
        let targetNode = aBarNode.cloneNode(true);
        targetNode.groupValue = aBarNode.groupValue;
        targetNode.groupItems = aBarNode.groupItems;
        targetNode.setAttribute("variety", aVariety);

        let targetParent = this[aVariety + "List"];
        targetParent.appendChild(targetNode);

        // create a flying clone
        let flyingNode = aBarNode.cloneNode(true);

        let dest = getRect(targetNode);

        // if the flying box wants to go higher than the content box goes, just
        //  send it to the top of the content box instead.
        let contentRect = getRect(this.contentBox);
        if (dest.top < contentRect.top)
          dest.top = contentRect.top;
        // likewise if it wants to go further south than the content box, stop
        //  that
        if (dest.top > (contentRect.top + contentRect.height))
          dest.top = contentRect.top + contentRect.height - dest.height;

        flyingNode.style.position = "absolute";
        flyingNode.style.width = origin.width + "px";
        flyingNode.style.height = origin.height + "px";
        flyingNode.style.top = origin.top + "px";
        flyingNode.style.left = origin.left + "px";
        flyingNode.style.zIndex = 1000;

        flyingNode.style.transitionDuration = (Math.abs(dest.top - origin.top) * 2) + "ms";
        flyingNode.style.transitionProperty = "top, left";

        flyingNode.addEventListener("transitionend", function() {
          aBarNode.parentNode.removeChild(aBarNode);
          targetNode.style.display = "block";
          flyingNode.parentNode.removeChild(flyingNode);

          if (aCallback)
            setTimeout(aCallback, 50);
        });

        document.body.appendChild(flyingNode);

        // animate the flying clone... flying!
        window.requestAnimationFrame(function() {
          flyingNode.style.top = dest.top + "px";
          flyingNode.style.left = dest.left + "px";
        });

        // hide the target (cloned) node
        targetNode.style.display = "none";

        // hide the original node and remove its JS properties
        aBarNode.style.visibility = "hidden";
        delete aBarNode.groupValue;
        delete aBarNode.groupItems;
      ]]></body>
    </method>
    <method name="barClicked">
      <parameter name="aBarNode" />
      <parameter name="aVariety" />
      <body><![CDATA[
        let groupValue = aBarNode.groupValue;
        let groupItems = aBarNode.groupItems;
        let dis = this;
        // These determine what goAnimate actually does.
        // flyAway allows us to cancel flying in the case the constraint is
        //  being fully dropped and so the facet is just going to get rebuilt
        let flyAway = true;

        function goAnimate() {
          setTimeout(function () {
            if (flyAway) {
              dis.afterListVisible(aVariety, function() {
                dis._flyBarAway(aBarNode, aVariety, function() {
                  dis.updateHeaderStates();
                });
              });
            }
          }, 0);
        };

        // Immediately apply the facet change, triggering the animation after
        //  the faceting completes.
        if (aVariety == "remainder") {
          let currentVariety = aBarNode.getAttribute("variety");
          let constraintGone = FacetContext.removeFacetConstraint(
                                 this.faceter,
                                 currentVariety == "include",
                                 [groupValue],
                                 goAnimate);
          // we will automatically rebuild if the constraint is gone, so
          //  just make the animation a no-op.
          if (constraintGone)
            flyAway = false;
        }
        // include/exclude
        else {
          let revalidate = FacetContext.addFacetConstraint(
                             this.faceter,
                             aVariety == "include",
                             [groupValue],
                             false, false, goAnimate);
          // revalidate means we need to blow away the other dudes, in which
          //  case it makes the most sense to just trigger a rebuild of ourself
          if (revalidate) {
            flyAway = false;
            this.build(false);
          }
        }
      ]]></body>
    </method>
    <method name="barHovered">
      <parameter name="aBarNode" />
      <parameter name="aInclude" />
      <body><![CDATA[
        let groupValue = aBarNode.groupValue;
        let groupItems = aBarNode.groupItems;

        FacetContext.hoverFacet(this.faceter, this.attrDef, groupValue, groupItems);
      ]]></body>
    </method>
    <!-- HoverGone! HoverGone!
         We know it's gone, but where has it gone? -->
    <method name="barHoverGone">
      <parameter name="aBarNode" />
      <parameter name="aInclude" />
      <body><![CDATA[
        let groupValue = aBarNode.groupValue;
        let groupItems = aBarNode.groupItems;

        FacetContext.unhoverFacet(this.faceter, this.attrDef, groupValue, groupItems);
      ]]></body>
    </method>
    <method name="includeFacet">
      <parameter name="node"/>
      <body>
      <![CDATA[
          this.barClicked(node,
                          (node.getAttribute("variety") == "remainder") ?
                            "include" : "remainder");
      ]]>
      </body>
    </method>
    <method name="undoFacet">
      <parameter name="node"/>
      <body>
      <![CDATA[
          this.barClicked(node,
                          (node.getAttribute("variety") == "remainder") ?
                            "include" : "remainder");
      ]]>
      </body>
    </method>
    <method name="excludeFacet">
      <parameter name="node"/>
      <body>
      <![CDATA[
        this.barClicked(node, "exclude");
      ]]>
      </body>
    </method>
    <method name="showPopup">
      <parameter name="event"/>
      <body>
        <![CDATA[
        try {
          // event.originalTarget could be the <li> node, or a span inside
          // of it, or perhaps the facet-more button, or maybe something
          // else that we'll handle in the next version.  We walk up its
          // parent chain until we get to the right level of the DOM
          // hierarchy, or the facet-content which seems to be the root.
          if (this.currentNode)
            this.currentNode.removeAttribute("selected");

          let node = event.originalTarget;
          while ((! (node && node.hasAttribute && node.hasAttribute("class"))) ||
                 (!node.classList.contains("bar") &&
                  !node.classList.contains("facet-more") &&
                  !node.classList.contains("facet-content")))
            node = node.parentNode;

          if (! (node && node.hasAttribute && node.hasAttribute("class")))
            return false;

          this.currentNode = node;
          node.setAttribute("selected", "true");
          if (node.classList.contains("bar"))
            document.getElementById("popup-menu").show(event, this, node);
          else if (node.classList.contains("facet-more"))
            this.changeMode("all");
          return false;
        } catch (e) {
          logException(e);
        }
      ]]>
      </body>
    </method>
    <method name="activateLink">
      <parameter name="event"/>
      <body>
        <![CDATA[
        try {
          let node = event.originalTarget;
          while ((!node.hasAttribute("class")) ||
                 (!node.classList.contains("facet-more") &&
                  !node.classList.contains("facet-content")))
            node = node.parentNode;
          if (node.classList.contains("facet-more"))
            this.changeMode("all");
          return false;
        } catch (e) {
          logException(e);
        }
      ]]>
      </body>
    </method>
  </implementation>
  <handlers>
    <handler event="click" phase="target" action="this.showPopup(event)"/>
    <handler event="keypress" keycode="VK_RETURN"
             action="this.showPopup(event);"
             phase="target" preventdefault="true"/>
    <handler event="keypress" key=" "
             action="this.activateLink(event);"
             phase="target" preventdefault="true"/>
    <handler event="mouseover"><![CDATA[
      // we dispatch based on the class of the thing we clicked on.
      // there are other ways we could accomplish this, but they all sorta suck.
      if (event.originalTarget.hasAttribute("class") &&
          event.originalTarget.classList.contains("bar-link")) {
        this.barHovered(event.originalTarget.parentNode, true);
      }
    ]]></handler>
    <handler event="mouseout"><![CDATA[
      // we dispatch based on the class of the thing we clicked on.
      // there are other ways we could accomplish this, but they all sorta suck.
      if (event.originalTarget.hasAttribute("class") &&
          event.originalTarget.classList.contains("bar-link")) {
        this.barHoverGone(event.originalTarget.parentNode, true);
      }
    ]]></handler>
  </handlers>
</binding>

<binding id="facet-date">
  <content>
    <html:div class="facet date-wrapper">
      <html:h2 anonid="name"></html:h2>
      <!-- we need to do this because of something protovis is doing where
           it attempts to re-interpret the text and the html namespace no
           longer exists in that context. -->
      <html:div anonid="canvas" class="date-vis-frame"></html:div>
      <html:div class="facet-date-zoom-out" onclick="FacetContext.zoomOut()"
                role="image"/>
    </html:div>
  </content>
  <implementation>
    <constructor><![CDATA[
      this.canvasNode = document.getAnonymousElementByAttribute(
                                   this, "anonid", "canvas");
      this.vis = null;
      if ("faceter" in this)
        this.build(true);
    ]]></constructor>
    <field name="canUpdate" readonly="true">true</field>
    <method name="build">
      <parameter name="aDoSize" />
      <body><![CDATA[
        if (!this.vis) {
          this.vis = new DateFacetVis(this, this.canvasNode);
          this.vis.build();
        }
        else {
          while (this.canvasNode.hasChildNodes())
            this.canvasNode.lastChild.remove();
          if (aDoSize)
            this.vis.build()
          else
            this.vis.rebuild();
        }
      ]]></body>
    </method>
    <method name="brushItems">
      <parameter name="aItems" />
      <body><![CDATA[
        this.vis.hoverItems(aItems);
      ]]></body>
    </method>
    <method name="clearBrushedItems">
      <body><![CDATA[
        this.vis.clearHover();
      ]]></body>
    </method>
  </implementation>
</binding>

<!-- ===== Results ===== -->

<binding id="results-message">
  <content>
    <html:div class="results-message-header">
      <html:h2 class="results-message-count" anonid="count"></html:h2>
      <html:div class="results-message-showall">
        <html:span class="results-message-showall-button"
            tabindex="0" anonid="showall" role="button"
            onclick="FacetContext.showActiveSetInTab()"></html:span>
      </html:div>
      <html:div anonid="sort" class="results-message-sort-bar">
        <html:span anonid="sort-label" class="results-message-sort-label"/>
        <html:span anonid="sort-relevance" class="results-message-sort-value"
            tabindex="0" role="button"/>
        <html:span anonid="sort-date" class="results-message-sort-value"
            tabindex="0" role="button"/>
      </html:div>
    </html:div>
    <html:div class="messages" anonid="messages">
    </html:div>
  </content>
  <implementation>
    <method name="setMessages">
      <parameter name="aMessages" />
      <body><![CDATA[
        // -- Count
        let countNode = document.getAnonymousElementByAttribute(
                          this, "anonid", "count");
        let topMessagesPluralFormat = glodaFacetStrings.get(
          "glodaFacetView.results.header.countLabel.NMessages");
        let outOfPluralFormat = glodaFacetStrings.get(
          "glodaFacetView.results.header.countLabel.ofN");
        let groupingFormat = glodaFacetStrings.get(
          "glodaFacetView.results.header.countLabel.grouping");

        let displayCount = aMessages.length;
        let totalCount = FacetContext.activeSet.length;

        // set the count so CSS selectors can know what the results look like
        this.setAttribute("state", (totalCount <= 0)? "empty" : "some");

        let topMessagesStr = PluralForm.get(displayCount,
                                            topMessagesPluralFormat)
                                       .replace("#1",
                                                displayCount.toLocaleString());
        let outOfStr = PluralForm.get(totalCount,
                                      outOfPluralFormat)
                                 .replace("#1", totalCount.toLocaleString());
        countNode.textContent = groupingFormat.replace("#1", topMessagesStr)
                                              .replace("#2", outOfStr);

        // -- Show All
        let showNode = document.getAnonymousElementByAttribute(
                          this, "anonid", "showall");
        const GlodaMessage = Gloda.lookupNounDef("message").clazz;
        let visible = aMessages.some(m => m instanceof GlodaMessage);
        showNode.style.display = visible ? "inline" : "none";
        showNode.textContent = glodaFacetStrings.get(
          "glodaFacetView.results.message.openEmailAsList.label");
        showNode.setAttribute("title", glodaFacetStrings.get(
          "glodaFacetView.results.message.openEmailAsList.tooltip"));
        showNode.onkeypress = function(event) {
          if (event.charCode == KeyEvent.DOM_VK_SPACE) {
            FacetContext.showActiveSetInTab()
            event.preventDefault();
          }
        }

        let sortLabelNode = document.getAnonymousElementByAttribute(
                          this, "anonid", "sort-label");
        sortLabelNode.textContent = glodaFacetStrings.get(
          "glodaFacetView.results.message.sort.label");

        let sortRelevanceNode = document.getAnonymousElementByAttribute(
                          this, "anonid", "sort-relevance");
        sortRelevanceNode.textContent = glodaFacetStrings.get(
          "glodaFacetView.results.message.sort.relevance");

        let dis = this;
        sortRelevanceNode.onclick = function() {
          FacetContext.sortBy = '-dascore';
          dis.updateSortLabels();
        }
        sortRelevanceNode.onkeypress = function(event) {
          if (event.charCode == KeyEvent.DOM_VK_SPACE) {
            FacetContext.sortBy = '-dascore';
            dis.updateSortLabels();
            event.preventDefault();
          }
        }

        let sortDateNode = document.getAnonymousElementByAttribute(
                          this, "anonid", "sort-date");
        sortDateNode.textContent = glodaFacetStrings.get(
          "glodaFacetView.results.message.sort.date");
        sortDateNode.onclick = function() {
          FacetContext.sortBy = '-date';
          dis.updateSortLabels();
        }
        sortDateNode.onkeypress = function(event) {
          if (event.charCode == KeyEvent.DOM_VK_SPACE) {
            FacetContext.sortBy = '-date';
            dis.updateSortLabels();
            event.preventDefault();
          }
        }

        this.updateSortLabels(FacetContext.sortBy);

        let messagesNode = document.getAnonymousElementByAttribute(
                             this, "anonid", "messages");
        while (messagesNode.hasChildNodes())
          messagesNode.lastChild.remove();
      try {
        // -- Messages
        for (let message of aMessages) {
          let msgNode = document.createElement("message");
          msgNode.message = message;
          msgNode.setAttribute("class", "message");
          messagesNode.appendChild(msgNode);
        }
      } catch (e) {
        logException(e);
      }
      ]]></body>
    </method>
    <method name="ensureNodeVisible">
      <parameter name="messageIndex"/>
      <body><![CDATA[
        let messagesNode = document.getAnonymousElementByAttribute(
                             this, "anonid", "messages");
        let message = messagesNode.childNodes[messageIndex];
        window.scrollTo(0, $(message).position()['top'] + $(window).scrollTop());
      ]]></body>
    </method>

    <method name="updateSortLabels">
      <body><![CDATA[
      try {
        let sortBy = FacetContext.sortBy;
        let sortRelevanceNode = document.getAnonymousElementByAttribute(
                          this, "anonid", "sort-relevance");
        let sortDateNode = document.getAnonymousElementByAttribute(
                          this, "anonid", "sort-date");

        if (sortBy == "-dascore") {
          sortRelevanceNode.setAttribute("selected", "true");
          sortDateNode.removeAttribute("selected");
        } else if (sortBy == "-date") {
          sortRelevanceNode.removeAttribute("selected");
          sortDateNode.setAttribute("selected", "true");
        }
      } catch (e ) {
        logException(e);
      }
      ]]></body>
    </method>
  </implementation>
</binding>

<binding id="result-message">
  <content>
    <html:div class="message-header">
      <html:div class="message-line">
        <html:div class="message-meta">
          <html:div anonid="addresses-group" class="message-addresses-group">
            <html:div anonid="author-group" class="message-author-group">
              <html:span anonid="author" class="message-author"></html:span>
              <html:div anonid="date" class="message-date"></html:div>
            </html:div>
          </html:div>
        </html:div>
        <html:div class="message-subject-group">
          <html:span anonid="star" class="message-star"></html:span>
          <html:span anonid="subject" class="message-subject" tabindex="0"
              role="link"></html:span>
          <html:span anonid="tags" class="message-tags"></html:span>
          <html:div anonid="recipients-group" class="message-recipients-group">
            <html:span anonid="to" class="message-to-label"></html:span>
            <html:div anonid="recipients" class="message-recipients"/>
          </html:div>
        </html:div>
      </html:div>
    </html:div>
    <html:pre anonid="snippet" class="message-body"></html:pre>
      <html:div anonid="attachments" class="message-attachments"></html:div>
  </content>
  <implementation>
    <constructor><![CDATA[
      ChromeUtils.import("resource:///modules/mailServices.js");
      this.build();
    ]]></constructor>
    <method name="build">
      <body><![CDATA[
        let message = this.message;
        let dis = this;
        function anonElem(aAnonId) {
          return document.getAnonymousElementByAttribute(dis, "anonid",
                                                         aAnonId);
        }
        let subject = anonElem("subject");
        // -- eventify
        subject.onclick = function(aEvent) {
          FacetContext.showConversationInTab(this,
                                             aEvent.button == 1);
        }.bind(this);
        subject.onkeypress = function(aEvent) {
          if (aEvent.keyCode == aEvent.DOM_VK_RETURN)
            FacetContext.showConversationInTab(this,
                                               aEvent.shiftKey == true);
        }.bind(this);

        // -- Content Poking
        if (message.subject.trim() == "")
          subject.textContent = glodaFacetStrings.get("glodaFacetView.result.message.noSubject");
        else
          subject.textContent = message.subject;
        let authorNode = anonElem("author");
        authorNode.setAttribute("title", message.from.value);
        authorNode.textContent = message.from.contact.name
        let toNode = anonElem("to");
        toNode.textContent = glodaFacetStrings.get("glodaFacetView.result.message.toLabel");

        //anonElem("author").textContent = ;
        anonElem("date").textContent = makeFriendlyDateAgo(message.date);

        // - Recipients
      try {
        let recipientsNode = anonElem("recipients");
        if (message.recipients) {
          let recipientCount = 0;
          const MAX_RECIPIENTS = 3;
          let totalRecipientCount = message.recipients.length;
          let recipientSeparator = glodaFacetStrings.get(
              "glodaFacetView.results.message.recipientSeparator")
          for (let index in message.recipients) {
            let recipNode = document.createElement("span");
            recipNode.setAttribute("class", "message-recipient");
            recipNode.textContent = message.recipients[index].contact.name;
            recipientsNode.appendChild(recipNode);
            recipientCount++;
            if (recipientCount == MAX_RECIPIENTS)
              break;
            if (index != totalRecipientCount - 1) {
              // add separators (usually commas)
              let sepNode = document.createElement("span");
              sepNode.setAttribute("class", "message-recipient-separator");
              sepNode.textContent = recipientSeparator;
              recipientsNode.appendChild(sepNode);
            }
          }
          if (totalRecipientCount > MAX_RECIPIENTS) {
            let nOthers = totalRecipientCount - recipientCount;
            let andNOthers = document.createElement("span");
            andNOthers.setAttribute("class", "message-recipients-andothers");

            let andOthersLabel= PluralForm.get(nOthers, glodaFacetStrings.get(
                              "glodaFacetView.results.message.andOthers"))
                             .replace("#1", nOthers);

            andNOthers.textContent = andOthersLabel;
            recipientsNode.appendChild(andNOthers);
          }
        }
      } catch (e) {
        logException(e);
      }

        // - Starred
        let starNode = anonElem("star");
        if (message.starred) {
          starNode.setAttribute("starred", "true")
        }

        // - Attachments
        if (message.attachmentNames) {
          let attachmentsNode = anonElem("attachments");
          let imgNode = document.createElement("div");
          imgNode.setAttribute("class", "message-attachment-icon");
          attachmentsNode.appendChild(imgNode);
          for (let attach of message.attachmentNames) {
            let attachNode = document.createElement("div");
            attachNode.setAttribute("class", "message-attachment");
            if (attach.length >= 28)
              attach = attach.substring(0, 24) + "…";
            attachNode.textContent = attach;
            attachmentsNode.appendChild(attachNode);
          }
        }

        // - Tags
        let tagsNode = anonElem("tags");
        if ("tags" in message && message.tags.length) {
          for (let tag of message.tags) {
            let tagNode = document.createElement("span");
            let colorClass = "blc-" + MailServices.tags.getColorForKey(tag.key).substr(1);
            tagNode.setAttribute("class", "message-tag tag " + colorClass);
            tagNode.textContent = tag.tag;
            tagsNode.appendChild(tagNode);
          }
        }

        // - Body
        if (message.indexedBodyText) {
          let bodyText = message.indexedBodyText;

          let matches = [];
          if ("stashedColumns" in FacetContext.collection) {
            let collection;
            if ("IMCollection" in FacetContext &&
                message instanceof Gloda.lookupNounDef("im-conversation").clazz)
              collection = FacetContext.IMCollection;
            else
              collection = FacetContext.collection;
            let offsets = collection.stashedColumns[message.id][0];
            let offsetNums = offsets.split(" ").map(x => parseInt(x));
            for (let i = 0; i < offsetNums.length; i += 4) {
              // i is the column index. The indexedBodyText is in the column 0.
              // Ignore matches for other columns.
              if (offsetNums[i] != 0)
                continue;

              // i+1 is the term index, indicating which queried term was found.
              // We can ignore for now...

              // i+2 is the *byte* offset at which the term is in the string.
              // i+3 is the term's length.
              matches.push([offsetNums[i + 2], offsetNums[i + 3]]);
            }

            // Sort the matches by index, just to be sure.
            // They are probably already sorted, but if they aren't it could
            // mess things up at the next step.
            matches.sort((a, b) => a[0] - b[0]);

            // Convert the byte offsets and lengths into character indexes.
            let charCodeToByteCount = function(c) {
              // UTF-8 stores:
              // - code points below U+0080 on 1 byte,
              // - code points below U+0800 on 2 bytes,
              // - code points U+D800 through U+DFFF are UTF-16 surrogate halves
              // (they indicate that JS has split a 4 bytes UTF-8 character
              // in two halves of 2 bytes each),
              // - other code points on 3 bytes.
              return c < 0x80 ? 1 : (c < 0x800 || (c >= 0xD800 && c <= 0xDFFF)) ? 2 : 3;
            }
            let byteOffset = 0;
            let offset = 0;
            for (let match of matches) {
              while (byteOffset < match[0])
                byteOffset += charCodeToByteCount(bodyText.charCodeAt(offset++));
              match[0] = offset;
              for (let i = offset; i < offset + match[1]; ++i) {
                let size = charCodeToByteCount(bodyText.charCodeAt(i));
                if (size > 1)
                  match[1] -= size - 1;
              }
            }
          }

          // how many lines of context we want before the first match:
          const kContextLines = 2;

          let startIndex = 0;
          if (matches.length > 0) {
            // Find where the snippet should begin to show at least the
            // first match and kContextLines of context before the match.
            startIndex = matches[0][0];
            for (let context = kContextLines; context >= 0; --context) {
              startIndex = bodyText.lastIndexOf("\n", startIndex - 1);
              if (startIndex == -1) {
                startIndex = 0;
                break;
              }
            }
          }

          // start assuming it's just one line that we want to show
          let idxNewline = -1;
          let ellipses = "…";

          let maxLineCount = 5;
          if (startIndex != 0) {
            // Avoid displaying an ellipses followed by an empty line.
            while (bodyText[startIndex + 1] == "\n")
              ++startIndex;
            bodyText = ellipses + bodyText.substring(startIndex);
            // The first line will only contain the ellipsis as the character
            // at startIndex is always \n, so we show an additional line.
            ++maxLineCount;
          }

          for (let newlineCount = 0; newlineCount < maxLineCount; newlineCount++) {
            idxNewline = bodyText.indexOf("\n", idxNewline+1);
            if (idxNewline == -1) {
              ellipses = '';
              break;
            }
          }
          let snippet = "";
          if (idxNewline > -1)
            snippet = bodyText.substring(0, idxNewline);
          else
            snippet = bodyText;
          if (ellipses)
            snippet = snippet.trimRight() + ellipses;

          let parent = anonElem("snippet");
          let node = document.createTextNode(snippet);
          parent.appendChild(node);

          let offset = startIndex ? startIndex - 1 : 0; // The ellipsis takes 1 character.
          for (let match of matches) {
            if (idxNewline > -1 && match[0] > startIndex + idxNewline)
              break;
            let secondNode = node.splitText(match[0] - offset);
            node = secondNode.splitText(match[1]);
            offset += match[0] + match[1] - offset;
            let span = document.createElement("span");
            span.textContent = secondNode.data;
            if (!this.firstMatchText)
              this.firstMatchText = secondNode.data;
            span.setAttribute("class", "message-body-fulltext-match");
            parent.replaceChild(span, secondNode);
          }
        }

        // - Misc attributes
        if (!message.read)
          this.setAttribute("unread", "true");
      ]]></body>
    </method>
  </implementation>
  <handlers>
    <handler event="mouseover"><![CDATA[
      FacetContext.hoverFacet(FacetContext.fakeResultFaceter,
                              FacetContext.fakeResultAttr,
                              this.message, [this.message]);
    ]]></handler>
    <handler event="mouseout"><![CDATA[
      FacetContext.unhoverFacet(FacetContext.fakeResultFaceter,
                                FacetContext.fakeResultAttr,
                                this.message, [this.message]);
    ]]></handler>
  </handlers>
</binding>

</bindings>