|
|
|
@ -1,14 +1,21 @@
|
|
|
|
|
let tagroot = document.querySelector('article.tags')
|
|
|
|
|
let sel_tags = tagroot.querySelector('div.tag_container.tags_selected')
|
|
|
|
|
let page_tags = tagroot.querySelector('div.tag_container.tags_inpage')
|
|
|
|
|
|
|
|
|
|
let searchroot = tagroot.querySelector('.searchbox')
|
|
|
|
|
searchroot.insertAdjacentHTML("beforeend", `<input type="hidden" name="tags">`)
|
|
|
|
|
let search_input = searchroot.querySelector('input[name=tags][type=text]')
|
|
|
|
|
search_input.removeAttribute('name')
|
|
|
|
|
search_input.value = ''
|
|
|
|
|
let search_hidden = document.querySelector('input[name=tags][type=hidden]')
|
|
|
|
|
let search_dropdown = searchroot.querySelector('.search_dropdown')
|
|
|
|
|
let tagroot = document.querySelector('.tag_input')
|
|
|
|
|
let sel_tags = tagroot.querySelector('div.tags_selected')
|
|
|
|
|
let predef_tags = tagroot.querySelector('div.tags_inpage') // Should be optional
|
|
|
|
|
|
|
|
|
|
let suggestroot = tagroot.querySelector('.tag_suggester')
|
|
|
|
|
|
|
|
|
|
suggestroot.insertAdjacentHTML("beforeend", `<input type="hidden" name="${suggestroot.dataset.inputname}">`)
|
|
|
|
|
let suggest_hidden = suggestroot.querySelector(`input[name=${suggestroot.dataset.inputname}][type=hidden]`)
|
|
|
|
|
|
|
|
|
|
let suggest_input = suggestroot.querySelector(`input[name=${suggestroot.dataset.inputname}][type=text]`)
|
|
|
|
|
suggest_input.removeAttribute('name')
|
|
|
|
|
if (suggest_input.hasAttribute('required')) {
|
|
|
|
|
suggest_input.removeAttribute('required')
|
|
|
|
|
suggest_hidden.setAttribute('required', '')
|
|
|
|
|
}
|
|
|
|
|
suggest_input.value = ''
|
|
|
|
|
|
|
|
|
|
let tag_suggest_dropdown = suggestroot.querySelector('.tag_suggest_dropdown')
|
|
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript
|
|
|
|
|
function getJsonFromUrl(url) {
|
|
|
|
@ -22,7 +29,7 @@ function getJsonFromUrl(url) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function newseltag(tagname) {
|
|
|
|
|
function create_selection_tag(tagname) {
|
|
|
|
|
let tag = document.createElement("a")
|
|
|
|
|
tag.classList.add("tagselected")
|
|
|
|
|
tag.dataset.tagname = tagname
|
|
|
|
@ -37,60 +44,65 @@ function newseltag(tagname) {
|
|
|
|
|
return tag
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function tags_input_addtag(tagname) {
|
|
|
|
|
let val = search_hidden.value.split(' ')
|
|
|
|
|
function form_addtag(tagname) {
|
|
|
|
|
let val = suggest_hidden.value.split(' ')
|
|
|
|
|
val.push(tagname)
|
|
|
|
|
search_hidden.value = val.join(' ').trim()
|
|
|
|
|
// console.log(search_hidden.value)
|
|
|
|
|
suggest_hidden.value = val.join(' ').trim()
|
|
|
|
|
}
|
|
|
|
|
function tags_input_removetag(tagname) {
|
|
|
|
|
let val = search_hidden.value.split(' ')
|
|
|
|
|
function form_removetag(tagname) {
|
|
|
|
|
let val = suggest_hidden.value.split(' ')
|
|
|
|
|
val.splice(val.indexOf(tagname), 1)
|
|
|
|
|
search_hidden.value = val.join(' ').trim()
|
|
|
|
|
// console.log(search_hidden.value)
|
|
|
|
|
suggest_hidden.value = val.join(' ').trim()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addseltag(tagname) {
|
|
|
|
|
// let pagetag = page_tags.querySelector("a.tag-"+tagname)
|
|
|
|
|
let pagetag = page_tags.querySelector(`a[data-tagname=${tagname}]`)
|
|
|
|
|
function add_selected(tagname) {
|
|
|
|
|
if (predef_tags) {
|
|
|
|
|
let pagetag = predef_tags.querySelector(`a[data-tagname=${tagname}]`)
|
|
|
|
|
if (pagetag) {
|
|
|
|
|
pagetag.classList.add('tag_hide')
|
|
|
|
|
}
|
|
|
|
|
let newtag = newseltag(tagname)
|
|
|
|
|
}
|
|
|
|
|
let newtag = create_selection_tag(tagname)
|
|
|
|
|
newtag.addEventListener('click', (event) => {
|
|
|
|
|
removeseltag(event.target.dataset.tagname)
|
|
|
|
|
remove_selected(event.target.dataset.tagname)
|
|
|
|
|
// console.log(`Deselected: ${event.target.dataset.tagname}`)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
sel_tags.appendChild(newtag)
|
|
|
|
|
tags_input_addtag(tagname)
|
|
|
|
|
form_addtag(tagname)
|
|
|
|
|
}
|
|
|
|
|
function removeseltag(tagname) {
|
|
|
|
|
let pagetag = page_tags.querySelector(`a[data-tagname=${tagname}]`)
|
|
|
|
|
function remove_selected(tagname) {
|
|
|
|
|
if (predef_tags) {
|
|
|
|
|
let pagetag = predef_tags.querySelector(`a[data-tagname=${tagname}]`)
|
|
|
|
|
if (pagetag) {
|
|
|
|
|
pagetag.classList.remove('tag_hide')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sel_tags.querySelector(`a[data-tagname=${tagname}]`).remove()
|
|
|
|
|
tags_input_removetag(tagname)
|
|
|
|
|
form_removetag(tagname)
|
|
|
|
|
}
|
|
|
|
|
page_tags.querySelectorAll("a").forEach(element => {
|
|
|
|
|
|
|
|
|
|
// Page tags click to select
|
|
|
|
|
if (predef_tags) {
|
|
|
|
|
predef_tags.querySelectorAll("a").forEach(element => {
|
|
|
|
|
element.addEventListener('click', (event) => {
|
|
|
|
|
addseltag(event.target.dataset.tagname)
|
|
|
|
|
// console.log(`Selected: ${event.target.dataset.tagname}`)
|
|
|
|
|
add_selected(event.target.dataset.tagname)
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate selected by url query
|
|
|
|
|
let query = getJsonFromUrl()
|
|
|
|
|
if (query['tags']) {
|
|
|
|
|
query['tags'].split('+').forEach((tag) => {
|
|
|
|
|
addseltag(tag)
|
|
|
|
|
add_selected(tag)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Suggestions
|
|
|
|
|
function newsugtag(tagname) {
|
|
|
|
|
function create_suggestion_tag(tagname) {
|
|
|
|
|
let tag = document.createElement("a")
|
|
|
|
|
tag.classList.add("tagsuggestion")
|
|
|
|
|
tag.dataset.tagname = tagname
|
|
|
|
@ -103,43 +115,50 @@ function newsugtag(tagname) {
|
|
|
|
|
tag.querySelector("span.name").textContent = tagname.replace(/_/g, ' ')
|
|
|
|
|
return tag
|
|
|
|
|
}
|
|
|
|
|
function rendersuggestions(data) {
|
|
|
|
|
search_dropdown.innerHTML = ''
|
|
|
|
|
// search_dropdown.querySelectorAll('a').forEach((element) => {
|
|
|
|
|
// search_dropdown.replaceChild(newsugtag)
|
|
|
|
|
// })
|
|
|
|
|
function render_suggestions(data) {
|
|
|
|
|
tag_suggest_dropdown.innerHTML = ''
|
|
|
|
|
|
|
|
|
|
let tagnames = Array.from(sel_tags.querySelectorAll('a')).map(a => a.dataset.tagname)
|
|
|
|
|
let i = 0
|
|
|
|
|
data.forEach((el) => {
|
|
|
|
|
let sugtag = newsugtag(el.content)
|
|
|
|
|
if (i > 5) return
|
|
|
|
|
if (tagnames.includes(el.content)) return
|
|
|
|
|
|
|
|
|
|
let sugtag = create_suggestion_tag(el.content)
|
|
|
|
|
|
|
|
|
|
sugtag.addEventListener('click', (event) => {
|
|
|
|
|
addseltag(event.target.dataset.tagname)
|
|
|
|
|
console.log(`Selected: ${event.target.dataset.tagname}`)
|
|
|
|
|
add_selected(event.target.dataset.tagname)
|
|
|
|
|
suggest_input.value = ''
|
|
|
|
|
render_suggestions([])
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
search_dropdown.appendChild(sugtag)
|
|
|
|
|
tag_suggest_dropdown.appendChild(sugtag)
|
|
|
|
|
|
|
|
|
|
i++
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
var search_timeout
|
|
|
|
|
search_input.addEventListener('input', (event) => {
|
|
|
|
|
clearTimeout(search_timeout)
|
|
|
|
|
|
|
|
|
|
let value = search_input.value.trim()
|
|
|
|
|
// Ajax suggestion handler
|
|
|
|
|
var ajax_timeout
|
|
|
|
|
suggest_input.addEventListener('input', (event) => {
|
|
|
|
|
clearTimeout(ajax_timeout)
|
|
|
|
|
|
|
|
|
|
let value = suggest_input.value.trim()
|
|
|
|
|
|
|
|
|
|
if (value.length < 2) {
|
|
|
|
|
rendersuggestions([])
|
|
|
|
|
render_suggestions([])
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
search_timeout = setTimeout(() => {
|
|
|
|
|
ajax_timeout = setTimeout(() => {
|
|
|
|
|
fetch('/api/tags?q='+value).then((response) => {
|
|
|
|
|
console.log(response)
|
|
|
|
|
return response.json()
|
|
|
|
|
}).then((data) => {
|
|
|
|
|
// fill suggestions
|
|
|
|
|
console.log(data)
|
|
|
|
|
rendersuggestions(data)
|
|
|
|
|
// console.log(data)
|
|
|
|
|
render_suggestions(data)
|
|
|
|
|
})
|
|
|
|
|
}, 500)
|
|
|
|
|
})
|
|
|
|
@ -159,8 +178,3 @@ search_input.addEventListener('input', (event) => {
|
|
|
|
|
// console.log(event)
|
|
|
|
|
// })
|
|
|
|
|
// page_tags.querySelectorAll('a').forEach((el) => el.removeAttribute('href'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// console.log(page_tags)
|
|
|
|
|
// console.log(sel_tags)
|
|
|
|
|
// console.log(search_dropdown.children[0])
|
|
|
|
|