bbb.js.es6 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { withPluginApi } from "discourse/lib/plugin-api";
  2. import showModal from "discourse/lib/show-modal";
  3. import { iconHTML } from "discourse-common/lib/icon-library";
  4. import { ajax } from "discourse/lib/ajax";
  5. import { popupAjaxError } from "discourse/lib/ajax-error";
  6. function launchBBB($elem, fullWindow) {
  7. const data = $elem.data();
  8. ajax("/bbb/create.json", {
  9. type: "POST",
  10. data: data
  11. })
  12. .then(res => {
  13. if (res.url) {
  14. if (fullWindow) {
  15. window.location.href = res.url;
  16. } else {
  17. $elem.children().hide();
  18. $elem.append(
  19. `<iframe src="${res.url}" allow="camera;microphone;fullscreen;speaker" width="690" height="500" style="border:none"></iframe>`
  20. );
  21. }
  22. }
  23. })
  24. .catch(function(error) {
  25. popupAjaxError(error);
  26. });
  27. }
  28. function attachButton($elem, fullWindow) {
  29. const buttonLabel = $elem.data("label") || I18n.t("bbb.launch");
  30. $elem.html(
  31. `<button class='launch-bbb btn'>${iconHTML(
  32. "video"
  33. )} ${buttonLabel}</button>`
  34. );
  35. $elem.find("button").on("click", () => launchBBB($elem, fullWindow));
  36. }
  37. function attachStatus($elem, helper) {
  38. const status = $elem.find(".bbb-status");
  39. const data = $elem.data();
  40. ajax(`/bbb/status/${data.meetingID}.json`).then(res => {
  41. if (res.avatars) {
  42. status.html(`<span>On the call: </span>`);
  43. res.avatars.forEach(function(avatar) {
  44. status.append(
  45. `<img src="${avatar.avatar_url}" class="avatar" width="25" height="25" title="${avatar.name}" />`
  46. );
  47. });
  48. }
  49. });
  50. }
  51. function attachBBB($elem, helper) {
  52. if (helper) {
  53. const siteSettings = Discourse.__container__.lookup("site-settings:main");
  54. const fullWindow = siteSettings.bbb_full_window;
  55. $elem.find("[data-wrap=discourse-bbb]").each((idx, val) => {
  56. attachButton($(val), fullWindow);
  57. $(val).append("<span class='bbb-status'></span>");
  58. attachStatus($(val), helper);
  59. });
  60. }
  61. }
  62. export default {
  63. name: "insert-bbb",
  64. initialize() {
  65. withPluginApi("0.8.31", api => {
  66. const currentUser = api.getCurrentUser();
  67. const siteSettings = api.container.lookup("site-settings:main");
  68. api.onToolbarCreate(toolbar => {
  69. if (siteSettings.bbb_staff_only && !currentUser.staff) {
  70. return;
  71. }
  72. toolbar.addButton({
  73. title: "bbb.composer_title",
  74. id: "insertBBB",
  75. group: "insertions",
  76. icon: "fab-bootstrap",
  77. perform: e =>
  78. showModal("insert-bbb").setProperties({ toolbarEvent: e })
  79. });
  80. });
  81. api.decorateCooked(attachBBB, { id: "discourse-bbb" });
  82. });
  83. }
  84. };