@nativescript/core
Version:
A JavaScript library providing an easy to use api for interacting with iOS and Android platform APIs.
122 lines • 4.76 kB
JavaScript
import { GridLayoutBase, ItemSpec as ItemSpecBase, rowProperty, columnProperty, rowSpanProperty, columnSpanProperty, GridUnitType } from './grid-layout-common';
import { View } from '../../core/view';
import { layout } from '../../../utils';
export * from './grid-layout-common';
function makeNativeSetter(setter) {
return function (value) {
const nativeView = this.nativeViewProtected;
const lp = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
if (lp instanceof org.nativescript.widgets.CommonLayoutParams) {
setter(lp, value);
nativeView.setLayoutParams(lp);
}
};
}
View.prototype[rowProperty.setNative] = makeNativeSetter((lp, value) => (lp.row = value));
View.prototype[columnProperty.setNative] = makeNativeSetter((lp, value) => (lp.column = value));
View.prototype[rowSpanProperty.setNative] = makeNativeSetter((lp, value) => (lp.rowSpan = value));
View.prototype[columnSpanProperty.setNative] = makeNativeSetter((lp, value) => (lp.columnSpan = value));
ItemSpecBase.prototype.toJSON = function () {
let result;
switch (this.gridUnitType) {
case GridUnitType.AUTO:
result = { type: 0 /* org.nativescript.widgets.GridUnitType.auto */, value: this.value };
break;
case GridUnitType.PIXEL:
result = { type: 1 /* org.nativescript.widgets.GridUnitType.pixel */, value: this.value * layout.getDisplayDensity() };
break;
case GridUnitType.STAR:
result = { type: 2 /* org.nativescript.widgets.GridUnitType.star */, value: this.value };
break;
default:
return null;
}
return result;
};
export class GridLayout extends GridLayoutBase {
createNativeView() {
return new org.nativescript.widgets.GridLayout(this._context);
}
initNativeView() {
super.initNativeView();
// Update native GridLayout
const jsonRows = JSON.stringify(this.rowsInternal.map((itemSpec) => itemSpec.toJSON()).filter((j) => !!j));
const jsonColumns = JSON.stringify(this.columnsInternal.map((itemSpec) => itemSpec.toJSON()).filter((j) => !!j));
this.nativeViewProtected.addRowsAndColumnsFromJSON(jsonRows, jsonColumns);
}
resetNativeView() {
// Update native GridLayout
this.nativeViewProtected.reset();
super.resetNativeView();
}
_onRowAdded(itemSpec) {
if (this.nativeViewProtected) {
this.nativeViewProtected.addRowsFromJSON(JSON.stringify([itemSpec.toJSON()]));
}
}
addRows(itemSpecs) {
const jsonArray = [];
const nativeView = this.nativeViewProtected;
const initialized = !!nativeView;
for (let index = 0; index < itemSpecs.length; index++) {
const itemSpec = itemSpecs[index];
this._addRow(itemSpec);
if (initialized) {
jsonArray.push(itemSpec.toJSON());
}
}
if (initialized) {
nativeView.addRowsFromJSON(JSON.stringify(jsonArray.filter((s) => !!s)));
}
}
addColumns(itemSpecs) {
const jsonArray = [];
const nativeView = this.nativeViewProtected;
const initialized = !!nativeView;
for (let index = 0; index < itemSpecs.length; index++) {
const itemSpec = itemSpecs[index];
this._addColumn(itemSpec);
if (initialized) {
jsonArray.push(itemSpec.toJSON());
}
}
if (initialized) {
nativeView.addColumnsFromJSON(JSON.stringify(jsonArray.filter((s) => !!s)));
}
}
_onColumnAdded(itemSpec) {
if (this.nativeViewProtected) {
this.nativeViewProtected.addColumnsFromJSON(JSON.stringify([itemSpec.toJSON()]));
}
}
removeColumns() {
if (this._cols.length) {
if (this.nativeViewProtected) {
this.nativeViewProtected.clearColumns();
}
this._cols.length = 0;
}
}
removeRows() {
if (this._rows.length) {
if (this.nativeViewProtected) {
this.nativeViewProtected.clearRows();
}
this._rows.length = 0;
}
}
_onRowRemoved(itemSpec, index) {
if (this.nativeViewProtected) {
this.nativeViewProtected.removeRowAt(index);
}
}
_onColumnRemoved(itemSpec, index) {
if (this.nativeViewProtected) {
this.nativeViewProtected.removeColumnAt(index);
}
}
invalidate() {
// No need to request layout for android because it will be done in the native call.
}
}
//# sourceMappingURL=index.android.js.map