federationsettingsview.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 $heading = self.$('#' + field + 'form h2');
  54. var $icon = self.$('#' + field + 'form h2 > span');
  55. var scopeMenu = new OC.Settings.FederationScopeMenu({field: field});
  56. self.listenTo(scopeMenu, 'select:scope', function(scope) {
  57. self._onScopeChanged(field, scope);
  58. });
  59. $heading.append(scopeMenu.$el);
  60. $icon.on('click', _.bind(scopeMenu.show, scopeMenu));
  61. // Restore initial state
  62. self._setFieldScopeIcon(field, self._config.get(field + 'Scope'));
  63. });
  64. },
  65. _registerEvents: function() {
  66. var self = this;
  67. _.each(this._inputFields, function(field) {
  68. if (field === 'avatar') {
  69. return;
  70. }
  71. self.$('#' + field).keyUpDelayedOrEnter(_.bind(self._onInputChanged, self), true);
  72. });
  73. },
  74. _onInputChanged: function(e) {
  75. var self = this;
  76. var $dialog = $('.oc-dialog:visible');
  77. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  78. if($dialog.length === 0) {
  79. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onInputChanged, this, e));
  80. }
  81. return;
  82. }
  83. var $target = $(e.target);
  84. var value = $target.val();
  85. var field = $target.attr('id');
  86. this._config.set(field, value);
  87. var savingData = this._config.save({
  88. error: function(jqXHR) {
  89. OC.msg.finishedSaving('#personal-settings-container .msg', jqXHR);
  90. }
  91. });
  92. $.when(savingData).done(function(data) {
  93. if (data.status === "success") {
  94. self._showInputChangeSuccess(field);
  95. } else {
  96. self._showInputChangeFail(field);
  97. }
  98. });
  99. },
  100. _onScopeChanged: function(field, scope) {
  101. var $dialog = $('.oc-dialog:visible');
  102. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  103. if($dialog.length === 0) {
  104. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this._onScopeChanged, this, field, scope));
  105. }
  106. return;
  107. }
  108. this._config.set(field + 'Scope', scope);
  109. $('#' + field + 'scope').val(scope);
  110. // TODO: user loading/success feedback
  111. this._config.save();
  112. this._setFieldScopeIcon(field, scope);
  113. this._updateVerifyButton(field, scope);
  114. },
  115. _updateVerifyButton: function(field, scope) {
  116. // show verification button if the value is set and the scope is 'public'
  117. if (field === 'twitter' || field === 'website'|| field === 'email') {
  118. var verify = this.$('#' + field + 'form > .verify');
  119. var scope = this.$('#' + field + 'scope').val();
  120. var value = this.$('#' + field).val();
  121. if (scope === 'public' && value !== '') {
  122. verify.removeClass('hidden');
  123. return true;
  124. } else {
  125. verify.addClass('hidden');
  126. }
  127. }
  128. return false;
  129. },
  130. _showInputChangeSuccess: function(field) {
  131. var $icon = this.$('#' + field + 'form > .icon-checkmark');
  132. $icon.fadeIn(200);
  133. setTimeout(function() {
  134. $icon.fadeOut(300);
  135. }, 2000);
  136. var scope = this.$('#' + field + 'scope').val();
  137. var verifyAvailable = this._updateVerifyButton(field, scope);
  138. // change verification buttons from 'verify' to 'verifying...' on value change
  139. if (verifyAvailable) {
  140. if (field === 'twitter' || field === 'website') {
  141. var verifyStatus = this.$('#' + field + 'form > .verify > #verify-' + field);
  142. verifyStatus.attr('data-origin-title', t('core', 'Verify'));
  143. verifyStatus.attr('src', OC.imagePath('core', 'actions/verify.svg'));
  144. verifyStatus.data('status', '0');
  145. verifyStatus.addClass('verify-action');
  146. } else if (field === 'email') {
  147. var verifyStatus = this.$('#' + field + 'form > .verify > #verify-' + field);
  148. verifyStatus.attr('data-origin-title', t('core', 'Verifying …'));
  149. verifyStatus.data('status', '1');
  150. verifyStatus.attr('src', OC.imagePath('core', 'actions/verifying.svg'));
  151. }
  152. }
  153. },
  154. _showInputChangeFail: function(field) {
  155. var $icon = this.$('#' + field + 'form > .icon-error');
  156. $icon.fadeIn(200);
  157. setTimeout(function() {
  158. $icon.fadeOut(300);
  159. }, 2000);
  160. },
  161. _setFieldScopeIcon: function(field, scope) {
  162. var $icon = this.$('#' + field + 'form > h2 > span');
  163. $icon.removeClass('icon-password');
  164. $icon.removeClass('icon-contacts-dark');
  165. $icon.removeClass('icon-link');
  166. $icon.addClass('hidden');
  167. switch (scope) {
  168. case 'private':
  169. $icon.addClass('icon-password');
  170. $icon.removeClass('hidden');
  171. break;
  172. case 'contacts':
  173. $icon.addClass('icon-contacts-dark');
  174. $icon.removeClass('hidden');
  175. break;
  176. case 'public':
  177. $icon.addClass('icon-link');
  178. $icon.removeClass('hidden');
  179. break;
  180. }
  181. }
  182. });
  183. OC.Settings = OC.Settings || {};
  184. OC.Settings.FederationSettingsView = FederationSettingsView;
  185. })(_, $, OC);