federationscopemenu.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. /* global OC, Handlebars */
  7. (function() {
  8. /**
  9. * Construct a new FederationScopeMenu instance
  10. * @constructs FederationScopeMenu
  11. * @memberof OC.Settings
  12. * @param {object} options
  13. * @param {array.<string>} [options.excludedScopes] array of excluded scopes
  14. */
  15. var FederationScopeMenu = OC.Backbone.View.extend({
  16. tagName: 'div',
  17. className: 'federationScopeMenu popovermenu bubble menu menu-center',
  18. field: undefined,
  19. _scopes: undefined,
  20. _excludedScopes: [],
  21. initialize: function(options) {
  22. this.field = options.field;
  23. this._scopes = [
  24. {
  25. name: 'v2-private',
  26. displayName: t('settings', 'Private'),
  27. tooltip: t('settings', 'Only visible to people matched via phone number integration through Talk on mobile'),
  28. iconClass: 'icon-phone',
  29. active: false
  30. },
  31. {
  32. name: 'v2-local',
  33. displayName: t('settings', 'Local'),
  34. tooltip: t('settings', 'Only visible to people on this instance and guests'),
  35. iconClass: 'icon-password',
  36. active: false
  37. },
  38. {
  39. name: 'v2-federated',
  40. displayName: t('settings', 'Federated'),
  41. tooltip: t('settings', 'Only synchronize to trusted servers'),
  42. iconClass: 'icon-contacts-dark',
  43. active: false
  44. },
  45. {
  46. name: 'v2-published',
  47. displayName: t('settings', 'Published'),
  48. tooltip: t('settings', 'Synchronize to trusted servers and the global and public address book'),
  49. iconClass: 'icon-link',
  50. active: false
  51. }
  52. ];
  53. if (options.excludedScopes && options.excludedScopes.length) {
  54. this._excludedScopes = options.excludedScopes
  55. }
  56. },
  57. /**
  58. * Current context
  59. *
  60. * @type OCA.Files.FileActionContext
  61. */
  62. _context: null,
  63. events: {
  64. 'click a.action': '_onSelectScope',
  65. 'keydown a.action': '_onSelectScopeKeyboard'
  66. },
  67. /**
  68. * Event handler whenever an action has been clicked within the menu
  69. *
  70. * @param {Object} event event object
  71. */
  72. _onSelectScope: function(event) {
  73. var $target = $(event.currentTarget);
  74. if (!$target.hasClass('menuitem')) {
  75. $target = $target.closest('.menuitem');
  76. }
  77. this.trigger('select:scope', $target.data('action'));
  78. OC.hideMenus();
  79. },
  80. _onSelectScopeKeyboard: function(event) {
  81. if (event.keyCode === 13 || event.keyCode === 32) {
  82. // Enter and space can be used to select a scope
  83. event.preventDefault();
  84. this._onSelectScope(event);
  85. }
  86. },
  87. /**
  88. * Renders the menu with the currently set items
  89. */
  90. render: function() {
  91. this.$el.html(OC.Settings.Templates['federationscopemenu']({
  92. items: this._scopes
  93. }));
  94. },
  95. /**
  96. * Displays the menu
  97. */
  98. show: function(context) {
  99. this._context = context;
  100. var currentlyActiveValue = $('#'+context.target.closest('form').id).find('input[type="hidden"]')[0].value;
  101. for(var i in this._scopes) {
  102. if (this._scopes[i].name === currentlyActiveValue) {
  103. this._scopes[i].active = true;
  104. } else {
  105. this._scopes[i].active = false;
  106. }
  107. var isExcludedScope = this._excludedScopes.includes(this._scopes[i].name)
  108. if (isExcludedScope && !this._scopes[i].active) {
  109. this._scopes[i].hidden = true
  110. } else if (isExcludedScope && this._scopes[i].active) {
  111. this._scopes[i].hidden = false
  112. this._scopes[i].disabled = true
  113. } else {
  114. this._scopes[i].hidden = false
  115. this._scopes[i].disabled = false
  116. }
  117. }
  118. this.render();
  119. this.$el.removeClass('hidden');
  120. OC.showMenu(null, this.$el);
  121. }
  122. });
  123. OC.Settings = OC.Settings || {};
  124. OC.Settings.FederationScopeMenu = FederationScopeMenu;
  125. })();