|
| 1 | +import { |
| 2 | + Directive, |
| 3 | + HostBinding, |
| 4 | + Input, |
| 5 | + OnChanges, |
| 6 | + OnDestroy, |
| 7 | + OnInit, |
| 8 | + Optional |
| 9 | +} from "@angular/core"; |
| 10 | +import { Subscription } from "rxjs"; |
| 11 | +import { GridService } from "./grid.service"; |
| 12 | + |
| 13 | +@Directive({ |
| 14 | + selector: "[cdsCol], [ibmCol]" |
| 15 | +}) |
| 16 | +export class ColumnDirective implements OnInit, OnChanges, OnDestroy { |
| 17 | + @HostBinding("class") |
| 18 | + get columnClasses(): string { |
| 19 | + return this._columnClasses.join(" "); |
| 20 | + } |
| 21 | + set columnClasses(classes: string) { |
| 22 | + this._columnClasses = classes.split(" "); |
| 23 | + } |
| 24 | + |
| 25 | + @Input() class = ""; |
| 26 | + |
| 27 | + /** |
| 28 | + * Defines columns width for specified breakpoint |
| 29 | + * Accepts the following formats: |
| 30 | + * - {[breakpoint]: number} |
| 31 | + * - {[breakpoint]: "auto"} - css only |
| 32 | + * - {[breakpoint]: {[start|end]: number}} - css only |
| 33 | + * |
| 34 | + * Example: |
| 35 | + * <div cdsCol [columnNumbers]={md: 3, lg: 4}></div> |
| 36 | + */ |
| 37 | + @Input() columnNumbers = {}; |
| 38 | + |
| 39 | + /** |
| 40 | + * Defines columns offset, which increases the left margin of the column. |
| 41 | + * This field will only work with flexbox grid. |
| 42 | + * |
| 43 | + * Accepts the following formats: |
| 44 | + * - {[breakpoint]: number} |
| 45 | + * |
| 46 | + * Example: |
| 47 | + * <div cdsCol [offsets]={md: 3, lg: 4}></div> |
| 48 | + */ |
| 49 | + @Input() offsets = {}; |
| 50 | + |
| 51 | + /** |
| 52 | + * Set to `true` to use css grid column hang class |
| 53 | + * This will only work when `isCss` property is set to true |
| 54 | + * |
| 55 | + * Useful when trying to align content across css grid/subgrid |
| 56 | + */ |
| 57 | + @Input() columnHang = false; |
| 58 | + |
| 59 | + protected _columnClasses: string[] = []; |
| 60 | + |
| 61 | + private isCssGrid = false; |
| 62 | + private subscription = new Subscription(); |
| 63 | + |
| 64 | + constructor(@Optional() private gridService: GridService) {} |
| 65 | + |
| 66 | + ngOnInit() { |
| 67 | + if (this.gridService) { |
| 68 | + this.gridService.gridObservable.subscribe((isCssGrid: boolean) => { |
| 69 | + this.isCssGrid = isCssGrid; |
| 70 | + this.updateColumnClasses(); |
| 71 | + }); |
| 72 | + } else { |
| 73 | + this.updateColumnClasses(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + ngOnChanges() { |
| 78 | + this.updateColumnClasses(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Unsubscribe from subscription |
| 83 | + */ |
| 84 | + ngOnDestroy() { |
| 85 | + this.subscription.unsubscribe(); |
| 86 | + } |
| 87 | + |
| 88 | + private updateColumnClasses() { |
| 89 | + try { |
| 90 | + this._columnClasses = []; |
| 91 | + const columnKeys = Object.keys(this.columnNumbers); |
| 92 | + |
| 93 | + // Assign classes based on the type of grid used. |
| 94 | + if (this.isCssGrid) { |
| 95 | + // Default css grid class |
| 96 | + this._columnClasses.push("cds--css-grid-column"); |
| 97 | + if (this.columnHang) { |
| 98 | + this._columnClasses.push("cds--grid-column-hang"); |
| 99 | + } |
| 100 | + |
| 101 | + columnKeys.forEach(key => { |
| 102 | + /** |
| 103 | + * Passing in `auto` to a breakpoint as such: {'md': 'auto'} |
| 104 | + * will assign the element which will automatically determine the width of the column |
| 105 | + * for the breakpoint passed |
| 106 | + */ |
| 107 | + if (this.columnNumbers[key] === "auto") { |
| 108 | + this._columnClasses.push(`cds--${key}:col-span-auto`); |
| 109 | + } else if (typeof this.columnNumbers[key] === "object") { |
| 110 | + /** |
| 111 | + * In css grid, objects can be passed to the keys in the following format: |
| 112 | + * {'md': {'start': 3}} |
| 113 | + * |
| 114 | + * These objects are used to position the column |
| 115 | + */ |
| 116 | + if (this.columnNumbers[key]["start"]) { |
| 117 | + // col-start is simular equivalent of flex offset |
| 118 | + this._columnClasses.push(`cds--${key}:col-start-${this.columnNumbers[key].start}`); |
| 119 | + } |
| 120 | + if (this.columnNumbers[key]["end"]) { |
| 121 | + this._columnClasses.push(`cds--${key}:col-end-${this.columnNumbers[key].end}`); |
| 122 | + } |
| 123 | + if (this.columnNumbers[key]["span"]) { |
| 124 | + this._columnClasses.push(`cds--${key}:col-span-${this.columnNumbers[key].span}`); |
| 125 | + } |
| 126 | + } else { |
| 127 | + this._columnClasses.push(`cds--${key}:col-span-${this.columnNumbers[key]}`); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + Object.keys(this.offsets).forEach(key => { |
| 132 | + this._columnClasses.push(`cds--${key}:col-start${this.offsets[key] + 1}`); |
| 133 | + }); |
| 134 | + } else { |
| 135 | + // Set column classes for flex grid |
| 136 | + if (columnKeys.length <= 0) { |
| 137 | + this._columnClasses.push("cds--col"); |
| 138 | + } |
| 139 | + |
| 140 | + columnKeys.forEach(key => { |
| 141 | + if (this.columnNumbers[key] === "nobreak") { |
| 142 | + this._columnClasses.push(`cds--col-${key}`); |
| 143 | + } else { |
| 144 | + this._columnClasses.push(`cds--col-${key}-${this.columnNumbers[key]}`); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + Object.keys(this.offsets).forEach(key => { |
| 149 | + this._columnClasses.push(`cds--offset-${key}-${this.offsets[key]}`); |
| 150 | + }); |
| 151 | + } |
| 152 | + } catch (err) { |
| 153 | + console.error(`Malformed \`offsets\` or \`columnNumbers\`: ${err}`); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Append the classes passed so they aren't overriden when we set the column classes |
| 158 | + * from host binding |
| 159 | + */ |
| 160 | + if (this.class) { |
| 161 | + this._columnClasses.push(this.class); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments