/usr/lib/thunderbird-addons/extensions/{e2fda1a4-762b-4020-b5ad-a41df1933103}/calendar-js/calFilter.js is in xul-ext-lightning 1:24.4.0+build1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* 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/. */
/**
* Object that contains a set of filter properties that may be used by a calFilter object
* to filter a set of items.
* Supported filter properties:
* start, end: Specifies the relative date range to use when calculating the filter date
* range. The relative date range may relative to the current date and time, the
* currently selected date, or the dates range of the current view. The actual
* date range used to filter items will be calculated by the calFilter object
* by using the updateFilterDates function, which may be called multiple times
* to reflect changes in the current date and time, and changes to the view.
*
*
* The properties may be set to one of the folowing values:
* - FILTER_DATE_ALL: An unbound date range.
* - FILTER_DATE_XXX: One of the defined relative date ranges.
* - A string that may be converted to a calIDuration object that will be used
* as an offset to the current date and time.
*
* The start and end properties may have values representing different relative
* date ranges, in which case the filter start date will be calculated as the start
* of the relative range specified by the start property, while the filter end date
* will be calculated as the end of the relative range specified by the end
* property.
*
* due: Specifies the filter property for the due date of tasks. This filter has no
* effect when filtering events.
*
* The property has a bit field value, with the FILTER_DUE_XXX bit flags set
* to indicate that tasks with the corresponding due property value should match
* the filter.
*
* If the value is set to null the due date will not be considered when filtering.
*
* status: Specifies the filter property for the status of tasks. This filter has no
* effect when filtering events.
*
* The property has a bit field value, with the FILTER_STATUS_XXX bit flags set
* to indicate that tasks with the corresponding status property value should match
* the filter.
*
* If the value is set to null the status will not be considered when filtering.
*
* category: Specifies the filter property for the item category.
*
* The property may be set to one of the folowing values:
* - null: The item category will not be considered when filtering.
* - A string: The item will match the filter if any of it's categories match the
* category specified by the property.
* - An array: The item will match the filter if any of it's categories match any
* of the categories contained in the Array specified by the property.
*
* occurrences: Specifies the filter property for returning occurrences of repeating items.
*
* The property may be set to one of the folowing values:
* - null, FILTER_OCCURRENCES_BOUND: The default occurrence handling. Occurrences
* will be returned only for date ranges with a bound end date.
* - FILTER_OCCURRENCES_NONE: Only the parent items will be returned.
* - FILTER_OCCURRENCES_PAST_AND_NEXT: Returns past occurrences and the next future
* matching occurrence if one is found.
*
* onfilter: A callback function that may be used to apply additional custom filter
* constraints. If specified, the callback function will be called after any other
* specified filter properties are tested.
*
* The callback function will be called with the following parameters:
* - function(aItem, aResults, aFilterProperties, aFilter)
* @param aItem The item being tested.
* @param aResults The results of the test of the other specified
* filter properties.
* @param aFilterProperties The current filter properties being tested.
* @param aFilter The calFilter object performing the filter test.
*
* If specified, the callback function is responsible for returning a value that
* can be converted to true if the item should match the filter, or a value that
* can be converted to false otherwise. The return value will override the results
* of the testing of any other specified filter properties.
*/
function calFilterProperties() {
this.wrappedJSObject = this;
}
calFilterProperties.prototype = {
FILTER_DATE_ALL: 0,
FILTER_DATE_VIEW: 1,
FILTER_DATE_SELECTED: 2,
FILTER_DATE_SELECTED_OR_NOW: 3,
FILTER_DATE_NOW: 4,
FILTER_DATE_TODAY: 5,
FILTER_DATE_CURRENT_WEEK: 6,
FILTER_DATE_CURRENT_MONTH: 7,
FILTER_DATE_CURRENT_YEAR: 8,
FILTER_STATUS_INCOMPLETE: 1,
FILTER_STATUS_IN_PROGRESS: 2,
FILTER_STATUS_COMPLETED_TODAY: 4,
FILTER_STATUS_COMPLETED_BEFORE: 8,
FILTER_STATUS_ALL: 15,
FILTER_DUE_PAST: 1,
FILTER_DUE_TODAY: 2,
FILTER_DUE_FUTURE: 4,
FILTER_DUE_NONE: 8,
FILTER_DUE_ALL: 15,
FILTER_OCCURRENCES_BOUND: 0,
FILTER_OCCURRENCES_NONE: 1,
FILTER_OCCURRENCES_PAST_AND_NEXT: 2,
start: null,
end: null,
due: null,
status: null,
category: null,
occurrences: null,
onfilter: null,
equals: function cFP_equals(aFilterProps) {
if (!(aFilterProps instanceof calFilterProperties)) {
return false;
}
let props = ["start", "end", "due", "status", "category", "occurrences", "onfilter"];
return props.every(function(prop) {
return (this[prop] == aFilterProps[prop]);
}, this);
},
clone: function cFP_clone() {
let cl = new calFilterProperties();
let props = ["start", "end", "due", "status", "category", "occurrences", "onfilter"];
props.forEach(function(prop) {
cl[prop] = this[prop];
}, this);
return cl;
},
LOG: function cFP_LOG(aString) {
cal.LOG("[calFilterProperties] " +
(aString || "") +
" start=" + this.start +
" end=" + this.end +
" status=" + this.status +
" due=" + this.due +
" category=" + this.category);
}
};
/**
* Object that allows filtering of a set of items using a set of filter properties. A set
* of property filters may be defined by a filter name, which may then be used to apply
* the defined filter properties. A set of commonly used property filters are predefined.
*/
function calFilter() {
this.wrappedJSObject = this;
this.mFilterProperties = new calFilterProperties();
this.initDefinedFilters();
this.mMaxIterations = cal.getPrefSafe("calendar.filter.maxiterations", 50);
}
calFilter.prototype = {
mStartDate: null,
mEndDate: null,
mSelectedDate: null,
mFilterText: "",
mDefinedFilters: {},
mFilterProperties: null,
mToday: null,
mTomorrow: null,
mMaxIterations: 50,
/**
* Initializes the predefined filters.
*/
initDefinedFilters: function cF_initDefinedFilters() {
let filters = ["all", "notstarted", "overdue", "open", "completed", "throughcurrent",
"throughtoday", "throughsevendays", "today", "thisCalendarMonth",
"future", "current", "currentview"];
filters.forEach(function(filter) {
if (!(filter in this.mDefinedFilters)) {
this.defineFilter(filter, this.getPreDefinedFilterProperties(filter));
}
}, this);
},
/**
* Gets the filter properties for a predefined filter.
*
* @param aFilter The name of the filter to retrieve the filter properties for.
* @result The filter properties for the specified filter, or null if the filter
* not predefined.
*/
getPreDefinedFilterProperties: function cF_getPreDefinedFilterProperties(aFilter) {
let props = new calFilterProperties();
if (!aFilter) {
return props;
}
switch (aFilter) {
// Predefined Task filters
case "notstarted":
props.status = props.FILTER_STATUS_INCOMPLETE;
props.due = props.FILTER_DUE_ALL;
props.start = props.FILTER_DATE_ALL;
props.end = props.FILTER_DATE_SELECTED_OR_NOW;
break;
case "overdue":
props.status = props.FILTER_STATUS_INCOMPLETE | props.FILTER_STATUS_IN_PROGRESS;
props.due = props.FILTER_DUE_PAST;
props.start = props.FILTER_DATE_ALL;
props.end = props.FILTER_DATE_SELECTED_OR_NOW;
break;
case "open":
props.status = props.FILTER_STATUS_INCOMPLETE | props.FILTER_STATUS_IN_PROGRESS;
props.due = props.FILTER_DUE_ALL;
props.start = props.FILTER_DATE_ALL;
props.end = props.FILTER_DATE_ALL;
props.occurrences = props.FILTER_OCCURRENCES_PAST_AND_NEXT;
break;
case "completed":
props.status = props.FILTER_STATUS_COMPLETED_TODAY | props.FILTER_STATUS_COMPLETED_BEFORE;
props.due = props.FILTER_DUE_ALL;
props.start = props.FILTER_DATE_ALL;
props.end = props.FILTER_DATE_SELECTED_OR_NOW;
break;
case "throughcurrent":
props.status = props.FILTER_STATUS_INCOMPLETE | props.FILTER_STATUS_IN_PROGRESS |
props.FILTER_STATUS_COMPLETED_TODAY;
props.due = props.FILTER_DUE_ALL;
props.start = props.FILTER_DATE_ALL;
props.end = props.FILTER_DATE_SELECTED_OR_NOW;
break;
case "throughtoday":
props.status = props.FILTER_STATUS_INCOMPLETE | props.FILTER_STATUS_IN_PROGRESS |
props.FILTER_STATUS_COMPLETED_TODAY;
props.due = props.FILTER_DUE_ALL;
props.start = props.FILTER_DATE_ALL;
props.end = props.FILTER_DATE_TODAY;
break;
case "throughsevendays":
props.status = props.FILTER_STATUS_INCOMPLETE | props.FILTER_STATUS_IN_PROGRESS |
props.FILTER_STATUS_COMPLETED_TODAY;
props.due = props.FILTER_DUE_ALL;
props.start = props.FILTER_DATE_ALL;
props.end = "P7D";
break;
// Predefined Event filters
case "today":
props.start = props.FILTER_DATE_TODAY;
props.end = props.FILTER_DATE_TODAY;
break;
case "thisCalendarMonth":
props.start = props.FILTER_DATE_CURRENT_MONTH;
props.end = props.FILTER_DATE_CURRENT_MONTH;
break;
case "future":
props.start = props.FILTER_DATE_NOW;
props.end = props.FILTER_DATE_ALL;
break;
case "current":
props.start = props.FILTER_DATE_SELECTED;
props.end = props.FILTER_DATE_SELECTED;
break;
case "currentview":
props.start = props.FILTER_DATE_VIEW;
props.end = props.FILTER_DATE_VIEW;
break;
case "all":
default:
props.status = props.FILTER_STATUS_ALL;
props.due = props.FILTER_DUE_ALL;
props.start = props.FILTER_DATE_ALL;
props.end = props.FILTER_DATE_ALL;
}
return props;
},
/**
* Defines a set of filter properties so that they may be applied by the filter name. If
* the specified filter name is already defined, it's associated filter properties will be
* replaced.
*
* @param aFilterName The name to define the filter properties as.
* @param aFilterProperties The filter properties to define.
*/
defineFilter: function cF_defineFilter(aFilterName, aFilterProperties) {
if (!(aFilterProperties instanceof calFilterProperties)) {
return;
}
this.mDefinedFilters[aFilterName] = aFilterProperties;
},
/**
* Returns the set of filter properties that were previously defined by a filter name.
*
* @param aFilter The filter name of the defined filter properties.
* @return The properties defined by the filter name, or null if
* the filter name was not previously defined.
*/
getDefinedFilterProperties: function cF_getDefinedFilterProperties(aFilter) {
if (aFilter in this.mDefinedFilters) {
return this.mDefinedFilters[aFilter].clone();
} else {
return null;
}
},
/**
* Returns the filter name that a set of filter properties were previously defined as.
*
* @param aFilterProperties The filter properties previously defined.
* @return The name of the first filter name that the properties
* were defined as, or null if the filter properties were
* not previously defined.
*/
getDefinedFilterName: function cF_getDefinedFilterName(aFilterProperties) {
for (filter in this.mDefinedFilters) {
if (this.mDefinedFilters[filter].equals(aFilterProperties)) {
return filter;
}
}
return null;
},
/**
* Checks if the item matches the current filter text
*
* @param aItem The item to check.
* @return Returns true if the item matches the filter text or no
* filter text has been set, false otherwise.
*/
textFilter: function cF_filterByText(aItem) {
if (!this.mFilterText) {
return true;
}
let searchText = this.mFilterText.toLowerCase();
if (!searchText.length || searchText.match(/^\s*$/)) {
return true;
}
//XXX TODO: Support specifying which fields to search on
for each (let field in ["SUMMARY", "DESCRIPTION", "LOCATION", "URL"]) {
let val = aItem.getProperty(field);
if (val && val.toLowerCase().indexOf(searchText) != -1) {
return true;
}
}
return aItem.getCategories({}).some(function(cat) {
return (cat.toLowerCase().indexOf(searchText) != -1);
});
},
/**
* Checks if the item matches the current filter date range.
*
* @param aItem The item to check.
* @return Returns true if the item falls within the date range
* specified by mStartDate and mEndDate, false otherwise.
*/
dateRangeFilter: function cF_dateRangeFilter(aItem) {
return checkIfInRange(aItem, this.mStartDate, this.mEndDate);
},
/**
* Checks if the item matches the currently applied filter properties. Filter properties
* with a value of null or that are not applicable to the item's type are not tested.
*
* @param aItem The item to check.
* @return Returns true if the item matches the filter properties
* currently applied, false otherwise.
*/
propertyFilter: function cF_propertyFilter(aItem) {
let result;
let props = this.mFilterProperties;
if (!props) {
return false;
}
// the today and tomorrow properties are precalculated in the updateFilterDates function
// for better performance when filtering batches of items.
let today = this.mToday;
if (!today) {
today = cal.now();
today.isDate = true;
}
let tomorrow = this.mTomorrow;
if (!tomorrow) {
tomorrow = today.clone();
tomorrow.day++;
}
// test the date range of the applied filter.
result = this.dateRangeFilter(aItem);
// test the category property. If the property value is an array, only one category must
// match.
if (result && props.category) {
let cats = [];
if (typeof(props.category) == "string") {
cats.push(props.category);
} else if (Array.isArray(props.category)) {
cats = props.category;
}
result = cats.some(function(cat) {
return aItem.getCategories({}).indexOf(cat) > -1;
});
}
// test the status property. Only applies to tasks.
if (result && props.status != null && cal.isToDo(aItem)) {
let completed = aItem.isCompleted;
let current = !aItem.completedDate || today.compare(aItem.completedDate) <= 0;
let percent = aItem.percentComplete || 0;
result = ((props.status & props.FILTER_STATUS_INCOMPLETE) ||
!(!completed && (percent == 0))) &&
((props.status & props.FILTER_STATUS_IN_PROGRESS) ||
!(!completed && (percent > 0))) &&
((props.status & props.FILTER_STATUS_COMPLETED_TODAY) ||
!(completed && current)) &&
((props.status & props.FILTER_STATUS_COMPLETED_BEFORE) ||
!(completed && !current));
}
// test the due property. Only applies to tasks.
if (result && props.due != null && cal.isToDo(aItem)) {
let due = aItem.dueDate;
let now = cal.now();
result = ((props.due & props.FILTER_DUE_PAST) ||
!(due && (due.compare(now) < 0))) &&
((props.due & props.FILTER_DUE_TODAY) ||
!(due && (due.compare(now) >= 0) && (due.compare(tomorrow) < 0))) &&
((props.due & props.FILTER_DUE_FUTURE) ||
!(due && (due.compare(tomorrow) >= 0))) &&
((props.due & props.FILTER_DUE_NONE) ||
!(due == null));
}
// Call the filter properties onfilter callback if set. The return value of the
// callback function will override the result of this function.
if (props.onfilter && (typeof(props.onfilter) == "function")) {
return props.onfilter(aItem, result, props, this);
}
return result;
},
/**
* Calculates the date from a date filter property.
*
* @param prop The value of the date filter property to calculate for. May
* be a constant specifying a relative date range, or a string
* representing a duration offset from the current date time.
* @param start If true, the function will return the date value for the
* start of the relative date range, otherwise it will return the
* date value for the end of the date range.
* @return The calculated date for the property.
*/
getDateForProperty: function cF_getDateForProperty(prop, start) {
let props = this.mFilterProperties || new calFilterProperties();
let result = null;
let selectedDate = this.mSelectedDate || currentView().selectedDay || cal.now();
if (typeof(prop) == "string") {
let duration = cal.createDuration(prop);
if (duration) {
result = cal.now();
result.addDuration(duration);
}
} else {
switch (prop) {
case props.FILTER_DATE_ALL:
result = null;
break;
case props.FILTER_DATE_VIEW:
result = start ? currentView().startDay.clone() :
currentView().endDay.clone();
break;
case props.FILTER_DATE_SELECTED:
result = selectedDate.clone();
result.isDate = true;
break;
case props.FILTER_DATE_SELECTED_OR_NOW:
result = selectedDate.clone();
if ((start && result.jsDate > cal.now().jsDate) ||
(!start && result.jsDate < cal.now().jsDate)) {
result = cal.now();
}
result.isDate = true;
break;
case props.FILTER_DATE_NOW:
result = cal.now();
break;
case props.FILTER_DATE_TODAY:
result = cal.now();
result.isDate = true;
break;
case props.FILTER_DATE_CURRENT_WEEK:
result = start ? cal.now().startOfWeek : cal.now().endOfWeek;
break;
case props.FILTER_DATE_CURRENT_MONTH:
result = start ? cal.now().startOfMonth : cal.now().endOfMonth;
break;
case props.FILTER_DATE_CURRENT_YEAR:
result = start ? cal.now().startOfYear : cal.now().endOfYear;
break;
}
// date ranges are inclusive, so we need to include the day for the end date
if (!start && result && prop != props.FILTER_DATE_NOW) {
result.day++;
}
}
return result;
},
/**
* Calculates the current start and end dates for the currently applied filter.
*
* @return The current [startDate, endDate] for the applied filter.
*/
getDatesForFilter: function cfp_getDatesForFilter() {
let startDate = null;
let endDate = null;
if (this.mFilterProperties) {
startDate = this.getDateForProperty(this.mFilterProperties.start, true);
endDate = this.getDateForProperty(this.mFilterProperties.end, false);
// swap the start and end dates if necessary
if (startDate && endDate && startDate.compare(endDate) > 0) {
let swap = startDate;
endDate = startDate;
startDate = swap;
}
}
return [startDate, endDate];
},
/**
* Gets the start date for the current filter date range.
*
* @return: The start date of the current filter date range, or null if
* the date range has an unbound start date.
*/
get startDate() {
return this.mStartDate;
},
/**
* Sets the start date for the current filter date range. This will override the date range
* calculated from the filter properties by the getDatesForFilter function.
*/
set startDate(aStartDate) {
return (this.mStartDate = aStartDate);
},
/**
* Gets the end date for the current filter date range.
*
* @return: The end date of the current filter date range, or null if
* the date range has an unbound end date.
*/
get endDate() {
return this.mEndDate;
},
/**
* Sets the end date for the current filter date range. This will override the date range
* calculated from the filter properties by the getDatesForFilter function.
*/
set endDate(aEndDate) {
return (this.mEndDate = aEndDate);
},
/**
* Gets the value used to perform the text filter.
*/
get filterText() {
return this.mFilterText;
},
/**
* Sets the value used to perform the text filter.
*
* @param aValue The string value to use for the text filter.
*/
set filterText(aValue) {
return (this.mFilterText = aValue);
},
/**
* Gets the selected date used by the getDatesForFilter function to calculate date ranges
* that are relative to the selected date.
*/
get selectedDate() {
return this.mSelectedDate;
},
/**
* Sets the selected date used by the getDatesForFilter function to calculate date ranges
* that are relative to the selected date.
*/
set selectedDate(aSelectedDate) {
return (this.mSelectedDate = aSelectedDate);
},
/**
* Gets the currently applied filter properties.
*
* @return The currently applied filter properties.
*/
get filterProperties() {
return this.mFilterProperties ? this.mFilterProperties.clone() : null;
},
/**
* Gets the name of the currently applied filter.
*
* @return The current defined name of the currently applied filter
* properties, or null if the current properties were not
* previously defined.
*/
get filterName() {
if (!this.mFilterProperties) {
return null;
}
return this.getDefinedFilterName(this.mFilterProperties);
},
/**
* Applies the specified filter.
*
* @param aFilter The filter to apply. May be one of the following types:
* - a calFilterProperties object specifying the filter properties
* - a String representing a previously defined filter name
* - a String representing a duration offset from now
* - a Function to use for the onfilter callback for a custom filter
*/
applyFilter: function cF_applyFilter(aFilter) {
this.mFilterProperties = null;
if (typeof(aFilter) == "string") {
if (aFilter in this.mDefinedFilters) {
this.mFilterProperties = this.getDefinedFilterProperties(aFilter);
} else {
let dur = cal.createDuration(aFilter);
if (dur.inSeconds > 0) {
this.mFilterProperties = new calFilterProperties();
this.mFilterProperties.start = this.mFilterProperties.FILTER_DATE_NOW;
this.mFilterProperties.end = aFilter;
}
}
} else if (typeof(aFilter) == "object" && (aFilter instanceof calFilterProperties)) {
this.mFilterProperties = aFilter;
} else if (typeof(aFilter) == "function") {
this.mFilterProperties = new calFilterProperties();
this.mFilterProperties.onfilter = aFilter;
} else {
this.mFilterProperties = new calFilterProperties();
}
if (!this.mFilterProperties) {
cal.WARN("[calFilter] Unable to apply filter " + aFilter);
} else {
this.updateFilterDates();
// this.mFilterProperties.LOG("Applying filter:");
}
},
/**
* Calculates the current start and end dates for the currently applied filter, and updates
* the current filter start and end dates. This function can be used to update the date range
* for date range filters that are relative to the selected date or current date and time.
*
* @return The current [startDate, endDate] for the applied filter.
*/
updateFilterDates: function cF_updateFilterDates() {
let [startDate, endDate] = this.getDatesForFilter();
this.mStartDate = startDate;
this.mEndDate = endDate;
// the today and tomorrow properties are precalculated here
// for better performance when filtering batches of items.
this.mToday = cal.now();
this.mToday.isDate = true;
this.mTomorrow = this.mToday.clone();
this.mTomorrow.day++;
return [startDate, endDate];
},
/**
* Filters an array of items, returning a new array containing the items that match
* the currently applied filter properties and text filter.
*
* @param aItems The array of items to check.
* @param aCallback An optional callback function to be called with each item and
* the result of it's filter test.
* @return A new array containing the items that match the filters, or
* null if no filter has been applied.
*/
filterItems: function cF_filterItems(aItems, aCallback) {
if (!this.mFilterProperties) {
return null;
}
return aItems.filter(function(aItem) {
let result = this.propertyFilter(aItem) && this.textFilter(aItem);
if (aCallback && (typeof(aCallback) == "function")) {
aCallback(aItem, result, this.mFilterProperties, this);
}
return result;
}, this);
},
/**
* Checks if the item matches the currently applied filter properties and text filter.
*
* @param aItem The item to check.
* @return Returns true if the item matches the filters,
* false otherwise.
*/
isItemInFilters: function cF_isItemInFilters(aItem) {
return (this.propertyFilter(aItem) && this.textFilter(aItem));
},
/**
* Finds the next occurrence of a repeating item that matches the currently applied
* filter properties.
*
* @param aItem The parent item to find the next occurrence of.
* @return Returns the next occurrence that matches the filters,
* or null if no match is found.
*/
getNextOccurrence: function cF_getNextOccurrence(aItem) {
if (!aItem.recurrenceInfo) {
return this.isItemInFilters(aItem) ? aItem : null;
}
let count = 0;
let start = cal.now();
// If the base item matches the filter, we need to check each future occurrence.
// Otherwise, we only need to check the exceptions.
if (this.isItemInFilters(aItem)) {
while (count++ < this.mMaxIterations) {
let next = aItem.recurrenceInfo.getNextOccurrence(start);
if (!next) {
// there are no more occurrences
return null;
}
if (this.isItemInFilters(next)) {
return next;
}
start = next.startDate || next.entryDate;
}
// we've hit the maximum number of iterations without finding a match
cal.WARN("[calFilter] getNextOccurrence: reached maximum iterations for " + aItem.title);
return null;
} else {
// the parent item doesn't match the filter, we can return the first future exception
// that matches the filter
let exMatch = null;
aItem.recurrenceInfo.getExceptionIds({}).forEach(function(rID) {
let ex = aItem.recurrenceInfo.getExceptionFor(rID);
if (ex && cal.now().compare((ex.startDate || ex.entryDate)) < 0 &&
this.isItemInFilters(ex)) {
exMatch = ex;
}
}, this);
return exMatch;
}
return null;
},
/**
* Gets the occurrences of a repeating item that match the currently applied
* filter properties and date range.
*
* @param aItem The parent item to find occurrence of.
* @return Returns an array containing the occurrences that
* match the filters, an empty array if there are no
* matches, or null if the filter is not initialized.
*/
getOccurrences: function cF_getOccurrences(aItem) {
if (!this.mFilterProperties) {
return null;
}
let props = this.mFilterProperties;
let occs;
if (!aItem.recurrenceInfo || (!props.occurrences && !this.mEndDate) ||
props.occurrences == props.FILTER_OCCURRENCES_NONE) {
// either this isn't a repeating item, the occurrence filter specifies that
// we don't want occurrences, or we have a default occurrence filter with an
// unbound date range, so we return just the unexpanded item.
occs = [aItem];
} else {
occs = aItem.getOccurrencesBetween(this.mStartDate || cal.createDateTime(),
this.mEndDate || cal.now(), {});
if ((props.occurrences == props.FILTER_OCCURRENCES_PAST_AND_NEXT) &&
!this.mEndDate) {
// we have an unbound date range and the occurrence filter specifies
// that we also want the next matching occurrence if available.
let next = this.getNextOccurrence(aItem);
if (next) {
occs.push(next);
}
}
}
return this.filterItems(occs);
},
/**
* Gets the items matching the currently applied filter properties from a calendar.
* This function is asynchronous, and returns results to a calIOperationListener object.
*
* @param aCalendar The calendar to get items from.
* @param aItemType The type of items to get, as defined by the calICalendar
* interface ITEM_FILTER_TYPE_XXX constants.
* @param aListener The calIOperationListener object to return results to.
* @return the calIOperation handle to track the operation.
*/
getItems: function cF_getItems(aCalendar, aItemType, aListener) {
if (!this.mFilterProperties) {
return null;
}
let props = this.mFilterProperties;
// we use a local proxy listener for the calICalendar.getItems() call, and use it
// to handle occurrence expansion and filter the results before forwarding them to
// the listener passed in the aListener argument.
let self = this;
let listener = {
onOperationComplete: aListener.onOperationComplete.bind(aListener),
onGetResult: function(aCalendar, aStatus, aItemType, aDetail, aCount, aItems) {
let items;
if (props.occurrences == props.FILTER_OCCURRENCES_PAST_AND_NEXT) {
// with the FILTER_OCCURRENCES_PAST_AND_NEXT occurrence filter we will
// get parent items returned here, so we need to let the getOccurrences
// function handle occurrence expansion.
items = [];
for each (let item in aItems) {
items = items.concat(self.getOccurrences(item));
}
} else {
// with other occurrence filters the calICalendar.getItems() function will
// return expanded occurrences appropriately, we only need to filter them.
items = self.filterItems(aItems);
}
aListener.onGetResult(aCalendar, aStatus, aItemType, aDetail, items.length, items);
}
};
// build the filter argument for calICalendar.getItems() from the filter properties
let filter = aItemType || aCalendar.FILTER_TYPE_ALL;
if (!props.status || (props.status & (props.FILTER_STATUS_COMPLETED_TODAY |
props.FILTER_STATUS_COMPLETED_BEFORE))) {
filter |= aCalendar.ITEM_FILTER_COMPLETED_YES;
}
if (!props.status || (props.status & (props.FILTER_STATUS_INCOMPLETE |
props.FILTER_STATUS_IN_PROGRESS))) {
filter |= aCalendar.ITEM_FILTER_COMPLETED_NO;
}
let startDate = this.startDate;
let endDate = this.endDate;
// we only want occurrences returned from calICalendar.getItems() with a default
// occurence filter property and a bound date range, otherwise the local listener
// will handle occurrence expansion.
if (!props.occurrences && this.endDate) {
filter |= aCalendar.ITEM_FILTER_CLASS_OCCURRENCES;
startDate = startDate || cal.createDateTime();
endDate = endDate || cal.now();
}
return aCalendar.getItems(filter, 0, startDate, endDate, listener);
}
};
|