class NewsManager {
constructor(){
this.queue = window.jvbQueue;
this.loading = window.jvbLoading;
this.cache = window.jvbCache;
this.a11y = window.jvbA11y;
this.error = window.jvbError;
this.activeTab = 'all';
this.tabs = new window.jvbTabs(document.querySelector('.replace'), {
'news': () => {
this.activeTab = 'all';
this.resetFilters();
this.loadItems(true).then(()=>{});
},
'mine': () => {
console.log('switching to mine tab');
this.activeTab = 'own';
this.resetFilters();
this.filters.artist = window.auth.getUser();
this.loadItems(true).then(()=>{});
},
'watching': () => {
this.activeTab = 'watching';
this.resetFilters();
this.filters.watched = true;
this.loadItems(true).then(()=>{});
}
});
this.isLoading = false;
this.alreadyHandling = false;
this.template = new Map();
this.endpoints = {
news: 'news',
vote: 'news/vote'
};
this.resetFilters();
this.state = {
hasMore: true,
pages: 1,
items: 0
};
this.initElements();
this.initEvents();
this.loadItems();
}
resetFilters(){
this.filters = {
page: 1,
order: 'DESC',
orderby: 'date',
shop: null,
type: null,
artist: null,
watched: false,
}
}
initElements(){
this.container = document.querySelector('.replace');
this.grid = this.container.querySelector('.item-grid');
this.addButton = this.container.querySelector('.add-item-btn');
this.addModal = new window.jvbModal(
this.container.querySelector('.create-modal'),
{
render: this.renderModal.bind(this),
open: this.addButton,
content: 'news',
openMessage: 'Opened modal to create a news post.',
onSave: this.saveModal.bind(this),
});
this.filterForm = this.container.querySelector('form');
this.dateRangeFilter = new window.jvbModal(
this.container.querySelector('dialog.date-range'),
{
open: false,
});
this.clearFilters = this.container.querySelector('.clear-filters');
this.replyModal = new window.jvbModal(
this.container.querySelector('.create-response'),
{
open: false,
content: 'response',
openMessage: 'Opened Response modal',
onSave: this.saveCreatedResponse.bind(this)
});
}
initEvents(){
this.filterForm.addEventListener('change', (e) => {
let value = e.target.value;
if(e.target.closest('.date-range')){
return;
}
if(value === 'custom'){
this.handleCustomDateRange();
}else{
let name = e.target.name;
if(name){
this.filters[name] = value;
}else{
this.resetFilters();
}
this.loadItems(true);
}
});
document.addEventListener('click', (e) => {
if(e.target === this.clearFilters){
this.filterForm.reset();
this.resetFilters();
this.loadItems(true);
}
if(e.target.closest('button.reply')){
//TODO:
//1) get content of what we are responding to, and add it to .original
//2) get the ID of the item we are responding to
let button = e.target.closest('button');
let itemID = button.closest('.item').dataset.id;
let original = '';
if(button.dataset.type === 'news'){
original = button.closest('.item').querySelector('.item-info').innerHTML;
}else{
original = button.closest('.response').querySelector('.content').innerHTML;
this.replyModal.modal.dataset.parent_id = button.id.replace('reply-to', '');
}
this.replyModal.modal.dataset.id = itemID;
this.replyModal.modal.dataset.type = button.dataset.type;
this.replyModal.modal.querySelector('.original').innerHTML = '
Replying to:
'+original;
this.replyModal.handleOpen();
}
});
}
renderModal(){
}
handleCustomDateRange(){
this.dateRangeFilter.handleOpen();
let dateStart = this.dateRangeFilter.modal.querySelector('input.date-start');
let dateEnd = this.dateRangeFilter.modal.querySelector('input.date-end');
let custom = this.dateRangeFilter.modal.querySelector('select');
let inputs = this.dateRangeFilter.modal.querySelectorAll('input, select').forEach(input => {
input.addEventListener('change', (e) => {
//We need both beginning and end filters in order to parse the request
if((input === dateStart && dateEnd.value !== '') || (input === dateEnd && dateStart.value !== '')){
this.filters.dateFrom = dateStart.value;
this.filters.dateTo = dateEnd.value;
this.dateRangeFilter.handleClose();
this.loadItems(true);
}else if (input === custom){
//Or one of the preset months
this.filters.customDate = custom.value;
this.dateRangeFilter.handleClose();
this.loadItems(true);
}
});
});
// this.alreadyHandling = false;
}
async saveModal(form){
const formData = new FormData(this.addModal.modal.querySelector('form'));
formData.append('user', window.auth.getUser());
this.queue.addToQueue({
type: 'new_news',
data: formData,
});
}
/**
* Load favourites from the server
* @returns {Promise