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
|
/* 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/. */
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
/**
* The base view implements everything that's common to the toolbar and
* menu views.
*/
function PlacesViewBase(aPlace) {
this.place = aPlace;
this._controller = new PlacesController(this);
this._viewElt.controllers.appendController(this._controller);
}
PlacesViewBase.prototype = {
// The xul element that holds the entire view.
_viewElt: null,
get viewElt() this._viewElt,
get associatedElement() this._viewElt,
get controllers() this._viewElt.controllers,
// The xul element that represents the root container.
_rootElt: null,
// Set to true for views that are represented by native widgets (i.e.
// the native mac menu).
_nativeView: false,
QueryInterface: XPCOMUtils.generateQI(
[Components.interfaces.nsINavHistoryResultObserver,
Components.interfaces.nsISupportsWeakReference]),
_place: "",
get place() this._place,
set place(val) {
this._place = val;
let history = PlacesUtils.history;
let queries = { }, options = { };
history.queryStringToQueries(val, queries, { }, options);
if (!queries.value.length)
queries.value = [history.getNewQuery()];
let result = history.executeQueries(queries.value, queries.value.length,
options.value);
result.addObserver(this, false);
return val;
},
_result: null,
get result() this._result,
set result(val) {
if (this._result == val)
return val;
if (this._result) {
this._result.removeObserver(this);
this._resultNode.containerOpen = false;
}
if (this._rootElt.localName == "menupopup")
this._rootElt._built = false;
this._result = val;
if (val) {
this._resultNode = val.root;
this._rootElt._placesNode = this._resultNode;
this._domNodes = new Map();
this._domNodes.set(this._resultNode, this._rootElt);
// This calls _rebuild through invalidateContainer.
this._resultNode.containerOpen = true;
}
else {
this._resultNode = null;
delete this._domNodes;
}
return val;
},
/**
* Gets the DOM node used for the given places node.
*
* @param aPlacesNode
* a places result node.
* @throws if there is no DOM node set for aPlacesNode.
*/
_getDOMNodeForPlacesNode:
function PVB__getDOMNodeForPlacesNode(aPlacesNode) {
let node = this._domNodes.get(aPlacesNode, null);
if (!node) {
throw new Error("No DOM node set for aPlacesNode.\nnode.type: " +
aPlacesNode.type + ". node.parent: " + aPlacesNode);
}
return node;
},
get controller() this._controller,
get selType() "single",
selectItems: function() { },
selectAll: function() { },
get selectedNode() {
if (this._contextMenuShown) {
let popup = document.popupNode;
return popup._placesNode || popup.parentNode._placesNode || null;
}
return null;
},
get hasSelection() this.selectedNode != null,
get selectedNodes() {
let selectedNode = this.selectedNode;
return selectedNode ? [selectedNode] : [];
},
get removableSelectionRanges() {
// On static content the current selectedNode would be the selection's
// parent node. We don't want to allow removing a node when the
// selection is not explicit.
if (document.popupNode &&
(document.popupNode == "menupopup" || !document.popupNode._placesNode))
return [];
return [this.selectedNodes];
},
get draggableSelection() [this._draggedElt],
get insertionPoint() {
// There is no insertion point for history queries, so bail out now and
// save a lot of work when updating commands.
let resultNode = this._resultNode;
if (PlacesUtils.nodeIsQuery(resultNode) &&
PlacesUtils.asQuery(resultNode).queryOptions.queryType ==
Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY)
return null;
// By default, the insertion point is at the top level, at the end.
let index = PlacesUtils.bookmarks.DEFAULT_INDEX;
let container = this._resultNode;
let orientation = Ci.nsITreeView.DROP_BEFORE;
let isTag = false;
let selectedNode = this.selectedNode;
if (selectedNode) {
let popup = document.popupNode;
if (!popup._placesNode || popup._placesNode == this._resultNode ||
popup._placesNode.itemId == -1) {
// If a static menuitem is selected, or if the root node is selected,
// the insertion point is inside the folder, at the end.
container = selectedNode;
orientation = Ci.nsITreeView.DROP_ON;
}
else {
// In all other cases the insertion point is before that node.
container = selectedNode.parent;
index = container.getChildIndex(selectedNode);
isTag = PlacesUtils.nodeIsTagQuery(container);
}
}
if (PlacesControllerDragHelper.disallowInsertion(container))
return null;
return new InsertionPoint(PlacesUtils.getConcreteItemId(container),
index, orientation, isTag);
},
buildContextMenu: function PVB_buildContextMenu(aPopup) {
this._contextMenuShown = true;
window.updateCommands("places");
return this.controller.buildContextMenu(aPopup);
},
destroyContextMenu: function PVB_destroyContextMenu(aPopup) {
this._contextMenuShown = false;
},
_cleanPopup: function PVB_cleanPopup(aPopup, aDelay) {
// Remove Places nodes from the popup.
let child = aPopup._startMarker;
while (child.nextSibling != aPopup._endMarker) {
let sibling = child.nextSibling;
if (sibling._placesNode && !aDelay) {
aPopup.removeChild(sibling);
}
else if (sibling._placesNode && aDelay) {
// HACK (bug 733419): the popups originating from the OS X native
// menubar don't live-update while open, thus we don't clean it
// until the next popupshowing, to avoid zombie menuitems.
if (!aPopup._delayedRemovals)
aPopup._delayedRemovals = [];
aPopup._delayedRemovals.push(sibling);
child = child.nextSibling;
}
else {
child = child.nextSibling;
}
}
},
_rebuildPopup: function PVB__rebuildPopup(aPopup) {
let resultNode = aPopup._placesNode;
if (!resultNode.containerOpen)
return;
if (this.controller.hasCachedLivemarkInfo(resultNode)) {
this._setEmptyPopupStatus(aPopup, false);
aPopup._built = true;
this._populateLivemarkPopup(aPopup);
return;
}
this._cleanPopup(aPopup);
let cc = resultNode.childCount;
if (cc > 0) {
this._setEmptyPopupStatus(aPopup, false);
for (let i = 0; i < cc; ++i) {
let child = resultNode.getChild(i);
this._insertNewItemToPopup(child, aPopup, null);
}
}
else {
this._setEmptyPopupStatus(aPopup, true);
}
aPopup._built = true;
},
_removeChild: function PVB__removeChild(aChild) {
// If document.popupNode pointed to this child, null it out,
// otherwise controller's command-updating may rely on the removed
// item still being "selected".
if (document.popupNode == aChild)
document.popupNode = null;
aChild.parentNode.removeChild(aChild);
},
_setEmptyPopupStatus:
function PVB__setEmptyPopupStatus(aPopup, aEmpty) {
if (!aPopup._emptyMenuitem) {
let label = PlacesUIUtils.getString("bookmarksMenuEmptyFolder");
aPopup._emptyMenuitem = document.createElement("menuitem");
aPopup._emptyMenuitem.setAttribute("label", label);
aPopup._emptyMenuitem.setAttribute("disabled", true);
}
if (aEmpty) {
aPopup.setAttribute("emptyplacesresult", "true");
// Don't add the menuitem if there is static content.
if (!aPopup._startMarker.previousSibling &&
!aPopup._endMarker.nextSibling)
aPopup.insertBefore(aPopup._emptyMenuitem, aPopup._endMarker);
}
else {
aPopup.removeAttribute("emptyplacesresult");
try {
aPopup.removeChild(aPopup._emptyMenuitem);
} catch (ex) {}
}
},
_createMenuItemForPlacesNode:
function PVB__createMenuItemForPlacesNode(aPlacesNode) {
this._domNodes.delete(aPlacesNode);
let element;
let type = aPlacesNode.type;
if (type == Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR) {
element = document.createElement("menuseparator");
}
else {
let itemId = aPlacesNode.itemId;
if (type == Ci.nsINavHistoryResultNode.RESULT_TYPE_URI) {
element = document.createElement("menuitem");
element.className = "menuitem-iconic bookmark-item menuitem-with-favicon";
element.setAttribute("scheme",
PlacesUIUtils.guessUrlSchemeForUI(aPlacesNode.uri));
}
else if (PlacesUtils.containerTypes.indexOf(type) != -1) {
element = document.createElement("menu");
element.setAttribute("container", "true");
if (aPlacesNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY) {
element.setAttribute("query", "true");
if (PlacesUtils.nodeIsTagQuery(aPlacesNode))
element.setAttribute("tagContainer", "true");
else if (PlacesUtils.nodeIsDay(aPlacesNode))
element.setAttribute("dayContainer", "true");
else if (PlacesUtils.nodeIsHost(aPlacesNode))
element.setAttribute("hostContainer", "true");
}
else if (itemId != -1) {
PlacesUtils.livemarks.getLivemark(
{ id: itemId },
function (aStatus, aLivemark) {
if (Components.isSuccessCode(aStatus)) {
element.setAttribute("livemark", "true");
#ifdef XP_MACOSX
// OS X native menubar doesn't track list-style-images since
// it doesn't have a frame (bug 733415). Thus enforce updating.
element.setAttribute("image", "");
element.removeAttribute("image");
#endif
this.controller.cacheLivemarkInfo(aPlacesNode, aLivemark);
}
}.bind(this)
);
}
let popup = document.createElement("menupopup");
popup._placesNode = PlacesUtils.asContainer(aPlacesNode);
if (!this._nativeView) {
popup.setAttribute("placespopup", "true");
}
#ifdef XP_MACOSX
// No context menu on mac.
popup.setAttribute("context", "placesContext");
#endif
element.appendChild(popup);
element.className = "menu-iconic bookmark-item";
this._domNodes.set(aPlacesNode, popup);
}
else
throw "Unexpected node";
element.setAttribute("label", PlacesUIUtils.getBestTitle(aPlacesNode));
let icon = aPlacesNode.icon;
if (icon)
element.setAttribute("image", icon);
}
element._placesNode = aPlacesNode;
if (!this._domNodes.has(aPlacesNode))
this._domNodes.set(aPlacesNode, element);
return element;
},
_insertNewItemToPopup:
function PVB__insertNewItemToPopup(aNewChild, aPopup, aBefore) {
let element = this._createMenuItemForPlacesNode(aNewChild);
let before = aBefore || aPopup._endMarker;
aPopup.insertBefore(element, before);
return element;
},
_setLivemarkSiteURIMenuItem:
function PVB__setLivemarkSiteURIMenuItem(aPopup) {
let livemarkInfo = this.controller.getCachedLivemarkInfo(aPopup._placesNode);
let siteUrl = livemarkInfo && livemarkInfo.siteURI ?
livemarkInfo.siteURI.spec : null;
if (!siteUrl && aPopup._siteURIMenuitem) {
aPopup.removeChild(aPopup._siteURIMenuitem);
aPopup._siteURIMenuitem = null;
aPopup.removeChild(aPopup._siteURIMenuseparator);
aPopup._siteURIMenuseparator = null;
}
else if (siteUrl && !aPopup._siteURIMenuitem) {
// Add "Open (Feed Name)" menuitem.
aPopup._siteURIMenuitem = document.createElement("menuitem");
aPopup._siteURIMenuitem.className = "openlivemarksite-menuitem";
aPopup._siteURIMenuitem.setAttribute("targetURI", siteUrl);
aPopup._siteURIMenuitem.setAttribute("oncommand",
"openUILink(this.getAttribute('targetURI'), event);");
// If a user middle-clicks this item we serve the oncommand event.
// We are using checkForMiddleClick because of Bug 246720.
// Note: stopPropagation is needed to avoid serving middle-click
// with BT_onClick that would open all items in tabs.
aPopup._siteURIMenuitem.setAttribute("onclick",
"checkForMiddleClick(this, event); event.stopPropagation();");
let label =
PlacesUIUtils.getFormattedString("menuOpenLivemarkOrigin.label",
[aPopup.parentNode.getAttribute("label")])
aPopup._siteURIMenuitem.setAttribute("label", label);
aPopup.insertBefore(aPopup._siteURIMenuitem, aPopup._startMarker);
aPopup._siteURIMenuseparator = document.createElement("menuseparator");
aPopup.insertBefore(aPopup._siteURIMenuseparator, aPopup._startMarker);
}
},
/**
* Add, update or remove the livemark status menuitem.
* @param aPopup
* The livemark container popup
* @param aStatus
* The livemark status
*/
_setLivemarkStatusMenuItem:
function PVB_setLivemarkStatusMenuItem(aPopup, aStatus) {
let statusMenuitem = aPopup._statusMenuitem;
if (!statusMenuitem) {
// Create the status menuitem and cache it in the popup object.
statusMenuitem = document.createElement("menuitem");
statusMenuitem.className = "livemarkstatus-menuitem";
statusMenuitem.setAttribute("disabled", true);
aPopup._statusMenuitem = statusMenuitem;
}
if (aStatus == Ci.mozILivemark.STATUS_LOADING ||
aStatus == Ci.mozILivemark.STATUS_FAILED) {
// Status has changed, update the cached status menuitem.
let stringId = aStatus == Ci.mozILivemark.STATUS_LOADING ?
"bookmarksLivemarkLoading" : "bookmarksLivemarkFailed";
statusMenuitem.setAttribute("label", PlacesUIUtils.getString(stringId));
if (aPopup._startMarker.nextSibling != statusMenuitem)
aPopup.insertBefore(statusMenuitem, aPopup._startMarker.nextSibling);
}
else {
// The livemark has finished loading.
if (aPopup._statusMenuitem.parentNode == aPopup)
aPopup.removeChild(aPopup._statusMenuitem);
}
},
toggleCutNode: function PVB_toggleCutNode(aPlacesNode, aValue) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// We may get the popup for menus, but we need the menu itself.
if (elt.localName == "menupopup")
elt = elt.parentNode;
if (aValue)
elt.setAttribute("cutting", "true");
else
elt.removeAttribute("cutting");
},
nodeURIChanged: function PVB_nodeURIChanged(aPlacesNode, aURIString) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// Here we need the <menu>.
if (elt.localName == "menupopup")
elt = elt.parentNode;
elt.setAttribute("scheme", PlacesUIUtils.guessUrlSchemeForUI(aURIString));
},
nodeIconChanged: function PVB_nodeIconChanged(aPlacesNode) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// There's no UI representation for the root node, thus there's nothing to
// be done when the icon changes.
if (elt == this._rootElt)
return;
// Here we need the <menu>.
if (elt.localName == "menupopup")
elt = elt.parentNode;
let icon = aPlacesNode.icon;
if (!icon)
elt.removeAttribute("image");
else if (icon != elt.getAttribute("image"))
elt.setAttribute("image", icon);
},
nodeAnnotationChanged:
function PVB_nodeAnnotationChanged(aPlacesNode, aAnno) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// All livemarks have a feedURI, so use it as our indicator of a livemark
// being modified.
if (aAnno == PlacesUtils.LMANNO_FEEDURI) {
let menu = elt.parentNode;
if (!menu.hasAttribute("livemark")) {
menu.setAttribute("livemark", "true");
#ifdef XP_MACOSX
// OS X native menubar doesn't track list-style-images since
// it doesn't have a frame (bug 733415). Thus enforce updating.
menu.setAttribute("image", "");
menu.removeAttribute("image");
#endif
}
PlacesUtils.livemarks.getLivemark(
{ id: aPlacesNode.itemId },
function (aStatus, aLivemark) {
if (Components.isSuccessCode(aStatus)) {
// Controller will use this to build the meta data for the node.
this.controller.cacheLivemarkInfo(aPlacesNode, aLivemark);
this.invalidateContainer(aPlacesNode);
}
}.bind(this)
);
}
},
nodeTitleChanged:
function PVB_nodeTitleChanged(aPlacesNode, aNewTitle) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// There's no UI representation for the root node, thus there's
// nothing to be done when the title changes.
if (elt == this._rootElt)
return;
// Here we need the <menu>.
if (elt.localName == "menupopup")
elt = elt.parentNode;
if (!aNewTitle && elt.localName != "toolbarbutton") {
// Many users consider toolbars as shortcuts containers, so explicitly
// allow empty labels on toolbarbuttons. For any other element try to be
// smarter, guessing a title from the uri.
elt.setAttribute("label", PlacesUIUtils.getBestTitle(aPlacesNode));
}
else {
elt.setAttribute("label", aNewTitle);
}
},
nodeRemoved:
function PVB_nodeRemoved(aParentPlacesNode, aPlacesNode, aIndex) {
let parentElt = this._getDOMNodeForPlacesNode(aParentPlacesNode);
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// Here we need the <menu>.
if (elt.localName == "menupopup")
elt = elt.parentNode;
if (parentElt._built) {
parentElt.removeChild(elt);
// Figure out if we need to show the "<Empty>" menu-item.
// TODO Bug 517701: This doesn't seem to handle the case of an empty
// root.
if (parentElt._startMarker.nextSibling == parentElt._endMarker)
this._setEmptyPopupStatus(parentElt, true);
}
},
nodeHistoryDetailsChanged:
function PVB_nodeHistoryDetailsChanged(aPlacesNode, aTime, aCount) {
if (aPlacesNode.parent &&
this.controller.hasCachedLivemarkInfo(aPlacesNode.parent)) {
// Find the node in the parent.
let popup = this._getDOMNodeForPlacesNode(aPlacesNode.parent);
for (let child = popup._startMarker.nextSibling;
child != popup._endMarker;
child = child.nextSibling) {
if (child._placesNode && child._placesNode.uri == aPlacesNode.uri) {
if (aCount)
child.setAttribute("visited", "true");
else
child.removeAttribute("visited");
break;
}
}
}
},
nodeTagsChanged: function() { },
nodeDateAddedChanged: function() { },
nodeLastModifiedChanged: function() { },
nodeKeywordChanged: function() { },
sortingChanged: function() { },
batching: function() { },
nodeInserted:
function PVB_nodeInserted(aParentPlacesNode, aPlacesNode, aIndex) {
let parentElt = this._getDOMNodeForPlacesNode(aParentPlacesNode);
if (!parentElt._built)
return;
let index = Array.indexOf(parentElt.childNodes, parentElt._startMarker) +
aIndex + 1;
this._insertNewItemToPopup(aPlacesNode, parentElt,
parentElt.childNodes[index]);
this._setEmptyPopupStatus(parentElt, false);
},
nodeMoved:
function PBV_nodeMoved(aPlacesNode,
aOldParentPlacesNode, aOldIndex,
aNewParentPlacesNode, aNewIndex) {
// Note: the current implementation of moveItem does not actually
// use this notification when the item in question is moved from one
// folder to another. Instead, it calls nodeRemoved and nodeInserted
// for the two folders. Thus, we can assume old-parent == new-parent.
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// Here we need the <menu>.
if (elt.localName == "menupopup")
elt = elt.parentNode;
// If our root node is a folder, it might be moved. There's nothing
// we need to do in that case.
if (elt == this._rootElt)
return;
let parentElt = this._getDOMNodeForPlacesNode(aNewParentPlacesNode);
if (parentElt._built) {
// Move the node.
parentElt.removeChild(elt);
let index = Array.indexOf(parentElt.childNodes, parentElt._startMarker) +
aNewIndex + 1;
parentElt.insertBefore(elt, parentElt.childNodes[index]);
}
},
containerStateChanged:
function PVB_containerStateChanged(aPlacesNode, aOldState, aNewState) {
if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_OPENED ||
aNewState == Ci.nsINavHistoryContainerResultNode.STATE_CLOSED) {
this.invalidateContainer(aPlacesNode);
if (PlacesUtils.nodeIsFolder(aPlacesNode)) {
let queryOptions = PlacesUtils.asQuery(this._result.root).queryOptions;
if (queryOptions.excludeItems) {
return;
}
PlacesUtils.livemarks.getLivemark({ id: aPlacesNode.itemId },
function (aStatus, aLivemark) {
if (Components.isSuccessCode(aStatus)) {
let shouldInvalidate =
!this.controller.hasCachedLivemarkInfo(aPlacesNode);
this.controller.cacheLivemarkInfo(aPlacesNode, aLivemark);
if (aNewState == Ci.nsINavHistoryContainerResultNode.STATE_OPENED) {
aLivemark.registerForUpdates(aPlacesNode, this);
// Prioritize the current livemark.
aLivemark.reload();
PlacesUtils.livemarks.reloadLivemarks();
if (shouldInvalidate)
this.invalidateContainer(aPlacesNode);
}
else {
aLivemark.unregisterForUpdates(aPlacesNode);
}
}
}.bind(this)
);
}
}
},
_populateLivemarkPopup: function PVB__populateLivemarkPopup(aPopup)
{
this._setLivemarkSiteURIMenuItem(aPopup);
// Show the loading status only if there are no entries yet.
if (aPopup._startMarker.nextSibling == aPopup._endMarker)
this._setLivemarkStatusMenuItem(aPopup, Ci.mozILivemark.STATUS_LOADING);
PlacesUtils.livemarks.getLivemark({ id: aPopup._placesNode.itemId },
function (aStatus, aLivemark) {
let placesNode = aPopup._placesNode;
if (!Components.isSuccessCode(aStatus) || !placesNode.containerOpen)
return;
if (aLivemark.status != Ci.mozILivemark.STATUS_LOADING)
this._setLivemarkStatusMenuItem(aPopup, aLivemark.status);
this._cleanPopup(aPopup,
this._nativeView && aPopup.parentNode.hasAttribute("open"));
let children = aLivemark.getNodesForContainer(placesNode);
for (let i = 0; i < children.length; i++) {
let child = children[i];
this.nodeInserted(placesNode, child, i);
if (child.accessCount)
this._getDOMNodeForPlacesNode(child).setAttribute("visited", true);
else
this._getDOMNodeForPlacesNode(child).removeAttribute("visited");
}
}.bind(this)
);
},
invalidateContainer: function PVB_invalidateContainer(aPlacesNode) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
elt._built = false;
// If the menupopup is open we should live-update it.
if (elt.parentNode.open)
this._rebuildPopup(elt);
},
uninit: function PVB_uninit() {
if (this._result) {
this._result.removeObserver(this);
this._resultNode.containerOpen = false;
this._resultNode = null;
this._result = null;
}
if (this._controller) {
this._controller.terminate();
this._viewElt.controllers.removeController(this._controller);
this._controller = null;
}
delete this._viewElt._placesView;
},
get isRTL() {
if ("_isRTL" in this)
return this._isRTL;
return this._isRTL = document.defaultView
.getComputedStyle(this.viewElt, "")
.direction == "rtl";
},
get ownerWindow() window,
/**
* Adds an "Open All in Tabs" menuitem to the bottom of the popup.
* @param aPopup
* a Places popup.
*/
_mayAddCommandsItems: function PVB__mayAddCommandsItems(aPopup) {
// The command items are never added to the root popup.
if (aPopup == this._rootElt)
return;
let hasMultipleURIs = false;
// Check if the popup contains at least 2 menuitems with places nodes.
// We don't currently support opening multiple uri nodes when they are not
// populated by the result.
if (aPopup._placesNode.childCount > 0) {
let currentChild = aPopup.firstChild;
let numURINodes = 0;
while (currentChild) {
if (currentChild.localName == "menuitem" && currentChild._placesNode) {
if (++numURINodes == 2)
break;
}
currentChild = currentChild.nextSibling;
}
hasMultipleURIs = numURINodes > 1;
}
if (!hasMultipleURIs) {
// We don't have to show any option.
if (aPopup._endOptOpenAllInTabs) {
aPopup.removeChild(aPopup._endOptOpenAllInTabs);
aPopup._endOptOpenAllInTabs = null;
aPopup.removeChild(aPopup._endOptSeparator);
aPopup._endOptSeparator = null;
}
}
else if (!aPopup._endOptOpenAllInTabs) {
// Create a separator before options.
aPopup._endOptSeparator = document.createElement("menuseparator");
aPopup._endOptSeparator.className = "bookmarks-actions-menuseparator";
aPopup.appendChild(aPopup._endOptSeparator);
// Add the "Open All in Tabs" menuitem.
aPopup._endOptOpenAllInTabs = document.createElement("menuitem");
aPopup._endOptOpenAllInTabs.className = "openintabs-menuitem";
aPopup._endOptOpenAllInTabs.setAttribute("oncommand",
"PlacesUIUtils.openContainerNodeInTabs(this.parentNode._placesNode, event, " +
"PlacesUIUtils.getViewForNode(this));");
aPopup._endOptOpenAllInTabs.setAttribute("onclick",
"checkForMiddleClick(this, event); event.stopPropagation();");
aPopup._endOptOpenAllInTabs.setAttribute("label",
gNavigatorBundle.getString("menuOpenAllInTabs.label"));
aPopup.appendChild(aPopup._endOptOpenAllInTabs);
}
},
_ensureMarkers: function PVB__ensureMarkers(aPopup) {
if (aPopup._startMarker)
return;
// _startMarker is an hidden menuseparator that lives before places nodes.
aPopup._startMarker = document.createElement("menuseparator");
aPopup._startMarker.hidden = true;
aPopup.insertBefore(aPopup._startMarker, aPopup.firstChild);
// _endMarker is an hidden menuseparator that lives after places nodes.
aPopup._endMarker = document.createElement("menuseparator");
aPopup._endMarker.hidden = true;
aPopup.appendChild(aPopup._endMarker);
// Move the markers to the right position.
let firstNonStaticNodeFound = false;
for (let i = 0; i < aPopup.childNodes.length; i++) {
let child = aPopup.childNodes[i];
// Menus that have static content at the end, but are initially empty,
// use a special "builder" attribute to figure out where to start
// inserting places nodes.
if (child.getAttribute("builder") == "end") {
aPopup.insertBefore(aPopup._endMarker, child);
break;
}
if (child._placesNode && !firstNonStaticNodeFound) {
firstNonStaticNodeFound = true;
aPopup.insertBefore(aPopup._startMarker, child);
}
}
if (!firstNonStaticNodeFound) {
aPopup.insertBefore(aPopup._startMarker, aPopup._endMarker);
}
},
_onPopupShowing: function PVB__onPopupShowing(aEvent) {
// Avoid handling popupshowing of inner views.
let popup = aEvent.originalTarget;
this._ensureMarkers(popup);
// Remove any delayed element, see _cleanPopup for details.
if ("_delayedRemovals" in popup) {
while (popup._delayedRemovals.length > 0) {
popup.removeChild(popup._delayedRemovals.shift());
}
}
if (popup._placesNode && PlacesUIUtils.getViewForNode(popup) == this) {
if (!popup._placesNode.containerOpen)
popup._placesNode.containerOpen = true;
if (!popup._built)
this._rebuildPopup(popup);
this._mayAddCommandsItems(popup);
}
},
_addEventListeners:
function PVB__addEventListeners(aObject, aEventNames, aCapturing) {
for (let i = 0; i < aEventNames.length; i++) {
aObject.addEventListener(aEventNames[i], this, aCapturing);
}
},
_removeEventListeners:
function PVB__removeEventListeners(aObject, aEventNames, aCapturing) {
for (let i = 0; i < aEventNames.length; i++) {
aObject.removeEventListener(aEventNames[i], this, aCapturing);
}
},
};
function PlacesToolbar(aPlace) {
let startTime = Date.now();
// Add some smart getters for our elements.
let thisView = this;
[
["_viewElt", "PlacesToolbar"],
["_rootElt", "PlacesToolbarItems"],
["_dropIndicator", "PlacesToolbarDropIndicator"],
["_chevron", "PlacesChevron"],
["_chevronPopup", "PlacesChevronPopup"]
].forEach(function (elementGlobal) {
let [name, id] = elementGlobal;
thisView.__defineGetter__(name, function () {
let element = document.getElementById(id);
if (!element)
return null;
delete thisView[name];
return thisView[name] = element;
});
});
this._viewElt._placesView = this;
this._addEventListeners(this._viewElt, this._cbEvents, false);
this._addEventListeners(this._rootElt, ["popupshowing", "popuphidden"], true);
this._addEventListeners(this._rootElt, ["overflow", "underflow"], true);
this._addEventListeners(window, ["resize", "unload"], false);
// If personal-bookmarks has been dragged to the tabs toolbar,
// we have to track addition and removals of tabs, to properly
// recalculate the available space for bookmarks.
// TODO (bug 734730): Use a performant mutation listener when available.
if (this._viewElt.parentNode.parentNode == document.getElementById("TabsToolbar")) {
this._addEventListeners(gBrowser.tabContainer, ["TabOpen", "TabClose"], false);
}
PlacesViewBase.call(this, aPlace);
Services.telemetry.getHistogramById("FX_BOOKMARKS_TOOLBAR_INIT_MS")
.add(Date.now() - startTime);
}
PlacesToolbar.prototype = {
__proto__: PlacesViewBase.prototype,
_cbEvents: ["dragstart", "dragover", "dragexit", "dragend", "drop",
"mousemove", "mouseover", "mouseout"],
QueryInterface: function PT_QueryInterface(aIID) {
if (aIID.equals(Ci.nsIDOMEventListener) ||
aIID.equals(Ci.nsITimerCallback))
return this;
return PlacesViewBase.prototype.QueryInterface.apply(this, arguments);
},
uninit: function PT_uninit() {
this._removeEventListeners(this._viewElt, this._cbEvents, false);
this._removeEventListeners(this._rootElt, ["popupshowing", "popuphidden"],
true);
this._removeEventListeners(this._rootElt, ["overflow", "underflow"], true);
this._removeEventListeners(window, ["resize", "unload"], false);
this._removeEventListeners(gBrowser.tabContainer, ["TabOpen", "TabClose"], false);
PlacesViewBase.prototype.uninit.apply(this, arguments);
},
_openedMenuButton: null,
_allowPopupShowing: true,
_rebuild: function PT__rebuild() {
// Clear out references to existing nodes, since they will be removed
// and re-added.
if (this._overFolder.elt)
this._clearOverFolder();
this._openedMenuButton = null;
while (this._rootElt.hasChildNodes()) {
this._rootElt.removeChild(this._rootElt.firstChild);
}
let cc = this._resultNode.childCount;
for (let i = 0; i < cc; ++i) {
this._insertNewItem(this._resultNode.getChild(i), null);
}
if (this._chevronPopup.hasAttribute("type")) {
// Chevron has already been initialized, but since we are forcing
// a rebuild of the toolbar, it has to be rebuilt.
// Otherwise, it will be initialized when the toolbar overflows.
this._chevronPopup.place = this.place;
}
},
_insertNewItem:
function PT__insertNewItem(aChild, aBefore) {
this._domNodes.delete(aChild);
let type = aChild.type;
let button;
if (type == Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR) {
button = document.createElement("toolbarseparator");
}
else {
button = document.createElement("toolbarbutton");
button.className = "bookmark-item";
button.setAttribute("label", aChild.title);
let icon = aChild.icon;
if (icon)
button.setAttribute("image", icon);
if (PlacesUtils.containerTypes.indexOf(type) != -1) {
button.setAttribute("type", "menu");
button.setAttribute("container", "true");
if (PlacesUtils.nodeIsQuery(aChild)) {
button.setAttribute("query", "true");
if (PlacesUtils.nodeIsTagQuery(aChild))
button.setAttribute("tagContainer", "true");
}
else if (PlacesUtils.nodeIsFolder(aChild)) {
PlacesUtils.livemarks.getLivemark(
{ id: aChild.itemId },
function (aStatus, aLivemark) {
if (Components.isSuccessCode(aStatus)) {
button.setAttribute("livemark", "true");
this.controller.cacheLivemarkInfo(aChild, aLivemark);
}
}.bind(this)
);
}
let popup = document.createElement("menupopup");
popup.setAttribute("placespopup", "true");
button.appendChild(popup);
popup._placesNode = PlacesUtils.asContainer(aChild);
#ifndef XP_MACOSX
popup.setAttribute("context", "placesContext");
#endif
this._domNodes.set(aChild, popup);
}
else if (PlacesUtils.nodeIsURI(aChild)) {
button.setAttribute("scheme",
PlacesUIUtils.guessUrlSchemeForUI(aChild.uri));
}
}
button._placesNode = aChild;
if (!this._domNodes.has(aChild))
this._domNodes.set(aChild, button);
if (aBefore)
this._rootElt.insertBefore(button, aBefore);
else
this._rootElt.appendChild(button);
},
_updateChevronPopupNodesVisibility:
function PT__updateChevronPopupNodesVisibility() {
for (let i = 0, node = this._chevronPopup._startMarker.nextSibling;
node != this._chevronPopup._endMarker;
i++, node = node.nextSibling) {
node.hidden = this._rootElt.childNodes[i].style.visibility != "hidden";
}
},
_onChevronPopupShowing:
function PT__onChevronPopupShowing(aEvent) {
// Handle popupshowing only for the chevron popup, not for nested ones.
if (aEvent.target != this._chevronPopup)
return;
if (!this._chevron._placesView)
this._chevron._placesView = new PlacesMenu(aEvent, this.place);
this._updateChevronPopupNodesVisibility();
},
handleEvent: function PT_handleEvent(aEvent) {
switch (aEvent.type) {
case "unload":
this.uninit();
break;
case "resize":
// This handler updates nodes visibility in both the toolbar
// and the chevron popup when a window resize does not change
// the overflow status of the toolbar.
this.updateChevron();
break;
case "overflow":
if (aEvent.target != aEvent.currentTarget)
return;
// Ignore purely vertical overflows.
if (aEvent.detail == 0)
return;
// Attach the popup binding to the chevron popup if it has not yet
// been initialized.
if (!this._chevronPopup.hasAttribute("type")) {
this._chevronPopup.setAttribute("place", this.place);
this._chevronPopup.setAttribute("type", "places");
}
this._chevron.collapsed = false;
this.updateChevron();
break;
case "underflow":
if (aEvent.target != aEvent.currentTarget)
return;
// Ignore purely vertical underflows.
if (aEvent.detail == 0)
return;
this.updateChevron();
this._chevron.collapsed = true;
break;
case "TabOpen":
case "TabClose":
this.updateChevron();
break;
case "dragstart":
this._onDragStart(aEvent);
break;
case "dragover":
this._onDragOver(aEvent);
break;
case "dragexit":
this._onDragExit(aEvent);
break;
case "dragend":
this._onDragEnd(aEvent);
break;
case "drop":
this._onDrop(aEvent);
break;
case "mouseover":
this._onMouseOver(aEvent);
break;
case "mousemove":
this._onMouseMove(aEvent);
break;
case "mouseout":
this._onMouseOut(aEvent);
break;
case "popupshowing":
this._onPopupShowing(aEvent);
break;
case "popuphidden":
this._onPopupHidden(aEvent);
break;
default:
throw "Trying to handle unexpected event.";
}
},
updateChevron: function PT_updateChevron() {
// If the chevron is collapsed there's nothing to update.
if (this._chevron.collapsed)
return;
// Update the chevron on a timer. This will avoid repeated work when
// lot of changes happen in a small timeframe.
if (this._updateChevronTimer)
this._updateChevronTimer.cancel();
this._updateChevronTimer = this._setTimer(100);
},
_updateChevronTimerCallback: function PT__updateChevronTimerCallback() {
let scrollRect = this._rootElt.getBoundingClientRect();
let childOverflowed = false;
for (let i = 0; i < this._rootElt.childNodes.length; i++) {
let child = this._rootElt.childNodes[i];
// Once a child overflows, all the next ones will.
if (!childOverflowed) {
let childRect = child.getBoundingClientRect();
childOverflowed = this.isRTL ? (childRect.left < scrollRect.left)
: (childRect.right > scrollRect.right);
}
child.style.visibility = childOverflowed ? "hidden" : "visible";
}
// We rebuild the chevron on popupShowing, so if it is open
// we must update it.
if (this._chevron.open)
this._updateChevronPopupNodesVisibility();
},
nodeInserted:
function PT_nodeInserted(aParentPlacesNode, aPlacesNode, aIndex) {
let parentElt = this._getDOMNodeForPlacesNode(aParentPlacesNode);
if (parentElt == this._rootElt) {
let children = this._rootElt.childNodes;
this._insertNewItem(aPlacesNode,
aIndex < children.length ? children[aIndex] : null);
this.updateChevron();
return;
}
PlacesViewBase.prototype.nodeInserted.apply(this, arguments);
},
nodeRemoved:
function PT_nodeRemoved(aParentPlacesNode, aPlacesNode, aIndex) {
let parentElt = this._getDOMNodeForPlacesNode(aParentPlacesNode);
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// Here we need the <menu>.
if (elt.localName == "menupopup")
elt = elt.parentNode;
if (parentElt == this._rootElt) {
this._removeChild(elt);
this.updateChevron();
return;
}
PlacesViewBase.prototype.nodeRemoved.apply(this, arguments);
},
nodeMoved:
function PT_nodeMoved(aPlacesNode,
aOldParentPlacesNode, aOldIndex,
aNewParentPlacesNode, aNewIndex) {
let parentElt = this._getDOMNodeForPlacesNode(aNewParentPlacesNode);
if (parentElt == this._rootElt) {
// Container is on the toolbar.
// Move the element.
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// Here we need the <menu>.
if (elt.localName == "menupopup")
elt = elt.parentNode;
this._removeChild(elt);
this._rootElt.insertBefore(elt, this._rootElt.childNodes[aNewIndex]);
// The chevron view may get nodeMoved after the toolbar. In such a case,
// we should ensure (by manually swapping menuitems) that the actual nodes
// are in the final position before updateChevron tries to updates their
// visibility, or the chevron may go out of sync.
// Luckily updateChevron runs on a timer, so, by the time it updates
// nodes, the menu has already handled the notification.
this.updateChevron();
return;
}
PlacesViewBase.prototype.nodeMoved.apply(this, arguments);
},
nodeAnnotationChanged:
function PT_nodeAnnotationChanged(aPlacesNode, aAnno) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
if (elt == this._rootElt)
return;
// We're notified for the menupopup, not the containing toolbarbutton.
if (elt.localName == "menupopup")
elt = elt.parentNode;
if (elt.parentNode == this._rootElt) {
// Node is on the toolbar.
// All livemarks have a feedURI, so use it as our indicator.
if (aAnno == PlacesUtils.LMANNO_FEEDURI) {
elt.setAttribute("livemark", true);
PlacesUtils.livemarks.getLivemark(
{ id: aPlacesNode.itemId },
function (aStatus, aLivemark) {
if (Components.isSuccessCode(aStatus)) {
this.controller.cacheLivemarkInfo(aPlacesNode, aLivemark);
this.invalidateContainer(aPlacesNode);
}
}.bind(this)
);
}
}
else {
// Node is in a submenu.
PlacesViewBase.prototype.nodeAnnotationChanged.apply(this, arguments);
}
},
nodeTitleChanged: function PT_nodeTitleChanged(aPlacesNode, aNewTitle) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
// There's no UI representation for the root node, thus there's
// nothing to be done when the title changes.
if (elt == this._rootElt)
return;
PlacesViewBase.prototype.nodeTitleChanged.apply(this, arguments);
// Here we need the <menu>.
if (elt.localName == "menupopup")
elt = elt.parentNode;
if (elt.parentNode == this._rootElt) {
// Node is on the toolbar
this.updateChevron();
}
},
invalidateContainer: function PT_invalidateContainer(aPlacesNode) {
let elt = this._getDOMNodeForPlacesNode(aPlacesNode);
if (elt == this._rootElt) {
// Container is the toolbar itself.
this._rebuild();
return;
}
PlacesViewBase.prototype.invalidateContainer.apply(this, arguments);
},
_overFolder: { elt: null,
openTimer: null,
hoverTime: 350,
closeTimer: null },
_clearOverFolder: function PT__clearOverFolder() {
// The mouse is no longer dragging over the stored menubutton.
// Close the menubutton, clear out drag styles, and clear all
// timers for opening/closing it.
if (this._overFolder.elt && this._overFolder.elt.lastChild) {
if (!this._overFolder.elt.lastChild.hasAttribute("dragover")) {
this._overFolder.elt.lastChild.hidePopup();
}
this._overFolder.elt.removeAttribute("dragover");
this._overFolder.elt = null;
}
if (this._overFolder.openTimer) {
this._overFolder.openTimer.cancel();
this._overFolder.openTimer = null;
}
if (this._overFolder.closeTimer) {
this._overFolder.closeTimer.cancel();
this._overFolder.closeTimer = null;
}
},
/**
* This function returns information about where to drop when dragging over
* the toolbar. The returned object has the following properties:
* - ip: the insertion point for the bookmarks service.
* - beforeIndex: child index to drop before, for the drop indicator.
* - folderElt: the folder to drop into, if applicable.
*/
_getDropPoint: function PT__getDropPoint(aEvent) {
let result = this.result;
if (!PlacesUtils.nodeIsFolder(this._resultNode))
return null;
let dropPoint = { ip: null, beforeIndex: null, folderElt: null };
let elt = aEvent.target;
if (elt._placesNode && elt != this._rootElt &&
elt.localName != "menupopup") {
let eltRect = elt.getBoundingClientRect();
let eltIndex = Array.indexOf(this._rootElt.childNodes, elt);
if (PlacesUtils.nodeIsFolder(elt._placesNode) &&
!PlacesUtils.nodeIsReadOnly(elt._placesNode)) {
// This is a folder.
// If we are in the middle of it, drop inside it.
// Otherwise, drop before it, with regards to RTL mode.
let threshold = eltRect.width * 0.25;
if (this.isRTL ? (aEvent.clientX > eltRect.right - threshold)
: (aEvent.clientX < eltRect.left + threshold)) {
// Drop before this folder.
dropPoint.ip =
new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
eltIndex, Ci.nsITreeView.DROP_BEFORE);
dropPoint.beforeIndex = eltIndex;
}
else if (this.isRTL ? (aEvent.clientX > eltRect.left + threshold)
: (aEvent.clientX < eltRect.right - threshold)) {
// Drop inside this folder.
dropPoint.ip =
new InsertionPoint(PlacesUtils.getConcreteItemId(elt._placesNode),
-1, Ci.nsITreeView.DROP_ON,
PlacesUtils.nodeIsTagQuery(elt._placesNode));
dropPoint.beforeIndex = eltIndex;
dropPoint.folderElt = elt;
}
else {
// Drop after this folder.
let beforeIndex =
(eltIndex == this._rootElt.childNodes.length - 1) ?
-1 : eltIndex + 1;
dropPoint.ip =
new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
beforeIndex, Ci.nsITreeView.DROP_BEFORE);
dropPoint.beforeIndex = beforeIndex;
}
}
else {
// This is a non-folder node or a read-only folder.
// Drop before it with regards to RTL mode.
let threshold = eltRect.width * 0.5;
if (this.isRTL ? (aEvent.clientX > eltRect.left + threshold)
: (aEvent.clientX < eltRect.left + threshold)) {
// Drop before this bookmark.
dropPoint.ip =
new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
eltIndex, Ci.nsITreeView.DROP_BEFORE);
dropPoint.beforeIndex = eltIndex;
}
else {
// Drop after this bookmark.
let beforeIndex =
eltIndex == this._rootElt.childNodes.length - 1 ?
-1 : eltIndex + 1;
dropPoint.ip =
new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
beforeIndex, Ci.nsITreeView.DROP_BEFORE);
dropPoint.beforeIndex = beforeIndex;
}
}
}
else {
// We are most likely dragging on the empty area of the
// toolbar, we should drop after the last node.
dropPoint.ip =
new InsertionPoint(PlacesUtils.getConcreteItemId(this._resultNode),
-1, Ci.nsITreeView.DROP_BEFORE);
dropPoint.beforeIndex = -1;
}
return dropPoint;
},
_setTimer: function PT_setTimer(aTime) {
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.initWithCallback(this, aTime, timer.TYPE_ONE_SHOT);
return timer;
},
notify: function PT_notify(aTimer) {
if (aTimer == this._updateChevronTimer) {
this._updateChevronTimer = null;
this._updateChevronTimerCallback();
}
// * Timer to turn off indicator bar.
else if (aTimer == this._ibTimer) {
this._dropIndicator.collapsed = true;
this._ibTimer = null;
}
// * Timer to open a menubutton that's being dragged over.
else if (aTimer == this._overFolder.openTimer) {
// Set the autoopen attribute on the folder's menupopup so that
// the menu will automatically close when the mouse drags off of it.
this._overFolder.elt.lastChild.setAttribute("autoopened", "true");
this._overFolder.elt.open = true;
this._overFolder.openTimer = null;
}
// * Timer to close a menubutton that's been dragged off of.
else if (aTimer == this._overFolder.closeTimer) {
// Close the menubutton if we are not dragging over it or one of
// its children. The autoopened attribute will let the menu know to
// close later if the menu is still being dragged over.
let currentPlacesNode = PlacesControllerDragHelper.currentDropTarget;
let inHierarchy = false;
while (currentPlacesNode) {
if (currentPlacesNode == this._rootElt) {
inHierarchy = true;
break;
}
currentPlacesNode = currentPlacesNode.parentNode;
}
// The _clearOverFolder() function will close the menu for
// _overFolder.elt. So null it out if we don't want to close it.
if (inHierarchy)
this._overFolder.elt = null;
// Clear out the folder and all associated timers.
this._clearOverFolder();
}
},
_onMouseOver: function PT__onMouseOver(aEvent) {
let button = aEvent.target;
if (button.parentNode == this._rootElt && button._placesNode &&
PlacesUtils.nodeIsURI(button._placesNode))
window.XULBrowserWindow.setOverLink(aEvent.target._placesNode.uri, null);
},
_onMouseOut: function PT__onMouseOut(aEvent) {
window.XULBrowserWindow.setOverLink("", null);
},
_cleanupDragDetails: function PT__cleanupDragDetails() {
// Called on dragend and drop.
PlacesControllerDragHelper.currentDropTarget = null;
this._draggedElt = null;
if (this._ibTimer)
this._ibTimer.cancel();
this._dropIndicator.collapsed = true;
},
_onDragStart: function PT__onDragStart(aEvent) {
// Sub menus have their own d&d handlers.
let draggedElt = aEvent.target;
if (draggedElt.parentNode != this._rootElt || !draggedElt._placesNode)
return;
if (draggedElt.localName == "toolbarbutton" &&
draggedElt.getAttribute("type") == "menu") {
// If the drag gesture on a container is toward down we open instead
// of dragging.
let translateY = this._cachedMouseMoveEvent.clientY - aEvent.clientY;
let translateX = this._cachedMouseMoveEvent.clientX - aEvent.clientX;
if ((translateY) >= Math.abs(translateX/2)) {
// Don't start the drag.
aEvent.preventDefault();
// Open the menu.
draggedElt.open = true;
return;
}
// If the menu is open, close it.
if (draggedElt.open) {
draggedElt.lastChild.hidePopup();
draggedElt.open = false;
}
}
// Activate the view and cache the dragged element.
this._draggedElt = draggedElt._placesNode;
this._rootElt.focus();
this._controller.setDataTransfer(aEvent);
aEvent.stopPropagation();
},
_onDragOver: function PT__onDragOver(aEvent) {
// Cache the dataTransfer
PlacesControllerDragHelper.currentDropTarget = aEvent.target;
let dt = aEvent.dataTransfer;
let dropPoint = this._getDropPoint(aEvent);
if (!dropPoint || !dropPoint.ip ||
!PlacesControllerDragHelper.canDrop(dropPoint.ip, dt)) {
this._dropIndicator.collapsed = true;
aEvent.stopPropagation();
return;
}
if (this._ibTimer) {
this._ibTimer.cancel();
this._ibTimer = null;
}
if (dropPoint.folderElt || aEvent.originalTarget == this._chevron) {
// Dropping over a menubutton or chevron button.
// Set styles and timer to open relative menupopup.
let overElt = dropPoint.folderElt || this._chevron;
if (this._overFolder.elt != overElt) {
this._clearOverFolder();
this._overFolder.elt = overElt;
this._overFolder.openTimer = this._setTimer(this._overFolder.hoverTime);
}
if (!this._overFolder.elt.hasAttribute("dragover"))
this._overFolder.elt.setAttribute("dragover", "true");
this._dropIndicator.collapsed = true;
}
else {
// Dragging over a normal toolbarbutton,
// show indicator bar and move it to the appropriate drop point.
let ind = this._dropIndicator;
let halfInd = ind.clientWidth / 2;
let translateX;
if (this.isRTL) {
halfInd = Math.ceil(halfInd);
translateX = 0 - this._rootElt.getBoundingClientRect().right - halfInd;
if (this._rootElt.firstChild) {
if (dropPoint.beforeIndex == -1)
translateX += this._rootElt.lastChild.getBoundingClientRect().left;
else {
translateX += this._rootElt.childNodes[dropPoint.beforeIndex]
.getBoundingClientRect().right;
}
}
}
else {
halfInd = Math.floor(halfInd);
translateX = 0 - this._rootElt.getBoundingClientRect().left +
halfInd;
if (this._rootElt.firstChild) {
if (dropPoint.beforeIndex == -1)
translateX += this._rootElt.lastChild.getBoundingClientRect().right;
else {
translateX += this._rootElt.childNodes[dropPoint.beforeIndex]
.getBoundingClientRect().left;
}
}
}
ind.style.transform = "translate(" + Math.round(translateX) + "px)";
ind.style.MozMarginStart = (-ind.clientWidth) + "px";
ind.collapsed = false;
// Clear out old folder information.
this._clearOverFolder();
}
aEvent.preventDefault();
aEvent.stopPropagation();
},
_onDrop: function PT__onDrop(aEvent) {
PlacesControllerDragHelper.currentDropTarget = aEvent.target;
let dropPoint = this._getDropPoint(aEvent);
if (dropPoint && dropPoint.ip) {
PlacesControllerDragHelper.onDrop(dropPoint.ip, aEvent.dataTransfer)
aEvent.preventDefault();
}
this._cleanupDragDetails();
aEvent.stopPropagation();
},
_onDragExit: function PT__onDragExit(aEvent) {
PlacesControllerDragHelper.currentDropTarget = null;
// Set timer to turn off indicator bar (if we turn it off
// here, dragenter might be called immediately after, creating
// flicker).
if (this._ibTimer)
this._ibTimer.cancel();
this._ibTimer = this._setTimer(10);
// If we hovered over a folder, close it now.
if (this._overFolder.elt)
this._overFolder.closeTimer = this._setTimer(this._overFolder.hoverTime);
},
_onDragEnd: function PT_onDragEnd(aEvent) {
this._cleanupDragDetails();
},
_onPopupShowing: function PT__onPopupShowing(aEvent) {
if (!this._allowPopupShowing) {
this._allowPopupShowing = true;
aEvent.preventDefault();
return;
}
let parent = aEvent.target.parentNode;
if (parent.localName == "toolbarbutton")
this._openedMenuButton = parent;
PlacesViewBase.prototype._onPopupShowing.apply(this, arguments);
},
_onPopupHidden: function PT__onPopupHidden(aEvent) {
let popup = aEvent.target;
let placesNode = popup._placesNode;
// Avoid handling popuphidden of inner views
if (placesNode && PlacesUIUtils.getViewForNode(popup) == this) {
// UI performance: folder queries are cheap, keep the resultnode open
// so we don't rebuild its contents whenever the popup is reopened.
// Though, we want to always close feed containers so their expiration
// status will be checked at next opening.
if (!PlacesUtils.nodeIsFolder(placesNode) ||
this.controller.hasCachedLivemarkInfo(placesNode)) {
placesNode.containerOpen = false;
}
}
let parent = popup.parentNode;
if (parent.localName == "toolbarbutton") {
this._openedMenuButton = null;
// Clear the dragover attribute if present, if we are dragging into a
// folder in the hierachy of current opened popup we don't clear
// this attribute on clearOverFolder. See Notify for closeTimer.
if (parent.hasAttribute("dragover"))
parent.removeAttribute("dragover");
}
},
_onMouseMove: function PT__onMouseMove(aEvent) {
// Used in dragStart to prevent dragging folders when dragging down.
this._cachedMouseMoveEvent = aEvent;
if (this._openedMenuButton == null ||
PlacesControllerDragHelper.getSession())
return;
let target = aEvent.originalTarget;
if (this._openedMenuButton != target &&
target.localName == "toolbarbutton" &&
target.type == "menu") {
this._openedMenuButton.open = false;
target.open = true;
}
}
};
/**
* View for Places menus. This object should be created during the first
* popupshowing that's dispatched on the menu.
*/
function PlacesMenu(aPopupShowingEvent, aPlace) {
this._rootElt = aPopupShowingEvent.target; // <menupopup>
this._viewElt = this._rootElt.parentNode; // <menu>
this._viewElt._placesView = this;
this._addEventListeners(this._rootElt, ["popupshowing", "popuphidden"], true);
this._addEventListeners(window, ["unload"], false);
#ifdef XP_MACOSX
// Must walk up to support views in sub-menus, like Bookmarks Toolbar menu.
for (let elt = this._viewElt.parentNode; elt; elt = elt.parentNode) {
if (elt.localName == "menubar") {
this._nativeView = true;
break;
}
}
#endif
PlacesViewBase.call(this, aPlace);
this._onPopupShowing(aPopupShowingEvent);
}
PlacesMenu.prototype = {
__proto__: PlacesViewBase.prototype,
QueryInterface: function PM_QueryInterface(aIID) {
if (aIID.equals(Ci.nsIDOMEventListener))
return this;
return PlacesViewBase.prototype.QueryInterface.apply(this, arguments);
},
_removeChild: function PM_removeChild(aChild) {
PlacesViewBase.prototype._removeChild.apply(this, arguments);
},
uninit: function PM_uninit() {
this._removeEventListeners(this._rootElt, ["popupshowing", "popuphidden"],
true);
this._removeEventListeners(window, ["unload"], false);
PlacesViewBase.prototype.uninit.apply(this, arguments);
},
handleEvent: function PM_handleEvent(aEvent) {
switch (aEvent.type) {
case "unload":
this.uninit();
break;
case "popupshowing":
this._onPopupShowing(aEvent);
break;
case "popuphidden":
this._onPopupHidden(aEvent);
break;
}
},
_onPopupHidden: function PM__onPopupHidden(aEvent) {
// Avoid handling popuphidden of inner views.
let popup = aEvent.originalTarget;
let placesNode = popup._placesNode;
if (!placesNode || PlacesUIUtils.getViewForNode(popup) != this)
return;
// UI performance: folder queries are cheap, keep the resultnode open
// so we don't rebuild its contents whenever the popup is reopened.
// Though, we want to always close feed containers so their expiration
// status will be checked at next opening.
if (!PlacesUtils.nodeIsFolder(placesNode) ||
this.controller.hasCachedLivemarkInfo(placesNode))
placesNode.containerOpen = false;
// The autoopened attribute is set for folders which have been
// automatically opened when dragged over. Turn off this attribute
// when the folder closes because it is no longer applicable.
popup.removeAttribute("autoopened");
popup.removeAttribute("dragstart");
}
};
|