forked from diaspora/diaspora
/
publisher_view.js
99 lines (81 loc) · 2.4 KB
/
publisher_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
//this file is the scary no-no-zone bad-touch of our backbone code.
//after re-writing/eliminating the existing Publisher let's re-write
//this with PANACHE! <333 Dennis
app.views.Publisher = Backbone.View.extend({
el : "#publisher",
events : {
"focus textarea" : "open",
"click #hide_publisher" : "clear",
"submit form" : "createStatusMessage"
},
initialize : function(){
this.collection = this.collection //takes a Posts collection
return this;
},
createStatusMessage : function(evt) {
if(evt){ evt.preventDefault(); }
var serializedForm = $(evt.target).closest("form").serializeObject();
// save status message
var statusMessage = new app.models.StatusMessage();
statusMessage.save({
"status_message" : {
"text" : serializedForm["status_message[text]"]
},
"aspect_ids" : serializedForm["aspect_ids[]"],
"photos" : serializedForm["photos[]"],
"services" : serializedForm["services[]"]
}, {
url : "/status_messages",
success : function() {
if(app.publisher) {
$(app.publisher.el).trigger('ajax:success');
}
if(app.stream) {
app.stream.posts.add(statusMessage.toJSON());
}
}
});
// clear state
this.clear();
},
clear : function() {
this.$('textarea').val("");
this.$('#publisher_textarea_wrapper').removeClass("with_attachments");
// remove photos
this.$("#photodropzone").find('li').remove();
this.$("input[name='photos[]']").remove();
// close publishing area (CSS)
this.close();
Publisher.clear()
return this;
},
open : function() {
$(this.el).removeClass('closed');
this.$("#publisher_textarea_wrapper").addClass('active');
this.$("textarea.ac_input").css('min-height', '42px');
return this;
},
close : function() {
$(this.el).addClass("closed");
this.$("#publisher_textarea_wrapper").removeClass("active");
this.$("textarea.ac_input").css('min-height', '');
return this;
}
});
// jQuery helper for serializing a <form> into JSON
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};