venusRow.js 4.72 KB
const DEFAULT_BREAKPOINTS = {
	xs: 0,
	sm: 375,
	md: 768,
	lg: 1024,
};

Component({
	externalClasses: ["custom-class"],
	options: {
		styleIsolation: "apply-shared",
	},
	relations: {
		"../venusCol/venusCol": {
			type: "descendant",
			linked() {
				this.notifyChildren();
			},
			linkChanged() {
				this.notifyChildren();
			},
			unlinked() {
				this.notifyChildren();
			},
		},
	},
	properties: {
		columns: {
			type: Number,
			value: 24,
			observer() {
				this.syncRow();
			},
		},
		gap: {
			type: null,
			value: [0, 0],
			observer() {
				this.syncRow();
			},
		},
		dense: {
			type: Boolean,
			value: false,
			observer() {
				this.syncRow();
			},
		},
		breakpoints: {
			type: Object,
			value: DEFAULT_BREAKPOINTS,
			observer() {
				this.syncRow();
			},
		},
	},
	data: {
		rowStyle: "",
		activeBreakpoint: "xs",
		viewportWidth: 0,
	},
	lifetimes: {
		attached() {
			this.bindWindowResize();
			this.syncRow();
		},
		detached() {
			this.unbindWindowResize();
		},
	},
	methods: {
		bindWindowResize() {
			if (typeof wx.onWindowResize !== "function" || this.resizeHandler) return;
			this.resizeHandler = (res) => {
				const size = (res && res.size) || {};
				const width = Number(size.windowWidth || res.windowWidth || this.getWindowWidth());
				if (width && width !== this.data.viewportWidth) {
					this.syncRow(width);
				}
			};
			wx.onWindowResize(this.resizeHandler);
		},

		unbindWindowResize() {
			if (!this.resizeHandler || typeof wx.offWindowResize !== "function") return;
			wx.offWindowResize(this.resizeHandler);
			this.resizeHandler = null;
		},

		getWindowWidth() {
			const info = typeof wx.getWindowInfo === "function" ? wx.getWindowInfo() : wx.getSystemInfoSync();
			return Number(info.windowWidth || info.screenWidth || 375);
		},

		syncRow(forcedWidth) {
			const columns = this.normalizeColumns(this.properties.columns);
			const viewportWidth = Number(forcedWidth || this.getWindowWidth()) || 375;
			const activeBreakpoint = this.getActiveBreakpoint(viewportWidth, this.properties.breakpoints);
			const rowStyle = this.buildRowStyle(columns, this.properties.gap, this.properties.dense);

			this.setData(
				{
					rowStyle,
					activeBreakpoint,
					viewportWidth,
				},
				() => {
					this.notifyChildren();
				},
			);
		},

		buildRowStyle(columns, gap, dense) {
			const normalizedGap = this.normalizeGap(gap);
			return [
				"display:grid",
				"width:100%",
				"box-sizing:border-box",
				"align-items:start",
				`grid-template-columns:repeat(${columns}, minmax(0, 1fr))`,
				`column-gap:${normalizedGap.columnGap}`,
				`row-gap:${normalizedGap.rowGap}`,
				`grid-auto-flow:${dense ? "row dense" : "row"}`,
			].join(";");
		},

		notifyChildren() {
			const children = this.getRelationNodes("../venusCol/venusCol") || [];
			const context = {
				columns: this.normalizeColumns(this.properties.columns),
				breakpoints: this.properties.breakpoints,
				activeBreakpoint: this.data.activeBreakpoint,
				viewportWidth: this.data.viewportWidth || this.getWindowWidth(),
			};
			children.forEach((child) => {
				if (child && typeof child.updateFromRow === "function") {
					child.updateFromRow(context);
				}
			});
		},

		emitItemTap(detail) {
			this.triggerEvent("itemtap", detail);
		},

		normalizeColumns(value) {
			const columns = Number(value);
			if (!Number.isFinite(columns)) return 24;
			return Math.max(1, Math.round(columns));
		},

		normalizeGap(value) {
			let columnGap = "0rpx";
			let rowGap = "0rpx";

			if (Array.isArray(value)) {
				columnGap = this.toCssSize(value[0]);
				rowGap = this.toCssSize(value.length > 1 ? value[1] : value[0]);
			} else if (value && typeof value === "object") {
				columnGap = this.toCssSize(value.x != null ? value.x : value.column);
				rowGap = this.toCssSize(value.y != null ? value.y : value.row);
			} else {
				columnGap = this.toCssSize(value);
				rowGap = this.toCssSize(value);
			}

			return {
				columnGap,
				rowGap,
			};
		},

		toCssSize(value) {
			if (value == null || value === "") return "0rpx";
			if (typeof value === "number") return `${value}rpx`;
			if (typeof value !== "string") return "0rpx";
			if (/^-?\d+(\.\d+)?$/.test(value)) {
				return `${value}rpx`;
			}
			return value;
		},

		getActiveBreakpoint(width, breakpoints) {
			const list = this.getSortedBreakpoints(breakpoints);
			if (!list.length) return "xs";

			let activeKey = list[0].key;
			list.forEach((item) => {
				if (width >= item.value) {
					activeKey = item.key;
				}
			});
			return activeKey;
		},

		getSortedBreakpoints(breakpoints) {
			return Object.keys(breakpoints || {})
				.map((key) => ({
					key,
					value: Number(breakpoints[key]),
				}))
				.filter((item) => Number.isFinite(item.value))
				.sort((a, b) => a.value - b.value);
		},
	},
});