1
0

federationsettingsview.js 6.3 KB

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