/
eb
/
Surf
Обзор
Документация
Войти
/
eb
/
Surf
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
node_modules/browser-sync-ui/lib/client-elements.js
93 строки
2 KB
EvgeniyBudaev
Initial commit
17 фев 2020, 09:02
17 фев 2020, 09:02
bb6f06f
Код
Авторство
О чём код?
var fs = require("fs"); const CLIENT_FILES_OPT = "clientFiles"; /** * Enable a element on clients * @param clients * @param ui * @param bs * @returns {Function} */ var types = { "css": "text/css", "js": "application/javascript" }; function enableElement (clients, ui, bs) { return function (file) { var uiItem = ui.getOptionIn([CLIENT_FILES_OPT, file.name]); var item = uiItem.toJS(); var enableFn = uiItem.getIn(["callbacks", "enable"]); if (item.active) { return; } ui.setOptionIn([CLIENT_FILES_OPT, item.name, "active"], true, {silent: true}); if (enableFn) { enableFn.call(ui, item); } if (item.file && !item.served) { ui.setOptionIn([CLIENT_FILES_OPT, item.name, "served"], true, {silent: true}); bs.serveFile(item.src, { type: types[item.type], content: fs.readFileSync(item.file) }); } addElement(clients, ui.getOptionIn([CLIENT_FILES_OPT, item.name]).toJS()); }; } /** * @param clients * @param ui * @returns {Function} */ function disableElement (clients, ui) { return function (file) { var uiItem = ui.getOptionIn([CLIENT_FILES_OPT, file.name]); var item = uiItem.toJS(); var disableFn = uiItem.getIn(["callbacks", "disable"]); if (disableFn) { disableFn.call(ui, item); } ui.setOptionIn([CLIENT_FILES_OPT, item.name, "active"], false, {silent: true}); removeElement(clients, item.id); }; } /** * @param clients * @param item */ function addElement (clients, item) { clients.emit("ui:element:add", item); } /** * @param clients * @param id */ function removeElement(clients, id) { clients.emit("ui:element:remove", {id: id}); } module.exports.addElement = addElement; module.exports.removeElement = removeElement; module.exports.enable = enableElement; module.exports.disable = disableElement;