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