Implement filter tasks by text & notes

In order to be able to filter tasks by it's text or notes - we need an
extra filter to be implemented, because angular does not support OR
condition for it's default filter directive.
This commit is contained in:
Jiri Chara
2015-06-11 20:39:29 +02:00
parent 2625b57f2d
commit 2c52829256
3 changed files with 46 additions and 1 deletions
+24
View File
@@ -27,4 +27,28 @@ describe('Custom Filters', function() {
});
});
});
describe('filterByTextAndNotes', function () {
it('returns undefined when no input given', function () {
expect(filter('filterByTextAndNotes')()).to.eql(undefined);
});
it('returns input if term is not a string', function () {
var input = [1, 2, 3];
expect(filter('filterByTextAndNotes')(input, '')).to.eql(input);
expect(filter('filterByTextAndNotes')(input, undefined)).to.eql(input);
expect(filter('filterByTextAndNotes')(input, [])).to.eql(input);
expect(filter('filterByTextAndNotes')(input, new Date())).to.eql(input);
});
it('filters items by notes and text', function () {
var tasks = [
{ text: 'foo' },
{ text: 'foo', notes: 'bar' }
];
expect(filter('filterByTextAndNotes')(tasks, 'bar')).to.eql([tasks[1]]);
expect(filter('filterByTextAndNotes')(tasks, 'foo')).to.eql([tasks[0], tasks[1]]);
});
});
});
+21
View File
@@ -28,4 +28,25 @@ angular.module('habitrpg')
}
return array;
};
}])
.filter('filterByTextAndNotes', ['$filter', function($filter) {
return function (input, term) {
if (!input) return;
if (!angular.isString(term) || term.legth === 0) {
return input;
}
term = new RegExp(term, 'i');
var result = [], i;
for (i = 0; i < input.length; i++) {
if (term.test(input[i].text) || term.test(input[i].notes)) {
result.push(input[i]);
}
}
return result;
};
}]);
+1 -1
View File
@@ -1,4 +1,4 @@
li(bindonce='list', id='task-{{::task.id}}', ng-repeat='task in obj[list.type+"s"] | filter:{text: obj.filterQuery} | conditionalOrderBy: list.view=="dated":"date"', class='task {{Shared.taskClasses(task, user.filters, user.preferences.dayStart, user.lastCron, list.showCompleted, main)}}', ng-click='spell && (list.type != "reward") && castEnd(task, "task", $event)', ng-class='{"cast-target":spell && (list.type != "reward"), "locked-task":obj._locked === true}', popover-trigger='mouseenter', data-popover-html="{{task.popoverOpen ? '' : task.notes | markdown}}", popover-placement="top", popover-append-to-body='{{::modal ? "false":"true"}}', ng-show='shouldShow(task, list, user.preferences)')
li(bindonce='list', id='task-{{::task.id}}', ng-repeat='task in obj[list.type+"s"] | filterByTextAndNotes: obj.filterQuery | conditionalOrderBy: list.view=="dated":"date"', class='task {{Shared.taskClasses(task, user.filters, user.preferences.dayStart, user.lastCron, list.showCompleted, main)}}', ng-click='spell && (list.type != "reward") && castEnd(task, "task", $event)', ng-class='{"cast-target":spell && (list.type != "reward"), "locked-task":obj._locked === true}', popover-trigger='mouseenter', data-popover-html="{{task.popoverOpen ? '' : task.notes | markdown}}", popover-placement="top", popover-append-to-body='{{::modal ? "false":"true"}}', ng-show='shouldShow(task, list, user.preferences)')
// right-hand side control buttons
.task-meta-controls