From 24f596abae0fbde5a97c6a4c16a5182bc9d380a0 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 22 Feb 2016 01:46:36 +0300 Subject: [PATCH] menu service ES6 refactoring --- src/client/app.js | 3 ++- src/client/services/menu.js | 32 ++++++++++++++++++++++++++++++++ src/client/ui/menu.js | 36 ------------------------------------ 3 files changed, 34 insertions(+), 37 deletions(-) create mode 100644 src/client/services/menu.js delete mode 100644 src/client/ui/menu.js diff --git a/src/client/app.js b/src/client/app.js index dc0cea0c..0e020831 100644 --- a/src/client/app.js +++ b/src/client/app.js @@ -11,6 +11,7 @@ import HomeController from './controllers/HomeController'; import TeamController from './controllers/TeamController'; import DocumentsController from './controllers/DocumentsController'; import IOService from './services/io'; +import MenuService from './services/menu'; import api from './api/api'; let deps = [ @@ -89,9 +90,9 @@ app.run(($location, $rootScope, $http) => { [ api, IOService, + MenuService, require('./directives/mainMenu'), require('./directives/backgroundPicture'), - require('./ui/menu'), require('./ui/breadcrumbs'), require('./ui/timeline'), require('./filters/strip'), diff --git a/src/client/services/menu.js b/src/client/services/menu.js new file mode 100644 index 00000000..53cc8784 --- /dev/null +++ b/src/client/services/menu.js @@ -0,0 +1,32 @@ +import menuItems from '../../models/menu'; + +class Menu { + constructor() { + this.items = menuItems; + this.current = null; + } + + activate(path) { + let filteredItems = this.items.filter(i => i.url === path); + + if (filteredItems.length) { + this.current = filteredItems[0]; + } + } +} + +export default function (app) { + app.factory('menu', ($route, $rootScope) => { + const menu = new Menu(); + $rootScope.$on('$routeChangeSuccess', (event, current) => { + try { + let path = current['$$route'].originalPath; + menu.activate(path); + } catch (e) { + + } + }); + + return menu; + }); +}; diff --git a/src/client/ui/menu.js b/src/client/ui/menu.js deleted file mode 100644 index 1b3e6c72..00000000 --- a/src/client/ui/menu.js +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Created by tmshv on 05/11/14. - */ - -import menuItems from '../../models/menu'; - -module.exports = function (app) { - app.factory("menu", function ($route, $rootScope) { - function Menu() { - this.items = menuItems; - this.current = null; - } - - Menu.prototype.activate = function (path) { - var f = this.items.filter(function (i) { - return i.url === path; - }); - - if (f.length) { - this.current = f[0]; - } - }; - - var menu = new Menu(); - $rootScope.$on('$routeChangeSuccess', function (event, current) { - try { - var path = current['$$route'].originalPath; - menu.activate(path); - } catch (e) { - - } - }); - - return menu; - }); -}; \ No newline at end of file