client-elements.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var fs = require("fs");
  2. const CLIENT_FILES_OPT = "clientFiles";
  3. /**
  4. * Enable a element on clients
  5. * @param clients
  6. * @param ui
  7. * @param bs
  8. * @returns {Function}
  9. */
  10. var types = {
  11. "css": "text/css",
  12. "js": "application/javascript"
  13. };
  14. function enableElement (clients, ui, bs) {
  15. return function (file) {
  16. var uiItem = ui.getOptionIn([CLIENT_FILES_OPT, file.name]);
  17. var item = uiItem.toJS();
  18. var enableFn = uiItem.getIn(["callbacks", "enable"]);
  19. if (item.active) {
  20. return;
  21. }
  22. ui.setOptionIn([CLIENT_FILES_OPT, item.name, "active"], true, {silent: true});
  23. if (enableFn) {
  24. enableFn.call(ui, item);
  25. }
  26. if (item.file && !item.served) {
  27. ui.setOptionIn([CLIENT_FILES_OPT, item.name, "served"], true, {silent: true});
  28. bs.serveFile(item.src, {
  29. type: types[item.type],
  30. content: fs.readFileSync(item.file)
  31. });
  32. }
  33. addElement(clients, ui.getOptionIn([CLIENT_FILES_OPT, item.name]).toJS());
  34. };
  35. }
  36. /**
  37. * @param clients
  38. * @param ui
  39. * @returns {Function}
  40. */
  41. function disableElement (clients, ui) {
  42. return function (file) {
  43. var uiItem = ui.getOptionIn([CLIENT_FILES_OPT, file.name]);
  44. var item = uiItem.toJS();
  45. var disableFn = uiItem.getIn(["callbacks", "disable"]);
  46. if (disableFn) {
  47. disableFn.call(ui, item);
  48. }
  49. ui.setOptionIn([CLIENT_FILES_OPT, item.name, "active"], false, {silent: true});
  50. removeElement(clients, item.id);
  51. };
  52. }
  53. /**
  54. * @param clients
  55. * @param item
  56. */
  57. function addElement (clients, item) {
  58. clients.emit("ui:element:add", item);
  59. }
  60. /**
  61. * @param clients
  62. * @param id
  63. */
  64. function removeElement(clients, id) {
  65. clients.emit("ui:element:remove", {id: id});
  66. }
  67. module.exports.addElement = addElement;
  68. module.exports.removeElement = removeElement;
  69. module.exports.enable = enableElement;
  70. module.exports.disable = disableElement;