You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SettingsMenu.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* global $, APP, AJS, interfaceConfig, JitsiMeetJS */
  2. import UIUtil from "../../util/UIUtil";
  3. import UIEvents from "../../../../service/UI/UIEvents";
  4. import languages from "../../../../service/translation/languages";
  5. import Settings from '../../../settings/Settings';
  6. /**
  7. * Generate html select options for available languages.
  8. *
  9. * @param {string[]} items available languages
  10. * @param {string} [currentLang] current language
  11. * @returns {string}
  12. */
  13. function generateLanguagesOptions(items, currentLang) {
  14. return items.map(function (lang) {
  15. let attrs = {
  16. value: lang,
  17. 'data-i18n': `languages:${lang}`
  18. };
  19. if (lang === currentLang) {
  20. attrs.selected = 'selected';
  21. }
  22. let attrsStr = UIUtil.attrsToString(attrs);
  23. return `<option ${attrsStr}></option>`;
  24. }).join('');
  25. }
  26. /**
  27. * Generate html select options for available physical devices.
  28. *
  29. * @param {{ deviceId, label }[]} items available devices
  30. * @param {string} [selectedId] id of selected device
  31. * @param {boolean} permissionGranted if permission to use selected device type
  32. * is granted
  33. * @returns {string}
  34. */
  35. function generateDevicesOptions(items, selectedId, permissionGranted) {
  36. if (!permissionGranted && items.length) {
  37. return '<option data-i18n="settings.noPermission"></option>';
  38. }
  39. var options = items.map(function (item) {
  40. let attrs = {
  41. value: item.deviceId
  42. };
  43. if (item.deviceId === selectedId) {
  44. attrs.selected = 'selected';
  45. }
  46. let attrsStr = UIUtil.attrsToString(attrs);
  47. return `<option ${attrsStr}>${item.label}</option>`;
  48. });
  49. if (!items.length) {
  50. options.unshift('<option data-i18n="settings.noDevice"></option>');
  51. }
  52. return options.join('');
  53. }
  54. /**
  55. * Replace html select element to select2 custom dropdown
  56. *
  57. * @param {jQueryElement} $el native select element
  58. * @param {function} onSelectedCb fired if item is selected
  59. */
  60. function initSelect2($el, onSelectedCb) {
  61. $el.auiSelect2({
  62. minimumResultsForSearch: Infinity
  63. });
  64. if (typeof onSelectedCb === 'function') {
  65. $el.change(onSelectedCb);
  66. }
  67. }
  68. export default {
  69. init (emitter) {
  70. //LANGUAGES BOX
  71. if (UIUtil.isSettingEnabled('language')) {
  72. const wrapperId = 'languagesSelectWrapper';
  73. const selectId = 'languagesSelect';
  74. const selectEl = AJS.$(`#${selectId}`);
  75. let selectInput;
  76. selectEl.html(generateLanguagesOptions(
  77. languages.getLanguages(),
  78. APP.translation.getCurrentLanguage()
  79. ));
  80. initSelect2(selectEl, () => {
  81. const val = selectEl.val();
  82. selectInput[0].dataset.i18n = `languages:${val}`;
  83. APP.translation.translateElement(selectInput);
  84. emitter.emit(UIEvents.LANG_CHANGED, val);
  85. });
  86. //find new selectInput element
  87. selectInput = $(`#s2id_${selectId} .select2-chosen`);
  88. //first select fix for languages options
  89. selectInput[0].dataset.i18n =
  90. `languages:${APP.translation.getCurrentLanguage()}`;
  91. APP.translation.translateElement(selectEl);
  92. UIUtil.showElement(wrapperId);
  93. }
  94. // DEVICES LIST
  95. if (UIUtil.isSettingEnabled('devices')) {
  96. const wrapperId = 'deviceOptionsWrapper';
  97. JitsiMeetJS.mediaDevices.isDeviceListAvailable()
  98. .then((isDeviceListAvailable) => {
  99. if (isDeviceListAvailable &&
  100. JitsiMeetJS.mediaDevices.isDeviceChangeAvailable()) {
  101. this._initializeDeviceSelectionSettings(emitter);
  102. }
  103. });
  104. // Only show the subtitle if this isn't the only setting section.
  105. if (interfaceConfig.SETTINGS_SECTIONS.length > 1)
  106. UIUtil.showElement("deviceOptionsTitle");
  107. UIUtil.showElement(wrapperId);
  108. }
  109. // MODERATOR
  110. if (UIUtil.isSettingEnabled('moderator')) {
  111. const wrapperId = 'moderatorOptionsWrapper';
  112. // START MUTED
  113. $("#startMutedOptions").change(function () {
  114. let startAudioMuted = $("#startAudioMuted").is(":checked");
  115. let startVideoMuted = $("#startVideoMuted").is(":checked");
  116. emitter.emit(
  117. UIEvents.START_MUTED_CHANGED,
  118. startAudioMuted,
  119. startVideoMuted
  120. );
  121. });
  122. // FOLLOW ME
  123. const followMeToggle = document.getElementById('followMeCheckBox');
  124. followMeToggle.addEventListener('change', () => {
  125. const isFollowMeEnabled = followMeToggle.checked;
  126. emitter.emit(UIEvents.FOLLOW_ME_ENABLED, isFollowMeEnabled);
  127. });
  128. UIUtil.showElement(wrapperId);
  129. }
  130. },
  131. _initializeDeviceSelectionSettings(emitter) {
  132. this.changeDevicesList([]);
  133. $('#selectCamera').change(function () {
  134. let cameraDeviceId = $(this).val();
  135. if (cameraDeviceId !== Settings.getCameraDeviceId()) {
  136. emitter.emit(UIEvents.VIDEO_DEVICE_CHANGED, cameraDeviceId);
  137. }
  138. });
  139. $('#selectMic').change(function () {
  140. let micDeviceId = $(this).val();
  141. if (micDeviceId !== Settings.getMicDeviceId()) {
  142. emitter.emit(UIEvents.AUDIO_DEVICE_CHANGED, micDeviceId);
  143. }
  144. });
  145. $('#selectAudioOutput').change(function () {
  146. let audioOutputDeviceId = $(this).val();
  147. if (audioOutputDeviceId !== Settings.getAudioOutputDeviceId()) {
  148. emitter.emit(
  149. UIEvents.AUDIO_OUTPUT_DEVICE_CHANGED, audioOutputDeviceId);
  150. }
  151. });
  152. },
  153. /**
  154. * If start audio muted/start video muted options should be visible or not.
  155. * @param {boolean} show
  156. */
  157. showStartMutedOptions (show) {
  158. if (show && UIUtil.isSettingEnabled('moderator')) {
  159. // Only show the subtitle if this isn't the only setting section.
  160. if (!$("#moderatorOptionsTitle").is(":visible")
  161. && interfaceConfig.SETTINGS_SECTIONS.length > 1)
  162. UIUtil.showElement("moderatorOptionsTitle");
  163. UIUtil.showElement("startMutedOptions");
  164. } else {
  165. // Only show the subtitle if this isn't the only setting section.
  166. if ($("#moderatorOptionsTitle").is(":visible"))
  167. UIUtil.hideElement("moderatorOptionsTitle");
  168. UIUtil.hideElement("startMutedOptions");
  169. }
  170. },
  171. updateStartMutedBox (startAudioMuted, startVideoMuted) {
  172. $("#startAudioMuted").attr("checked", startAudioMuted);
  173. $("#startVideoMuted").attr("checked", startVideoMuted);
  174. },
  175. /**
  176. * Shows/hides the follow me options in the settings dialog.
  177. *
  178. * @param {boolean} show {true} to show those options, {false} to hide them
  179. */
  180. showFollowMeOptions (show) {
  181. if (show && UIUtil.isSettingEnabled('moderator')) {
  182. UIUtil.showElement("followMeOptions");
  183. } else {
  184. UIUtil.hideElement("followMeOptions");
  185. }
  186. },
  187. /**
  188. * Check if settings menu is visible or not.
  189. * @returns {boolean}
  190. */
  191. isVisible () {
  192. return UIUtil.isVisible(document.getElementById("settings_container"));
  193. },
  194. /**
  195. * Sets microphone's <select> element to select microphone ID from settings.
  196. */
  197. setSelectedMicFromSettings () {
  198. $('#selectMic').val(Settings.getMicDeviceId());
  199. },
  200. /**
  201. * Sets camera's <select> element to select camera ID from settings.
  202. */
  203. setSelectedCameraFromSettings () {
  204. $('#selectCamera').val(Settings.getCameraDeviceId());
  205. },
  206. /**
  207. * Sets audio outputs's <select> element to select audio output ID from
  208. * settings.
  209. */
  210. setSelectedAudioOutputFromSettings () {
  211. $('#selectAudioOutput').val(Settings.getAudioOutputDeviceId());
  212. },
  213. /**
  214. * Change available cameras/microphones or hide selects completely if
  215. * no devices available.
  216. * @param {{ deviceId, label, kind }[]} devices list of available devices
  217. */
  218. changeDevicesList (devices) {
  219. let $selectCamera= AJS.$('#selectCamera'),
  220. $selectMic = AJS.$('#selectMic'),
  221. $selectAudioOutput = AJS.$('#selectAudioOutput'),
  222. $selectAudioOutputParent = $selectAudioOutput.parent();
  223. let audio = devices.filter(device => device.kind === 'audioinput'),
  224. video = devices.filter(device => device.kind === 'videoinput'),
  225. audioOutput = devices
  226. .filter(device => device.kind === 'audiooutput'),
  227. selectedAudioDevice = audio.find(
  228. d => d.deviceId === Settings.getMicDeviceId()) || audio[0],
  229. selectedVideoDevice = video.find(
  230. d => d.deviceId === Settings.getCameraDeviceId()) || video[0],
  231. selectedAudioOutputDevice = audioOutput.find(
  232. d => d.deviceId === Settings.getAudioOutputDeviceId()),
  233. videoPermissionGranted =
  234. JitsiMeetJS.mediaDevices.isDevicePermissionGranted('video'),
  235. audioPermissionGranted =
  236. JitsiMeetJS.mediaDevices.isDevicePermissionGranted('audio');
  237. $selectCamera
  238. .html(generateDevicesOptions(
  239. video,
  240. selectedVideoDevice ? selectedVideoDevice.deviceId : '',
  241. videoPermissionGranted))
  242. .prop('disabled', !video.length || !videoPermissionGranted);
  243. initSelect2($selectCamera);
  244. $selectMic
  245. .html(generateDevicesOptions(
  246. audio,
  247. selectedAudioDevice ? selectedAudioDevice.deviceId : '',
  248. audioPermissionGranted))
  249. .prop('disabled', !audio.length || !audioPermissionGranted);
  250. initSelect2($selectMic);
  251. if (JitsiMeetJS.mediaDevices.isDeviceChangeAvailable('output')) {
  252. $selectAudioOutput
  253. .html(generateDevicesOptions(
  254. audioOutput,
  255. selectedAudioOutputDevice
  256. ? selectedAudioOutputDevice.deviceId
  257. : 'default',
  258. videoPermissionGranted || audioPermissionGranted))
  259. .prop('disabled', !audioOutput.length ||
  260. (!videoPermissionGranted && !audioPermissionGranted));
  261. initSelect2($selectAudioOutput);
  262. $selectAudioOutputParent.show();
  263. } else {
  264. $selectAudioOutputParent.hide();
  265. }
  266. APP.translation.translateElement($('#settings_container option'));
  267. }
  268. };