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-06-14 13:42:38 +00:00
|
|
|
import { Directive, ElementRef, OnDestroy } from '@angular/core';
|
2020-10-22 11:30:23 +00:00
|
|
|
import { IonContent } from '@ionic/angular';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]',
|
|
|
|
})
|
|
|
|
export class CoreFabDirective implements OnDestroy {
|
|
|
|
|
|
|
|
protected static readonly PADDINGBOTTOM = 56;
|
|
|
|
|
2021-04-26 12:14:15 +00:00
|
|
|
protected scrollElement?: HTMLElement;
|
2020-10-22 11:30:23 +00:00
|
|
|
protected done = false;
|
2021-06-14 13:42:38 +00:00
|
|
|
protected element: HTMLElement;
|
2020-10-22 11:30:23 +00:00
|
|
|
|
2021-06-14 13:42:38 +00:00
|
|
|
constructor(el: ElementRef, protected content: IonContent) {
|
|
|
|
this.element = el.nativeElement;
|
2020-10-22 11:30:23 +00:00
|
|
|
this.asyncInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Component.
|
|
|
|
*/
|
|
|
|
async asyncInit(): Promise<void> {
|
|
|
|
if (this.content) {
|
2021-04-26 12:14:15 +00:00
|
|
|
this.scrollElement = await this.content.getScrollElement();
|
2020-10-22 11:30:23 +00:00
|
|
|
if (!this.done) {
|
2021-06-14 13:42:38 +00:00
|
|
|
// Move element to the nearest ion-content if it's not the parent
|
|
|
|
if (this.element.parentElement?.nodeName != 'ION-CONTENT') {
|
|
|
|
const ionContent = this.element.closest('ion-content');
|
|
|
|
ionContent?.appendChild(this.element);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add space at the bottom to let the user see the whole content.
|
2021-05-13 12:12:42 +00:00
|
|
|
const bottom = parseInt(this.scrollElement.style.paddingBottom, 10) || 0;
|
2021-04-26 12:14:15 +00:00
|
|
|
this.scrollElement.style.paddingBottom = (bottom + CoreFabDirective.PADDINGBOTTOM) + 'px';
|
2020-10-22 11:30:23 +00:00
|
|
|
this.done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy component.
|
|
|
|
*/
|
|
|
|
ngOnDestroy(): void {
|
2021-04-26 12:14:15 +00:00
|
|
|
if (this.done && this.scrollElement) {
|
2021-05-13 12:12:42 +00:00
|
|
|
const bottom = parseInt(this.scrollElement.style.paddingBottom, 10) || 0;
|
2021-04-26 12:14:15 +00:00
|
|
|
this.scrollElement.style.paddingBottom = (bottom - CoreFabDirective.PADDINGBOTTOM) + 'px';
|
2020-10-22 11:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|