UNPKG

@akveo/nga-theme

Version:
199 lines 7.93 kB
/** * @license * Copyright Akveo. All Rights Reserved. * Licensed under the MIT License. See License.txt in the project root for license information. */ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import { Injectable, Inject, InjectionToken } from '@angular/core'; import { Location } from '@angular/common'; import { Router } from '@angular/router'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; import { ReplaySubject } from 'rxjs/ReplaySubject'; import { List } from 'immutable'; import 'rxjs/add/operator/publish'; var itemClick$ = new ReplaySubject(1); var addItems$ = new ReplaySubject(1); var navigateHome$ = new ReplaySubject(1); var getSelectedItem$ = new ReplaySubject(1); var itemSelect$ = new ReplaySubject(1); var itemHover$ = new ReplaySubject(1); var submenuToggle$ = new ReplaySubject(1); var NgaMenuItem = (function () { function NgaMenuItem() { this.pathMath = 'full'; // TODO: is not set if item is initialized by default, should be set anyway } return NgaMenuItem; }()); export { NgaMenuItem }; export var ngaMenuOptionsToken = new InjectionToken('NGA_MENU_OPTIONS'); // TODO: map select events to router change events var NgaMenuService = (function () { function NgaMenuService() { } NgaMenuService.prototype.addItems = function (items, tag) { addItems$.next({ tag: tag, items: items }); }; NgaMenuService.prototype.navigateHome = function (tag) { navigateHome$.next({ tag: tag }); }; NgaMenuService.prototype.getSelectedItem = function (tag) { var listener = new BehaviorSubject(null); getSelectedItem$.next({ tag: tag, listener: listener }); return listener.asObservable(); }; NgaMenuService.prototype.onItemClick = function () { return itemClick$.publish().refCount(); }; NgaMenuService.prototype.onItemSelect = function () { return itemSelect$.publish().refCount(); }; NgaMenuService.prototype.onItemHover = function () { return itemHover$.publish().refCount(); }; NgaMenuService.prototype.onSubmenuToggle = function () { return submenuToggle$.publish().refCount(); }; return NgaMenuService; }()); NgaMenuService = __decorate([ Injectable() ], NgaMenuService); export { NgaMenuService }; var NgaMenuInternalService = (function () { function NgaMenuInternalService(router, location, options) { this.router = router; this.location = location; this.options = options; this.stack = List(); this.items = List(); if (options && options.items) { this.items = List(this.options.items); } else { this.items = List(); } } NgaMenuInternalService.prototype.getItems = function () { return List(this.items); }; NgaMenuInternalService.prototype.prepareItems = function (items) { var _this = this; items.forEach(function (i) { return _this.setParent(i); }); items.forEach(function (i) { return _this.prepareItem(i); }); this.clearStack(); }; NgaMenuInternalService.prototype.onAddItem = function () { return addItems$.publish().refCount(); }; NgaMenuInternalService.prototype.onNavigateHome = function () { return navigateHome$.publish().refCount(); }; NgaMenuInternalService.prototype.onGetSelectedItem = function () { return getSelectedItem$.publish().refCount(); }; NgaMenuInternalService.prototype.itemHover = function (item, tag) { itemHover$.next({ tag: tag, item: item, }); }; NgaMenuInternalService.prototype.submenuToggle = function (item, tag) { submenuToggle$.next({ tag: tag, item: item, }); }; NgaMenuInternalService.prototype.resetItems = function (items) { var _this = this; items.forEach(function (i) { return _this.resetItem(i); }); this.clearStack(); }; NgaMenuInternalService.prototype.itemSelect = function (item, tag) { itemSelect$.next({ tag: tag, item: item, }); }; NgaMenuInternalService.prototype.itemClick = function (item, tag) { itemClick$.next({ tag: tag, item: item, }); }; NgaMenuInternalService.prototype.resetItem = function (parent) { var _this = this; parent.selected = false; this.stack = this.stack.push(parent); if (parent.children && parent.children.size > 0) { var firstSelected = parent.children.filter(function (c) { return !_this.stack.contains(c); }).first(); if (firstSelected) { firstSelected.selected = false; this.resetItem(firstSelected); } } if (parent.parent) { this.resetItem(parent.parent); } }; NgaMenuInternalService.prototype.setParent = function (parent) { if (parent.children && parent.children.size > 0) { var firstItemWithoutParent = parent.children.filter(function (c) { return c.parent === null || c.parent === undefined; }).first(); if (firstItemWithoutParent) { firstItemWithoutParent.parent = parent; this.setParent(firstItemWithoutParent); } } if (parent.parent) { this.setParent(parent.parent); } }; NgaMenuInternalService.prototype.prepareItem = function (parent) { var _this = this; parent.selected = false; this.stack = this.stack.push(parent); if (parent.expanded) { if (parent.parent) { parent.parent.expanded = true; } } var exact = parent.pathMath === 'full'; if ((exact && this.location.path() === parent.link) || (!exact && this.location.path().includes(parent.link))) { parent.selected = true; if (parent.parent) { parent.parent.expanded = true; } } if (parent.children && parent.children.size > 0) { var firstUnchecked = parent.children.filter(function (c) { return !_this.stack.contains(c); }).first(); if (firstUnchecked) { this.prepareItem(firstUnchecked); } } if (parent.parent) { this.prepareItem(parent.parent); } }; NgaMenuInternalService.prototype.clearStack = function () { this.stack = this.stack.clear(); }; return NgaMenuInternalService; }()); NgaMenuInternalService = __decorate([ Injectable(), __param(2, Inject(ngaMenuOptionsToken)), __metadata("design:paramtypes", [Router, Location, Object]) ], NgaMenuInternalService); export { NgaMenuInternalService }; //# sourceMappingURL=menu.service.js.map