1 | var GridLayoutBase_1;
|
2 | import { LayoutBase } from '../layout-base';
|
3 | import { View, CSSType } from '../../core/view';
|
4 | import { Property, makeParser, makeValidator } from '../../core/properties';
|
5 | import { Observable } from '../../../data/observable';
|
6 | function validateArgs(element) {
|
7 | if (!element) {
|
8 | throw new Error('element cannot be null or undefined.');
|
9 | }
|
10 | return element;
|
11 | }
|
12 | View.prototype.row = 0;
|
13 | View.prototype.col = 0;
|
14 | View.prototype.rowSpan = 1;
|
15 | View.prototype.colSpan = 1;
|
16 | Object.defineProperty(View.prototype, 'column', {
|
17 | get() {
|
18 | return this.col;
|
19 | },
|
20 | set(value) {
|
21 | this.col = value;
|
22 | },
|
23 | enumerable: true,
|
24 | configurable: true,
|
25 | });
|
26 | Object.defineProperty(View.prototype, 'columnSpan', {
|
27 | get() {
|
28 | return this.colSpan;
|
29 | },
|
30 | set(value) {
|
31 | this.colSpan = value;
|
32 | },
|
33 | enumerable: true,
|
34 | configurable: true,
|
35 | });
|
36 | function validateItemSpec(itemSpec) {
|
37 | if (!itemSpec) {
|
38 | throw new Error('Value cannot be undefined.');
|
39 | }
|
40 | if (itemSpec.owner) {
|
41 | throw new Error('itemSpec is already added to GridLayout.');
|
42 | }
|
43 | }
|
44 | function convertGridLength(value) {
|
45 | if (value === GridUnitType.AUTO) {
|
46 | return ItemSpec.create(1, GridUnitType.AUTO);
|
47 | }
|
48 | else if (value.indexOf('*') !== -1) {
|
49 | const starCount = parseInt(value.replace('*', '') || '1');
|
50 | return ItemSpec.create(starCount, GridUnitType.STAR);
|
51 | }
|
52 | else if (!isNaN(parseInt(value))) {
|
53 | return ItemSpec.create(parseInt(value), GridUnitType.PIXEL);
|
54 | }
|
55 | else {
|
56 | throw new Error(`Cannot parse item spec from string: ${value}`);
|
57 | }
|
58 | }
|
59 | function parseAndAddItemSpecs(value) {
|
60 |
|
61 | const specs = [];
|
62 | const arr = `${value}`.split(/[\s,]+/);
|
63 | for (let i = 0, length = arr.length; i < length; i++) {
|
64 | const str = arr[i].trim();
|
65 | if (str.length > 0) {
|
66 | specs.push(convertGridLength(arr[i].trim()));
|
67 | }
|
68 | }
|
69 | return specs;
|
70 | }
|
71 | export class ItemSpec extends Observable {
|
72 | constructor(...args) {
|
73 | super();
|
74 | this._actualLength = 0;
|
75 | if (args.length === 0) {
|
76 | this._value = 1;
|
77 | this._unitType = GridUnitType.STAR;
|
78 | }
|
79 | else if (arguments.length === 2) {
|
80 | const value = args[0];
|
81 | const type = args[1];
|
82 | if (typeof value === 'number' && typeof type === 'string') {
|
83 | if (value < 0 || isNaN(value) || !isFinite(value)) {
|
84 | throw new Error(`Value should not be negative, NaN or Infinity: ${value}`);
|
85 | }
|
86 | this._value = value;
|
87 | this._unitType = GridUnitType.parse(type);
|
88 | }
|
89 | else {
|
90 | throw new Error('First argument should be number, second argument should be string.');
|
91 | }
|
92 | }
|
93 | else {
|
94 | throw new Error('ItemSpec expects 0 or 2 arguments');
|
95 | }
|
96 | this.index = -1;
|
97 | }
|
98 | static create(value, type) {
|
99 | const spec = new ItemSpec();
|
100 | spec._value = value;
|
101 | spec._unitType = type;
|
102 | return spec;
|
103 | }
|
104 | get actualLength() {
|
105 | return this._actualLength;
|
106 | }
|
107 | static equals(value1, value2) {
|
108 | return value1.gridUnitType === value2.gridUnitType && value1.value === value2.value && value1.owner === value2.owner && value1.index === value2.index;
|
109 | }
|
110 | get gridUnitType() {
|
111 | return this._unitType;
|
112 | }
|
113 | get isAbsolute() {
|
114 | return this._unitType === GridUnitType.PIXEL;
|
115 | }
|
116 | get isAuto() {
|
117 | return this._unitType === GridUnitType.AUTO;
|
118 | }
|
119 | get isStar() {
|
120 | return this._unitType === GridUnitType.STAR;
|
121 | }
|
122 | get value() {
|
123 | return this._value;
|
124 | }
|
125 | }
|
126 | let GridLayoutBase = GridLayoutBase_1 = class GridLayoutBase extends LayoutBase {
|
127 | constructor() {
|
128 | super(...arguments);
|
129 | this._rows = new Array();
|
130 | this._cols = new Array();
|
131 | }
|
132 | static getColumn(element) {
|
133 | return validateArgs(element).col;
|
134 | }
|
135 | static setColumn(element, value) {
|
136 | validateArgs(element).col = value;
|
137 | }
|
138 | static getColumnSpan(element) {
|
139 | return validateArgs(element).colSpan;
|
140 | }
|
141 | static setColumnSpan(element, value) {
|
142 | validateArgs(element).colSpan = value;
|
143 | }
|
144 | static getRow(element) {
|
145 | return validateArgs(element).row;
|
146 | }
|
147 | static setRow(element, value) {
|
148 | validateArgs(element).row = value;
|
149 | }
|
150 | static getRowSpan(element) {
|
151 | return validateArgs(element).rowSpan;
|
152 | }
|
153 | static setRowSpan(element, value) {
|
154 | validateArgs(element).rowSpan = value;
|
155 | }
|
156 | _addRow(itemSpec) {
|
157 | validateItemSpec(itemSpec);
|
158 | itemSpec.owner = this;
|
159 | this._rows.push(itemSpec);
|
160 | }
|
161 | addRow(itemSpec) {
|
162 | this._addRow(itemSpec);
|
163 | this._onRowAdded(itemSpec);
|
164 | this.invalidate();
|
165 | }
|
166 | addRows(itemSpecs) {
|
167 | for (let index = 0; index < itemSpecs.length; index++) {
|
168 | const itemSpec = itemSpecs[index];
|
169 | this._addRow(itemSpec);
|
170 | this._onRowAdded(itemSpec);
|
171 | }
|
172 | this.invalidate();
|
173 | }
|
174 | _addColumn(itemSpec) {
|
175 | validateItemSpec(itemSpec);
|
176 | itemSpec.owner = this;
|
177 | this._cols.push(itemSpec);
|
178 | }
|
179 | addColumn(itemSpec) {
|
180 | this._addColumn(itemSpec);
|
181 | this._onColumnAdded(itemSpec);
|
182 | this.invalidate();
|
183 | }
|
184 | addColumns(itemSpecs) {
|
185 | for (let index = 0; index < itemSpecs.length; index++) {
|
186 | const itemSpec = itemSpecs[index];
|
187 | this._addColumn(itemSpec);
|
188 | this._onColumnAdded(itemSpec);
|
189 | }
|
190 | this.invalidate();
|
191 | }
|
192 | addChildAtCell(view, row, column, rowSpan, columnSpan) {
|
193 | this.addChild(view);
|
194 | GridLayoutBase_1.setRow(view, row);
|
195 | GridLayoutBase_1.setColumn(view, column);
|
196 | if (rowSpan) {
|
197 | GridLayoutBase_1.setRowSpan(view, rowSpan);
|
198 | }
|
199 | if (columnSpan) {
|
200 | GridLayoutBase_1.setColumnSpan(view, columnSpan);
|
201 | }
|
202 | }
|
203 | removeRow(itemSpec) {
|
204 | if (!itemSpec) {
|
205 | throw new Error('Value is null.');
|
206 | }
|
207 | const index = this._rows.indexOf(itemSpec);
|
208 | if (itemSpec.owner !== this || index < 0) {
|
209 | throw new Error('Row is not child of this GridLayout');
|
210 | }
|
211 | itemSpec.index = -1;
|
212 | this._rows.splice(index, 1);
|
213 | this._onRowRemoved(itemSpec, index);
|
214 | this.invalidate();
|
215 | }
|
216 | removeColumn(itemSpec) {
|
217 | if (!itemSpec) {
|
218 | throw new Error('Value is null.');
|
219 | }
|
220 | const index = this._cols.indexOf(itemSpec);
|
221 | if (itemSpec.owner !== this || index < 0) {
|
222 | throw new Error('Column is not child of this GridLayout');
|
223 | }
|
224 | itemSpec.index = -1;
|
225 | this._cols.splice(index, 1);
|
226 | this._onColumnRemoved(itemSpec, index);
|
227 | this.invalidate();
|
228 | }
|
229 | removeColumns() {
|
230 | for (let i = this._cols.length - 1; i >= 0; i--) {
|
231 | const colSpec = this._cols[i];
|
232 | this._onColumnRemoved(colSpec, i);
|
233 | colSpec.index = -1;
|
234 | }
|
235 | this._cols.length = 0;
|
236 | this.invalidate();
|
237 | }
|
238 | removeRows() {
|
239 | for (let i = this._rows.length - 1; i >= 0; i--) {
|
240 | const rowSpec = this._rows[i];
|
241 | this._onRowRemoved(rowSpec, i);
|
242 | rowSpec.index = -1;
|
243 | }
|
244 | this._rows.length = 0;
|
245 | this.invalidate();
|
246 | }
|
247 | onRowChanged(element, oldValue, newValue) {
|
248 | this.invalidate();
|
249 | }
|
250 | onRowSpanChanged(element, oldValue, newValue) {
|
251 | this.invalidate();
|
252 | }
|
253 | onColumnChanged(element, oldValue, newValue) {
|
254 | this.invalidate();
|
255 | }
|
256 | onColumnSpanChanged(element, oldValue, newValue) {
|
257 | this.invalidate();
|
258 | }
|
259 | _onRowAdded(itemSpec) {
|
260 |
|
261 | }
|
262 | _onColumnAdded(itemSpec) {
|
263 |
|
264 | }
|
265 | _onRowRemoved(itemSpec, index) {
|
266 |
|
267 | }
|
268 | _onColumnRemoved(itemSpec, index) {
|
269 |
|
270 | }
|
271 | getColumns() {
|
272 | return this._cols.slice();
|
273 | }
|
274 | getRows() {
|
275 | return this._rows.slice();
|
276 | }
|
277 | get columnsInternal() {
|
278 | return this._cols;
|
279 | }
|
280 | get rowsInternal() {
|
281 | return this._rows;
|
282 | }
|
283 | invalidate() {
|
284 |
|
285 | }
|
286 | set rows(value) {
|
287 | this.removeRows();
|
288 | const specs = parseAndAddItemSpecs(value);
|
289 | this.addRows(specs);
|
290 | }
|
291 | set columns(value) {
|
292 | this.removeColumns();
|
293 | const specs = parseAndAddItemSpecs(value);
|
294 | this.addColumns(specs);
|
295 | }
|
296 | };
|
297 | GridLayoutBase = GridLayoutBase_1 = __decorate([
|
298 | CSSType('GridLayout')
|
299 | ], GridLayoutBase);
|
300 | export { GridLayoutBase };
|
301 | GridLayoutBase.prototype.recycleNativeView = 'auto';
|
302 | export const columnProperty = new Property({
|
303 | name: 'col',
|
304 | defaultValue: 0,
|
305 | valueChanged: (target, oldValue, newValue) => {
|
306 | const grid = target.parent;
|
307 | if (grid instanceof GridLayoutBase) {
|
308 | grid.onColumnChanged(target, oldValue, newValue);
|
309 | }
|
310 | },
|
311 | valueConverter: (v) => Math.max(0, parseInt(v)),
|
312 | });
|
313 | columnProperty.register(View);
|
314 | export const columnSpanProperty = new Property({
|
315 | name: 'colSpan',
|
316 | defaultValue: 1,
|
317 | valueChanged: (target, oldValue, newValue) => {
|
318 | const grid = target.parent;
|
319 | if (grid instanceof GridLayoutBase) {
|
320 | grid.onColumnSpanChanged(target, oldValue, newValue);
|
321 | }
|
322 | },
|
323 | valueConverter: (v) => Math.max(1, parseInt(v)),
|
324 | });
|
325 | columnSpanProperty.register(View);
|
326 | export const rowProperty = new Property({
|
327 | name: 'row',
|
328 | defaultValue: 0,
|
329 | valueChanged: (target, oldValue, newValue) => {
|
330 | const grid = target.parent;
|
331 | if (grid instanceof GridLayoutBase) {
|
332 | grid.onRowChanged(target, oldValue, newValue);
|
333 | }
|
334 | },
|
335 | valueConverter: (v) => Math.max(0, parseInt(v)),
|
336 | });
|
337 | rowProperty.register(View);
|
338 | export const rowSpanProperty = new Property({
|
339 | name: 'rowSpan',
|
340 | defaultValue: 1,
|
341 | valueChanged: (target, oldValue, newValue) => {
|
342 | const grid = target.parent;
|
343 | if (grid instanceof GridLayoutBase) {
|
344 | grid.onRowSpanChanged(target, oldValue, newValue);
|
345 | }
|
346 | },
|
347 | valueConverter: (v) => Math.max(1, parseInt(v)),
|
348 | });
|
349 | rowSpanProperty.register(View);
|
350 | export var GridUnitType;
|
351 | (function (GridUnitType) {
|
352 | GridUnitType.PIXEL = 'pixel';
|
353 | GridUnitType.STAR = 'star';
|
354 | GridUnitType.AUTO = 'auto';
|
355 | GridUnitType.isValid = makeValidator(GridUnitType.PIXEL, GridUnitType.STAR, GridUnitType.AUTO);
|
356 | GridUnitType.parse = makeParser(GridUnitType.isValid);
|
357 | })(GridUnitType || (GridUnitType = {}));
|
358 |
|
\ | No newline at end of file |