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

Component({
	externalClasses: ["custom-class"],
	options: {
		virtualHost: true,
		styleIsolation: "apply-shared",
	},
	relations: {
		"../venusRow/venusRow": {
			type: "ancestor",
		},
	},
	properties: {
		span: {
			type: null,
			value: 24,
			observer() {
				this.syncCol();
			},
		},
		offset: {
			type: Number,
			value: 0,
			observer() {
				this.syncCol();
			},
		},
		align: {
			type: String,
			value: "stretch",
			observer() {
				this.syncCol();
			},
		},
		customStyle: {
			type: String,
			value: "",
			observer() {
				this.syncCol();
			},
		},
		clickable: {
			type: Boolean,
			value: false,
			observer() {
				this.syncCol();
			},
		},
		contentSpan: {
			type: Number,
			value: 24,
			observer() {
				this.syncCol();
			},
		},
		responsive: {
			type: Object,
			value: null,
			observer() {
				this.syncCol();
			},
		},
	},
	data: {
		itemClass: "",
		itemStyleText: "",
		clickable: false,
		currentBreakpoint: "xs",
	},
	lifetimes: {
		attached() {
			this.syncCol();
		},
	},
	methods: {
		updateFromRow(rowContext) {
			this.rowContext = rowContext || null;
			this.syncCol();
		},

		getParentRow() {
			const rows = this.getRelationNodes("../venusRow/venusRow") || [];
			return rows[0] || null;
		},

		getStandaloneContext() {
			const width = this.getWindowWidth();
			const breakpoints = DEFAULT_BREAKPOINTS;
			return {
				columns: 24,
				breakpoints,
				viewportWidth: width,
				activeBreakpoint: this.getActiveBreakpoint(width, breakpoints),
			};
		},

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

		syncCol() {
			const rowContext = this.rowContext || this.getStandaloneContext();
			const config = this.resolveConfig(rowContext);
			const columns = this.normalizeColumns(rowContext.columns);
			const isContent = config.span === "content";
			const span = isContent ? this.normalizeSpan(config.contentSpan, columns) : this.normalizeSpan(config.span, columns);
			const offset = this.normalizeOffset(config.offset, columns);
			const align = this.normalizeAlign(config.align || (isContent ? "start" : "stretch"));
			const clickable = Boolean(config.clickable);
			const itemClassList = [];

			if (isContent) itemClassList.push("is-content");
			if (clickable) itemClassList.push("is-clickable");

			const itemStyleList = [`grid-column-end:span ${span}`, `justify-self:${align}`, "min-width:0", "box-sizing:border-box"];
			if (offset > 0) {
				itemStyleList.push(`grid-column-start:${offset + 1}`);
			}
			if (isContent) {
				itemStyleList.push("width:fit-content");
				itemStyleList.push("max-width:100%");
			}
			if (config.customStyle) {
				itemStyleList.push(config.customStyle);
			}

			this.currentConfig = config;
			this.setData({
				itemClass: itemClassList.join(" "),
				itemStyleText: itemStyleList.join(";"),
				clickable,
				currentBreakpoint: rowContext.activeBreakpoint || "xs",
			});
		},

		resolveConfig(rowContext) {
			let config = {
				span: this.properties.span,
				offset: this.properties.offset,
				align: this.properties.align,
				customStyle: this.properties.customStyle,
				clickable: this.properties.clickable,
				contentSpan: this.properties.contentSpan,
			};

			const responsive = this.properties.responsive;
			if (responsive && typeof responsive === "object") {
				const merged = {};
				this.getSortedBreakpoints(rowContext.breakpoints).forEach((point) => {
					const pointConfig = responsive[point.key];
					if (rowContext.viewportWidth >= point.value && this.isPlainObject(pointConfig)) {
						Object.assign(merged, pointConfig);
					}
				});
				config = Object.assign({}, config, merged);
			}

			return config;
		},

		onTap() {
			if (!this.data.clickable) return;

			const detail = {
				item: this.currentConfig || {},
				breakpoint: this.data.currentBreakpoint,
			};

			this.triggerEvent("itemtap", detail);

			const row = this.getParentRow();
			if (row && typeof row.emitItemTap === "function") {
				row.emitItemTap(detail);
			}
		},

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

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

		normalizeOffset(value, columns) {
			const offset = Number(value);
			if (!Number.isFinite(offset)) return 0;
			return Math.min(Math.max(columns - 1, 0), Math.max(0, Math.round(offset)));
		},

		normalizeAlign(value) {
			const alignMap = {
				start: "start",
				center: "center",
				end: "end",
				stretch: "stretch",
			};
			return alignMap[value] || "stretch";
		},

		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);
		},

		isPlainObject(value) {
			return !!value && Object.prototype.toString.call(value) === "[object Object]";
		},
	},
});