federationsettingsview.js 7.6 KB

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