fipamo/src/com/controllers/NavIndex.js

60 lines
1.6 KiB
JavaScript

import ApiUtils, { REQUEST_TYPE_POST, CONTENT_TYPE_JSON } from '../../../src/com/utils/APIUtils';
import NavActions from '../actions/NavActions';
import * as DataEvent from '../events/DataEvent';
import Notifications from '../ui/Notifications';
const notify = new Notifications();
const api = new ApiUtils();
export default class NavIndex {
//--------------------------
// constructor
//--------------------------
constructor() {
api.authStatus();
this.start();
}
//--------------------------
// methods
//--------------------------
start() {
Sortable.create(document.getElementById('nav-pages'), {
onUpdate: () => {
new NavActions().syncMenu().then(data => {
api.request(
'/api/v1/settings/nav-sync',
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
).then(response => {
let r = JSON.parse(response.request['response']);
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);
break;
case 'edit-item':
window.location = '/@/dashboard/pages/edit/' + e.target.getAttribute('data-id');
break;
}
}
}