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.

federationsettingsview.js 7.2KB

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