forked from diaspora/diaspora
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uploader_view.js
133 lines (114 loc) · 4.39 KB
/
uploader_view.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
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
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3-or-Later
// Uploader view for the publisher.
// Initializes the file uploader plugin and handles callbacks for the upload
// progress. Attaches previews of finished uploads to the publisher.
app.views.PublisherUploader = Backbone.View.extend({
initialize: function(opts) {
this.publisher = opts.publisher;
this.publisher.photozoneEl.on("click", ".x", _.bind(this._removePhoto, this));
// Initialize the PostPhotoUploader and subscribe its events
this.uploader = new Diaspora.PostPhotoUploader(this.el, opts.dropZoneElementIds);
this.uploader.onUploadStarted = _.bind(this.uploadStartedHandler, this);
this.uploader.onProgress = _.bind(this.progressHandler, this);
this.uploader.onUploadCompleted = _.bind(this.uploadCompleteHandler, this);
},
// add photo placeholders to the publisher to indicate an upload in progress
_addPhotoPlaceholder: function(id) {
var publisher = this.publisher;
publisher.setButtonsEnabled(false);
publisher.wrapperEl.addClass("with_attachments");
publisher.photozoneEl.append(
"<li id=\"upload-" + id + "\"class=\"publisher_photo loading\" style=\"position:relative;\">" +
" <div class=\"progress\">" +
" <div class=\"progress-bar progress-bar-striped active\" role=\"progressbar\"></div>"+
" </div>" +
" <div class=\"spinner\"></div>" +
"</li>"
);
},
uploadStartedHandler: function(id) {
this.$el.addClass("loading");
this._addPhotoPlaceholder(id);
},
progressHandler: function(id, fileName, progress) {
this.publisher.photozoneEl
.find("li.loading#upload-" + id + " .progress-bar")
.width(progress + "%");
},
uploadCompleteHandler: function(id, fileName, response) {
if (response.success){
var photoId = response.data.photo.id,
image = response.data.photo.unprocessed_image;
this._addFinishedPhoto(id, photoId, image);
this.trigger("change");
} else {
this._cancelPhotoUpload(id);
this.publisher.wrapperEl.find("#photodropzone_container").first().after(
"<div id=\"upload_error\">" +
Diaspora.I18n.t("photo_uploader.error", {file: fileName}) +
"</div>"
);
this.trigger("change");
}
},
// replace the first photo placeholder with the finished uploaded image and
// add the id to the publishers form
_addFinishedPhoto: function(id, photoId, image) {
var publisher = this.publisher;
// add form input element
publisher.$(".content_creation form").append(
"<input type=\"hidden\", value=\"" + photoId + "\" name=\"photos[]\" />"
);
// replace placeholder
var placeholder = publisher.photozoneEl.find("li.loading#upload-" + id);
var imgElement = document.createElement("img");
imgElement.src = image.thumb_medium.url;
imgElement.classList.add("hidden");
imgElement.dataset.id = photoId;
imgElement.dataset.small = image.thumb_small.url;
imgElement.dataset.scaled = image.scaled_full.url;
placeholder
.prepend(
'<div class="x"></div>' +
'<div class="circle"></div>' +
imgElement.outerHTML
).removeClass("loading");
placeholder.find("div.progress").remove();
placeholder.find("img").on("load", function(ev) {
$(ev.target).removeClass("hidden");
placeholder.find(".spinner").remove();
});
// no more placeholders? enable buttons
if (publisher.photozoneEl.find("li.loading").length === 0) {
this.$el.removeClass("loading");
publisher.setButtonsEnabled(true);
}
},
_cancelPhotoUpload: function(id) {
this.publisher.photozoneEl.find("li.loading#upload-" + id).remove();
},
// remove an already uploaded photo
_removePhoto: function(evt) {
var self = this;
var photo = $(evt.target).parents(".publisher_photo");
var img = photo.find("img");
photo.addClass("dim");
$.ajax({
url: "/photos/"+img.attr("data-id"),
dataType: "json",
type: "DELETE",
success: function() {
photo.fadeOut(400, function() {
photo.remove();
if (self.publisher.$(".publisher_photo").length === 0) {
// no more photos left...
self.publisher.wrapperEl.removeClass("with_attachments");
}
self.trigger("change");
});
}
});
return false;
}
});
// @license-end