ulogger-server/js/track.js

177 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-05-15 11:32:36 +02:00
import { config } from './constants.js';
import uAjax from './ajax.js';
import uData from './data.js';
import uEvent from './event.js';
import uPosition from './position.js';
/**
* @class uTrack
* @extends {uData}
* @property {number} id
* @property {string} name
* @property {uUser} user
* @property {?uPosition[]} positions
* @property {?Array<{x: number, y: number}>} plotData
2017-04-14 17:24:09 +02:00
*/
2019-05-15 11:32:36 +02:00
export default class uTrack extends uData {
2017-04-14 17:24:09 +02:00
2019-05-15 11:32:36 +02:00
/**
* @param {number} id
* @param {string} name
* @param {uUser} user
*/
constructor(id, name, user) {
super(id, name, 'id', 'name');
this._user = user;
this._positions = null;
this._plotData = null;
this._maxId = 0;
this._onlyLatest = false;
}
2017-04-14 17:24:09 +02:00
/**
2019-05-15 11:32:36 +02:00
* @return {?uPosition[]}
*/
2019-05-15 11:32:36 +02:00
get positions() {
return this._positions;
}
/**
* @param {uUser} user
*/
set user(user) {
this._user = user;
}
/**
* @return {uUser}
*/
get user() {
return this._user;
}
/**
* @param {boolean} value
*/
set onlyLatest(value) {
this._onlyLatest = value;
}
clear() {
this._positions = null;
this._plotData = null;
}
/**
* Get track data from xml
* @param {XMLDocument} xml
* @param {boolean} isUpdate
*/
fromXml(xml, isUpdate) {
let positions = [];
let plotData = [];
let totalDistance = 0;
let totalSeconds = 0;
if (isUpdate && this._positions) {
positions = this._positions;
plotData = this._plotData;
totalDistance = positions[positions.length - 1].totalDistance;
totalSeconds = positions[positions.length - 1].totalSeconds;
}
2019-05-15 11:32:36 +02:00
const xmlPos = xml.getElementsByTagName('position');
for (xml of xmlPos) {
const position = uPosition.fromXml(xml);
totalDistance += position.distance;
totalSeconds += position.seconds;
position.totalDistance = totalDistance;
position.totalSeconds = totalSeconds;
positions.push(position);
if (position.altitude != null) {
plotData.push({ x: position.totalDistance, y: position.altitude * config.factor_m });
}
if (position.id > this._maxId) {
this._maxId = position.id;
}
}
2019-05-15 11:32:36 +02:00
this._positions = positions;
this._plotData = plotData;
}
2017-04-14 17:24:09 +02:00
2019-05-15 11:32:36 +02:00
/**
* @return {?Array<{x: number, y: number}>}
*/
get plotData() {
return this._plotData;
}
2017-04-14 17:24:09 +02:00
/**
2019-05-15 11:32:36 +02:00
* @return {number}
*/
get length() {
return this._positions ? this._positions.length : 0;
}
/**
* @return {boolean}
*/
2019-05-15 11:32:36 +02:00
get hasPositions() {
return this._positions !== null;
2017-04-14 17:24:09 +02:00
}
/**
2019-05-15 11:32:36 +02:00
* @throws
* @return {Promise<void>}
*/
2019-05-15 11:32:36 +02:00
fetch() {
const data = {
userid: this._user.id
};
let isUpdate = this.hasPositions;
if (config.showLatest) {
data.last = 1;
isUpdate = false;
} else {
data.trackid = this.id;
2017-04-14 17:24:09 +02:00
}
2019-05-15 11:32:36 +02:00
if (this._onlyLatest !== config.showLatest) {
this._onlyLatest = config.showLatest;
isUpdate = false;
} else {
data.afterid = this._maxId;
2017-04-14 17:24:09 +02:00
}
2019-05-15 11:32:36 +02:00
return uAjax.get('utils/getpositions.php', data, {
// loader: ui.trackTitle
}).then((xml) => {
this.fromXml(xml, isUpdate);
return this.render();
});
}
2019-05-15 11:32:36 +02:00
/**
*
* @param {string} action
* @return {Promise<void>}
*/
update(action) {
return uAjax.post('utils/handletrack.php',
{
action: action,
trackid: this.id,
trackname: this.name
});
2017-04-14 17:24:09 +02:00
}
2019-05-15 11:32:36 +02:00
render() {
this.emit(uEvent.TRACK_READY);
}
2019-05-15 11:32:36 +02:00
/**
* Export to file
* @param {string} type File type
*/
export(type) {
const url = `utils/export.php?type=${type}&userid=${this._user.id}&trackid=${this.id}`;
this.emit(uEvent.OPEN_URL, url);
}
}