{"size": 2477, "ext": "js", "max_stars_repo_path": "Page.loaderPlugin.js", "max_stars_repo_name": "YveOne/Page.js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Page.loaderPlugin.js", "max_issues_repo_name": "YveOne/Page.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Page.loaderPlugin.js", "max_forks_repo_name": "YveOne/Page.js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.4712643678, "max_line_length": 65, "alphanum_fraction": 0.4473153008}
﻿/*
 *  Page.loaderPlugin.js v1.1
 *  Author: Yvonne P. (yveone.com)
 *  License: MIT
**/
"use strict";

((name, deps, factory) => {

    // AMD
    if (typeof define !== "undefined" && define.amd){
        define(deps, factory);

    // Node.js
    } else if (typeof module !== "undefined" && module.exports) {
        module.exports = factory.apply(deps.map(require));

    // Browser
    } else {
        let d, i = 0, global = this, old = global[name], mod;
        while(d = deps[i]){
            deps[i++] = this[d];
        }
        global[name] = mod = factory.apply(null, deps);
        mod.noConflict = () => {
            global[name] = old;
            return mod;
        };
    }

})("Page.loaderPlugin", ["Page"], (Page) => {

    let plugin = {};

    function loadFile(target, tagName, attr) {
        return new Promise((resolve, reject) => {
            let el = document.createElement(tagName);
            Object.keys(attr).forEach((k) => {
                el.setAttribute(k, attr[k]);
            });
            el.onload = () => {
                resolve(el);
            };
            el.onerror = () => {
                target.removeChild(el);
                reject();
            };
            target.appendChild(el);
        });
    }

    function loadPagesJS(path, ids) {
        path = path.replace(/\/+$/g, '');
        return new Promise((resolve, reject) => {
            Promise.all(ids.map((id) => {
                return new Promise((resolve, reject) => {
                    loadFile(document.head, "script", {
                        "type": "text/javascript",
                        "src": `${path}/${id}.js`
                    }).then(resolve).catch(reject);
                });
            })).then(resolve).catch(reject);
        });
    }

    function loadPagesCSS(path, ids) {
        path = path.replace(/\/+$/g, '');
        return new Promise((resolve, reject) => {
            Promise.all(ids.map((id) => {
                return new Promise((resolve, reject) => {
                    loadFile(document.head, "link", {
                        "rel": "stylesheet",
                        "type": "text/css",
                        "href": `${path}/${id}.js`
                    }).then(resolve).catch(reject);
                });
            })).then(resolve).catch(reject);
        });
    }

    plugin.loadJS = loadPagesJS;
    plugin.loadCSS = loadPagesCSS;

    Page.loaderPlugin = plugin;
    return plugin;
});
