ulogger-server/js/src/userviewmodel.js

92 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-12-16 12:08:16 +01:00
/*
* μlogger
*
* Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
2019-12-19 22:20:55 +01:00
import { lang as $, auth } from './initializer.js';
2019-12-16 12:08:16 +01:00
import ViewModel from './viewmodel.js';
import uSelect from './select.js';
import uUser from './user.js';
import uUtils from './utils.js';
/**
* @class UserViewModel
*/
export default class UserViewModel extends ViewModel {
/**
* @param {uState} state
*/
constructor(state) {
super({
/** @type {uUser[]} */
userList: [],
/** @type {string} */
currentUserId: '0'
});
/** @type HTMLSelectElement */
const listEl = document.querySelector('#user');
2019-12-19 22:20:55 +01:00
this.select = new uSelect(listEl, $._('suser'), `- ${$._('allusers')} -`);
2019-12-16 12:08:16 +01:00
this.state = state;
}
2019-12-22 20:00:23 +01:00
/**
* @return {UserViewModel}
*/
2019-12-16 12:08:16 +01:00
init() {
2019-12-18 08:24:38 +01:00
this.setObservers(this.state);
2019-12-16 12:08:16 +01:00
this.bindAll();
uUser.fetchList()
.then((_users) => {
this.model.userList = _users;
if (_users.length) {
let userId = _users[0].listValue;
if (auth.isAuthenticated) {
const user = this.model.userList.find((_user) => _user.listValue === auth.user.listValue);
if (user) {
userId = user.listValue;
}
}
this.model.currentUserId = userId;
}
})
2019-12-19 22:20:55 +01:00
.catch((e) => { uUtils.error(e, `${$._('actionfailure')}\n${e.message}`); });
2019-12-22 20:00:23 +01:00
return this;
2019-12-16 12:08:16 +01:00
}
2019-12-18 08:24:38 +01:00
/**
* @param {uState} state
*/
setObservers(state) {
this.onChanged('userList', (list) => {
this.select.setOptions(list);
});
this.onChanged('currentUserId', (listValue) => {
this.state.showAllUsers = listValue === uSelect.allValue;
this.state.currentUser = this.model.userList.find((_user) => _user.listValue === listValue) || null;
});
state.onChanged('showLatest', (showLatest) => {
if (showLatest) {
this.select.showAllOption();
} else {
this.select.hideAllOption();
}
});
}
2019-12-16 12:08:16 +01:00
}