From 4abd7f85f8c7323a63628dbd9106007ac3417668 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sat, 27 Jun 2015 22:21:50 -0500 Subject: [PATCH] Adjustments to PR#5494 * Update filter name to be more accurate * Add additional tests * Simplify checklist checking logic * Prevent errors from multiple matches in checklist --- test/spec/filters/taskOrderingSpec.js | 58 +++++++++++++++++------ website/public/js/filters/taskOrdering.js | 25 +++++----- website/views/shared/tasks/task.jade | 10 ++-- 3 files changed, 60 insertions(+), 33 deletions(-) diff --git a/test/spec/filters/taskOrderingSpec.js b/test/spec/filters/taskOrderingSpec.js index 27c1e461cb..2eeb1c9856 100644 --- a/test/spec/filters/taskOrderingSpec.js +++ b/test/spec/filters/taskOrderingSpec.js @@ -29,36 +29,66 @@ describe('Task Ordering Filters', function() { }); }); - describe('filterByTextAndNotes', function () { + describe('filterByTaskInfo', function () { it('returns undefined when no input given', function () { - expect(filter('filterByTextAndNotes')()).to.eql(undefined); + expect(filter('filterByTaskInfo')()).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('returns all tasks if term is not a string', function () { + var tasks = [1, 2, 3]; + expect(filter('filterByTaskInfo')(tasks, undefined)).to.eql(tasks); + expect(filter('filterByTaskInfo')(tasks, [])).to.eql(tasks); + expect(filter('filterByTaskInfo')(tasks, new Date())).to.eql(tasks); }); - it('filters items by notes and text', function () { + it('returns tasks if term is an empty string', function () { + var tasks = [1, 2, 3]; + expect(filter('filterByTaskInfo')(tasks, '')).to.eql(tasks); + }); + + it('filters items by text', function () { var tasks = [ { text: 'foo' }, - { text: 'foo', notes: 'bar' } + { text: 'some text that contains foo' }, + { text: 'some text that should not be matched' } ]; - expect(filter('filterByTextAndNotes')(tasks, 'bar')).to.eql([tasks[1]]); - expect(filter('filterByTextAndNotes')(tasks, 'foo')).to.eql([tasks[0], tasks[1]]); + expect(filter('filterByTaskInfo')(tasks, 'foo')).to.eql([tasks[0], tasks[1]]); + }); + + it('filters items by notes', function () { + var tasks = [ + { text: 'some text', notes: 'foo' }, + { text: 'some text', notes: 'a note that contains foo' }, + { text: 'some text', notes: 'some text' }, + { text: 'some text' } + ]; + + expect(filter('filterByTaskInfo')(tasks, 'foo')).to.eql([tasks[0], tasks[1]]); }); it('filters items by checklists', function () { var tasks = [ { text: 'foo' }, - { text: 'foo', notes: 'bar', checklist: [ {text: "checkListToFind"} ] } + { text: 'foo', notes: 'bar', checklist: [ {text: "checkListToFind"} ] }, + { text: 'foo', notes: 'bar', checklist: [ {text: "checkListToNotFind"} ] } ]; - expect(filter('filterByTextAndNotes')(tasks, 'checkListToFind')).to.eql([tasks[1]]); + expect(filter('filterByTaskInfo')(tasks, 'checkListToFind')).to.eql([tasks[1]]); + }); + + it('only includes task once, even with multiple matches in checklist', function() { + var tasks = [ + { + text: 'foo', notes: 'bar', checklist: [ + {text: "checkListToFind"}, + {text: "checkListToFind"}, + {text: "checkListToFind"} + ] + } + ]; + + expect(filter('filterByTaskInfo')(tasks, 'checkListToFind')).to.eql([tasks[0]]); }); }); }); diff --git a/website/public/js/filters/taskOrdering.js b/website/public/js/filters/taskOrdering.js index 7c1f346f7f..c2b6f93b45 100644 --- a/website/public/js/filters/taskOrdering.js +++ b/website/public/js/filters/taskOrdering.js @@ -7,28 +7,25 @@ angular.module('habitrpg') return array; }; }]) - .filter('filterByTextAndNotes', ['$filter', function($filter) { - return function (input, term) { - if (!input) return; + .filter('filterByTaskInfo', ['$filter', function($filter) { + return function (tasks, term) { + if (!tasks) return; if (!angular.isString(term) || term.legth === 0) { - return input; + return tasks; } term = new RegExp(term, 'i'); var result = []; - for (var i = 0; i < input.length; i++) { - if (term.test(input[i].text) || term.test(input[i].notes)) { - result.push(input[i]); - }else if (input[i].checklist) { - var checkListLen = input[i].checklist.length; - for (var j = 0; j < checkListLen; j++) { - if ( term.test(input[i].checklist[j].text) ) { - result.push(input[i]); - } - } + for (var i = 0; i < tasks.length; i++) { + var checklist = tasks[i].checklist; + if (term.test(tasks[i].text) || term.test(tasks[i].notes)) { + result.push(tasks[i]); + } else if (checklist) { + var found = _.find(checklist, function(box) { return term.test(box.text); }); + if (found) { result.push(tasks[i]) } } } diff --git a/website/views/shared/tasks/task.jade b/website/views/shared/tasks/task.jade index 3675e158f5..3a69e1f760 100644 --- a/website/views/shared/tasks/task.jade +++ b/website/views/shared/tasks/task.jade @@ -1,8 +1,8 @@ -li(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-class='{"cast-target":spell && (list.type != "reward"), "locked-task":obj._locked === true}', - ng-click='spell && (list.type != "reward") && castEnd(task, "task", $event)', +li(id='task-{{::task.id}}', + ng-repeat='task in obj[list.type+"s"] | filterByTaskInfo: obj.filterQuery | conditionalOrderBy: list.view=="dated":"date"', + class='task {{Shared.taskClasses(task, user.filters, user.preferences.dayStart, user.lastCron, list.showCompleted, main)}}', + ng-class='{"cast-target":spell && (list.type != "reward"), "locked-task":obj._locked === true}', + ng-click='spell && (list.type != "reward") && castEnd(task, "task", $event)', ng-show='shouldShow(task, list, user.preferences)', popover-trigger='mouseenter', popover-placement="top", popover-append-to-body='{{::modal ? "false":"true"}}', data-popover-html="{{task.popoverOpen ? '' : task.notes | markdown}}")