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
|
/* 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:///modules/iteratorUtils.jsm");
Components.utils.import("resource:///modules/errUtils.js");
Components.utils.import("resource:///modules/quickFilterManager.js");
Components.utils.import("resource:///modules/searchSpec.js");
////////////////////////////////////////////////////////////////////////////////
//// Proper Code
/**
* There is only one message filter bar widget; the muxer deals with tab
* changes and directing modifications to and reflecting the state of the
* actual filterer objects.
*/
var QuickFilterBarMuxer = {
/**
* This gets called by OnLoadMessenger in order to ensure that the monitor
* gets registered prior to the first tab being opened. This avoids
* complications about generating synthetic tab notifications.
*/
_init: function QFBM__init() {
// -- folder display hookup
FolderDisplayListenerManager.registerListener(this);
// -- tab monitor hookup
this.tabmail = document.getElementById("tabmail");
this.tabmail.registerTabMonitor(this);
// -- window hookups
let dis = this;
// know when a resize happens so we can expand things collapsed by our
// overflow handler (registered by attribute in the XUL file).
window.addEventListener("resize", function QFBM_resizeWrap() {
dis.onWindowResize();
}, false);
this._bindUI();
},
//////////////////////////////////////////////////////////////////////////////
// FolderDisplayListener
/**
* Decide whether to display the filter bar toggle button whenever a folder
* display is made active. makeActive is what triggers the display of
* account central so this is the perfect spot to do so.
*/
onMakeActive: function QFBM_onMakeActive(aFolderDisplay) {
let tab = aFolderDisplay._tabInfo;
this._updateToggle(tab);
// The case in that previous aFolderDisplay is showing a normal folder is
// handled by onLoadingFolder. Here we handle the case where previous
// aFolderDisplay shows an account folder instead (this cannot be done
// in onLoadingFolder because that event is not raised).
if (!aFolderDisplay.displayedFolder ||
aFolderDisplay.displayedFolder.isServer) {
let filterer = this.maybeActiveFilterer;
if (!filterer)
return;
// Clear displayedFolder to force next onLoadingFolder to recreate the view
filterer.displayedFolder = null;
}
},
/**
* Based on the passed in 3pane aTabInfo, determine whether or not the
* quickFilter toggle should be enabled, and set it appropriately.
*/
_updateToggle: function QFBM__updateToggle(aTabInfo) {
if (!this.isCommandEnabled("cmd_toggleQuickFilterBar", aTabInfo)) {
document.getElementById("view_toolbars_popup_quickFilterBar")
.setAttribute("checked", false);
}
this._updateCommands();
},
/**
* Update the commands associated with the quick filter bar.
*/
_updateCommands: function QFBM__updateCommands() {
goUpdateCommand("cmd_popQuickFilterBarStack");
goUpdateCommand("cmd_showQuickFilterBar");
goUpdateCommand("cmd_toggleQuickFilterBar");
},
/**
* Clear out our state when notified the user has changed folders and re-apply
* search constraints if we are in sticky mode. It is important that we
* re-apply search constraints here in onLoadingFolder as this is the only
* notification we receive where we have a chance to avoid creating a view
* just to nuke it and re-create it with our new search constraints shortly
* afterwards.
*/
onLoadingFolder: function QFBM_onFolderChanged(aFolderDisplay,
aIsOutbound) {
let tab = aFolderDisplay._tabInfo;
let filterer = ("quickFilter" in tab._ext) ? tab._ext.quickFilter : null;
if (!filterer)
return;
// check if there actually was a change (notification might not be for us)
if (tab.folderDisplay.displayedFolder != filterer.displayedFolder) {
// perform state propagation to a new filter state
tab._ext.quickFilter = filterer = new QuickFilterState(filterer);
this.updateSearch();
this.reflectFiltererState(filterer, tab.folderDisplay);
}
},
/**
* Once the view is fully populated:
* - Invoke postFilterProcess on all filter definitions that expose such a
* method. If they return a value, cram it in their state and (assuming
* this is the current tab), call their reflectInDOM method so they can
* update their state.
* - Update UI to reflect some/no matches.
*/
onActiveMessagesLoaded:
function QFBM_onFolderDisplayMessagesLoaded(aFolderDisplay) {
let filterer = this.maybeActiveFilterer;
if (!filterer)
return;
let filtering = aFolderDisplay.view.search.userTerms != null;
// - postFilterProcess everyone who cares
// This may need to be converted into an asynchronous process at some point.
for (let filterDef of QuickFilterManager.filterDefs) {
if ("postFilterProcess" in filterDef) {
let preState = (filterDef.name in filterer.filterValues) ?
filterer.filterValues[filterDef.name] : null;
let [newState, update, treatAsUserAction] =
filterDef.postFilterProcess(preState, aFolderDisplay.view, filtering);
filterer.setFilterValue(filterDef.name, newState, !treatAsUserAction);
if (update) {
if (aFolderDisplay._tabInfo == this.tabmail.currentTabInfo &&
("reflectInDOM" in filterDef)) {
let domNode = document.getElementById(filterDef.domId);
// We are passing update as a super-secret data propagation channel
// exclusively for one-off cases like the text filter gloda upsell.
filterDef.reflectInDOM(domNode, newState, document, this, update);
}
}
}
}
// - Update match status.
this.reflectFiltererResults(filterer, aFolderDisplay);
},
/**
* If we're searching, update the filter results. (If we stop searching,
* we're going to end up in the onFolderDisplayMessagesLoaded
* notification. Mayhaps we should lose that vector and just use this one.)
*/
onSearching: function QFBM_onSearching(
aFolderDisplay, aIsSearching) {
// we only care if we just started searching and we are active
if (!aIsSearching || !aFolderDisplay.active)
return;
// - Update match status.
this.reflectFiltererResults(this.activeFilterer, aFolderDisplay);
},
//////////////////////////////////////////////////////////////////////////////
// UI State Manipulation
/**
* Add appropriate event handlers to the DOM elements. We do this rather
* than requiring lots of boilerplate "oncommand" junk on the nodes.
*
* We hook up the following:
* - "command" event listener.
* - reflect filter state
*/
_bindUI: function QFBM__bindUI() {
for (let filterDef of QuickFilterManager.filterDefs) {
let domNode = document.getElementById(filterDef.domId);
// the loop let binding does not latch, at least in 1.9.2
let latchedFilterDef = filterDef;
let handler;
if (!("onCommand" in filterDef)) {
handler = function(aEvent) {
try {
let postValue = domNode.checked ? true : null;
QuickFilterBarMuxer.activeFilterer.setFilterValue(
latchedFilterDef.name, postValue);
QuickFilterBarMuxer.deferredUpdateSearch();
}
catch (ex) {
logException(ex);
}
};
}
else {
handler = function(aEvent) {
let filterValues = QuickFilterBarMuxer.activeFilterer.filterValues;
let preValue = (latchedFilterDef.name in filterValues) ?
filterValues[latchedFilterDef.name] : null;
let [postValue, update] =
latchedFilterDef.onCommand(preValue, domNode, aEvent, document);
QuickFilterBarMuxer.activeFilterer.setFilterValue(
latchedFilterDef.name, postValue, !update);
if (update)
QuickFilterBarMuxer.deferredUpdateSearch();
};
}
domNode.addEventListener("command", handler, false);
if ("domBindExtra" in filterDef)
filterDef.domBindExtra(document, this, domNode);
}
},
/**
* Update the UI to reflect the state of the filterer constraints.
*
* @param aFilterer The active filterer.
* @param aFolderDisplay The active FolderDisplayWidget.
* @param [aFilterName] If only a single filter needs to be updated, name it.
*/
reflectFiltererState: function QFBM_reflectFiltererState(aFilterer,
aFolderDisplay,
aFilterName) {
// If we aren't visible then there is no need to update the widgets.
if (aFilterer.visible) {
let filterValues = aFilterer.filterValues;
for (let filterDef of QuickFilterManager.filterDefs) {
// If we only need to update one state, check and skip as appropriate.
if (aFilterName && filterDef.name != aFilterName)
continue;
let domNode = document.getElementById(filterDef.domId);
let value = (filterDef.name in filterValues) ?
filterValues[filterDef.name] : null;
if (!("reflectInDOM" in filterDef))
domNode.checked = Boolean(value);
else
filterDef.reflectInDOM(domNode, value, document, this);
}
}
this.reflectFiltererResults(aFilterer, aFolderDisplay);
document.getElementById("quick-filter-bar").collapsed =
!aFilterer.visible;
document.getElementById("view_toolbars_popup_quickFilterBar")
.setAttribute("checked", aFilterer.visible);
},
/**
* Update the UI to reflect the state of the folderDisplay in terms of
* filtering. This is expected to be called by |reflectFiltererState| and
* when something happens event-wise in terms of search.
*
* We can have one of four states:
* - No filter is active; no attributes exposed for CSS to do anything.
* - A filter is active and we are still searching; filterActive=searching.
* - A filter is active, completed searching, and we have results;
* filterActive=matches.
* - A filter is active, completed searching, and we have no results;
* filterActive=nomatches.
*/
reflectFiltererResults: function QFBM_reflectFiltererResults(aFilterer,
aFolderDisplay) {
let view = aFolderDisplay.view;
let threadPane = document.getElementById("threadTree");
let qfb = document.getElementById("quick-filter-bar");
// bail early if the view is in the process of being created
if (!view.dbView)
return;
// no filter active
if (!view.search || !view.search.userTerms) {
threadPane.removeAttribute("filterActive");
qfb.removeAttribute("filterActive");
}
// filter active, still searching
else if (view.searching) {
// Do not set this immediately; wait a bit and then only set this if we
// still are in this same state (and we are still the active tab...)
setTimeout(function() {
if (!view.searching ||
(QuickFilterBarMuxer.maybeActiveFilterer != aFilterer))
return;
threadPane.setAttribute("filterActive", "searching");
qfb.setAttribute("filterActive", "searching");
}, 500);
}
// filter completed, results
else if (view.dbView.numMsgsInView) {
// some matches
threadPane.setAttribute("filterActive", "matches");
qfb.setAttribute("filterActive", "matches");
}
// filter completed, no results
else {
// no matches! :(
threadPane.setAttribute("filterActive", "nomatches");
qfb.setAttribute("filterActive", "nomatches");
}
},
//////////////////////////////////////////////////////////////////////////////
// Resizing regimen
/**
* Are the button labels currently collapsed?
*/
_buttonLabelsCollapsed: false,
/**
* The minimum width the bar must be before we can un-collapse the button
* labels.
*/
_minExpandedBarWidth: null,
/**
* Where we stash the minwidth for the text search box, if present, when
* _buttonLabelsCollapsed.
*/
_savedOffTextWidgetMinWidth: null,
/**
* Our general strategy is this:
* - All collapsible buttons are set to not flex and live in the
* "quick-filter-bar-collapsible-buttons" hbox. This provides us with a
* nice minimum size.
* - All flexy widgets have some minimum size configured.
* - When the bar starts to overflow we save off the (minimum) size of the bar
* so that once it gets large enough again we can restore the buttons.
* - On overflow we also lose the minwidth constraint on the search box so it
* resizes down in a reasonable fashion.
*
* This method handles the overflow case where we transition to collapsed
* buttons. Our onWindowResize logic handles detecting when it is time to
* un-collapse.
*/
onOverflow: function QFBM_onOverflow() {
// If we are already collapsed, there is nothing more to do.
if (this._buttonLabelsCollapsed)
return;
let quickFilterBarBox =
document.getElementById("quick-filter-bar-main-bar");
let collapsibleButtonBox =
document.getElementById("quick-filter-bar-collapsible-buttons");
// the scroll width is the actual size it wants to be...
this._minExpandedBarWidth = quickFilterBarBox.scrollWidth;
this._buttonLabelsCollapsed = true;
let textWidget = document.getElementById(QuickFilterManager.textBoxDomId);
if (textWidget) {
this._savedOffTextWidgetMinWidth = textWidget.getAttribute("minwidth");
textWidget.removeAttribute("minwidth");
}
collapsibleButtonBox.setAttribute("shrink", "true");
},
/**
* Counterpart to |onOverflow| un-collapses the buttons once the quick filter
* bar gets wide enough to support the desired minimum widget of the bar when
* the buttons are not collapsed.
*/
onWindowResize: function QFB_onWindowResize() {
// nothing to do here if the buttons are not collapsed
if (!this._buttonLabelsCollapsed)
return;
let quickFilterBarBox =
document.getElementById("quick-filter-bar-main-bar");
// the client width is how big it actually is (thanks to overflow:hidden)
if (quickFilterBarBox.clientWidth < this._minExpandedBarWidth)
return;
this._buttonLabelsCollapsed = false;
this._minExpandedBarWidth = null;
// restore the text widget's min width...
let textWidget = document.getElementById(QuickFilterManager.textBoxDomId);
if (textWidget && this._savedOffTextWidgetMinWidth) {
textWidget.setAttribute("minwidth", this._savedOffTextWidgetMinWidth);
this._savedOffTextWidgetMinWidth = null;
}
let collapsibleButtonBox =
document.getElementById("quick-filter-bar-collapsible-buttons");
collapsibleButtonBox.removeAttribute("shrink");
},
//////////////////////////////////////////////////////////////////////////////
// Tab Monitor Interaction
monitorName: "quickFilter",
onTabTitleChanged: function QFBM_onTabTitleChanged(aTab) {
// nop
},
/**
* Whenever an appropriate new tab is opened, initialize its quick filter
* state.
*/
onTabOpened: function QFBM_onTabOpened(aTab, aFirstTab, aOldTab) {
if (aTab.mode.name == "folder" ||
aTab.mode.name == "glodaList") {
let modelTab =
this.tabmail.getTabInfoForCurrentOrFirstModeInstance(aTab.mode);
let oldFilterer = (modelTab && ("quickFilter" in modelTab._ext)) ?
modelTab._ext.quickFilter : undefined;
aTab._ext.quickFilter = new QuickFilterState(oldFilterer);
this.updateSearch(aTab);
this._updateToggle(aTab);
}
},
onTabRestored: function QFBM_onTabRestored(aTab, aState, aFirstTab) {
let filterer = aTab._ext.quickFilter = new QuickFilterState(null, aState);
this.updateSearch(aTab);
if (aTab == this.tabmail.currentTabInfo)
this.reflectFiltererState(filterer, aTab.folderDisplay);
},
onTabPersist: function QFBM_onTabPersist(aTab) {
let filterer = ("quickFilter" in aTab._ext) ? aTab._ext.quickFilter : null;
if (filterer)
return filterer.persistToObj();
return null;
},
/**
* On tab switch we need to:
* - Restore state for already existing state
* - Create state if it's a new (to us) tab
*/
onTabSwitched: function QFBM_onTabSwitched(aTab, aOldTab) {
// (Note: we used to explicitly handle the possibility that the user had
// typed something but an ontimeout had not yet fired in the textbox.
// We are bailing on that because it adds complexity without much functional
// gain. Our UI will be consistent when we switch back to the tab, which
// is good enough.)
let filterer = this.maybeActiveFilterer;
if (filterer)
this.reflectFiltererState(filterer, aTab.folderDisplay);
this._updateCommands();
},
supportsCommand: function QFBM_supportsCommand(aCommand, aTab) {
// we are not active on tab types we do not support (message tabs)
if (!("quickFilter" in aTab._ext))
return null;
if (aCommand == "cmd_popQuickFilterBarStack" ||
aCommand == "cmd_showQuickFilterBar" ||
aCommand == "cmd_toggleQuickFilterBar")
return true;
else
return null;
},
isCommandEnabled: function QFBM_isCommandEnabled(aCommand, aTab) {
// we are not active on tab types we do not support (message tabs)
if (!("quickFilter" in aTab._ext))
return null;
let isFolderView = (aTab.mode.name == "folder" &&
aTab.folderDisplay.displayedFolder &&
!aTab.folderDisplay.displayedFolder.isServer);
let isGlodaList = aTab.mode.name == "glodaList";
if (!isFolderView && !isGlodaList)
return null;
if (aCommand == "cmd_popQuickFilterBarStack" ||
aCommand == "cmd_showQuickFilterBar" ||
aCommand == "cmd_toggleQuickFilterBar")
return true;
else
return null;
},
doCommand: function QFBM_doCommand(aCommand, aTab) {
// we are not active on tab types we do not support (message tabs)
if (!("quickFilter" in aTab._ext))
return null;
if (aCommand == "cmd_popQuickFilterBarStack") {
QuickFilterBarMuxer.cmdEscapeFilterStack();
return true;
}
else if (aCommand == "cmd_showQuickFilterBar") {
let textWidget = document.getElementById(QuickFilterManager.textBoxDomId);
if (this.activeFilterer.visible == false)
QuickFilterBarMuxer._showFilterBar(true);
textWidget.select();
return true;
}
else if (aCommand == "cmd_toggleQuickFilterBar") {
let show = !this.activeFilterer.visible;
this._showFilterBar(show);
if (show) {
let textWidget = document.getElementById(QuickFilterManager.textBoxDomId);
textWidget.select();
}
return true;
}
return null;
},
get maybeActiveFilterer() {
if (this.tabmail.currentTabInfo &&
"quickFilter" in this.tabmail.currentTabInfo._ext)
return this.tabmail.currentTabInfo._ext.quickFilter;
return null;
},
get activeFilterer() {
if (this.tabmail.currentTabInfo &&
"quickFilter" in this.tabmail.currentTabInfo._ext)
return this.tabmail.currentTabInfo._ext.quickFilter;
throw errorWithDebug("There is no active filterer but we want one.");
},
//////////////////////////////////////////////////////////////////////////////
// Event Handling Support
/**
* Retrieve the current filter state value (presumably an object) for mutation
* purposes. This causes the filter to be the last touched filter for escape
* undo-ish purposes.
*/
getFilterValueForMutation: function QFBM_getFilterValueForMutation(aName) {
return this.activeFilterer.getFilterValue(aName);
},
/**
* Set the filter state for the given named filter to the given value. This
* causes the filter to be the last touched filter for escape undo-ish
* purposes.
*
* @param aName Filter name.
* @param aValue The new filter state.
*/
setFilterValue: function QFBM_setFilterValue(aName, aValue) {
this.activeFilterer.setFilterValue(aName, aValue);
},
/**
* For UI responsiveness purposes, defer the actual initiation of the search
* until after the button click handling has completed and had the ability
* to paint such.
*/
deferredUpdateSearch: function QFBM_deferredUpdateSearch() {
setTimeout(this._deferredInvocUpdateSearch, 10);
},
/**
* The actual helper function to call updateSearch for deferredUpdateSearch
* that makes 'this' relevant.
*/
_deferredInvocUpdateSearch: function QFBM__deferredInvocUpdateSearch() {
QuickFilterBarMuxer.updateSearch();
},
/**
* Update the user terms part of the search definition to reflect the active
* filterer's current state.
*/
updateSearch: function QFBM_updateSearch(aTab) {
let tab = aTab || this.tabmail.currentTabInfo;
// bail if things don't really exist yet
if (!tab.folderDisplay || !tab.folderDisplay.view.search)
return;
let filterer = tab._ext.quickFilter;
filterer.displayedFolder = tab.folderDisplay.displayedFolder;
let [terms, listeners] =
filterer.createSearchTerms(tab.folderDisplay.view.search.session);
for (let [listener, filterDef] of listeners) {
// it registers itself with the search session.
new QuickFilterSearchListener(
tab.folderDisplay, filterer, filterDef,
listener, QuickFilterBarMuxer);
}
tab.folderDisplay.view.search.userTerms = terms;
// Uncomment to know what the search state is when we (try and) update it.
//dump(tab.folderDisplay.view.search.prettyString());
},
_showFilterBar: function QFBM__showFilterBar(aShow) {
this.activeFilterer.visible = aShow;
if (!aShow) {
this.activeFilterer.clear();
this.updateSearch();
let threadPane = document.getElementById("threadTree");
threadPane.focus();
}
this.reflectFiltererState(this.activeFilterer,
this.tabmail.currentTabInfo.folderDisplay);
},
/**
* Invoked when the user chooses the popup from the gloda search box.
*/
cmdGlodaSearchDownSell: function QFBM_cmdGlodaSearchDownSell(aEvent) {
aEvent.stopPropagation();
this._showFilterBar(true);
let textWidget = document.getElementById(
QuickFilterManager.textBoxDomId);
textWidget.select();
},
/**
* User explicitly closed the filter bar.
*/
cmdClose: function QFBM_cmdClose(aEvent) {
this._showFilterBar(false);
},
/**
* User hit the escape key; do our undo-ish thing keeping in mind that this
* may be invoked in situations where the filter bar is not legal / enabled.
*/
cmdEscapeFilterStack: function QFBM_cmdEscapeFilterStack() {
let filterer = this.maybeActiveFilterer;
if (!filterer || !filterer.visible)
return;
// update the search if we were relaxing something
if (filterer.userHitEscape()) {
this.updateSearch();
this.reflectFiltererState(filterer,
this.tabmail.currentTabInfo.folderDisplay);
}
// close the filter since there was nothing left to relax
else {
this.cmdClose();
}
},
_testHelperResetFilterState: function QFBM_resetFilterState() {
let filterer = this.maybeActiveFilterer;
if (!filterer)
return;
let tab = this.tabmail.currentTabInfo;
tab._ext.quickFilter = filterer = new QuickFilterState();
this.updateSearch();
this.reflectFiltererState(filterer, tab.folderDisplay);
},
};
|