federationsettingsview.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* global OC, result, _ */
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. (function(_, $, OC) {
  8. 'use strict';
  9. /**
  10. * Construct a new FederationScopeMenu instance
  11. * @constructs FederationScopeMenu
  12. * @memberof OC.Settings
  13. * @param {object} options
  14. * @param {boolean} [options.showFederatedScope=false] whether show the
  15. * "v2-federated" scope or not
  16. * @param {boolean} [options.showPublishedScope=false] whether show the
  17. * "v2-published" scope or not
  18. */
  19. var FederationSettingsView = OC.Backbone.View.extend({
  20. _inputFields: undefined,
  21. /** @var Backbone.Model */
  22. _config: undefined,
  23. initialize: function(options) {
  24. options = options || {};
  25. if (options.config) {
  26. this._config = options.config;
  27. } else {
  28. this._config = new OC.Settings.UserSettings();
  29. }
  30. this.showFederatedScope = !!options.showFederatedScope;
  31. this.showPublishedScope = !!options.showPublishedScope;
  32. this._inputFields = [
  33. 'displayname',
  34. 'phone',
  35. 'email',
  36. 'website',
  37. 'twitter',
  38. 'address',
  39. 'avatar'
  40. ];
  41. var self = this;
  42. _.each(this._inputFields, function(field) {
  43. var scopeOnly = field === 'avatar';
  44. // Initialize config model
  45. if (!scopeOnly) {
  46. self._config.set(field, $('#' + field).val());
  47. }
  48. self._config.set(field + 'Scope', $('#' + field + 'scope').val());
  49. // Set inputs whenever model values change
  50. if (!scopeOnly) {
  51. self.listenTo(self._config, 'change:' + field, function() {
  52. self.$('#' + field).val(self._config.get(field));
  53. });
  54. }
  55. self.listenTo(self._config, 'change:' + field + 'Scope', function() {
  56. self._setFieldScopeIcon(field, self._config.get(field + 'Scope'));
  57. });
  58. });
  59. this._registerEvents();
  60. },
  61. render: function() {
  62. var self = this;
  63. var fieldsWithV2Private = [
  64. 'avatar',
  65. 'phone',
  66. 'twitter',
  67. 'website',
  68. 'address',
  69. ];
  70. _.each(this._inputFields, function(field) {
  71. var $icon = self.$('#' + field + 'form .headerbar-label > .federation-menu');
  72. var excludedScopes = []
  73. if (fieldsWithV2Private.indexOf(field) === -1) {
  74. excludedScopes.push('v2-private');
  75. }
  76. if (!self.showFederatedScope) {
  77. excludedScopes.push('v2-federated');
  78. }
  79. if (!self.showPublishedScope) {
  80. excludedScopes.push('v2-published');
  81. }
  82. var scopeMenu = new OC.Settings.FederationScopeMenu({
  83. field: field,
  84. excludedScopes: excludedScopes,
  85. });
  86. self.listenTo(scopeMenu, 'select:scope', function(scope) {
  87. self._onScopeChanged(field, scope);
  88. });
  89. $icon.append(scopeMenu.$el);
  90. $icon.attr('aria-expanded', 'false');
  91. $icon.on('click', _.bind(scopeMenu.show, scopeMenu));
  92. $icon.on('keydown', function(e) {
  93. if (e.keyCode === 32) {
  94. // Open the menu when the user presses the space bar
  95. e.preventDefault();
  96. scopeMenu.show(e);
  97. } else if (e.keyCode === 27) {
  98. // Close the menu again if opened
  99. OC.hideMenus();
  100. }
  101. }.bind(this));
  102. // Restore initial state
  103. self._setFieldScopeIcon(field, self._config.get(field + 'Scope'));
  104. });
  105. },
  106. _registerEvents: function() {
  107. var self = this;
  108. _.each(this._inputFields, function(field) {
  109. if (
  110. field === 'avatar' ||
  111. field === 'email' ||
  112. field === 'displayname' ||
  113. field === 'twitter' ||
  114. field === 'address' ||
  115. field === 'website' ||
  116. field === 'phone'
  117. ) {
  118. return;
  119. }
  120. self.$('#' + field).keyUpDelayedOrEnter(_.bind(self._onInputChanged, self), true);
  121. });
  122. },
  123. _onInputChanged: function(e) {
  124. var self = this;
  125. var $dialog = $('.oc-dialog:visible');
  126. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  127. if($dialog.length === 0) {
  128. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onInputChanged, this, e));
  129. }
  130. return;
  131. }
  132. var $target = $(e.target);
  133. var value = $target.val();
  134. var field = $target.attr('id');
  135. this._config.set(field, value);
  136. var savingData = this._config.save({
  137. error: function(jqXHR) {
  138. OC.msg.finishedSaving('#personal-settings-container .msg', jqXHR);
  139. }
  140. });
  141. $.when(savingData).done(function(data) {
  142. if (data.status === "success") {
  143. self._showInputChangeSuccess(field);
  144. } else {
  145. self._showInputChangeFail(field);
  146. }
  147. });
  148. },
  149. _onScopeChanged: function(field, scope) {
  150. var $dialog = $('.oc-dialog:visible');
  151. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  152. if($dialog.length === 0) {
  153. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onScopeChanged, this, field, scope));
  154. }
  155. return;
  156. }
  157. this._config.set(field + 'Scope', scope);
  158. $('#' + field + 'scope').val(scope);
  159. // TODO: user loading/success feedback
  160. this._config.save();
  161. this._setFieldScopeIcon(field, scope);
  162. this._updateVerifyButton(field, scope);
  163. },
  164. _updateVerifyButton: function(field, scope) {
  165. // show verification button if the value is set and the scope is 'public'
  166. if (field === 'twitter' || field === 'website'|| field === 'email') {
  167. var verify = this.$('#' + field + 'form > .verify');
  168. var scope = this.$('#' + field + 'scope').val();
  169. var value = this.$('#' + field).val();
  170. if (scope === 'public' && value !== '') {
  171. verify.removeClass('hidden');
  172. return true;
  173. } else {
  174. verify.addClass('hidden');
  175. }
  176. }
  177. return false;
  178. },
  179. _showInputChangeSuccess: function(field) {
  180. var $icon = this.$('#' + field + 'form > .icon-checkmark');
  181. $icon.fadeIn(200);
  182. setTimeout(function() {
  183. $icon.fadeOut(300);
  184. }, 2000);
  185. var scope = this.$('#' + field + 'scope').val();
  186. var verifyAvailable = this._updateVerifyButton(field, scope);
  187. // change verification buttons from 'verify' to 'verifying...' on value change
  188. if (verifyAvailable) {
  189. if (field === 'twitter' || field === 'website') {
  190. var verifyStatus = this.$('#' + field + 'form > .verify > #verify-' + field);
  191. verifyStatus.attr('data-origin-title', t('settings', 'Verify'));
  192. verifyStatus.attr('src', OC.imagePath('core', 'actions/verify.svg'));
  193. verifyStatus.data('status', '0');
  194. verifyStatus.addClass('verify-action');
  195. } else if (field === 'email') {
  196. var verifyStatus = this.$('#' + field + 'form > .verify > #verify-' + field);
  197. verifyStatus.attr('data-origin-title', t('settings', 'Verifying …'));
  198. verifyStatus.data('status', '1');
  199. verifyStatus.attr('src', OC.imagePath('core', 'actions/verifying.svg'));
  200. }
  201. }
  202. },
  203. _showInputChangeFail: function(field) {
  204. var $icon = this.$('#' + field + 'form > .icon-error');
  205. $icon.fadeIn(200);
  206. setTimeout(function() {
  207. $icon.fadeOut(300);
  208. }, 2000);
  209. },
  210. _setFieldScopeIcon: function(field, scope) {
  211. var $icon = this.$('#' + field + 'form > .headerbar-label .icon-federation-menu');
  212. $icon.removeClass('icon-phone');
  213. $icon.removeClass('icon-password');
  214. $icon.removeClass('icon-contacts-dark');
  215. $icon.removeClass('icon-link');
  216. $icon.addClass('hidden');
  217. switch (scope) {
  218. case 'v2-private':
  219. $icon.addClass('icon-phone');
  220. $icon.removeClass('hidden');
  221. break;
  222. case 'v2-local':
  223. $icon.addClass('icon-password');
  224. $icon.removeClass('hidden');
  225. break;
  226. case 'v2-federated':
  227. $icon.addClass('icon-contacts-dark');
  228. $icon.removeClass('hidden');
  229. break;
  230. case 'v2-published':
  231. $icon.addClass('icon-link');
  232. $icon.removeClass('hidden');
  233. break;
  234. }
  235. }
  236. });
  237. OC.Settings = OC.Settings || {};
  238. OC.Settings.FederationSettingsView = FederationSettingsView;
  239. })(_, $, OC);