// Copyright (c) 2015, InfluxFramework LLC
// MIT License. See license.txt

influxframework.provide("influxframework.pages");
influxframework.provide("influxframework.views");

influxframework.views.Factory = class Factory {
	constructor(opts) {
		$.extend(this, opts);
	}

	show() {
		this.route = influxframework.get_route();
		this.page_name = influxframework.get_route_str();

		if (this.before_show && this.before_show() === false) return;

		if (influxframework.pages[this.page_name]) {
			influxframework.container.change_to(this.page_name);
			if (this.on_show) {
				this.on_show();
			}
		} else {
			if (this.route[1]) {
				this.make(this.route);
			} else {
				influxframework.show_not_found(this.route);
			}
		}
	}

	make_page(double_column, page_name) {
		return influxframework.make_page(double_column, page_name);
	}
};

influxframework.make_page = function (double_column, page_name) {
	if (!page_name) {
		page_name = influxframework.get_route_str();
	}

	const page = influxframework.container.add_page(page_name);

	influxframework.ui.make_app_page({
		parent: page,
		single_column: !double_column,
	});

	influxframework.container.change_to(page_name);
	return page;
};
