| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 | var fs         = require("fs");var path       = require("path");var pluginTmpl     = templateFile("/plugin.tmpl");var configTmpl     = templateFile("/config.tmpl");var configItem     = templateFile("/config.item.tmpl");var inlineTemp     = templateFile("/inline.template.tmpl");var pluginItemTmpl = fs.readFileSync(path.resolve(__dirname, "../", "templates/plugin.item.tmpl"), "utf-8");function templateFile (filepath) {    return fs.readFileSync(path.join(__dirname, "/../templates", filepath || ""), "utf-8");}/** * @type {{page: Function, markup: Function, client:js: Function, templates: Function}} */module.exports = {    /**     * Create the url config for each section of the ui     * @param hooks     * @param ui     */    "page": function (hooks, ui) {        var config = hooks            .map(transformConfig)            .reduce(createConfigItem, {});        return {            /**             * pagesConfig - This is the angular configuration such as routes             */            pagesConfig: configTmpl                .replace("%when%", hooks.reduce(                    createAngularRoutes,                    ""                ))                .replace("%pages%", JSON.stringify(                    config,                    null,                    4                )),            /**             * pagesConfig in object form             */            pagesObj: config,            pageMarkup: function () {                return preAngular(ui.pluginManager.plugins, config, ui);            }        };    },    /**     * Controller markup for each plugin     * @param hooks     * @returns {*}     */    "markup": function (hooks) {        return hooks.reduce(pluginTemplate, "");    },    /**     * @param hooks     * @param {UI} ui     * @returns {*|string}     */    "client:js": function (hooks, ui) {        /**         * Add client JS from Browsersync Plugins         */        ui.bsPlugins.forEach(function (plugin) {            if (plugin.has("client:js")) {                plugin.get("client:js").forEach(function (value) {                    hooks.push(value);                });            }        });        var out = hooks.reduce(function (all, item) {            if (typeof item === "string") {                all += ";" + item;            } else if (Array.isArray(item)) {                item.forEach(function (item) {                    all += ";" + item;                });            }            return all;        }, "");        return out;    },    /**     * @param hooks     * @param initial     * @param {UI} ui     * @returns {String}     */    "templates": function (hooks, initial, ui) {        /**         * Add templates from each Browsersync registered plugin         * @type {string}         */        var pluginDirectives = ui.bsPlugins.reduce(function (all, plugin) {            if (!plugin.has("templates")) {                return all;            }            /**             * Slugify-ish the plugin name             *  eg: Test Browsersync Plugin             *    = test-browsersync-plugin             * @type {string}             */            var slug = plugin.get("name")                .trim()                .split(" ")                .map(function (word) {                    return word.trim().toLowerCase();                })                .join("-");            /**             * For every plugin that has templates, wrap             * the markup in the <script type="text/ng-template" id="{{slug}}"></script>             * markup to result in the single output string.             */            plugin.get("templates").forEach(function (value, key) {                all +=  angularWrap([slug, path.basename(key)].join("/"), value);            });            return all;        }, "");        /**         * Combine the markup from the plugins done above with any         * others registered via hooks + initial         * to create the final markup         */        return [pluginDirectives, createInlineTemplates(hooks.concat([initial]))].join("");    },    /**     * Allow plugins to register toggle-able elements     * @param hooks     * @returns {{}}     */    "elements": function (hooks) {        var obj = {};        hooks.forEach(function (elements) {            elements.forEach(function (item) {                if (!obj[item.name]) {                    obj[item.name] = item;                }            });        });        return obj;    }};/** * @param hooks * @returns {String} */function createInlineTemplates (hooks) {    return hooks.reduce(function (combined, item) {        return combined + item.reduce(function (all, filepath) {            return all + angularWrap(                path.basename(filepath),                fs.readFileSync(filepath));        }, "");    }, "");}/** * @param item * @returns {*} */function transformConfig (item) {    return item;}/** * @param {String} all * @param {Object} item * @returns {*} */function createAngularRoutes(all, item) {    return all + configItem.replace(/%(.+)%/g, function () {        var key = arguments[1];        if (item[key]) {            return item[key];        }    });}/** * @param joined * @param item * @returns {*} */function createConfigItem (joined, item) {    if (item.path === "/") {        joined["overview"] = item;    } else {        joined[item.path.slice(1)] = item;    }    return joined;}/** * @returns {*} */function pluginTemplate (combined, item) {    return [combined, pluginTmpl.replace("%markup%", item)].join("\n");}/** * @param plugins * @param config * @returns {*} */function preAngular (plugins, config, ui) {    return Object.keys(plugins)        .filter(function (key) {            return config[key]; // only work on plugins that have pages        })        .map(function (key) {            if (key === "plugins") {                var pluginMarkup = ui.bsPlugins.reduce(function (all, item, i) {                    all += pluginItemTmpl                        .replace("%content%", item.get("markup") || "")                        .replace(/%index%/g, i)                        .replace(/%name%/g, item.get("name"));                    return all;                }, "");                plugins[key].hooks.markup = plugins[key].hooks.markup.replace("%pluginlist%", pluginMarkup);            }            return angularWrap(config[key].template, bindOnce(plugins[key].hooks.markup, config[key]));        })        .reduce(function (combined, item) {            return combined + item;        }, "");}/** * @param templateName * @param markup * @returns {*} */function angularWrap (templateName, markup) {    return inlineTemp        .replace("%content%", markup)        .replace("%id%", templateName);}/** * @param markup * @param config * @returns {*|string} */function bindOnce (markup, config) {    return markup.toString().replace(/\{\{ctrl.section\.(.+?)\}\}/g, function ($1, $2) {        return config[$2] || "";    });}module.exports.bindOnce = bindOnce;
 |