From 577a0a65988ba0e980f8a41f5b2d3bf1adb77b84 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 5 Feb 2018 12:31:48 +0100 Subject: [PATCH] MOBILE-2335 contextmenu: Merge menus in navbuttons component --- src/components/context-menu/context-menu.ts | 67 +++++++++++--- .../navbar-buttons/navbar-buttons.ts | 88 +++++++++++++++---- src/core/course/pages/section/section.html | 4 +- src/providers/utils/dom.ts | 52 +++++++++++ 4 files changed, 182 insertions(+), 29 deletions(-) diff --git a/src/components/context-menu/context-menu.ts b/src/components/context-menu/context-menu.ts index 7c96cfcd6..6ef697cc4 100644 --- a/src/components/context-menu/context-menu.ts +++ b/src/components/context-menu/context-menu.ts @@ -12,9 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, Input, OnInit } from '@angular/core'; +import { Component, Input, OnInit, OnDestroy, ElementRef } from '@angular/core'; import { PopoverController } from 'ionic-angular'; import { TranslateService } from '@ngx-translate/core'; +import { CoreDomUtilsProvider } from '../../providers/utils/dom'; import { CoreContextMenuItemComponent } from './context-menu-item'; import { CoreContextMenuPopoverComponent } from './context-menu-popover'; import { Subject } from 'rxjs'; @@ -26,7 +27,7 @@ import { Subject } from 'rxjs'; selector: 'core-context-menu', templateUrl: 'context-menu.html' }) -export class CoreContextMenuComponent implements OnInit { +export class CoreContextMenuComponent implements OnInit, OnDestroy { @Input() icon?: string; // Icon to be shown on the navigation bar. Default: Kebab menu icon. @Input() title?: string; // Aria label and text to be shown on the top of the popover. @@ -34,8 +35,11 @@ export class CoreContextMenuComponent implements OnInit { ariaLabel: string; protected items: CoreContextMenuItemComponent[] = []; protected itemsChangedStream: Subject; // Stream to update the hideMenu boolean when items change. + protected instanceId: string; + protected parentContextMenu: CoreContextMenuComponent; - constructor(private translate: TranslateService, private popoverCtrl: PopoverController) { + constructor(private translate: TranslateService, private popoverCtrl: PopoverController, private elementRef: ElementRef, + private domUtils: CoreDomUtilsProvider) { // Create the stream and subscribe to it. We ignore successive changes during 250ms. this.itemsChangedStream = new Subject(); this.itemsChangedStream.auditTime(250).subscribe(() => { @@ -49,6 +53,8 @@ export class CoreContextMenuComponent implements OnInit { return a.priority <= b.priority ? 1 : -1; }); }); + + this.instanceId = this.domUtils.storeInstanceByElement(elementRef.nativeElement, this); } /** @@ -65,15 +71,44 @@ export class CoreContextMenuComponent implements OnInit { * @param {CoreContextMenuItemComponent} item The item to add. */ addItem(item: CoreContextMenuItemComponent): void { - this.items.push(item); - this.itemsChanged(); + if (this.parentContextMenu) { + // All items were moved to the "parent" menu. Add the item in there. + this.parentContextMenu.addItem(item); + } else { + this.items.push(item); + this.itemsChanged(); + } } /** * Function called when the items change. */ itemsChanged(): void { - this.itemsChangedStream.next(); + if (this.parentContextMenu) { + // All items were moved to the "parent" menu, call the function in there. + this.parentContextMenu.itemsChanged(); + } else { + this.itemsChangedStream.next(); + } + } + + /** + * Merge the current context menu with the one passed as parameter. All the items in this menu will be moved to the + * one passed as parameter. + * + * @param {CoreContextMenuComponent} contextMenu The context menu where to move the items. + */ + mergeContextMenus(contextMenu: CoreContextMenuComponent): void { + this.parentContextMenu = contextMenu; + + // Add all the items to the other menu. + for (let i = 0; i < this.items.length; i++) { + contextMenu.addItem(this.items[i]); + } + + // Remove all items from the current menu. + this.items = []; + this.itemsChanged(); } /** @@ -82,11 +117,16 @@ export class CoreContextMenuComponent implements OnInit { * @param {CoreContextMenuItemComponent} item The item to remove. */ removeItem(item: CoreContextMenuItemComponent): void { - const index = this.items.indexOf(item); - if (index >= 0) { - this.items.splice(index, 1); + if (this.parentContextMenu) { + // All items were moved to the "parent" menu. Remove the item from there. + this.parentContextMenu.removeItem(item); + } else { + const index = this.items.indexOf(item); + if (index >= 0) { + this.items.splice(index, 1); + } + this.itemsChanged(); } - this.itemsChanged(); } /** @@ -100,4 +140,11 @@ export class CoreContextMenuComponent implements OnInit { ev: event }); } + + /** + * Component destroyed. + */ + ngOnDestroy(): void { + this.domUtils.removeInstanceById(this.instanceId); + } } diff --git a/src/components/navbar-buttons/navbar-buttons.ts b/src/components/navbar-buttons/navbar-buttons.ts index 98edb3421..3bdb50e26 100644 --- a/src/components/navbar-buttons/navbar-buttons.ts +++ b/src/components/navbar-buttons/navbar-buttons.ts @@ -14,6 +14,7 @@ import { Component, Input, OnInit, ContentChildren, ElementRef, QueryList } from '@angular/core'; import { Button } from 'ionic-angular'; +import { CoreLoggerProvider } from '../../providers/logger'; import { CoreDomUtilsProvider } from '../../providers/utils/dom'; /** @@ -63,41 +64,80 @@ export class CoreNavBarButtonsComponent implements OnInit { protected element: HTMLElement; protected _buttons: QueryList