fipamo/src/com/controllers/NavIndex.js

61 lines
1.7 KiB
JavaScript

import FipamoAPI from '../../libraries/FipamoAPI';
import NavActions from '../actions/NavActions';
import * as DataEvent from '../events/DataEvent';
import Notifications from '../ui/Notifications';
const notify = new Notifications();
const api = new FipamoAPI();
export default class NavIndex {
//--------------------------
// constructor
//--------------------------
constructor() {
this.start();
}
//--------------------------
// methods
//--------------------------
start() {
Sortable.create(document.getElementById('nav-pages'), {
onUpdate: () => {
new NavActions().syncMenu().then(data => {
api.syncNav(data).then(r => {
if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true);
} else {
notify.alert(r.message, true);
}
});
});
}
});
var nav = document.querySelectorAll('.nav-btn');
for (var i = 0, length = nav.length; i < length; i++) {
nav[i].addEventListener('click', e => this.handleNavButton(e), false);
}
}
//--------------------------
// event handlers
//--------------------------
handleNavButton(e) {
let id = '';
switch (e.target.id) {
case 'remove-item':
id = e.target.getAttribute('data-id');
new NavActions().removeItem(id);
new NavActions().syncMenu().then(data => {
data.remove = e.target.getAttribute('data-uuid');
api.syncNav(data).then(r => {
if (r.type == DataEvent.MENU_UPDATED) {
notify.alert(r.message, true);
} else {
notify.alert(r.message, true);
}
});
});
break;
case 'edit-item':
window.location = '/@/dashboard/page/edit/' + e.target.getAttribute('data-id');
break;
}
}
}