Skip to content

Commit

Permalink
Merge pull request #865 from LibCrowds/2020-fixes
Browse files Browse the repository at this point in the history
Merges 2020 fixes from KDL into master for use by NIIT team.
  • Loading branch information
harryjmoss committed Aug 11, 2020
2 parents d8bf671 + 06965a7 commit 86fc5d0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 23 deletions.
23 changes: 19 additions & 4 deletions components/data/SearchTags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
no-result="No tags found, try changing the search query"
@select="selectTag"
@remove="removeTag"
@search-change="searchTags">
</multiselect>
@search-change="searchTags"
></multiselect>
</template>

<script>
Expand All @@ -44,7 +44,8 @@ export default {
* The query string.
*/
searchTags (query) {
return this.search(query).then(r => {
// return this.search(query).then(r => {
return this.suggestTags(query).then(r => {
if (r.data.total > 0) {
this.foundTags = r.data.first.items
} else {
Expand Down Expand Up @@ -79,6 +80,21 @@ export default {
return this.$explicates.search(params)
},
/**
* Search for current tag Annotations.
* @param {String} query
* The query string.
* @param {Boolean} strict
* True to use the query as a prefix, false otherwise.
*/
suggestTags (query, strict = false, contains = null) {
const safeQuery = query.replace(/[^\w\s&]/gi, '')
return this.$explicates.suggestTags({
collection: this.containerIri,
q: safeQuery
})
},
/**
* Get the label to display for a tag.
* @param {Object} tag
Expand All @@ -103,7 +119,6 @@ export default {
You can still continue using the result of the application as normal.`
this.tagsLoading = false
this.$emit('error', err)
console.error(err)
this.$notifications.error({ message: errorMessage })
},
Expand Down
61 changes: 47 additions & 14 deletions components/data/SelectTags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
@tag="addTag"
@select="selectTag"
@remove="removeTag"
@search-change="searchTags">
</multiselect>
@search-change="searchTags"
></multiselect>
</template>

<script>
Expand Down Expand Up @@ -82,6 +82,8 @@ export default {
*/
searchTags (query) {
if (!query || !query.length) {
this.foundTags = []
this.tagsLoading = false
return
}
Expand All @@ -92,7 +94,10 @@ export default {
return this.addTag(parts[0])
}
return this.search(query).then(r => {
// return this.search(query).then(r => {
let ret = this.suggestTags(query)
ret.then(r => {
if (r.data.total > 0) {
this.foundTags = r.data.first.items
} else {
Expand All @@ -102,6 +107,8 @@ export default {
}).catch(err => {
this.handleError(err)
})
return ret
},
/**
Expand All @@ -124,6 +131,21 @@ export default {
})
},
/**
* Search for current tag Annotations.
* @param {String} query
* The query string.
* @param {Boolean} strict
* True to use the query as a prefix, false otherwise.
*/
suggestTags (query, strict = false, contains = null) {
const safeQuery = query.replace(/[^\w\s&]/gi, '')
return this.$explicates.suggestTags({
collection: this.containerIri,
q: safeQuery
})
},
/**
* Get the label to display for a tag.
* @param {Object} tag
Expand Down Expand Up @@ -192,15 +214,28 @@ export default {
* @param {String} value
* The tag value.
* @param {String} id
* The multiselect item id.
* @param {Boolean} isNew
* New if adding a new tag, false if selecting.
* The multiselect item id
* (the explicates annotation id if user selected existing tag one)
* (if user pressed space or selected new tag => null/undefined)
*/
async addTag (value, id, isNew = true) {
async addTag (value, id) {
value = (value || '').trim()
if (!value) {
this.$notifications.info({ message: 'Tag is empty' })
// if (isNew) this.selectedTags.pop()
return
}
const exists = await this.checkTagExists(value)
if (id) {
// we unselect that tag as we want to create a new one instead
this.selectedTags.pop()
}
if (exists) {
this.$notifications.info({ message: 'Tag already exists' })
this.selectedTags.pop()
return
}
Expand All @@ -210,9 +245,7 @@ export default {
return this.$explicates.createAnnotation(iri, newTag).then(r => {
this.$notifications.success({ message: 'Tag added' })
this.tagsLoading = false
if (isNew) {
this.selectedTags.push(r.data)
}
this.selectedTags.push(r.data)
}).catch(err => {
this.handleError(err)
})
Expand All @@ -223,8 +256,9 @@ export default {
* @param {Object} tag
* The tag.
*/
selectTag (tag) {
this.addTag(tag.body.value, false)
selectTag (tag, id) {
// create a new tag in the DB and select it
this.addTag(tag.body.value, tag.id)
},
/**
Expand Down Expand Up @@ -259,7 +293,6 @@ export default {
You can still continue using the result of the application as normal.`
this.tagsLoading = false
this.$emit('error', err)
console.error(err)
this.$notifications.error({ message: errorMessage })
}
}
Expand Down
12 changes: 9 additions & 3 deletions components/infiniteload/Annotations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<infinite-loading
ref="infiniteload"
class="infinite-loading"
@infinite="infiniteLoadAnnotations">
@infinite="infiniteLoadAnnotations"
>
<span slot="no-results">
<span v-if="noResults">{{ noResults }}</span>
</span>
Expand All @@ -21,7 +22,8 @@ import Fuse from 'fuse.js'
export default {
data () {
return {
items: []
items: [],
offset: 0
}
},
Expand Down Expand Up @@ -61,18 +63,22 @@ export default {
async infiniteLoadAnnotations ($state) {
let response = null
const limit = 20
// const limit = 100
try {
response = await this.$explicates.search({
// response = await this.$explicates.suggestTags({
collection: this.containerIri,
limit: limit,
offset: this.items.length
offset: this.offset
})
} catch (err) {
this.$nuxt.error(err)
}
if (response.data.hasOwnProperty('first')) {
this.items = uniqBy(this.items.concat(response.data.first.items), 'id')
// this.items = this.items.concat(response.data.first.items)
this.offset += response.data.first.items.length
}
if (
Expand Down
15 changes: 15 additions & 0 deletions modules/nuxt-explicates/plugin.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ class Explicates {
})
}

/**
* Suggests tags
*/
suggestTags (params, minimal = false, iris = false) {
const endpoint = '/tags/'
return this.client.get(endpoint, {
params: params,
headers: {}
})
}

/**
* Return the Prefer header.
* @param {Boolean} minimal
Expand Down Expand Up @@ -215,6 +226,10 @@ export default (ctx, inject) => {
accept: '<%= options.accept %>'
}

// GN: 'text/plain' to avoid unnecessary preflight request (OPTION)
// when doing GET.
options.accept = 'text/plain'

// Create a new Explicates instance
const explicates = new Explicates(options)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports[`App footer renders correctly 1`] = `
<a href="#/" class="router-link-exact-active router-link-active">
Part of the My Brand crowdsourcing platform
</a>
<p class="my-2">&copy; My Company, 2019</p>
<p class="my-2">&copy; My Company, 2020</p>
<ul class="list-inline mt-2 mb-0">
<li class="list-inline-item mx-1">
<a href="https://twitter.com/mytwitterhandle" class="text-muted">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ exports[`Dashboard footer renders correctly 1`] = `
</a>
</li>
</ul>
<p class="my-2 mx-1 float-right text-muted">&copy; My Company, 2019</p>
<p class="my-2 mx-1 float-right text-muted">&copy; My Company, 2020</p>
</div>
</footer>
`;

0 comments on commit 86fc5d0

Please sign in to comment.