This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add section search component for use with directory pages.
Add search box component for section search at the top of direcotry landing pages.
- Loading branch information
Showing
6 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/marko-web-theme-monorail/browser/section-search.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<template> | ||
<div ref="search" class="section-search" :class="{'open':openSuggestion}"> | ||
<input | ||
v-model="selection" | ||
class="form-control" | ||
type="text" | ||
placeholder="Search Categories..." | ||
> | ||
<div v-if="selection" class="list-group"> | ||
<div v-for="suggestion in matches" :key="suggestion" class="list-group-item"> | ||
<a :href="'/'+suggestion.alias" @click="emitAction()"> | ||
{{ suggestion.name }} | ||
</a> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
inject: ['EventBus'], | ||
props: { | ||
sections: { | ||
type: Array, | ||
required: true, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
open: false, | ||
current: 0, | ||
selection: '', | ||
}; | ||
}, | ||
computed: { | ||
matches() { | ||
return this.sections.filter((section) => { | ||
const match = section.name.toLowerCase().indexOf(this.selection.toLowerCase()) >= 0; | ||
return match; | ||
}); | ||
// return this.sectionNames.filter((str) => { | ||
// const match = str.toLowerCase().indexOf(this.selection.toLowerCase()) >= 0; | ||
// return match; | ||
// }); | ||
}, | ||
openSuggestion() { | ||
return this.selection !== '' | ||
&& this.matches.length !== 0 | ||
&& this.open === true; | ||
}, | ||
}, | ||
mounted() { | ||
this.addListeners(); | ||
}, | ||
beforeDestroy() { | ||
this.removeListeners(); | ||
}, | ||
methods: { | ||
detectOutclick(event) { | ||
const el = this.$refs.search; | ||
if (!el.contains(event.target) && el !== event.target) { | ||
this.selection = ''; | ||
} | ||
}, | ||
addListeners() { | ||
document.addEventListener('click', this.detectOutclick.bind(this)); | ||
document.addEventListener('touchstart', this.detectOutclick.bind(this)); | ||
}, | ||
removeListeners() { | ||
document.removeEventListener('click', this.detectOutclick.bind(this)); | ||
document.removeEventListener('touchstart', this.detectOutclick.bind(this)); | ||
}, | ||
emitAction() { | ||
const payload = { | ||
category: 'Content Header Search', | ||
action: 'Click', | ||
label: 'Website Section Page', | ||
}; | ||
this.EventBus.$emit('content-header-search', payload); | ||
}, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/marko-web-theme-monorail/components/blocks/section-search.marko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import queryFragment from "../../graphql/fragments/sections-search" | ||
import { getChildSectionsById } from "../../utils/get-child-sections-by-id"; | ||
|
||
$ const { sectionIds, includeSelf } = input; | ||
|
||
<if(sectionIds.length)> | ||
<marko-web-query|{ nodes: parent }| | ||
name="website-sections" | ||
params={ includeIds: sectionIds, queryFragment } | ||
> | ||
$ const nodesById = getChildSectionsById(parent[0], {}, includeSelf); | ||
<marko-web-browser-component name="ThemeSectionSearch" props={ sections: Object.values(nodesById) } /> | ||
</marko-web-query> | ||
</if> |
46 changes: 46 additions & 0 deletions
46
packages/marko-web-theme-monorail/graphql/fragments/sections-search.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const gql = require('graphql-tag'); | ||
|
||
module.exports = gql` | ||
fragment WebsiteSectionSearchFragment on WebsiteSection { | ||
id | ||
alias | ||
name | ||
children(input: { pagination: { limit: 0 } }) { | ||
edges { | ||
node { | ||
id | ||
alias | ||
name | ||
children(input: { pagination: { limit: 0 } }) { | ||
edges { | ||
node { | ||
id | ||
alias | ||
name | ||
children(input: { pagination: { limit: 0 } }) { | ||
edges { | ||
node { | ||
id | ||
alias | ||
name | ||
children(input: { pagination: { limit: 0 } }) { | ||
edges { | ||
node { | ||
id | ||
alias | ||
name | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; |
11 changes: 11 additions & 0 deletions
11
packages/marko-web-theme-monorail/utils/get-child-sections-by-id.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { getAsArray } = require('@parameter1/base-cms-object-path'); | ||
|
||
const getChildSectionsById = (section, childSections = {}, includeSelf = true) => { | ||
const { id } = section; | ||
const children = getAsArray(section, 'children.edges').map(({ node }) => node); | ||
return { | ||
...children.reduce((obj, child) => getChildSectionsById(child, obj), childSections), | ||
...(includeSelf && !section[id] && { [id]: section }), | ||
}; | ||
}; | ||
module.exports = { getChildSectionsById }; |