{"size": 2878, "ext": "js", "max_stars_repo_path": "src/js/plugins/wecodeart-JSManager.js", "max_stars_repo_name": "BicanMarianValeriu/wecodeart-framework", "max_stars_repo_stars_event_min_datetime": "2019-01-02T21:13:51.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-20T23:06:33.000Z", "max_issues_repo_path": "src/js/plugins/wecodeart-JSManager.js", "max_issues_repo_name": "BicanMarianValeriu/wecodeart-framework", "max_issues_repo_issues_event_min_datetime": "2020-03-16T11:53:00.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-20T04:39:20.000Z", "max_forks_repo_path": "src/js/plugins/wecodeart-JSManager.js", "max_forks_repo_name": "BicanMarianValeriu/wecodeart-framework", "max_forks_repo_forks_event_min_datetime": "2019-02-19T01:38:10.000Z", "max_forks_repo_forks_event_max_datetime": "2019-02-19T01:38:10.000Z"}
{"max_stars_count": 13, "max_issues_count": 3, "max_forks_count": 1, "avg_line_length": 29.3673469388, "max_line_length": 123, "alphanum_fraction": 0.6299513551}
import camelCase from '../helpers/camelCase';
import requireJs from '../helpers/requireJs';

/**
 * Utility to trigger JS code based on clasname routing
 */
export default (function (wecodeart) {

	class JSManager {
		/**
		 * Generic constructor 
		 * @constructor
		 * @param {object} namespace 	| JS Object
		 * @param {string} routes		| Key name containing routes 
		 */
		constructor(namespace) {
			const { routes, lazyJs } = namespace;
			const { doAction, applyFilters } = wp.hooks;
			this.routes = routes;
			this.lazyJs = lazyJs;
			this.loaded = [];
			this.doAction = doAction;
			this.applyFilters = applyFilters;
			this.extendedR = Object.keys(routes).filter(k => routes[k]?.extends && routes[k].extends instanceof Array);
		}

		/**
		 * Meant to be run on load
		 * @info 	It handles all necessary JS init from routes
		 */
		fireRoute(route, funcname, args) {
			let fire;
			funcname = funcname === undefined ? 'init' : funcname;
			fire = route !== '';
			fire = fire && this.routes[route];
			fire = fire && (typeof this.routes[route][funcname] === 'function' || typeof this.routes[route][funcname] === 'object');

			if (fire) {
				args = this.applyFilters('wecodeart.route', args, route, funcname);

				if (typeof this.routes[route][funcname] === 'object') {
					const { id: bundleIds = [], callback = () => { }, condition = true } = this.routes[route][funcname];
					const condMeet = typeof condition === 'function' ? condition() !== false : condition;
					const hasBundle = [...bundleIds].filter(k => Object.keys(this.lazyJs).includes(k));
					if (condMeet && hasBundle.length) {
						requireJs(this.lazyJs, bundleIds, () => {
							callback(args);
							this.doAction('wecodeart.route', route, 'lazy', args);
						});
						this.loaded.push(route);
						return;
					}
					return;
				}

				this.routes[route][funcname](args);
				this.doAction('wecodeart.route', route, funcname, args);
				this.loaded.push(route);
				return;
			}
		}

		/**
		 * Meant to be run on load
		 * @info 	It handles all necessary JS init from routes
		 */
		sequence(route) {
			if (this.loaded.includes(route)) return;
			this.fireRoute(route);
			this.fireRoute(route, 'complete');
			this.fireRoute(route, 'lazy');
		}

		/**
		 * Meant to be run on load
		 * @info 	It handles all necessary JS init from routes
		 */
		loadEvents() {
			this.fireRoute('common');
			for (let cls of document.body.classList) {
				const route = camelCase(cls.toLowerCase().replace(/-/g, '_'));
				// Fire Manual Routes
				this.sequence(route);
				// Additional Extended Routes
				this.extendedR.filter(k => this.routes[k].extends.includes(cls) && this.sequence(k));
			}
			this.fireRoute('common', 'complete');
			this.fireRoute('common', 'lazy');
		}
	}

	/**
	 * @static
	 * @memberof JSManager
	 */
	wecodeart.JSM = JSManager;

}).apply(this, [window.wecodeart]);