bbb.js.es6 2.5 KB

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