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