All files / web/bundles/pimui/js router.js

80.82% Statements 59/73
70.37% Branches 19/27
90.48% Functions 19/21
80.82% Lines 59/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261    1183x                                                       1183x                           1183x 1183x 1183x   1183x   1183x                 1133x                 3883x 2750x     3883x       3883x 3883x     3883x       3883x 3883x   3883x 3883x 2718x     3883x 3883x   3883x 4x   4x     3879x 3879x 3879x 3879x   3717x                               1x 1x     1x             1x 1x                   1x                   6x 6x                 3883x 3883x 3883x 3883x 3883x                 3717x 3717x 3717x 3717x             3900x             3730x             1656x             3883x             835x 1x     834x 834x             317x             4x 4x             3725x                             1183x      
'use strict';
 
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'backbone',
        'routing',
        'pim/route-matcher',
        'oro/loading-mask',
        'pim/controller-registry',
        'oro/mediator',
        'pim/error',
        'pim/security-context',
        'pim/controller/template'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        Routing,
        RouteMatcher,
        LoadingMask,
        ControllerRegistry,
        mediator,
        Error,
        securityContext
    ) {
        var Router = Backbone.Router.extend({
            DEFAULT_ROUTE: 'oro_default',
            routes: {
                '': 'index',
                '*path': 'defaultRoute'
            },
            indexRoute: null,
            loadingMask: null,
            currentController: null,
 
            /**
             * {@inheritdoc}
             */
            initialize: function () {
                this.loadingMask = new LoadingMask();
                this.loadingMask.render().$el.appendTo($('.hash-loading-mask'));
                _.bindAll(this, 'showLoadingMask', 'hideLoadingMask');
 
                this.indexRoute = __moduleConfig.indexRoute;
 
                this.listenTo(mediator, 'route_complete', this._processLinks);
            },
 
            /**
             * Go to the index of the app
             *
             * @return {String}
             */
            index: function () {
                return this.defaultRoute(this.generate(this.indexRoute));
            },
 
            /**
             * Render the given route
             *
             * @param {String} path
             */
            defaultRoute: function (path) {
                if (path.indexOf('/') !== 0) {
                    path = '/' + path;
                }
 
                Iif (path.indexOf('|') !== -1) {
                    path = path.substring(0, path.indexOf('|'));
                }
 
                var route = this.match(path);
                Iif (false === route) {
                    return this.notFound();
                }
                Iif (this.DEFAULT_ROUTE === route.name) {
                    return this.index();
                }
 
                this.showLoadingMask();
                this.triggerStart(route);
 
                ControllerRegistry.get(route.name).done(function (controller) {
                    if (this.currentController) {
                        this.currentController.remove();
                    }
 
                    $('#container').empty();
                    var $view = $('<div class="view"/>').appendTo($('#container'));
 
                    if (controller.aclResourceId && !securityContext.isGranted(controller.aclResourceId)) {
                        this.hideLoadingMask();
 
                        return this.displayErrorPage(__('error.forbidden'), '403');
                    }
 
                    controller.el = $view;
                    this.currentController = new controller.class(controller);
                    this.currentController.setActive(true);
                    this.currentController.renderRoute(route, path)
                        .done(function () {
                            this.triggerComplete(route);
                        }.bind(this))
                        .fail(this.handleError.bind(this))
                        .always(this.hideLoadingMask)
                    ;
                }.bind(this));
            },
 
            /**
             * Manage not found error
             */
            notFound: function () {
                this.displayErrorPage('Page not found', 404);
            },
 
            handleError: function (xhr) {
                Eif (undefined !== xhr) {
                    this.displayErrorPage('An error occured');
                }
 
                switch (xhr.status) {
                    case 401:
                        window.location = this.generate('pim_user_security_login');
                        break;
                    case 200:
                        break;
                    default:
                        this.errorPage(xhr);
                        break;
                }
            },
 
            /**
             * Manage error from xhr calls
             *
             * @param {Object} xhr
             */
            errorPage: function (xhr) {
                this.displayErrorPage(xhr.statusText, xhr.status);
            },
 
            /**
             * Display the error page
             *
             * @param {String} message
             * @param {Integer} code
             */
            displayErrorPage: function (message, code) {
                var errorView = new Error(message, code);
                errorView.setElement($('#container')).render();
            },
 
            /**
             * Trigger route start events
             *
             * @param {String} route
             */
            triggerStart: function (route) {
                this.trigger('route:' + route.name, route.params);
                this.trigger('route_start', route.name, route.params);
                this.trigger('route_start:' + route.name, route.params);
                mediator.trigger('route_start', route.name, route.params);
                mediator.trigger('route_start:' + route.name, route.params);
            },
 
            /**
             * Trigger completed route events
             *
             * @param {String} route
             */
            triggerComplete: function (route) {
                this.trigger('route_complete', route.name, route.params);
                this.trigger('route_complete:' + route.name, route.params);
                mediator.trigger('route_complete', route.name, route.params);
                mediator.trigger('route_complete:' + route.name, route.params);
            },
 
            /**
             * Display the loading mask
             */
            showLoadingMask: function () {
                this.loadingMask.show();
            },
 
            /**
             * Hide the loading mask
             */
            hideLoadingMask: function () {
                this.loadingMask.hide();
            },
 
            /**
             * {@inheritdoc}
             */
            generate: function () {
                return Routing.generate.apply(Routing, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            match: function () {
                return RouteMatcher.match.apply(RouteMatcher, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            redirect: function (fragment, options) {
                if (this.currentController && !this.currentController.canLeave()) {
                    return false;
                }
 
                fragment = fragment.indexOf('#') === 0 ? fragment : '#' + fragment;
                Backbone.history.navigate(fragment, options);
            },
 
            /**
             * Redirect to the given route
             */
            redirectToRoute: function (route, routeParams, options) {
                return this.redirect(Routing.generate(route, routeParams), options);
            },
 
            /**
             * Reload the current page
             */
            reloadPage: function () {
                var fragment = window.location.hash;
                this.redirect(fragment, {trigger: true});
            },
 
            /**
             * Process route links in the current page
             */
            _processLinks: function () {
                _.each($('a[route]'), function (link) {
                    var route = link.getAttribute('route');
                    var routeParams = link.getAttribute('route-params');
                    if (routeParams) {
                        try {
                            routeParams = JSON.parse(routeParams.replace(/'/g, '"'));
                        } catch (error) {
                            routeParams = null;
                        }
                    }
                    link.setAttribute('href', '#' + Routing.generate(route, routeParams));
                });
            }
        });
 
        return new Router();
    }
);