151 lines
3.3 KiB
JavaScript
151 lines
3.3 KiB
JavaScript
"use strict";
|
|
|
|
import {Common} from "../Utils/Common.ecma.js";
|
|
import {RouteModel} from "../Models/RouteModel.ecma.js";
|
|
|
|
/**
|
|
* @typedef {import("../AnP.ecma.js").AnP} AnP
|
|
*/
|
|
|
|
/**
|
|
* @class RoutesManager
|
|
* @constructor
|
|
* @param {!AnP} anp
|
|
* @return {void}
|
|
* @access public
|
|
* @static
|
|
*/
|
|
export const RoutesManager = (function(){
|
|
|
|
/**
|
|
* @callback continue_callback
|
|
* @param {!boolean} ok
|
|
* @return {boolean}
|
|
*/
|
|
|
|
/**
|
|
* @constructs RoutesManager
|
|
* @param {!AnP} anp
|
|
* @return {void}
|
|
* @access private
|
|
* @static
|
|
*/
|
|
const RoutesManager = function(anp){
|
|
|
|
/** @type {RoutesManager} */
|
|
const self = this,
|
|
/** @type {Array.<RouteModel>} */
|
|
routes = [];
|
|
/** @type {boolean} */
|
|
let started = false,
|
|
/** @type {string} */
|
|
last_url = "",
|
|
/** @type {number|null} */
|
|
thread = null;
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const constructor = () => {
|
|
|
|
thread = anp.threads.add(thread_method, {
|
|
bucle : true,
|
|
autoplay : true
|
|
});
|
|
|
|
};
|
|
|
|
/**
|
|
* @returns {void}
|
|
* @access private
|
|
*/
|
|
const thread_method = () => {
|
|
|
|
/** @type {string} */
|
|
const current_url = window.location.hash.substring(1);
|
|
|
|
if(last_url != current_url){
|
|
last_url = current_url;
|
|
self.go(last_url);
|
|
};
|
|
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.update = (callback = null) => {
|
|
Common.execute_array(["default_routes_files", "routes_files", "default_routes", "routes"], (key, next) => {
|
|
self.add(anp.settings.get(key), true, next);
|
|
}, callback, true);
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.reset = (callback = null) => {
|
|
|
|
routes.splice(0, routes.length);
|
|
|
|
self.update(callback);
|
|
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.start = (callback = null) => {
|
|
|
|
/** @type {continue_callback} */
|
|
const end = ok => Common.execute(callback, ok);
|
|
|
|
if(started){
|
|
end(false);
|
|
return false;
|
|
};
|
|
started = true;
|
|
|
|
self.update(() => end(true));
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* @param {?continue_callback} callback
|
|
* @returns {boolean}
|
|
* @access public
|
|
*/
|
|
this.close = (callback = null) => {
|
|
|
|
/** @type {continue_callback} */
|
|
const end = ok => Common.execute(callback, ok);
|
|
|
|
if(!started){
|
|
end(false);
|
|
return false;
|
|
};
|
|
started = false;
|
|
|
|
end(true);
|
|
|
|
return true;
|
|
};
|
|
|
|
this.add = (inputs, overwrite = false, callback = null) => {
|
|
Common.execute(callback, true);
|
|
};
|
|
|
|
this.go = path => {};
|
|
|
|
constructor();
|
|
};
|
|
|
|
return RoutesManager;
|
|
})(); |