forked from NG-ZORRO/ng-zorro-antd-mobile
/
grid.component.ts
163 lines (151 loc) · 3.96 KB
/
grid.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Component, OnInit, Input, Output, EventEmitter, TemplateRef, ElementRef, HostBinding } from '@angular/core';
@Component({
selector: 'Grid, nzm-grid',
templateUrl: './grid.component.html'
})
export class GridComponent implements OnInit {
wrapCls = {};
itemCls = {};
carouselProps = {
dots: false,
dragging: false
};
defaultProps = {
data: [],
hasLine: true,
isCarousel: false,
columnNum: 4,
carouselMaxRow: 2,
prefixCls: 'am-grid',
square: true,
itemStyle: {}
};
carouselData = [];
carouselDataTmp = [];
gridData = [];
private _data = [];
@Input()
columnNum: number = 4;
@Input()
carouselMaxRow: number = 2;
@Input()
itemStyle: object = {};
@Input()
square: boolean = true;
@Input()
hasLine: boolean = true;
@Input()
get isCarousel(): boolean {
return this.defaultProps.isCarousel;
}
set isCarousel (value: boolean) {
this.defaultProps.isCarousel = value;
this.init();
}
@Input()
activeStyle: boolean = true;
@Input()
set data(value: Array<any>) {
this._data = value;
this.init();
}
@Output()
OnClick: EventEmitter<any> = new EventEmitter();
@HostBinding('class.am-grid')
amGrid: boolean = true;
@HostBinding('class.am-grid-square')
get amGridSquare(): boolean {
return true === this.square;
}
@HostBinding('class.am-grid-line')
get amGridLine(): boolean {
return true === this.hasLine;
}
@HostBinding('class.am-grid-carousel')
get amGridCarousel(): boolean {
return true === this.isCarousel;
}
constructor() {}
isTemplateRef(value: {}): boolean {
return value instanceof TemplateRef;
}
isURL(value: string): boolean {
if (value) {
return value.indexOf('http') >= 0;
} else {
return false;
}
}
init() {
const dataLength = (this._data && this._data.length) || 0;
let rowCount = Math.ceil(dataLength / this.columnNum);
let rowsArr;
if (this.defaultProps.isCarousel) {
if (rowCount % this.carouselMaxRow !== 0) {
rowCount = rowCount + this.carouselMaxRow - (rowCount % this.carouselMaxRow);
}
const pageCount = Math.ceil(rowCount / this.carouselMaxRow);
rowsArr = this.getRows(rowCount, dataLength);
if (pageCount <= 1) {
this.carouselProps = {
dots: false,
dragging: false
};
} else {
this.carouselProps = {
dots: true,
dragging: true
};
}
this.carouselDataTmp = this.getCarouselData(rowsArr, pageCount, rowCount);
} else {
this.gridData = this.getRows(rowCount, dataLength);
}
}
getCarouselData(rowsArr: any[], pageCount: number, rowCount: number) {
const pagesArr: any[] = [];
for (let pageIndex = 0; pageIndex < pageCount; pageIndex++) {
const pageRows: any[] = [];
for (let ii = 0; ii < this.carouselMaxRow; ii++) {
const rowIndex = pageIndex * this.carouselMaxRow + ii;
if (rowIndex < rowCount) {
pageRows.push(rowsArr[rowIndex]);
} else {
// 空节点为了确保末尾页的最后未到底的行有底线(样式中last-child会没线)
pageRows.push(null);
}
}
pagesArr.push(pageRows);
}
return pagesArr;
}
getRows(rowCount: number, dataLength: number) {
const columnNum = this.columnNum;
const rowArr = new Array();
for (let i = 0; i < rowCount; i++) {
rowArr[i] = new Array();
for (let j = 0; j < columnNum; j++) {
const dataIndex = i * columnNum + j;
if (dataIndex < dataLength) {
rowArr[i][j] = this._data[dataIndex];
} else {
rowArr[i][j] = null;
}
}
}
return rowArr;
}
click(data, index) {
const outputData = {
data: data,
index: index
};
this.OnClick.emit(outputData);
}
ngOnInit() {
this.itemCls = {
[`${this.defaultProps.prefixCls}-item`]: true,
[`${this.defaultProps.prefixCls}-active-item`]: false
};
}
}