|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { ChangeDetectionStrategy } from "@angular/core"; |
| 4 | +import { Component } from "@angular/core"; |
| 5 | + |
| 6 | +// ----------------------------------------------------------------------------------- // |
| 7 | +// ----------------------------------------------------------------------------------- // |
| 8 | + |
| 9 | +@Component({ |
| 10 | + selector: "app-smart-shrink", |
| 11 | + inputs: [ "text" ], |
| 12 | + host: { |
| 13 | + "[title]": "fullText" |
| 14 | + }, |
| 15 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 16 | + styleUrls: [ "./smart-shrink.component.less" ], |
| 17 | + template: |
| 18 | + ` |
| 19 | + <span class="left" [innerText]="leftText"></span> |
| 20 | + <span class="right" [innerText]="rightText"></span> |
| 21 | + ` |
| 22 | +}) |
| 23 | +export class SmartShrinkComponent { |
| 24 | + |
| 25 | + public fullText: string; |
| 26 | + public leftText: string; |
| 27 | + public rightText: string; |
| 28 | + |
| 29 | + // I initialize the smart-shrink component. |
| 30 | + constructor() { |
| 31 | + |
| 32 | + this.leftText = ""; |
| 33 | + this.rightText = ""; |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + // --- |
| 38 | + // PUBLIC METHODS. |
| 39 | + // --- |
| 40 | + |
| 41 | + // I handle the setting of the "text" input binding. |
| 42 | + set text( value: string ) { |
| 43 | + |
| 44 | + // We're going to split the string towards the end. This is just a judgment call. |
| 45 | + // Since we can't dynamically change the split as the container changes size (at |
| 46 | + // least, not with a lot more work), we have to pick a location that scales the |
| 47 | + // ellipsis well. |
| 48 | + var splitIndex = Math.round( value.length * 0.75 ); |
| 49 | + |
| 50 | + this.fullText = value; |
| 51 | + this.leftText = value.slice( 0, splitIndex ); |
| 52 | + this.rightText = value.slice( splitIndex ); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments