-
Notifications
You must be signed in to change notification settings - Fork 1
/
storyPost.js
142 lines (118 loc) · 5.19 KB
/
storyPost.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
134
135
136
137
138
139
140
141
142
/*global Solidarity, Backbone, JST, Modernizr, Transifex */
Solidarity.Views = Solidarity.Views || {};
(function () {
'use strict';
Solidarity.Views.StoryPost = Solidarity.Views.FormView.extend({
template: JST['app/templates/storyPost.html'],
el: '#shareBar',
form: 'form#storyPost',
events: {
'click textarea[name="content"]': 'show',
'keyup textarea[name="content"].closed': 'show',
'click a.mobileOnly': 'show',
'click button#geolocate': 'geolocate',
'click button#cancel': 'hide',
},
beenRendered: false,
isOpen: false,
initialize: function() {
_.extend(this.events, Solidarity.Views.FormView.prototype.events);
this.delegateEvents(this.events);
},
render: function() {
Solidarity.log('storyPost.render');
this.$el.hide().html(this.template()).slideDown(1000);
this.$form = $(this.form);
this.beenRendered = true;
return this;
},
show: function() {
Solidarity.log('storyPost.show');
if (!this.isOpen) {
Solidarity.mainContent.showOverlay(true);
// delay activate until open animation starts
$('#overlay').delay(500).addClass('active');
if (!this.beenRendered) {
this.render();
} else {
// force translations update
Solidarity.log('Transifex translateNode', this.$el);
if(Transifex) { Transifex.live.translateNode(this.$el); }
this.$el.animate({'height': '100%'}, 500);
$('textarea', this.$el).delay(500).removeClass('closed').addClass('open');
}
this.$el.removeClass('closed');
this.isOpen = true;
this.onShow();
} else {
}
},
hide: function(event) {
event.preventDefault(); // stop form submit
// hide without replacing content
this.$el.animate({'height': '100%'}, 500).delay(500).addClass('closed');
$('textarea', this.$el).removeClass('open').addClass('closed');
this.isOpen = false;
Solidarity.mainContent.hideOverlay();
},
onShow: function() {
this.$form = $(this.form);
// init bootstrap form plugins
$('.selectpicker').selectpicker({
size: '5', // show this many by default
});
},
onSuccess: function(resp) {
Solidarity.log('storyPosted',resp);
var newStoryUrl = Solidarity.siteRoot + '#read/story/'+resp.id;
var linkText = newStoryUrl.split('//')[1];
// reset content and close
this.$el.html('<span class="icon icon-write"></span>'+
'<textarea name="content" placeholder="SHARE YOUR STORY"></textarea>');
this.$el.addClass('closed');
this.isOpen = false;
this.beenRendered = false;
// show thanks view
this.thanksView = new Solidarity.Views.StoryThanks();
this.thanksView.render({url: newStoryUrl, link: linkText});
},
geolocate: function() {
if (Modernizr.geolocation) {
Solidarity.log('geolocate!');
navigator.geolocation.getCurrentPosition(this.locationToCityState, this.geoError.bind(this));
$('.icon-locate').addClass('pulse');
} else {
return this.geoError({code: 0, message: 'Your browser does not support geolocation.'});
}
},
geoError: function(error) {
this.clearAlerts();
var message = 'Geolocation error';
if (error.code === 1) { message = 'Please allow the browser to access your location.'; }
if (error.code === 2) { message = 'Geolocation position unavailable.'; }
if (error.code === 3) { message = 'Geolocation position timeout.'; }
this.$form.append(this.templateError({'non_field_errors': message}));
},
locationToCityState: function(position) {
var self = this;
$.ajax('https://nominatim.openstreetmap.org/reverse', {
type: 'GET',
data: {
'lat': position.coords.latitude,
'lon': position.coords.longitude,
'format': 'jsonv2'
},
success: function(match) {
$('input#city').val(match.address.city || match.address.town || match.address.village || match.address.hamlet);
$('input#county').val(match.address.county);
$('select#state').selectpicker('val', match.address.state);
$('button#geolocate').attr('disabled','disabled');
$('.icon-locate').removeClass('pulse');
},
error: function(resp, status, err) {
return self.geoError({code: 4, message: err});
}
});
}
});
})();