forked from diaspora/diaspora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobile_profile_photo_uploader.js
90 lines (81 loc) · 3.15 KB
/
mobile_profile_photo_uploader.js
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
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
Diaspora.ProfilePhotoUploader = function() {
this.initialize();
};
Diaspora.ProfilePhotoUploader.prototype = {
constructor: Diaspora.ProfilePhotoUploader,
initialize: function() {
new qq.FineUploaderBasic({
element: document.getElementById("file-upload"),
validation: {
acceptFiles: "image/png, image/jpeg, image/gif, image/heic, image/webp",
allowedExtensions: ["png", "jpg", "jpeg", "gif", "heic", "webp"],
sizeLimit: 4194304
},
request: {
endpoint: Routes.photos(),
params: {
/* eslint-disable camelcase */
authenticity_token: $("meta[name='csrf-token']").attr("content"),
/* eslint-enable camelcase */
photo: {"pending": true, "aspect_ids": "all", "set_profile_photo": true}
}
},
button: document.getElementById("file-upload"),
messages: {
typeError: Diaspora.I18n.t("photo_uploader.invalid_ext"),
sizeError: Diaspora.I18n.t("photo_uploader.size_error"),
emptyError: Diaspora.I18n.t("photo_uploader.empty")
},
callbacks: {
onProgress: function(id, fileName, loaded, total) {
var progress = Math.round(loaded / total * 100);
$("#fileInfo").text(fileName + " " + progress + "%");
},
onSubmit: function() {
$("#file-upload").addClass("loading");
$("#profile_photo_upload").find(".avatar").addClass("loading");
$("#file-upload-spinner").removeClass("hidden");
$("#fileInfo").show();
},
onComplete: function(id, fileName, responseJSON) {
$("#file-upload-spinner").addClass("hidden");
$("#fileInfo").text(Diaspora.I18n.t("photo_uploader.completed", {"file": fileName}));
$("#file-upload").removeClass("loading");
if (responseJSON.data !== undefined) {
/* flash message prompt */
var message = Diaspora.I18n.t("photo_uploader.looking_good");
if (app.flashMessages) {
app.flashMessages.success(message);
} else {
alert(message);
}
var photoId = responseJSON.data.photo.id;
var url = responseJSON.data.photo.unprocessed_image.url;
var oldPhoto = $("#photo_id");
if (oldPhoto.length === 0) {
$("#update_profile_form")
.prepend("<input type='hidden' value='" + photoId + "' id='photo_id' name='photo_id'/>");
} else {
oldPhoto.val(photoId);
}
$("#profile_photo_upload").find(".avatar").attr("src", url);
$(".avatar[data-person_id=" + gon.user.id + "]").attr("src", url);
}
$("#profile_photo_upload").find(".avatar").removeClass("loading");
},
onError: function(id, name, errorReason) {
if (app.flashMessages) {
app.flashMessages.error(errorReason);
} else {
alert(errorReason);
}
}
},
text: {
fileInputTitle: ""
}
});
}
};
// @license-end