bbb.js.es6 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.decorateCooked(attachBBB, {
  69. id: "discourse-bbb",
  70. });
  71. if (siteSettings.bbb_staff_only && !currentUser.staff) {
  72. return;
  73. }
  74. api.modifyClass("controller:composer", {
  75. actions: {
  76. insertBBBModal() {
  77. showModal("insert-bbb").setProperties({
  78. toolbarEvent: this.get("toolbarEvent"),
  79. });
  80. },
  81. },
  82. });
  83. api.addToolbarPopupMenuOptionsCallback((controller) => {
  84. return {
  85. id: "insert-bbb",
  86. icon: "video",
  87. action: "insertBBBModal",
  88. label: "bbb.composer_title",
  89. };
  90. });
  91. });
  92. },
  93. };