federationscopemenu.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2016
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. /* global OC, Handlebars */
  11. (function() {
  12. /**
  13. * Construct a new FederationScopeMenu instance
  14. * @constructs FederationScopeMenu
  15. * @memberof OC.Settings
  16. */
  17. var FederationScopeMenu = OC.Backbone.View.extend({
  18. tagName: 'div',
  19. className: 'federationScopeMenu popovermenu bubble menu menu-center',
  20. field: undefined,
  21. _scopes: undefined,
  22. initialize: function(options) {
  23. this.field = options.field;
  24. this._scopes = [
  25. {
  26. name: 'private',
  27. displayName: (this.field === 'avatar' || this.field === 'displayname') ? t('settings', 'Local') : t('settings', 'Private'),
  28. tooltip: (this.field === 'avatar' || this.field === 'displayname') ? t('settings', 'Only visible to local users') : t('settings', 'Only visible to you'),
  29. iconClass: 'icon-password',
  30. active: false
  31. },
  32. {
  33. name: 'contacts',
  34. displayName: t('settings', 'Contacts'),
  35. tooltip: t('settings', 'Visible to local users and to trusted servers'),
  36. iconClass: 'icon-contacts-dark',
  37. active: false
  38. },
  39. {
  40. name: 'public',
  41. displayName: t('settings', 'Public'),
  42. tooltip: t('settings', 'Will be synced to a global and public address book'),
  43. iconClass: 'icon-link',
  44. active: false
  45. }
  46. ];
  47. },
  48. /**
  49. * Current context
  50. *
  51. * @type OCA.Files.FileActionContext
  52. */
  53. _context: null,
  54. events: {
  55. 'click a.action': '_onSelectScope',
  56. 'keydown a.action': '_onSelectScopeKeyboard'
  57. },
  58. /**
  59. * Event handler whenever an action has been clicked within the menu
  60. *
  61. * @param {Object} event event object
  62. */
  63. _onSelectScope: function(event) {
  64. var $target = $(event.currentTarget);
  65. if (!$target.hasClass('menuitem')) {
  66. $target = $target.closest('.menuitem');
  67. }
  68. this.trigger('select:scope', $target.data('action'));
  69. OC.hideMenus();
  70. },
  71. _onSelectScopeKeyboard: function(event) {
  72. if (event.keyCode === 13 || event.keyCode === 32) {
  73. // Enter and space can be used to select a scope
  74. event.preventDefault();
  75. this._onSelectScope(event);
  76. }
  77. },
  78. /**
  79. * Renders the menu with the currently set items
  80. */
  81. render: function() {
  82. this.$el.html(OC.Settings.Templates['federationscopemenu']({
  83. items: this._scopes
  84. }));
  85. },
  86. /**
  87. * Displays the menu
  88. */
  89. show: function(context) {
  90. this._context = context;
  91. var currentlyActiveValue = $('#'+context.target.closest('form').id).find('input[type="hidden"]')[0].value;
  92. for(var i in this._scopes) {
  93. this._scopes[i].active = false;
  94. }
  95. switch (currentlyActiveValue) {
  96. case "private":
  97. this._scopes[0].active = true;
  98. break;
  99. case "contacts":
  100. this._scopes[1].active = true;
  101. break;
  102. case "public":
  103. this._scopes[2].active = true;
  104. break;
  105. }
  106. this.render();
  107. this.$el.removeClass('hidden');
  108. OC.showMenu(null, this.$el);
  109. }
  110. });
  111. OC.Settings = OC.Settings || {};
  112. OC.Settings.FederationScopeMenu = FederationScopeMenu;
  113. })();