(function () { "use strict"; angular.module('app.service') .constant('navigationMenu', (function () { var menu = { global: { index: { _state: { name: 'index', url: '/', templateUrl: 'controller/index/indexCtrl.html', controller: 'IndexCtrl', data: { name: 'Home' } } }, login: { _state: { name: 'login', url: '/login', templateUrl: 'controller/login/loginCtrl.html', controller: 'LoginCtrl', data: { name: 'Login' } } }, register: { _state: { name: 'register', url: '/register', templateUrl: 'controller/register/registerCtrl.html', controller: 'RegisterCtrl', data: { name: 'Register' } } }, contact: { _state: { name: 'contact', url: '/contact', templateUrl: 'controller/contact/contactCtrl.html', controller: 'ContactCtrl', data: { name: 'Contact' } } }, about: { _state: { name: 'about', url: '/about', templateUrl: 'controller/about/aboutCtrl.html', controller: 'AboutCtrl', data: { name: 'About' } } }, ticket: { _state: { name: 'ticket', url: '/prop/{alias:.+}', templateUrl: 'controller/ticket/ticketCtrl.html', controller: 'TicketCtrl', data: { name: 'Ticket' } } }, menu: { _state: { name: 'menu', url: '/menu', views: { '': { resolve: { auth: ['user', function (user) { return user.authResolve(null); }] }, templateUrl: 'controller/menu/menuCtrl.html', controller: 'MenuCtrl' } }, data: { name: 'Menu' } } } }, user: { _state: { abstract: true, name: 'menu.user', url: '/user', template: '
', resolve: { auth: ['user', function (user) { return user.authResolve('user'); }] } }, profile: { _state: { _page: true, name: 'menu.user.profile', url: '/profile', data: { name: 'Profile', attr: {'ad-user-profile-form': ''} } } }, ticket: { _state: { _page: true, name: 'menu.user.ticket', url: '/ticket', data: { name: 'Tickets', attr: {'ad-ticket-manage-list': ''} } }, create: { _state: { _page: true, name: 'menu.user.ticket.create', url: '/create', data: { name: 'Ticket', attr: {'ad-ticket-form': ''} } } }, edit: { _state: { _page: true, name: 'menu.user.ticket.edit', url: '/edit/{id:[0-9]{1,10}}', data: { name: 'Ticket', attr: {'ad-ticket-form': '{id}'} } } }, file: { _state: { _page: true, name: 'menu.admin.ticket.file', url: '/file/{id:[0-9]{1,10}}', data: { name: 'Ticket Images', attr: {'ad-admin-ticket-file-form': '{id}'} } } } } }, manager: { _state: { abstract: true, name: 'menu.manager', url: '/manager', template: '
', resolve: { auth: ['user', function (user) { return user.authResolve('manager'); }] } }, profile: { _state: { _page: true, name: 'menu.manager.profile', url: '/profile', data: { name: 'Profile', attr: {'ad-user-profile-form': ''} } } }, user: { _state: { _page: true, name: 'menu.manager.user', url: '/user', data: { name: 'Users', attr: {'ad-admin-user-list': ''} } }, view: { _state: { _page: true, name: 'menu.manager.user.view', url: '/view/{id:[0-9]{1,10}}', data: { name: 'User', attr: {'ad-admin-user-view': '{id}'} } } }, create: { _state: { _page: true, name: 'menu.manager.user.create', url: '/create', data: { name: 'User', attr: {'ad-admin-user-manage-form': ''} } } }, edit: { _state: { _page: true, name: 'menu.manager.user.edit', url: '/edit/{id:[0-9]{1,10}}', data: { name: 'User', attr: {'ad-admin-user-manage-form': '{id}'} } } } }, ticket: { _state: { _page: true, name: 'menu.manager.ticket', url: '/ticket', data: { name: 'Tickets', attr: {'ad-admin-ticket-list': ''} } }, create: { _state: { _page: true, name: 'menu.manager.ticket.create', url: '/create', data: { name: 'Ticket', attr: {'ad-ticket-form': ''} } } }, edit: { _state: { _page: true, name: 'menu.manager.ticket.edit', url: '/edit/{id:[0-9]{1,10}}', data: { name: 'Ticket', attr: {'ad-ticket-form': '{id}'} } } }, file: { _state: { _page: true, name: 'menu.manager.ticket.file', url: '/file/{id:[0-9]{1,10}}', data: { name: 'Ticket Images', attr: {'ad-admin-ticket-file-form': '{id}'} } } } } }, admin: { _state: { abstract: true, name: 'menu.admin', url: '/admin', template: '
', resolve: { auth: ['user', function (user) { return user.authResolve('admin'); }] } }, profile: { _state: { _page: true, name: 'menu.admin.profile', url: '/profile', data: { name: 'Profile', attr: {'ad-user-profile-form': ''} } } }, user: { _state: { _page: true, name: 'menu.admin.user', url: '/user', data: { name: 'Users', attr: {'ad-admin-user-list': ''} } }, view: { _state: { _page: true, name: 'menu.admin.user.view', url: '/view/{id:[0-9]{1,10}}', data: { name: 'User', attr: {'ad-admin-user-view': '{id}'} } } }, create: { _state: { _page: true, name: 'menu.admin.user.create', url: '/create', data: { name: 'User', attr: {'ad-admin-user-manage-form': ''} } } }, edit: { _state: { _page: true, name: 'menu.admin.user.edit', url: '/edit/{id:[0-9]{1,10}}', data: { name: 'User', attr: {'ad-admin-user-manage-form': '{id}'} } } } }, ticket: { _state: { _page: true, name: 'menu.admin.ticket', url: '/ticket', data: { name: 'Tickets', attr: {'ad-admin-ticket-list': ''} } }, create: { _state: { _page: true, name: 'menu.admin.ticket.create', url: '/create', data: { name: 'Ticket', attr: {'ad-ticket-form': ''} } } }, edit: { _state: { _page: true, name: 'menu.admin.ticket.edit', url: '/edit/{id:[0-9]{1,10}}', data: { name: 'Ticket', attr: {'ad-ticket-form': '{id}'} } } } }, tag: { _state: { _page: true, name: 'menu.admin.tag', url: '/tag', data: { name: 'Tags', attr: {'ad-admin-tag-list': ''} } }, create: { _state: { _page: true, name: 'menu.admin.tag.create', url: '/create', data: { name: 'Tag', attr: {'ad-admin-tag-manage-form': ''} } } }, edit: { _state: { _page: true, name: 'menu.admin.tag.edit', url: '/edit/{id:[0-9]{1,10}}', data: { name: 'Tag', attr: {'ad-admin-tag-manage-form': '{id}'} } } }, link: { _state: { _page: true, name: 'menu.admin.tag.link', url: '/link/{id:[0-9]{1,10}}', data: { name: 'Tag Links', attr: {'ad-admin-tag-link-form': '{id}'} } } } } } }; var processMenu = function (menu) { angular.forEach(menu, function (state, key) { if ('_state' === key) { if (state._page) { processState(state); } } else { processMenu(state); } }); }; var processState = function (state) { state.views = { 'header@menu': { templateProvider: ['translate', function (translate) { return (state.data && state.data.name) ? translate(state.data.name) : '' }] }, '@menu': { resolve: { item: (function (item) { return function () { return item; } })(state) }, templateUrl: 'controller/page/pageCtrl.html', controller: 'PageCtrl' } }; }; angular.forEach(['global', 'user', 'manager', 'admin'], function (name) { processMenu(menu[name]); }); return menu; })()); angular.module('app.service') .factory('navigation', ['$rootScope', '$state', 'cfg', 'navigationMenu', 'translate', 'user', 'notification', function ($rootScope, $state, cfg, navigationMenu, translate, user, notification) { var roleMenuList = { user: [ navigationMenu.user.profile._state, navigationMenu.user.ticket._state ], manager: [ navigationMenu.manager.profile._state, navigationMenu.manager.user._state, navigationMenu.manager.ticket._state ], admin: [ navigationMenu.admin.profile._state, navigationMenu.admin.user._state, navigationMenu.admin.ticket._state, navigationMenu.admin.tag._state ] }; var Navigation = function () { /** * Menu items list (filled according to user.roleId) * @type {Array} */ this.list = []; this.init(); }; Navigation.prototype.refresh = function () { this.list = (user.authenticated && user.roleId in roleMenuList) ? angular.copy(roleMenuList[user.roleId]) : []; return this; }; Navigation.prototype.init = function () { var self = this; $rootScope.$watch(function () { return user.authenticated; }, function () { self.refresh(); }); $rootScope.$on('$stateChangeStart', function (e, to, toParams, from, fromParams) { var route = 'menu.' + user.roleId + '.profile'; if (user.authenticated && null == user.phone && to.name !== route) { notification.temporary(translate('Please enter your phone number'), 'warning', 'navigation'); $state.go(route); e.preventDefault(); } }); return this; }; return new Navigation(); }]); })();