2017-05-18 01:22:20 +08:00
|
|
|
window.APP = {
|
|
|
|
template: '#app_template',
|
|
|
|
name: 'app',
|
|
|
|
data() {
|
|
|
|
return {
|
2017-05-22 04:51:51 +08:00
|
|
|
style: CONFIG.style,
|
2017-05-18 01:22:20 +08:00
|
|
|
showInput: false,
|
|
|
|
showWindow: false,
|
|
|
|
suggestions: [],
|
2017-05-21 22:52:55 +08:00
|
|
|
templates: CONFIG.templates,
|
2017-05-18 01:22:20 +08:00
|
|
|
message: '',
|
|
|
|
messages: [],
|
|
|
|
oldMessages: [],
|
|
|
|
oldMessagesIndex: -1,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
destroyed() {
|
|
|
|
clearInterval(this.focusTimer);
|
|
|
|
window.removeEventListener('message', this.listener);
|
|
|
|
},
|
|
|
|
mounted() {
|
2017-05-21 22:52:55 +08:00
|
|
|
$.post('http://chat/loaded', JSON.stringify({}));
|
2017-05-18 01:22:20 +08:00
|
|
|
this.listener = window.addEventListener('message', (event) => {
|
|
|
|
const item = event.data || event.detail; //'detail' is for debuging via browsers
|
|
|
|
if (this[item.type]) {
|
|
|
|
this[item.type](item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
messages() {
|
|
|
|
if (this.showWindowTimer) {
|
|
|
|
clearTimeout(this.showWindowTimer);
|
|
|
|
}
|
|
|
|
this.showWindow = true;
|
2017-05-21 22:52:55 +08:00
|
|
|
this.resetShowWindowTimer();
|
2017-05-18 01:22:20 +08:00
|
|
|
|
|
|
|
const messagesObj = this.$refs.messages;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
messagesObj.scrollTop = messagesObj.scrollHeight;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
ON_OPEN() {
|
|
|
|
this.showInput = true;
|
|
|
|
this.showWindow = true;
|
|
|
|
if (this.showWindowTimer) {
|
|
|
|
clearTimeout(this.showWindowTimer);
|
|
|
|
}
|
|
|
|
this.focusTimer = setInterval(() => {
|
|
|
|
if (this.$refs.input) {
|
|
|
|
this.$refs.input.focus();
|
|
|
|
} else {
|
|
|
|
clearInterval(this.focusTimer);
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
},
|
2017-05-21 22:52:55 +08:00
|
|
|
ON_MESSAGE({ message }) {
|
|
|
|
this.messages.push(message);
|
|
|
|
},
|
|
|
|
ON_CLEAR() {
|
|
|
|
this.messages = [];
|
|
|
|
this.oldMessages = [];
|
|
|
|
this.oldMessagesIndex = -1;
|
2017-05-18 01:22:20 +08:00
|
|
|
},
|
2017-05-21 22:52:55 +08:00
|
|
|
ON_SUGGESTION_ADD({ suggestion }) {
|
2017-05-18 01:22:20 +08:00
|
|
|
if (!suggestion.params) {
|
2017-05-21 22:52:55 +08:00
|
|
|
suggestion.params = []; //TODO Move somewhere else
|
2017-05-18 01:22:20 +08:00
|
|
|
}
|
|
|
|
this.suggestions.push(suggestion);
|
|
|
|
},
|
2017-05-21 22:52:55 +08:00
|
|
|
ON_SUGGESTION_REMOVE({ name }) {
|
|
|
|
this.suggestions = this.suggestions.filter((sug) => sug.name !== name)
|
|
|
|
},
|
|
|
|
ON_TEMPLATE_ADD({ template }) {
|
|
|
|
if (this.templates[template.id]) {
|
|
|
|
this.warn(`Tried to add duplicate template '${template.id}'`)
|
|
|
|
} else {
|
|
|
|
this.templates[template.id] = template.html;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
warn(msg) {
|
|
|
|
this.messages.push({
|
|
|
|
args: [msg],
|
|
|
|
template: '^3<b>CHAT-WARN</b>: ^0{0}',
|
|
|
|
});
|
|
|
|
},
|
|
|
|
clearShowWindowTimer() {
|
|
|
|
clearTimeout(this.showWindowTimer);
|
|
|
|
},
|
|
|
|
resetShowWindowTimer() {
|
|
|
|
this.clearShowWindowTimer();
|
|
|
|
this.showWindowTimer = setTimeout(() => {
|
|
|
|
if (!this.showInput) {
|
|
|
|
this.showWindow = false;
|
|
|
|
}
|
|
|
|
}, CONFIG.fadeTimeout);
|
2017-05-18 01:22:20 +08:00
|
|
|
},
|
|
|
|
keyUp() {
|
|
|
|
this.resize();
|
|
|
|
},
|
|
|
|
keyDown(e) {
|
|
|
|
if (e.which === 38 || e.which === 40) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.moveOldMessageIndex(e.which === 38);
|
2017-05-21 22:52:55 +08:00
|
|
|
} else if (e.which == 33) {
|
|
|
|
const buf = $(this.$refs.messages);
|
|
|
|
buf.scrollTop(buf.scrollTop() - 50);
|
|
|
|
} else if (e.which == 34) {
|
|
|
|
const buf = $(this.$refs.messages);
|
|
|
|
buf.scrollTop(buf.scrollTop() + 50);
|
2017-05-18 01:22:20 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
moveOldMessageIndex(up) {
|
|
|
|
if (up && this.oldMessages.length > this.oldMessagesIndex + 1) {
|
|
|
|
this.oldMessagesIndex += 1;
|
|
|
|
this.message = this.oldMessages[this.oldMessagesIndex];
|
|
|
|
} else if (!up && this.oldMessagesIndex - 1 >= 0) {
|
|
|
|
this.oldMessagesIndex -= 1;
|
|
|
|
this.message = this.oldMessages[this.oldMessagesIndex];
|
|
|
|
} else if (!up && this.oldMessagesIndex - 1 === -1) {
|
|
|
|
this.oldMessagesIndex = -1;
|
|
|
|
this.message = '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resize() {
|
|
|
|
const input = this.$refs.input;
|
|
|
|
input.style.height = '5px';
|
|
|
|
input.style.height = `${input.scrollHeight + 2}px`;
|
|
|
|
},
|
|
|
|
send(e) {
|
2017-05-21 22:52:55 +08:00
|
|
|
if (e.shiftKey) {
|
|
|
|
this.message += '\n';
|
|
|
|
this.resize();
|
|
|
|
} else {
|
|
|
|
if(this.message !== '') {
|
|
|
|
$.post('http://chat/chatResult', JSON.stringify({
|
|
|
|
message: this.message,
|
|
|
|
}));
|
|
|
|
this.oldMessages.unshift(this.message);
|
|
|
|
this.message = '';
|
|
|
|
this.oldMessagesIndex = -1;
|
|
|
|
this.hideInput();
|
|
|
|
} else {
|
|
|
|
this.hideInput(true);
|
|
|
|
}
|
2017-05-18 01:22:20 +08:00
|
|
|
}
|
|
|
|
},
|
2017-05-21 22:52:55 +08:00
|
|
|
hideInput(canceled = false) {
|
2017-05-18 01:22:20 +08:00
|
|
|
if (canceled) {
|
2017-05-21 22:52:55 +08:00
|
|
|
$.post('http://chat/chatResult', JSON.stringify({ canceled }));
|
2017-05-18 01:22:20 +08:00
|
|
|
}
|
|
|
|
this.showInput = false;
|
|
|
|
clearInterval(this.focusTimer);
|
2017-05-21 22:52:55 +08:00
|
|
|
this.resetShowWindowTimer();
|
2017-05-18 01:22:20 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|