2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-29 12:32:51 +02:00
|
|
|
const handlebars = require('handlebars');
|
|
|
|
|
2016-03-19 21:37:04 +01:00
|
|
|
// fix iterating over NodeList in Chrome and Opera
|
|
|
|
NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
|
|
|
|
|
|
|
|
class BaseView {
|
2016-03-29 12:32:51 +02:00
|
|
|
constructor() {
|
2016-03-19 21:37:04 +01:00
|
|
|
this.contentHolder = document.getElementById('content-holder');
|
|
|
|
}
|
|
|
|
|
|
|
|
getTemplate(templatePath) {
|
|
|
|
const templateElement = document.getElementById(templatePath);
|
|
|
|
if (!templateElement) {
|
|
|
|
console.log('Missing template: ' + templatePath);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const templateText = templateElement.innerHTML;
|
2016-03-29 12:32:51 +02:00
|
|
|
return handlebars.compile(templateText);
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 22:33:49 +02:00
|
|
|
notifyError(message) {
|
|
|
|
this.notify(message, 'error');
|
|
|
|
}
|
|
|
|
|
|
|
|
notifySuccess(message) {
|
|
|
|
this.notify(message, 'success');
|
|
|
|
}
|
|
|
|
|
|
|
|
notify(message, className) {
|
|
|
|
const messagesHolder = this.contentHolder.querySelector('.messages');
|
2016-03-28 22:33:20 +02:00
|
|
|
/* TODO: animate this */
|
|
|
|
const node = document.createElement('div');
|
2016-04-02 18:57:17 +02:00
|
|
|
node.innerHTML = message.replace(/\n/g, '<br/>');
|
2016-03-28 22:33:20 +02:00
|
|
|
node.classList.add('message');
|
2016-03-31 22:33:49 +02:00
|
|
|
node.classList.add(className);
|
2016-03-28 22:33:20 +02:00
|
|
|
messagesHolder.appendChild(node);
|
|
|
|
}
|
|
|
|
|
2016-03-31 22:33:49 +02:00
|
|
|
clearMessages() {
|
|
|
|
const messagesHolder = this.contentHolder.querySelector('.messages');
|
2016-03-28 22:33:20 +02:00
|
|
|
/* TODO: animate that */
|
|
|
|
while (messagesHolder.lastChild) {
|
|
|
|
messagesHolder.removeChild(messagesHolder.lastChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 21:37:04 +01:00
|
|
|
decorateValidator(form) {
|
|
|
|
// postpone showing form fields validity until user actually tries
|
|
|
|
// to submit it (seeing red/green form w/o doing anything breaks POLA)
|
2016-03-28 22:33:20 +02:00
|
|
|
const submitButton = form.querySelector('.buttons input');
|
|
|
|
submitButton.addEventListener('click', e => {
|
2016-03-19 21:37:04 +01:00
|
|
|
form.classList.add('show-validation');
|
|
|
|
});
|
2016-03-28 22:33:20 +02:00
|
|
|
form.addEventListener('submit', e => {
|
|
|
|
form.classList.remove('show-validation');
|
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-04-06 15:11:20 +02:00
|
|
|
disableForm(form) {
|
|
|
|
for (let input of form.querySelectorAll('input')) {
|
|
|
|
input.disabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enableForm(form) {
|
|
|
|
for (let input of form.querySelectorAll('input')) {
|
|
|
|
input.disabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:49:26 +02:00
|
|
|
empty() {
|
|
|
|
this.showView('<div class="messages"></div>');
|
|
|
|
}
|
|
|
|
|
2016-03-19 21:37:04 +01:00
|
|
|
showView(html) {
|
|
|
|
this.contentHolder.innerHTML = html;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BaseView;
|