Henrique Dias 834ce7ac4a vue updates ^_^
Former-commit-id: 336b37cf681ec2337a1e4d577213aa45f12b81d6 [formerly d8cbb6ff242f9ab3e5c857da6f6758abb0f4fc1a] [formerly 8b9089c816fae3608bf5ef8592cb776fa420a6f6 [formerly e2077efbc6a49a82c9f0fc8741304fd2fc9c7e93]]
Former-commit-id: 30b063fdab7de6f2c1c5f46dd8a1dd354897f5b6 [formerly 8f83b525334b9430ddbe779c6eae3251a5590b75]
Former-commit-id: bbe19a047d103531a542bebb1fe0263bec4cbd88
2017-06-28 16:05:30 +01:00

140 lines
3.6 KiB
Vue

<template>
<div id="search" v-on:mouseleave="hover = false" v-on:click="click" v-bind:class="{ active: focus || hover }">
<i class="material-icons" title="Search">search</i>
<input type="text"
v-on:focus="focus = true"
v-on:blur="focus = false"
v-on:keyup="keyup"
v-on:keyup.enter="submit"
aria-label="Write here to search"
placeholder="Search or execute a command...">
<div v-on:mouseover="hover = true">
<div>Loading...</div>
<p><i class="material-icons spin">autorenew</i></p>
</div>
</div>
</template>
<script>
// Remove the last directory of an url
var removeLastDirectoryPartOf = function (url) {
var arr = url.split('/')
if (arr.pop() === '') {
arr.pop()
}
return (arr.join('/'))
}
var user = window.info.user
var ssl = window.ssl
export default {
name: 'search',
data: function () {
return {
hover: false,
focus: false,
scrollable: null,
box: null,
input: null
}
},
mounted: function () {
this.scrollable = document.querySelector('#search > div')
this.box = document.querySelector('#search > div div')
this.input = document.querySelector('#search input')
this.reset()
},
methods: {
reset: function () {
if (user.allowCommands && user.commands.length > 0) {
this.box.innerHTML = `Search or use one of your supported commands: ${user.commands.join(', ')}.`
} else {
this.box.innerHTML = 'Type and press enter to search.'
}
},
supported: function () {
let value = this.input.value
let pieces = value.split(' ')
for (let i = 0; i < user.commands.length; i++) {
if (pieces[0] === user.commands[0]) {
return true
}
}
return false
},
click: function (event) {
event.currentTarget.classList.add('active')
this.$el.querySelector('input').focus()
},
keyup: function (event) {
let el = event.currentTarget
if (el.value.length === 0) {
this.reset()
return
}
if (!this.supported() || !user.allowCommands) {
this.box.innerHTML = 'Press enter to search.'
} else {
this.box.innerHTML = 'Press enter to execute.'
}
},
submit: function (event) {
this.box.innerHTML = ''
this.$el.classList.add('ongoing')
let url = window.location.host + window.location.pathname
if (document.getElementById('editor')) {
url = removeLastDirectoryPartOf(url)
}
let protocol = ssl ? 'wss:' : 'ws:'
if (this.supported() && user.allowCommands) {
let conn = new window.WebSocket(`${protocol}//${url}?command=true`)
conn.onopen = () => {
conn.send(this.input.value)
}
conn.onmessage = (event) => {
this.box.innerHTML = event.data
this.scrollable.scrollTop = this.scrollable.scrollHeight
}
conn.onclose = (event) => {
this.$el.classList.remove('ongoing')
// TODO: if is listing!
// listing.reload()
}
return
}
this.box.innerHTML = '<ul></ul>'
let ul = this.box.querySelector('ul')
let conn = new window.WebSocket(`${protocol}//${url}?search=true`)
conn.onopen = () => {
conn.send(this.input.value)
}
conn.onmessage = (event) => {
ul.innerHTML += `<li><a href=".${event.data}">${event.data}</a></li>`
this.scrollable.scrollTop = this.scrollable.scrollHeight
}
conn.onclose = () => {
this.$el.classList.remove('ongoing')
}
}
}
}
</script>