Skip to content

Commit 647abe5

Browse files
committed
reduce code duplication, fix parse error, prevent page reload on hitting enter while changing the display name - refs #8085
1 parent 0c444fb commit 647abe5

File tree

4 files changed

+52
-52
lines changed

4 files changed

+52
-52
lines changed

.jshintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"beforeEach": true,
2424
"afterEach": true,
2525
"sinon": true,
26-
"fakeServer": true
26+
"fakeServer": true,
27+
"_": true
2728
}
2829
}

core/js/core.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"jquery-showpassword.js",
77
"jquery.infieldlabel.js",
88
"jquery.placeholder.js",
9-
"jquery-tipsy.js"
9+
"jquery-tipsy.js",
10+
"underscore.js"
1011
],
1112
"modules": [
1213
"compatibility.js",

lib/base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ public static function initTemplateEngine() {
305305
OC_Util::addScript("jquery.placeholder");
306306
OC_Util::addScript("jquery-tipsy");
307307
OC_Util::addScript("compatibility");
308+
OC_Util::addScript("underscore");
308309
OC_Util::addScript("jquery.ocdialog");
309310
OC_Util::addScript("oc-dialogs");
310311
OC_Util::addScript("js");

settings/js/personal.js

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@
55
* See the COPYING-README file.
66
*/
77

8+
/* global OC, t */
9+
10+
/**
11+
* The callback will be fired as soon as enter is pressed by the
12+
* user or 1 second after the last data entry
13+
*
14+
* @param callback
15+
*/
16+
jQuery.fn.keyUpDelayedOrEnter = function(callback){
17+
var cb = callback;
18+
var that = this;
19+
this.keyup(_.debounce(function (event) {
20+
// enter is already handled in keypress
21+
if(event.keyCode === 13) {
22+
return;
23+
}
24+
if (that.val() !== '') {
25+
cb();
26+
}
27+
}, 1000));
28+
29+
this.keypress(function () {
30+
if (event.keyCode === 13 && that.val() !== '' ){
31+
event.preventDefault();
32+
cb();
33+
}
34+
});
35+
};
36+
37+
838
/**
939
* Post the email address change to the server.
1040
*/
@@ -42,13 +72,12 @@ function changeDisplayName(){
4272
}
4373
OC.msg.finishedSaving('#displaynameform .msg', data);
4474
});
45-
return false;
4675
}
4776
}
4877

4978
function updateAvatar (hidedefault) {
50-
$headerdiv = $('#header .avatardiv');
51-
$displaydiv = $('#displayavatar .avatardiv');
79+
var $headerdiv = $('#header .avatardiv');
80+
var $displaydiv = $('#displayavatar .avatardiv');
5281

5382
if(hidedefault) {
5483
$headerdiv.hide();
@@ -65,11 +94,12 @@ function updateAvatar (hidedefault) {
6594
}
6695

6796
function showAvatarCropper() {
68-
$cropper = $('#cropper');
97+
var $cropper = $('#cropper');
6998
$cropper.prepend("<img>");
70-
$cropperImage = $('#cropper img');
99+
var $cropperImage = $('#cropper img');
71100

72-
$cropperImage.attr('src', OC.generateUrl('/avatar/tmp')+'?requesttoken='+oc_requesttoken+'#'+Math.floor(Math.random()*1000));
101+
$cropperImage.attr('src',
102+
OC.generateUrl('/avatar/tmp')+'?requesttoken='+oc_requesttoken+'#'+Math.floor(Math.random()*1000));
73103

74104
// Looks weird, but on('load', ...) doesn't work in IE8
75105
$cropperImage.ready(function(){
@@ -90,12 +120,12 @@ function showAvatarCropper() {
90120
function sendCropData() {
91121
cleanCropper();
92122

93-
var cropperdata = $('#cropper').data();
123+
var cropperData = $('#cropper').data();
94124
var data = {
95-
x: cropperdata.x,
96-
y: cropperdata.y,
97-
w: cropperdata.w,
98-
h: cropperdata.h
125+
x: cropperData.x,
126+
y: cropperData.y,
127+
w: cropperData.w,
128+
h: cropperData.h
99129
};
100130
$.post(OC.generateUrl('/avatar/cropped'), {crop: data}, avatarResponseHandler);
101131
}
@@ -105,7 +135,7 @@ function saveCoords(c) {
105135
}
106136

107137
function cleanCropper() {
108-
$cropper = $('#cropper');
138+
var $cropper = $('#cropper');
109139
$('#displayavatar').show();
110140
$cropper.hide();
111141
$('.jcrop-holder').remove();
@@ -114,7 +144,7 @@ function cleanCropper() {
114144
}
115145

116146
function avatarResponseHandler(data) {
117-
$warning = $('#avatar .warning');
147+
var $warning = $('#avatar .warning');
118148
$warning.hide();
119149
if (data.status === "success") {
120150
updateAvatar();
@@ -157,41 +187,8 @@ $(document).ready(function(){
157187

158188
});
159189

160-
$('#displayName').keyup(function(){
161-
if ($('#displayName').val() !== '' ){
162-
if(typeof timeout !== 'undefined'){
163-
clearTimeout(timeout);
164-
}
165-
timeout = setTimeout(changeDisplayName, 1000);
166-
}
167-
});
168-
169-
170-
$('#email').keyup(function(event){
171-
if ($('#email').val() !== '' ){
172-
// if this is the enter key changeEmailAddress() is already invoked
173-
// so it doesn't need to be triggered again
174-
if(event.keyCode === 13) {
175-
return;
176-
}
177-
if(typeof timeout !== 'undefined'){
178-
clearTimeout(timeout);
179-
}
180-
timeout = setTimeout(changeEmailAddress, 1000);
181-
}
182-
});
183-
184-
$('#email').keypress(function(event){
185-
// check for enter key and non empty email
186-
if (event.keyCode === 13 && $('#email').val() !== '' ){
187-
event.preventDefault()
188-
// clear timeout of previous keyup event - prevents duplicate changeEmailAddress call
189-
if(typeof timeout !== 'undefined'){
190-
clearTimeout(timeout);
191-
}
192-
changeEmailAddress();
193-
}
194-
});
190+
$('#displayName').keyUpDelayedOrEnter(changeDisplayName);
191+
$('#email').keyUpDelayedOrEnter(changeEmailAddress);
195192

196193
$("#languageinput").change( function(){
197194
// Serialize the data
@@ -256,7 +253,7 @@ $(document).ready(function(){
256253
$.ajax({
257254
type: 'DELETE',
258255
url: OC.generateUrl('/avatar/'),
259-
success: function(msg) {
256+
success: function() {
260257
updateAvatar(true);
261258
$('#removeavatar').hide();
262259
}
@@ -321,7 +318,7 @@ OC.Encryption.msg={
321318
},
322319
finishedDecrypting:function(selector, data){
323320
if( data.status === "success" ){
324-
$(selector).html( data.data.message )
321+
$(selector).html( data.data.message )
325322
.addClass('success')
326323
.stop(true, true)
327324
.delay(3000);

0 commit comments

Comments
 (0)