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
|
/* 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/. */
/*
* This file provides the global context for the faceting environment. In the
* Model View Controller (paradigm), we are the view and the XBL widgets are
* the the view and controller.
*
* Because much of the work related to faceting is not UI-specific, we try and
* push as much of it into mailnews/db/gloda/facet.js. In some cases we may
* get it wrong and it may eventually want to migrate.
*/
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cr = Components.results;
var Cu = Components.utils;
Cu.import("resource:///modules/gloda/log4moz.js");
Cu.import("resource:///modules/StringBundle.js");
Cu.import("resource://gre/modules/PluralForm.jsm");
Cu.import("resource:///modules/errUtils.js");
Cu.import("resource:///modules/templateUtils.js");
Cu.import("resource:///modules/gloda/public.js");
Cu.import("resource:///modules/gloda/facet.js");
var glodaFacetStrings =
new StringBundle("chrome://messenger/locale/glodaFacetView.properties");
/**
* Represents the active constraints on a singular facet. Singular facets can
* only have an inclusive set or an exclusive set, but not both. Non-singular
* facets can have both. Because they are different worlds, non-singular gets
* its own class, |ActiveNonSingularConstraint|.
*/
function ActiveSingularConstraint(aFaceter, aRanged) {
this.faceter = aFaceter;
this.attrDef = aFaceter.attrDef;
this.facetDef = aFaceter.facetDef;
this.ranged = Boolean(aRanged);
this.clear();
}
ActiveSingularConstraint.prototype = {
_makeQuery: function() {
// have the faceter make the query and the invert decision for us if it
// implements the makeQuery method.
if ("makeQuery" in this.faceter) {
[this.query, this.invertQuery] = this.faceter.makeQuery(this.groupValues,
this.inclusive);
return;
}
let query = this.query = Gloda.newQuery(Gloda.NOUN_MESSAGE);
let constraintFunc;
// If the facet definition references a queryHelper defined by the noun
// type, use that instead of the standard constraint function.
if ("queryHelper" in this.facetDef)
constraintFunc = query[this.attrDef.boundName +
this.facetDef.queryHelper];
else
constraintFunc = query[this.ranged ? (this.attrDef.boundName + "Range")
: this.attrDef.boundName];
constraintFunc.apply(query, this.groupValues);
this.invertQuery = !this.inclusive;
},
/**
* Adjust the constraint given the incoming faceting constraint desired.
* Mainly, if the inclusive flag is the same as what we already have, we
* just append the new values to the existing set of values. If it is not
* the same, we replace them.
*
* @return true if the caller needs to revalidate their understanding of the
* constraint because we have flipped whether we are inclusive or
* exclusive and have thrown away some constraints as a result.
*/
constrain: function(aInclusive, aGroupValues) {
if (aInclusive == this.inclusive) {
this.groupValues = this.groupValues.concat(aGroupValues);
this._makeQuery();
return false;
}
let needToRevalidate = (this.inclusive != null);
this.inclusive = aInclusive;
this.groupValues = aGroupValues;
this._makeQuery();
return needToRevalidate;
},
/**
* Relax something we previously constrained. Remove it, some might say. It
* is possible after relaxing that we will no longer be an active constraint.
*
* @return true if we are no longer constrained at all.
*/
relax: function(aInclusive, aGroupValues) {
if (aInclusive != this.inclusive)
throw new Error("You can't relax a constraint that isn't possible.");
for (let groupValue of aGroupValues) {
let index = this.groupValues.indexOf(groupValue);
if (index == -1)
throw new Error("Tried to relax a constraint that was not in force.");
this.groupValues.splice(index, 1);
}
if (this.groupValues.length == 0) {
this.clear();
return true;
}
this._makeQuery();
return false;
},
/**
* Indicate whether this constraint is actually doing anything anymore.
*/
get isConstrained() {
return this.inclusive != null;
},
/**
* Clear the constraint so that the next call to adjust initializes it.
*/
clear: function() {
this.inclusive = null;
this.groupValues = null;
this.query = null;
this.invertQuery = null;
},
/**
* Filter the items against our constraint.
*/
sieve: function(aItems) {
let query = this.query;
let expectedResult = !this.invertQuery;
return aItems.filter(item => query.test(item) == expectedResult);
},
isIncludedGroup: function(aGroupValue) {
if (!this.inclusive)
return false;
return this.groupValues.contains(aGroupValue);
},
isExcludedGroup: function(aGroupValue) {
if (this.inclusive)
return false;
return this.groupValues.contains(aGroupValue);
}
};
function ActiveNonSingularConstraint(aFaceter, aRanged) {
this.faceter = aFaceter;
this.attrDef = aFaceter.attrDef;
this.facetDef = aFaceter.facetDef;
this.ranged = Boolean(aRanged);
this.clear();
}
ActiveNonSingularConstraint.prototype = {
_makeQuery: function(aInclusive, aGroupValues) {
// have the faceter make the query and the invert decision for us if it
// implements the makeQuery method.
if ("makeQuery" in this.faceter) {
// returns [query, invertQuery] directly
return this.faceter.makeQuery(aGroupValues, aInclusive);
}
let query = Gloda.newQuery(Gloda.NOUN_MESSAGE);
let constraintFunc;
// If the facet definition references a queryHelper defined by the noun
// type, use that instead of the standard constraint function.
if ("queryHelper" in this.facetDef)
constraintFunc = query[this.attrDef.boundName +
this.facetDef.queryHelper];
else
constraintFunc = query[this.ranged ? (this.attrDef.boundName + "Range")
: this.attrDef.boundName];
constraintFunc.apply(query, aGroupValues);
return [query, false];
},
/**
* Adjust the constraint given the incoming faceting constraint desired.
* Mainly, if the inclusive flag is the same as what we already have, we
* just append the new values to the existing set of values. If it is not
* the same, we replace them.
*/
constrain: function(aInclusive, aGroupValues) {
let groupIdAttr = this.attrDef.objectNounDef.isPrimitive ? null
: this.facetDef.groupIdAttr;
let idMap = aInclusive ? this.includedGroupIds
: this.excludedGroupIds;
let valList = aInclusive ? this.includedGroupValues
: this.excludedGroupValues;
for (let groupValue of aGroupValues) {
let valId = (groupIdAttr !== null && groupValue != null) ?
groupValue[groupIdAttr] : groupValue;
idMap[valId] = true;
valList.push(groupValue);
}
let [query, invertQuery] = this._makeQuery(aInclusive, valList);
if (aInclusive && !invertQuery)
this.includeQuery = query;
else
this.excludeQuery = query;
return false;
},
/**
* Relax something we previously constrained. Remove it, some might say. It
* is possible after relaxing that we will no longer be an active constraint.
*
* @return true if we are no longer constrained at all.
*/
relax: function(aInclusive, aGroupValues) {
let groupIdAttr = this.attrDef.objectNounDef.isPrimitive ? null
: this.facetDef.groupIdAttr;
let idMap = aInclusive ? this.includedGroupIds
: this.excludedGroupIds;
let valList = aInclusive ? this.includedGroupValues
: this.excludedGroupValues;
for (let groupValue of aGroupValues) {
let valId = (groupIdAttr !== null && groupValue != null) ?
groupValue[groupIdAttr] : groupValue;
if (!(valId in idMap))
throw new Error("Tried to relax a constraint that was not in force.");
delete idMap[valId];
let index = valList.indexOf(groupValue);
valList.splice(index, 1);
}
if (valList.length == 0) {
if (aInclusive)
this.includeQuery = null;
else
this.excludeQuery = null;
}
else {
let [query, invertQuery] = this._makeQuery(aInclusive, valList);
if (aInclusive && !invertQuery)
this.includeQuery = query;
else
this.excludeQuery = query;
}
return this.includeQuery == null && this.excludeQuery == null;
},
/**
* Indicate whether this constraint is actually doing anything anymore.
*/
get isConstrained() {
return this.includeQuery == null && this.excludeQuery == null;
},
/**
* Clear the constraint so that the next call to adjust initializes it.
*/
clear: function() {
this.includeQuery = null;
this.includedGroupIds = {};
this.includedGroupValues = [];
this.excludeQuery = null;
this.excludedGroupIds = {};
this.excludedGroupValues = [];
},
/**
* Filter the items against our constraint.
*/
sieve: function(aItems) {
let includeQuery = this.includeQuery;
let excludeQuery = this.excludeQuery;
return aItems.filter(item => (!includeQuery || includeQuery.test(item)) &&
(!excludeQuery || !excludeQuery.test(item)));
},
isIncludedGroup: function(aGroupValue) {
let valId = aGroupValue[this.facetDef.groupIdAttr];
return (valId in this.includedGroupIds);
},
isExcludedGroup: function(aGroupValue) {
let valId = aGroupValue[this.facetDef.groupIdAttr];
return (valId in this.excludedGroupIds);
}
};
var FacetContext = {
facetDriver: new FacetDriver(Gloda.lookupNounDef("message"), window),
/**
* The root collection which our active set is a subset of. We hold onto this
* for garbage collection reasons, although the tab that owns us should also
* be holding on.
*/
_collection: null,
set collection(aCollection) {
this._collection = aCollection;
},
get collection() {
return this._collection;
},
_sortBy: null,
get sortBy() {
return this._sortBy;
},
set sortBy(val) {
try {
if (val == this._sortBy)
return;
this._sortBy = val;
this.build(this._sieveAll());
} catch (e) {
logException(e);
}
},
/**
* List of the current working set
*/
_activeSet: null,
get activeSet() {
return this._activeSet;
},
/**
* fullSet is a special attribute which is passed a set of items that we're
* displaying, but the order of which is determined by the sortBy property.
* On setting the fullSet, we compute both sorted lists, and then on getting,
* we return the appropriate one.
*/
get fullSet() {
return (this._sortBy == '-dascore' ?
this._relevantSortedItems :
this._dateSortedItems);
},
set fullSet(items) {
let scores;
if (this.searcher && this.searcher.scores)
scores = this.searcher.scores;
else
scores = Gloda.scoreNounItems(items);
let scoredItems = items.map(function(item, index) { return [scores[index], item]; });
scoredItems.sort((a, b) => b[0]-a[0]);
this._relevantSortedItems = scoredItems.map(scoredItem => scoredItem[1]);
this._dateSortedItems =
this._relevantSortedItems.concat().sort((a, b) => b.date-a.date);
},
initialBuild: function() {
let queryExplanation = document.getElementById("query-explanation");
if (this.searcher)
queryExplanation.setFulltext(this.searcher);
else
queryExplanation.setQuery(this.collection.query);
// we like to sort them so should clone the list
this.faceters = this.facetDriver.faceters.concat();
this._timelineShown = ! Application.prefs.getValue('gloda.facetview.hidetimeline', true);
this.everFaceted = false;
this._activeConstraints = {};
if (this.searcher)
this._sortBy = '-dascore';
else
this._sortBy = '-date';
this.fullSet = this._removeDupes(this._collection.items.concat());
if ("IMCollection" in this)
this.fullSet = this.fullSet.concat(this.IMCollection.items);
this.build(this.fullSet);
},
/**
* Remove duplicate messages from search results.
*
* @param aItems the initial set of messages to deduplicate
* @return the subset of those, with duplicates removed.
*
* Some IMAP servers (here's looking at you, Gmail) will create message
* duplicates unbeknownst to the user. We'd like to deal with them earlier
* in the pipeline, but that's a bit hard right now. So as a workaround
* we'd rather not show them in the Search Results UI. The simplest way
* of doing that is just to cull (from the display) messages with have the
* Message-ID of a message already displayed.
*/
_removeDupes: function(aItems) {
let deduped = [];
let msgIdsSeen = {};
for (let item of aItems) {
if (item.headerMessageID in msgIdsSeen)
continue;
deduped.push(item);
msgIdsSeen[item.headerMessageID] = true;
}
return deduped;
},
/**
* Kick-off a new faceting pass.
*
* @param aNewSet the set of items to facet.
* @param aCallback the callback to invoke when faceting is completed.
*/
build: function(aNewSet, aCallback) {
this._activeSet = aNewSet;
this._callbackOnFacetComplete = aCallback;
this.facetDriver.go(this._activeSet, this.facetingCompleted, this);
},
/**
* Attempt to figure out a reasonable number of rows to limit each facet to
* display. While the number will ordinarily be dominated by the maximum
* number of rows we believe the user can easily scan, this may also be
* impacted by layout concerns (since we want to avoid scrolling).
*/
planLayout: function() {
// XXX arbitrary!
this.maxDisplayRows = 8;
this.maxMessagesToShow = 10;
},
/**
* Clean up the UI in preparation for a new query to come in.
*/
_resetUI: function() {
for (let faceter of this.faceters) {
if (faceter.xblNode && !faceter.xblNode.explicit)
faceter.xblNode.remove();
faceter.xblNode = null;
faceter.constraint = null;
}
},
_groupCountComparator: function(a, b) {
return b.groupCount - a.groupCount;
},
/**
* Tells the UI about all the facets when notified by the |facetDriver| when
* it is done faceting everything.
*/
facetingCompleted: function() {
this.planLayout();
let uiFacets = document.getElementById("facets");
if (!this.everFaceted) {
this.everFaceted = true;
this.faceters.sort(this._groupCountComparator);
for (let faceter of this.faceters) {
let attrName = faceter.attrDef.attributeName;
let explicitBinding = document.getElementById("facet-" + attrName);
if (explicitBinding) {
explicitBinding.explicit = true;
explicitBinding.faceter = faceter;
explicitBinding.attrDef = faceter.attrDef;
explicitBinding.facetDef = faceter.facetDef;
explicitBinding.nounDef = faceter.attrDef.objectNounDef;
explicitBinding.orderedGroups = faceter.orderedGroups;
// explicit booleans should always be displayed for consistency
if (faceter.groupCount >= 1 ||
(explicitBinding.getAttribute("type").includes("boolean"))) {
try {
explicitBinding.build(true);
} catch (e) {
logObject(explicitBinding);
logException(e);
}
explicitBinding.removeAttribute("uninitialized");
}
faceter.xblNode = explicitBinding;
continue;
}
// ignore facets that do not vary!
if (faceter.groupCount <= 1) {
faceter.xblNode = null;
continue;
}
faceter.xblNode = uiFacets.addFacet(faceter.type, faceter.attrDef, {
faceter: faceter,
facetDef: faceter.facetDef,
orderedGroups: faceter.orderedGroups,
maxDisplayRows: this.maxDisplayRows,
explicit: false
});
}
}
else {
for (let faceter of this.faceters) {
// Do not bother with un-displayed facets, or that are locked by a
// constraint. But do bother if the widget can be updated without
// losing important data.
if (!faceter.xblNode ||
(faceter.constraint && !faceter.xblNode.canUpdate))
continue;
// hide things that have 0/1 groups now and are not constrained and not
// explicit
if (faceter.groupCount <= 1 && !faceter.constraint &&
(!faceter.xblNode.explicit || faceter.type == "date"))
faceter.xblNode.style.display = "none";
// otherwise, update
else {
faceter.xblNode.orderedGroups = faceter.orderedGroups;
faceter.xblNode.build(false);
faceter.xblNode.style.display = "block";
}
}
}
if (! this._timelineShown)
this._hideTimeline(true);
this._showResults();
if (this._callbackOnFacetComplete) {
let callback = this._callbackOnFacetComplete;
this._callbackOnFacetComplete = null;
callback();
}
},
_showResults: function()
{
let results = document.getElementById("results");
let numMessageToShow = Math.min(this.maxMessagesToShow * this._numPages,
this._activeSet.length);
results.setMessages(this._activeSet.slice(0, numMessageToShow));
let showLoading = document.getElementById("showLoading");
showLoading.style.display = "none"; /* hide spinner, we're done thinking */
let showEmpty = document.getElementById("showEmpty");
let dateToggle = document.getElementById("date-toggle");
/* check for no messages at all */
if (this._activeSet.length == 0) {
showEmpty.style.display = "block";
dateToggle.style.display = "none";
}
else {
showEmpty.style.display = "none";
dateToggle.style.display = "block";
}
let showMore = document.getElementById("showMore");
if (this._activeSet.length > numMessageToShow)
showMore.style.display = "block";
else
showMore.style.display = "none";
},
showMore: function() {
this._numPages += 1;
this._showResults();
let results = document.getElementById("results");
},
zoomOut: function() {
let facetDate = document.getElementById('facet-date');
this.removeFacetConstraint(facetDate.faceter, true, facetDate.vis.constraints)
facetDate.setAttribute("zoomedout", "true");
},
toggleTimeline: function() {
try {
this._timelineShown = ! this._timelineShown;
if (this._timelineShown)
this._showTimeline();
else
this._hideTimeline(false);
} catch (e) {
logException(e);
}
},
_showTimeline: function() {
let facetDate = document.getElementById("facet-date");
if (facetDate.style.display == "none") {
facetDate.style.display = "inherit";
// Force binding attachment so the transition to the
// visible state actually happens.
facetDate.getBoundingClientRect();
}
let listener = () => {
// Need to set overflow to visible so that the zoom button
// is not cut off at the top, and overflow=hidden causes
// the transition to not work as intended.
facetDate.removeAttribute("style");
facetDate.removeEventListener("transitionend", listener);
};
facetDate.addEventListener("transitionend", listener);
facetDate.removeAttribute("hide");
document.getElementById("date-toggle").removeAttribute("tucked");
Application.prefs.setValue('gloda.facetview.hidetimeline', false);
},
_hideTimeline: function(immediate) {
let facetDate = document.getElementById("facet-date");
if (immediate)
facetDate.style.display = "none";
facetDate.style.overflow = "hidden";
facetDate.setAttribute("hide", "true");
document.getElementById("date-toggle").setAttribute("tucked", "true");
Application.prefs.setValue('gloda.facetview.hidetimeline', true);
},
_timelineShown: true,
/** For use in hovering specific results. */
fakeResultFaceter: {},
/** For use in hovering specific results. */
fakeResultAttr: {},
_numPages: 1,
_HOVER_STABILITY_DURATION_MS: 100,
_brushedFacet: null,
_brushedGroup: null,
_brushedItems: null,
_brushTimeout: null,
hoverFacet: function(aFaceter, aAttrDef, aGroupValue, aGroupItems) {
// bail if we are already brushing this item
if (this._brushedFacet == aFaceter && this._brushedGroup == aGroupValue)
return;
this._brushedFacet = aFaceter;
this._brushedGroup = aGroupValue;
this._brushedItems = aGroupItems;
if (this._brushTimeout != null)
clearTimeout(this._brushTimeout);
this._brushTimeout = setTimeout(this._timeoutHoverWrapper,
this._HOVER_STABILITY_DURATION_MS, this);
},
_timeoutHover: function() {
this._brushTimeout = null;
for (let faceter of this.faceters) {
if (faceter == this._brushedFacet || !faceter.xblNode)
continue;
if (this._brushedItems != null)
faceter.xblNode.brushItems(this._brushedItems);
else
faceter.xblNode.clearBrushedItems();
}
},
_timeoutHoverWrapper: function(aThis) {
aThis._timeoutHover();
},
unhoverFacet: function(aFaceter, aAttrDef, aGroupValue, aGroupItems) {
// have we already brushed from some other source already? ignore then.
if (this._brushedFacet != aFaceter || this._brushedGroup != aGroupValue)
return;
// reuse hover facet to null everyone out
this.hoverFacet(null, null, null, null);
},
/**
* Maps attribute names to their corresponding |ActiveConstraint|, if they
* have one.
*/
_activeConstraints: null,
/**
* Called by facet bindings when the user does some clicking and wants to
* impose a new constraint.
*
* @param aFaceter The faceter that is the source of this constraint. We
* need to know this because once a facet has a constraint attached,
* the UI stops updating it.
* @param {Boolean} aInclusive Is this an inclusive (true) or exclusive
* (false) constraint? The constraint instance is the one that deals with
* the nuances resulting from this.
* @param aGroupValues A list of the group values this constraint covers. In
* general, we expect that only one group value will be present in the
* list since this method should get called each time the user clicks
* something. Previously, we provided support for an "other" case which
* covered multiple groupValues so a single click needed to be able to
* pass in a list. The "other" case is gone now, but semantically it's
* okay for us to support a list.
* @param [aRanged] Is it a ranged constraint? (Currently only for dates)
* @param [aNukeExisting] Do we need to replace the existing constraint and
* re-sieve everything? This currently only happens for dates, where
* our display allows a click to actually make our range more generic
* than it currently is. (But this only matters if we already have
* a date constraint applied.)
* @param [aCallback] The callback to call once (re-)faceting has completed.
*
* @return true if the caller needs to revalidate because the constraint has
* changed in a way other than explicitly requested. This can occur if
* a singular constraint flips its inclusive state and throws away
* constraints.
*/
addFacetConstraint: function(aFaceter, aInclusive, aGroupValues,
aRanged, aNukeExisting, aCallback) {
let attrName = aFaceter.attrDef.attributeName;
let constraint;
let needToSieveAll = false;
if (attrName in this._activeConstraints) {
constraint = this._activeConstraints[attrName];
needToSieveAll = true;
if (aNukeExisting)
constraint.clear();
}
else {
let constraintClass = aFaceter.attrDef.singular ? ActiveSingularConstraint
: ActiveNonSingularConstraint;
constraint = this._activeConstraints[attrName] =
new constraintClass(aFaceter, aRanged);
aFaceter.constraint = constraint;
}
let needToRevalidate = constraint.constrain(aInclusive, aGroupValues);
// Given our current implementation, we can only be further constraining our
// active set, so we can just sieve the existing active set with the
// (potentially updated) constraint. In some cases, it would be much
// cheaper to use the facet's knowledge about the items in the groups, but
// for now let's keep a single code-path for how we refine the active set.
this.build(needToSieveAll ? this._sieveAll()
: constraint.sieve(this.activeSet),
aCallback);
return needToRevalidate;
},
/**
* Remove a constraint previously imposed by addFacetConstraint. The
* constraint must still be active, which means you need to pay attention
* when |addFacetConstraint| returns true indicating that you need to
* revalidate.
*
* @param aFaceter
* @param aInclusive Whether the group values were previously included /
* excluded. If you want to remove some values that were included and
* some that were excluded then you need to call us once for each case.
* @param aGroupValues The list of group values to remove.
* @param aCallback The callback to call once all facets have been updated.
*
* @return true if the constraint has been completely removed. Under the
* current regime, this will likely cause the binding that is calling us
* to be rebuilt, so be aware if you are trying to do any cool animation
* that might no longer make sense.
*/
removeFacetConstraint: function(aFaceter, aInclusive, aGroupValues,
aCallback) {
let attrName = aFaceter.attrDef.attributeName;
let constraint = this._activeConstraints[attrName];
let constraintGone = false;
if (constraint.relax(aInclusive, aGroupValues)) {
delete this._activeConstraints[attrName];
aFaceter.constraint = null;
constraintGone = true;
}
// we definitely need to re-sieve everybody in this case...
this.build(this._sieveAll(), aCallback);
return constraintGone;
},
/**
* Sieve the items from the underlying collection against all constraints,
* returning the value.
*/
_sieveAll: function() {
let items = this.fullSet;
for (let elem in this._activeConstraints) {
items = this._activeConstraints[elem].sieve(items);
}
return items;
},
toggleFulltextCriteria: function() {
this.tab.searcher.andTerms = !this.tab.searcher.andTerms;
this._resetUI();
this.collection = this.tab.searcher.getCollection(this);
},
/**
* Show the active message set in a glodaList tab.
*/
showActiveSetInTab: function() {
let tabmail = this.rootWin.document.getElementById("tabmail");
tabmail.openTab("glodaList", {
collection: Gloda.explicitCollection(Gloda.NOUN_MESSAGE, this.activeSet),
title: this.tab.title
});
},
/**
* Show the conversation in a new glodaList tab.
*
* @param {glodaFacetBindings.xml#result-message} aResultMessage The
* result the user wants to see in more details.
* @param {Boolean} [aBackground] Whether it should be in the background.
*/
showConversationInTab: function(aResultMessage, aBackground) {
let tabmail = this.rootWin.document.getElementById("tabmail");
let message = aResultMessage.message;
if ("IMCollection" in this &&
message instanceof Gloda.lookupNounDef("im-conversation").clazz) {
tabmail.openTab("chat", {
convType: "log",
conv: message,
searchTerm: aResultMessage.firstMatchText,
background: aBackground
});
return;
}
tabmail.openTab("glodaList", {
conversation: message.conversation,
message: message,
title: message.conversation.subject,
background: aBackground
});
},
/**
* Show the message in a new tab.
*
* @param {GlodaMessage} aMessage The message to show.
* @param {Boolean} [aBackground] Whether it should be in the background.
*/
showMessageInTab: function(aMessage, aBackground) {
let tabmail = this.rootWin.document.getElementById("tabmail");
let msgHdr = aMessage.folderMessage;
if (!msgHdr)
throw new Error("Unable to translate gloda message to message header.");
tabmail.openTab("message", {
msgHdr: msgHdr,
background: aBackground
});
},
onItemsAdded: function(aItems, aCollection) {
},
onItemsModified: function(aItems, aCollection) {
},
onItemsRemoved: function(aItems, aCollection) {
},
onQueryCompleted: function(aCollection) {
if (this.tab.query.completed &&
(!("IMQuery" in this.tab) || this.tab.IMQuery.completed))
this.initialBuild();
}
};
/**
* addEventListener betrayals compel us to establish our link with the
* outside world from inside. NeilAway suggests the problem might have
* been the registration of the listener prior to initiating the load. Which
* is odd considering it works for the XUL case, but I could see how that might
* differ. Anywho, this works for now and is a delightful reference to boot.
*/
function reachOutAndTouchFrame() {
let us = window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem);
FacetContext.rootWin = us.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
let parentWin = us.parent
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
let aTab = FacetContext.tab = parentWin.tab;
parentWin.tab = null;
window.addEventListener("resize", function() {
document.getElementById("facet-date").build(true);
});
// we need to hook the context up as a listener in all cases since
// removal notifications are required.
if ("searcher" in aTab) {
FacetContext.searcher = aTab.searcher;
aTab.searcher.listener = FacetContext;
if ("IMSearcher" in aTab) {
FacetContext.IMSearcher = aTab.IMSearcher;
aTab.IMSearcher.listener = FacetContext;
}
}
else {
FacetContext.searcher = null;
aTab.collection.listener = FacetContext;
}
FacetContext.collection = aTab.collection;
if ("IMCollection" in aTab)
FacetContext.IMCollection = aTab.IMCollection;
// if it has already completed, we need to prod things
if (aTab.query.completed && (!("IMQuery" in aTab) || aTab.IMQuery.completed))
FacetContext.initialBuild();
}
function clickOnBody(event) {
if (event.bubbles) {
document.getElementById('popup-menu').hide();
}
return 0;
}
|