init

method
 init() 

Option name Type Description
options

Init the history

init: function(options) {
    _options = owl.util.extend(_defaultOptions, options, true);
},

getOption

method
 getOption() 

Option name Type Description
name
return any

Gets an option

getOption: function(name) {
    return _options[name];
},

start

method
 start() 

Starts watching popstate event

start: function() {
    var that = this;
    this.open(this.getLocation());
    _listener = window.addEventListener('popstate', function() {
        that.open(that.getLocation());
    }, false);
    _started = true;
},

isStarted

method
 isStarted() 

Option name Type Description
return boolean

Checks is history started

isStarted: function() {
    return _started;
},

stop

method
 stop() 

Stop watching popstate event

stop: function() {
    window.removeEventListener('popstate', _listener);
},
Option name Type Description
path

Adds new item to the navigation history

navigate: function(path) {
    window.history.pushState(null, null, _options.baseUrl + path);
    this.open(path);
},

replace

method
 replace() 

Option name Type Description
path

Replaces current item in the navigation history

replace: function(path) {
    window.history.replaceState(null, null, _options.baseUrl + path);
    this.open(path);
},

getLocation

method
 getLocation() 

Option name Type Description
return string

Gets current location

getLocation: function () {
    return window.location.pathname.replace(_options.baseUrl, '').replace(/\/$/, '');
},

getHash

method
 getHash() 

Option name Type Description
return string

Gets current hash

getHash: function() {
    return window.location.hash.substr(1);
},

setHash

method
 setHash() 

Option name Type Description
hash

Sets hash

setHash: function(hash) {
    window.location.hash = hash;
},

open

method
 open() 

Option name Type Description
path
return Promise

A promise that resolves to set destroyer function if any given

Opens the page by path

open: function(path) {
    var router;
    if (_destroyFunction) {
        _destroyFunction();
        _destroyFunction = null;
    }
    Object.keys(_routers).some(function(routerPath) {
        if(path === routerPath ||
            (path.indexOf(routerPath) === 0 && path.length > routerPath.length && path[routerPath.length] === '/')) {
            router = _routers[routerPath];
            path = path.replace(routerPath, '');
            return true;
        }
        return false;
    });
    if (!router) {
        router = _defaultRouter;
    }
    if (!router) {
        console.log('Default router is not defined');
        return;
    }
    this.trigger('change');

    return router.open(path).then(function (destroyer) {
      _destroyFunction = destroyer;
    });
},

setRouter

method
 setRouter() 

Option name Type Description
name
router

Sets router by name

setRouter: function(path, router) {
    _routers[path] = router;
},

removeRouter

method
 removeRouter() 

Option name Type Description
name

Removes router by name

removeRouter: function(path) {
    delete _routers[path];
},

getRouter

method
 getRouter() 

Option name Type Description
name
return owl.Router

Gets router by name

getRouter: function(path) {
    return _routers[path];
},

setDefaultRouter

method
 setDefaultRouter() 

Option name Type Description
router

Sets default router

setDefaultRouter: function(router) {
    _defaultRouter = router;
},

getDefaultRouter

method
 getDefaultRouter() 

Gets default router

getDefaultRouter: function() {
    return _defaultRouter;
},

setResolve

method
 setResolve() 

Option name Type Description
resolveName
resolveCallback

Sets resolve

setResolve: function(resolveName, resolveCallback) {
    _resolves[resolveName] = resolveCallback;
},

removeResolve

method
 removeResolve() 

Option name Type Description
resolveName

Removes resolve by name

removeResolve: function(resolveName) {
    delete _resolves[resolveName];
},

getResolve

method
 getResolve() 

Option name Type Description
resolveName

Gets resolve by name

getResolve: function(resolveName) {
    return _resolves[resolveName];
},

on

method
 on() 

Option name Type Description
event
listener

Adds event listener

on: function(event, listener) {
    if (!_events[event]) {
        _events[event] = [];
    }
    _events[event].push(listener);
},

off

method
 off() 

Option name Type Description
event
listener

Removes event listener

off: function(event, listener) {
    if (_events[event]) {
        var listenerIndex = -1;
        _events[event].forEach(function(func, index) {
            if(func === listener) {
                listenerIndex = index;
            }
        });
        _events[event].splice(listenerIndex, 1);
    }
},

trigger

method
 trigger() 

Option name Type Description
event

Trigger event

trigger: function(event) {
    _events[event] && _events[event].forEach(function(listener) {
        listener();
    });
}
    };
})(window, owl);