1
0

federationsettingsview.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 h3 > .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.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. ) {
  114. return;
  115. }
  116. self.$('#' + field).keyUpDelayedOrEnter(_.bind(self._onInputChanged, self), true);
  117. });
  118. },
  119. _onInputChanged: function(e) {
  120. var self = this;
  121. var $dialog = $('.oc-dialog:visible');
  122. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  123. if($dialog.length === 0) {
  124. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onInputChanged, this, e));
  125. }
  126. return;
  127. }
  128. var $target = $(e.target);
  129. var value = $target.val();
  130. var field = $target.attr('id');
  131. this._config.set(field, value);
  132. var savingData = this._config.save({
  133. error: function(jqXHR) {
  134. OC.msg.finishedSaving('#personal-settings-container .msg', jqXHR);
  135. }
  136. });
  137. $.when(savingData).done(function(data) {
  138. if (data.status === "success") {
  139. self._showInputChangeSuccess(field);
  140. } else {
  141. self._showInputChangeFail(field);
  142. }
  143. }).fail(function(data) {
  144. if (data.status === 429) {
  145. OC.Notification.showTemporary(t('settings', 'There were too many requests from your network. Retry later or contact your administrator if this is an error.'))
  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. var savingData = this._config.save();
  161. $.when(savingData).fail(function(data) {
  162. if (data.status === 429) {
  163. OC.Notification.showTemporary(t('settings', 'There were too many requests from your network. Retry later or contact your administrator if this is an error.'))
  164. }
  165. });
  166. this._setFieldScopeIcon(field, scope);
  167. this._updateVerifyButton(field, scope);
  168. },
  169. _updateVerifyButton: function(field, scope) {
  170. // show verification button if the value is set and the scope is 'public'
  171. if (field === 'twitter' || field === 'website'|| field === 'email') {
  172. var verify = this.$('#' + field + 'form > .verify');
  173. var scope = this.$('#' + field + 'scope').val();
  174. var value = this.$('#' + field).val();
  175. if (scope === 'public' && value !== '') {
  176. verify.removeClass('hidden');
  177. return true;
  178. } else {
  179. verify.addClass('hidden');
  180. }
  181. }
  182. return false;
  183. },
  184. _showInputChangeSuccess: function(field) {
  185. var $icon = this.$('#' + field + 'form > .icon-checkmark');
  186. $icon.fadeIn(200);
  187. setTimeout(function() {
  188. $icon.fadeOut(300);
  189. }, 2000);
  190. var scope = this.$('#' + field + 'scope').val();
  191. var verifyAvailable = this._updateVerifyButton(field, scope);
  192. // change verification buttons from 'verify' to 'verifying...' on value change
  193. if (verifyAvailable) {
  194. if (field === 'twitter' || field === 'website') {
  195. var verifyStatus = this.$('#' + field + 'form > .verify > #verify-' + field);
  196. verifyStatus.attr('data-origin-title', t('settings', 'Verify'));
  197. verifyStatus.attr('src', OC.imagePath('core', 'actions/verify.svg'));
  198. verifyStatus.data('status', '0');
  199. verifyStatus.addClass('verify-action');
  200. } else if (field === 'email') {
  201. var verifyStatus = this.$('#' + field + 'form > .verify > #verify-' + field);
  202. verifyStatus.attr('data-origin-title', t('settings', 'Verifying …'));
  203. verifyStatus.data('status', '1');
  204. verifyStatus.attr('src', OC.imagePath('core', 'actions/verifying.svg'));
  205. }
  206. }
  207. },
  208. _showInputChangeFail: function(field) {
  209. var $icon = this.$('#' + field + 'form > .icon-error');
  210. $icon.fadeIn(200);
  211. setTimeout(function() {
  212. $icon.fadeOut(300);
  213. }, 2000);
  214. },
  215. _setFieldScopeIcon: function(field, scope) {
  216. var $icon = this.$('#' + field + 'form > h3 .icon-federation-menu');
  217. $icon.removeClass('icon-phone');
  218. $icon.removeClass('icon-password');
  219. $icon.removeClass('icon-contacts-dark');
  220. $icon.removeClass('icon-link');
  221. $icon.addClass('hidden');
  222. switch (scope) {
  223. case 'v2-private':
  224. $icon.addClass('icon-phone');
  225. $icon.removeClass('hidden');
  226. break;
  227. case 'v2-local':
  228. $icon.addClass('icon-password');
  229. $icon.removeClass('hidden');
  230. break;
  231. case 'v2-federated':
  232. $icon.addClass('icon-contacts-dark');
  233. $icon.removeClass('hidden');
  234. break;
  235. case 'v2-published':
  236. $icon.addClass('icon-link');
  237. $icon.removeClass('hidden');
  238. break;
  239. }
  240. }
  241. });
  242. OC.Settings = OC.Settings || {};
  243. OC.Settings.FederationSettingsView = FederationSettingsView;
  244. })(_, $, OC);