bbb.js.es6 3.0 KB

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