2020-10-22 11:30:23 +00:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-11-30 12:12:50 +00:00
|
|
|
import { Directive, ElementRef, OnDestroy, OnInit } from '@angular/core';
|
2022-03-18 16:42:01 +00:00
|
|
|
import { CoreCancellablePromise } from '@classes/cancellable-promise';
|
2022-03-22 09:47:14 +00:00
|
|
|
import { CoreDom } from '@singletons/dom';
|
2020-10-22 11:30:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive to move ion-fab components as direct children of the nearest ion-content.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
|
|
|
* <ion-fab core-fab>
|
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: 'ion-fab[core-fab]',
|
|
|
|
})
|
2021-11-30 12:12:50 +00:00
|
|
|
export class CoreFabDirective implements OnInit, OnDestroy {
|
2020-10-22 11:30:23 +00:00
|
|
|
|
2021-06-14 13:42:38 +00:00
|
|
|
protected element: HTMLElement;
|
2021-11-30 12:12:50 +00:00
|
|
|
protected content?: HTMLIonContentElement | null;
|
|
|
|
protected initialPaddingBottom = 0;
|
2022-03-18 16:42:01 +00:00
|
|
|
protected domPromise?: CoreCancellablePromise<void>;
|
2020-10-22 11:30:23 +00:00
|
|
|
|
2021-11-30 12:12:50 +00:00
|
|
|
constructor(el: ElementRef) {
|
2021-06-14 13:42:38 +00:00
|
|
|
this.element = el.nativeElement;
|
2021-11-30 12:12:50 +00:00
|
|
|
this.element.setAttribute('slot', 'fixed');
|
2020-10-22 11:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-30 12:12:50 +00:00
|
|
|
* @inheritdoc
|
2020-10-22 11:30:23 +00:00
|
|
|
*/
|
2022-03-18 16:42:01 +00:00
|
|
|
async ngOnInit(): Promise<void> {
|
2022-03-22 09:47:14 +00:00
|
|
|
this.domPromise = CoreDom.waitToBeInDOM(this.element);
|
2022-03-18 16:42:01 +00:00
|
|
|
await this.domPromise;
|
|
|
|
|
2021-11-30 12:12:50 +00:00
|
|
|
this.content = this.element.closest('ion-content');
|
2022-03-18 16:42:01 +00:00
|
|
|
|
2021-11-30 12:12:50 +00:00
|
|
|
if (!this.content) {
|
2022-03-18 16:42:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add space at the bottom to let the user see the whole content.
|
|
|
|
this.initialPaddingBottom = parseFloat(this.content.style.getPropertyValue('--padding-bottom') || '0');
|
2021-06-14 13:42:38 +00:00
|
|
|
|
2022-03-18 16:42:01 +00:00
|
|
|
await this.calculatePlace();
|
2021-11-30 12:12:50 +00:00
|
|
|
|
2022-03-22 09:47:14 +00:00
|
|
|
CoreDom.onElementSlot(this.element, () => {
|
2022-03-18 16:42:01 +00:00
|
|
|
this.calculatePlace();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the height of the footer.
|
|
|
|
*/
|
|
|
|
protected async calculatePlace(): Promise<void> {
|
|
|
|
if (!this.content) {
|
2021-11-30 12:12:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialHeight = this.element.getBoundingClientRect().height || 56;
|
|
|
|
|
|
|
|
// Move element to the nearest ion-content if it's not the parent
|
|
|
|
if (this.element.parentElement?.nodeName != 'ION-CONTENT') {
|
|
|
|
this.content.appendChild(this.element);
|
2020-10-22 11:30:23 +00:00
|
|
|
}
|
2021-11-30 12:12:50 +00:00
|
|
|
|
|
|
|
this.content.style.setProperty('--padding-bottom', this.initialPaddingBottom + initialHeight + 'px');
|
2020-10-22 11:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-30 12:12:50 +00:00
|
|
|
* @inheritdoc
|
2020-10-22 11:30:23 +00:00
|
|
|
*/
|
|
|
|
ngOnDestroy(): void {
|
2021-11-30 12:12:50 +00:00
|
|
|
if (this.content) {
|
|
|
|
this.content.style.setProperty('--padding-bottom', this.initialPaddingBottom + 'px');
|
2020-10-22 11:30:23 +00:00
|
|
|
}
|
2022-03-18 16:42:01 +00:00
|
|
|
this.domPromise?.cancel();
|
2020-10-22 11:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|