|
| 1 | +/* |
| 2 | + Copyright © 2019 Kerry Shetline, kerry@shetline.com. |
| 3 | +
|
| 4 | + This code is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + This code is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with this code. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +
|
| 17 | + For commercial, proprietary, or other uses not compatible with |
| 18 | + GPL-3.0-or-later, terms of licensing for this code may be |
| 19 | + negotiated by contacting the author, Kerry Shetline, otherwise all |
| 20 | + other uses are restricted. |
| 21 | +*/ |
| 22 | + |
| 23 | +import { Directive, ElementRef, HostBinding, Input } from '@angular/core'; |
| 24 | +import { DomSanitizer, SafeStyle } from '@angular/platform-browser'; |
| 25 | +import { isIOS, toNumber } from 'ks-util'; |
| 26 | +import { Dialog } from 'primeng/dialog'; |
| 27 | + |
| 28 | +@Directive({ |
| 29 | + selector: '[ksSizer]' |
| 30 | +}) |
| 31 | +export class KsSizerDirective { |
| 32 | + @HostBinding('style') style: string | SafeStyle; |
| 33 | + |
| 34 | + @Input() set ksSizer(size: string) { |
| 35 | + let [width, height] = size.split(',').map(s => s.trim()); |
| 36 | + let iosWidthFix = false; |
| 37 | + |
| 38 | + if (/!$/.test(width)) { |
| 39 | + iosWidthFix = true; |
| 40 | + width = width.substr(0, width.length - 1); |
| 41 | + } |
| 42 | + |
| 43 | + if (toNumber(width) !== 0) |
| 44 | + width += 'px'; |
| 45 | + |
| 46 | + if (toNumber(height) !== 0) |
| 47 | + height += 'px'; |
| 48 | + |
| 49 | + if (this.pDialogHost) { |
| 50 | + const style: any = {width, height}; |
| 51 | + |
| 52 | + if (iosWidthFix && isIOS()) |
| 53 | + style['max-width'] = width; |
| 54 | + |
| 55 | + this.pDialogHost.style = style; |
| 56 | + } |
| 57 | + else { |
| 58 | + const style = `width: ${width}; height: ${height};`; |
| 59 | + this.style = this.sanitizer.bypassSecurityTrustStyle(style); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + constructor( |
| 64 | + private sanitizer: DomSanitizer, |
| 65 | + private elementRef: ElementRef, |
| 66 | + private pDialogHost: Dialog |
| 67 | + ) { } |
| 68 | +} |
0 commit comments