Add initializer class
This commit is contained in:
parent
c576634923
commit
42b48b4254
67
js/src/initializer.js
Normal file
67
js/src/initializer.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* μ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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import uAjax from './ajax.js';
|
||||||
|
import uAuth from './auth.js';
|
||||||
|
import uConfig from './config.js';
|
||||||
|
import uLang from './lang.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Class uInitializer
|
||||||
|
* @property {uAuth} auth
|
||||||
|
* @property {uConfig} config
|
||||||
|
* @property {uLang} lang
|
||||||
|
*/
|
||||||
|
export class uInitializer {
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.auth = new uAuth();
|
||||||
|
this.config = new uConfig();
|
||||||
|
this.lang = new uLang();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {Promise<void, Error>}
|
||||||
|
*/
|
||||||
|
initialize() {
|
||||||
|
return uAjax.get('utils/getinit.php').then((_data) => {
|
||||||
|
if (!_data || !_data.auth || !_data.config || !_data.lang) {
|
||||||
|
throw new Error('Corrupted initialization data');
|
||||||
|
}
|
||||||
|
this.auth.load(_data.auth);
|
||||||
|
this.config.load(_data.config);
|
||||||
|
this.lang.init(this.config, _data.lang);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static waitForDom() {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
||||||
|
setTimeout(resolve, 1);
|
||||||
|
} else {
|
||||||
|
document.addEventListener('DOMContentLoaded', resolve);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const initializer = new uInitializer();
|
||||||
|
export const config = initializer.config;
|
||||||
|
export const lang = initializer.lang;
|
||||||
|
export const auth = initializer.auth;
|
124
js/test/initiallizer.test.js
Normal file
124
js/test/initiallizer.test.js
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
/*
|
||||||
|
* μ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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import uAjax from '../src/ajax.js';
|
||||||
|
import uAuth from '../src/auth.js';
|
||||||
|
import uConfig from '../src/config.js';
|
||||||
|
import { uInitializer } from '../src/initializer.js';
|
||||||
|
import uLang from '../src/lang.js';
|
||||||
|
|
||||||
|
describe('Initializer tests', () => {
|
||||||
|
|
||||||
|
let initializer;
|
||||||
|
let data;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
data = {
|
||||||
|
auth: {},
|
||||||
|
config: {},
|
||||||
|
lang: {}
|
||||||
|
};
|
||||||
|
initializer = new uInitializer();
|
||||||
|
spyOn(initializer.auth, 'load');
|
||||||
|
spyOn(initializer.config, 'load');
|
||||||
|
spyOn(initializer.lang, 'init');
|
||||||
|
spyOn(uAjax, 'get').and.returnValue(Promise.resolve(data));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create instance', () => {
|
||||||
|
expect(initializer.auth).toBeInstanceOf(uAuth);
|
||||||
|
expect(initializer.config).toBeInstanceOf(uConfig);
|
||||||
|
expect(initializer.lang).toBeInstanceOf(uLang);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should load data from server', (done) => {
|
||||||
|
// when
|
||||||
|
initializer.initialize().then(() => {
|
||||||
|
// then
|
||||||
|
expect(uAjax.get).toHaveBeenCalledWith('utils/getinit.php');
|
||||||
|
expect(initializer.auth.load).toHaveBeenCalledWith(data.auth);
|
||||||
|
expect(initializer.config.load).toHaveBeenCalledWith(data.config);
|
||||||
|
expect(initializer.lang.init).toHaveBeenCalledWith(initializer.config, data.lang);
|
||||||
|
done();
|
||||||
|
}).catch((e) => done.fail(`reject callback called (${e})`));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error on missing data.config', (done) => {
|
||||||
|
// given
|
||||||
|
delete data.config;
|
||||||
|
// when
|
||||||
|
initializer.initialize().then(() => {
|
||||||
|
// then
|
||||||
|
done.fail('resolve callback called');
|
||||||
|
}).catch((e) => {
|
||||||
|
expect(e).toEqual(jasmine.any(Error));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error on missing data.auth', (done) => {
|
||||||
|
// given
|
||||||
|
delete data.auth;
|
||||||
|
// when
|
||||||
|
initializer.initialize().then(() => {
|
||||||
|
// then
|
||||||
|
done.fail('resolve callback called');
|
||||||
|
}).catch((e) => {
|
||||||
|
expect(e).toEqual(jasmine.any(Error));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw error on missing data.lang', (done) => {
|
||||||
|
// given
|
||||||
|
delete data.lang;
|
||||||
|
// when
|
||||||
|
initializer.initialize().then(() => {
|
||||||
|
// then
|
||||||
|
done.fail('resolve callback called');
|
||||||
|
}).catch((e) => {
|
||||||
|
expect(e).toEqual(jasmine.any(Error));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should resolve on DOMContentLoaded event', (done) => {
|
||||||
|
// given
|
||||||
|
spyOnProperty(document, 'readyState').and.returnValue('loading');
|
||||||
|
// when
|
||||||
|
uInitializer.waitForDom().then(() => {
|
||||||
|
// then
|
||||||
|
console.log(document.readyState);
|
||||||
|
done();
|
||||||
|
}).catch((e) => done.fail(`reject callback called (${e})`));
|
||||||
|
|
||||||
|
document.dispatchEvent(new Event('DOMContentLoaded'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should resolve on DOM ready', (done) => {
|
||||||
|
// given
|
||||||
|
spyOnProperty(document, 'readyState').and.returnValue('complete');
|
||||||
|
// when
|
||||||
|
uInitializer.waitForDom().then(() => {
|
||||||
|
// then
|
||||||
|
done();
|
||||||
|
}).catch((e) => done.fail(`reject callback called (${e})`));
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
67
utils/getinit.php
Normal file
67
utils/getinit.php
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
/* μlogger
|
||||||
|
*
|
||||||
|
* Copyright(C) 2017 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once(dirname(__DIR__) . "/helpers/auth.php");
|
||||||
|
require_once(ROOT_DIR . "/helpers/config.php");
|
||||||
|
require_once(ROOT_DIR . "/helpers/lang.php");
|
||||||
|
|
||||||
|
$auth = new uAuth();
|
||||||
|
$langStrings = (new uLang(uConfig::$lang))->getStrings();
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
$resultAuth = [
|
||||||
|
"isAdmin" => $auth->isAdmin(),
|
||||||
|
"isAuthenticated" => $auth->isAuthenticated()
|
||||||
|
];
|
||||||
|
if ($auth->isAuthenticated()) {
|
||||||
|
$resultAuth["userId"] = $auth->user->id;
|
||||||
|
$resultAuth["userLogin"] = $auth->user->login;
|
||||||
|
}
|
||||||
|
|
||||||
|
$resultConfig = [
|
||||||
|
"interval" => uConfig::$interval,
|
||||||
|
"units" => uConfig::$units,
|
||||||
|
"lang" => uConfig::$lang,
|
||||||
|
"mapApi" => uConfig::$mapapi,
|
||||||
|
"gkey" => uConfig::$gkey,
|
||||||
|
"initLatitude" => uConfig::$init_latitude,
|
||||||
|
"initLongitude" => uConfig::$init_longitude,
|
||||||
|
"passRegex" => uConfig::passRegex(),
|
||||||
|
"strokeWeight" => uConfig::$strokeWeight,
|
||||||
|
"strokeColor" => uConfig::$strokeColor,
|
||||||
|
"strokeOpacity" => uConfig::$strokeOpacity,
|
||||||
|
"olLayers" => []
|
||||||
|
];
|
||||||
|
foreach (uConfig::$ol_layers as $key => $val) {
|
||||||
|
$resultConfig["olLayers"][$key] = $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
$resultLang = [];
|
||||||
|
foreach ($langStrings as $key => $val) {
|
||||||
|
$resultLang[$key] = $val;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result["auth"] = $resultAuth;
|
||||||
|
$result["config"] = $resultConfig;
|
||||||
|
$result["lang"] = $resultLang;
|
||||||
|
|
||||||
|
header("Content-type: application/json");
|
||||||
|
echo json_encode($result);
|
||||||
|
|
||||||
|
?>
|
Loading…
x
Reference in New Issue
Block a user