Update based on review comments

Found a few javascript errors when visiting the search page with ?expanded=true and no query.
This commit is contained in:
cpradio 2016-10-17 00:02:58 -04:00
parent 8b904cc976
commit ddacda0388
2 changed files with 36 additions and 24 deletions

View File

@ -62,7 +62,7 @@ export default Em.Component.extend({
_init() { _init() {
this.setProperties({ this.setProperties({
searchedTerms: { searchedTerms: {
username: null, username: '',
category: null, category: null,
group: [], group: [],
badge: [], badge: [],
@ -111,20 +111,24 @@ export default Em.Component.extend({
if (!searchTerm) if (!searchTerm)
return []; return [];
let result = [];
const blocks = searchTerm.match(REGEXP_BLOCKS); const blocks = searchTerm.match(REGEXP_BLOCKS);
_.each(blocks, function(block) { if (!blocks) return [];
if (block.length === 0) return;
result.push(block); let result = [];
blocks.forEach(block => {
if (block.length !== 0)
result.push(block);
}); });
this.set('searchTermBlocks', result); this.set('searchTermBlocks', result);
}, },
filterBlocks(regexPrefix) { filterBlocks(regexPrefix) {
const blocks = this.get('searchTermBlocks');
if (!blocks) return [];
let result = []; let result = [];
_.each(this.get('searchTermBlocks'), function(block) { blocks.forEach(block => {
if (block.search(regexPrefix) !== -1) if (block.search(regexPrefix) !== -1)
result.push(block); result.push(block);
}); });
@ -141,14 +145,13 @@ export default Em.Component.extend({
if (this.get(key) !== userInput) { if (this.get(key) !== userInput) {
this.set(key, userInput); this.set(key, userInput);
} }
} else if(this.get(key) !== null) { } else if(this.get(key).length !== 0) {
this.set(key, null); this.set(key, '');
} }
}, },
setSearchedTermSpecialInValue(key, replaceRegEx, matchRegEx = null) { setSearchedTermSpecialInValue(key, replaceRegEx) {
matchRegEx = matchRegEx || replaceRegEx; const match = this.filterBlocks(replaceRegEx);
const match = this.filterBlocks(matchRegEx);
if (match.length !== 0) { if (match.length !== 0) {
if (this.get(key) !== true) { if (this.get(key) !== true) {
@ -260,7 +263,7 @@ export default Em.Component.extend({
updateSearchTermForUsername() { updateSearchTermForUsername() {
const match = this.filterBlocks(REGEXP_USERNAME_PREFIX); const match = this.filterBlocks(REGEXP_USERNAME_PREFIX);
const userFilter = this.get('searchedTerms.username'); const userFilter = this.get('searchedTerms.username');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (userFilter && userFilter.length !== 0) { if (userFilter && userFilter.length !== 0) {
if (match.length !== 0) { if (match.length !== 0) {
@ -280,7 +283,7 @@ export default Em.Component.extend({
updateSearchTermForCategory() { updateSearchTermForCategory() {
const match = this.filterBlocks(REGEXP_CATEGORY_PREFIX); const match = this.filterBlocks(REGEXP_CATEGORY_PREFIX);
const categoryFilter = Discourse.Category.findById(this.get('searchedTerms.category')); const categoryFilter = Discourse.Category.findById(this.get('searchedTerms.category'));
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
const slugCategoryMatches = (match.length !== 0) ? match[0].match(REGEXP_CATEGORY_SLUG) : null; const slugCategoryMatches = (match.length !== 0) ? match[0].match(REGEXP_CATEGORY_SLUG) : null;
const idCategoryMatches = (match.length !== 0) ? match[0].match(REGEXP_CATEGORY_ID) : null; const idCategoryMatches = (match.length !== 0) ? match[0].match(REGEXP_CATEGORY_ID) : null;
@ -321,7 +324,7 @@ export default Em.Component.extend({
updateSearchTermForGroup() { updateSearchTermForGroup() {
const match = this.filterBlocks(REGEXP_GROUP_PREFIX); const match = this.filterBlocks(REGEXP_GROUP_PREFIX);
const groupFilter = this.get('searchedTerms.group'); const groupFilter = this.get('searchedTerms.group');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (groupFilter && groupFilter.length !== 0) { if (groupFilter && groupFilter.length !== 0) {
if (match.length !== 0) { if (match.length !== 0) {
@ -339,10 +342,9 @@ export default Em.Component.extend({
@observes('searchedTerms.badge') @observes('searchedTerms.badge')
updateSearchTermForBadge() { updateSearchTermForBadge() {
let searchTerm = this.get('searchTerm');
const match = this.filterBlocks(REGEXP_BADGE_PREFIX); const match = this.filterBlocks(REGEXP_BADGE_PREFIX);
const badgeFilter = this.get('searchedTerms.badge'); const badgeFilter = this.get('searchedTerms.badge');
let searchTerm = this.get('searchTerm') || '';
if (badgeFilter && badgeFilter.length !== 0) { if (badgeFilter && badgeFilter.length !== 0) {
if (match.length !== 0) { if (match.length !== 0) {
@ -362,7 +364,7 @@ export default Em.Component.extend({
updateSearchTermForTags() { updateSearchTermForTags() {
const match = this.filterBlocks(REGEXP_TAGS_PREFIX); const match = this.filterBlocks(REGEXP_TAGS_PREFIX);
const tagFilter = this.get('searchedTerms.tags'); const tagFilter = this.get('searchedTerms.tags');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (tagFilter && tagFilter.length !== 0) { if (tagFilter && tagFilter.length !== 0) {
const tags = tagFilter.join(','); const tags = tagFilter.join(',');
@ -384,7 +386,7 @@ export default Em.Component.extend({
updateSearchTermForIn() { updateSearchTermForIn() {
const match = this.filterBlocks(REGEXP_IN_MATCH); const match = this.filterBlocks(REGEXP_IN_MATCH);
const inFilter = this.get('searchedTerms.in'); const inFilter = this.get('searchedTerms.in');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (inFilter) { if (inFilter) {
if (match.length !== 0) { if (match.length !== 0) {
@ -404,7 +406,7 @@ export default Em.Component.extend({
updateSearchTermForSpecialInLikes() { updateSearchTermForSpecialInLikes() {
const match = this.filterBlocks(REGEXP_SPECIAL_IN_LIKES_MATCH); const match = this.filterBlocks(REGEXP_SPECIAL_IN_LIKES_MATCH);
const inFilter = this.get('searchedTerms.special.in.likes'); const inFilter = this.get('searchedTerms.special.in.likes');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (inFilter) { if (inFilter) {
if (match.length === 0) { if (match.length === 0) {
@ -421,7 +423,7 @@ export default Em.Component.extend({
updateSearchTermForSpecialInPrivate() { updateSearchTermForSpecialInPrivate() {
const match = this.filterBlocks(REGEXP_SPECIAL_IN_PRIVATE_MATCH); const match = this.filterBlocks(REGEXP_SPECIAL_IN_PRIVATE_MATCH);
const inFilter = this.get('searchedTerms.special.in.private'); const inFilter = this.get('searchedTerms.special.in.private');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (inFilter) { if (inFilter) {
if (match.length === 0) { if (match.length === 0) {
@ -438,7 +440,7 @@ export default Em.Component.extend({
updateSearchTermForSpecialInWiki() { updateSearchTermForSpecialInWiki() {
const match = this.filterBlocks(REGEXP_SPECIAL_IN_WIKI_MATCH); const match = this.filterBlocks(REGEXP_SPECIAL_IN_WIKI_MATCH);
const inFilter = this.get('searchedTerms.special.in.wiki'); const inFilter = this.get('searchedTerms.special.in.wiki');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (inFilter) { if (inFilter) {
if (match.length === 0) { if (match.length === 0) {
@ -455,7 +457,7 @@ export default Em.Component.extend({
updateSearchTermForStatus() { updateSearchTermForStatus() {
const match = this.filterBlocks(REGEXP_STATUS_PREFIX); const match = this.filterBlocks(REGEXP_STATUS_PREFIX);
const statusFilter = this.get('searchedTerms.status'); const statusFilter = this.get('searchedTerms.status');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (statusFilter) { if (statusFilter) {
if (match.length !== 0) { if (match.length !== 0) {
@ -475,7 +477,7 @@ export default Em.Component.extend({
updateSearchTermForPostTime() { updateSearchTermForPostTime() {
const match = this.filterBlocks(REGEXP_POST_TIME_PREFIX); const match = this.filterBlocks(REGEXP_POST_TIME_PREFIX);
const timeDaysFilter = this.get('searchedTerms.time.days'); const timeDaysFilter = this.get('searchedTerms.time.days');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (timeDaysFilter) { if (timeDaysFilter) {
const when = this.get('searchedTerms.time.when'); const when = this.get('searchedTerms.time.when');
@ -496,7 +498,7 @@ export default Em.Component.extend({
updateSearchTermForPostsCount() { updateSearchTermForPostsCount() {
const match = this.filterBlocks(REGEXP_POST_COUNT_PREFIX); const match = this.filterBlocks(REGEXP_POST_COUNT_PREFIX);
const postsCountFilter = this.get('searchedTerms.posts_count'); const postsCountFilter = this.get('searchedTerms.posts_count');
let searchTerm = this.get('searchTerm'); let searchTerm = this.get('searchTerm') || '';
if (postsCountFilter) { if (postsCountFilter) {
if (match.length !== 0) { if (match.length !== 0) {

View File

@ -281,3 +281,13 @@ test("update posts count through advanced search ui", assert => {
assert.equal(find('.search input.full-page-search').val(), "none posts_count:5", 'has updated search term to "none posts_count:5"'); assert.equal(find('.search input.full-page-search').val(), "none posts_count:5", 'has updated search term to "none posts_count:5"');
}); });
}); });
test("validate advanced search when initially empty", assert => {
visit("/search?expanded=true");
click('.search-advanced-options .in-likes');
andThen(() => {
assert.ok(exists('.search-advanced-options .in-likes:checked'), 'has "I liked" populated');
assert.equal(find('.search input.full-page-search').val(), "in:likes", 'has updated search term to "in:likes"');
});
});