travlrmap 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,7 @@
26
26
  #:admin_salt: d34fda332c9a0f8997d33db8172b67b1a319fd79d108568aa4fbdb2b
27
27
  #:admin_hash: 267d09bb203ec5c9a20eb7dbc1a
28
28
  #:google_analytics_id: UA-99999999-9
29
+ #:save_to: incoming
29
30
 
30
31
  :views:
31
32
  :default:
@@ -144,6 +144,24 @@ module Travlrmap
144
144
  kml.render
145
145
  end
146
146
 
147
+ def point_from_json(json)
148
+ data = JSON.parse(json)
149
+
150
+ point = {}
151
+
152
+ ["title", "comment", "country", "date", "href", "linktext", "linkimg", "lon", "lat"].each do |i|
153
+ if data[i].is_a?(String)
154
+ point[i.intern] = data[i] unless data[i].empty?
155
+ else
156
+ point[i.intern] = data[i]
157
+ end
158
+ end
159
+
160
+ point[:type] = data["type"].intern
161
+
162
+ point
163
+ end
164
+
147
165
  get '/view/:view/?:set?' do
148
166
  params[:view] ? view = params[:view].intern : view = :default
149
167
  params[:set] ? set = params[:set].intern : set = nil
@@ -196,24 +214,55 @@ module Travlrmap
196
214
  to_json
197
215
  end
198
216
 
199
- post '/points/validate' do
217
+ post '/points/save' do
218
+ content_type :"application/json"
219
+
220
+ if !@map[:authenticate]
221
+ halt(403, "Can only save data if authentication is enabled")
222
+ end
223
+
200
224
  protected!
201
225
 
202
- content_type :"application/json"
226
+ halt(403, "No :save_to location configured") unless @map[:save_to]
203
227
 
204
- data = JSON.parse(request.body.read)
228
+ save_file = File.join(APPROOT, "config", "%s.yaml" % @map[:save_to])
205
229
 
206
- point = {}
230
+ halt(403, ":save_to location is not writable") unless File.writable?(save_file)
207
231
 
208
- ["title", "comment", "country", "date", "href", "linktext", "linkimg", "lon", "lat"].each do |i|
209
- if data[i].is_a?(String)
210
- point[i.intern] = data[i] unless data[i].empty?
232
+ points = YAML.load(File.read(save_file))
233
+
234
+ halt(403, "Failed to load :save_to location as valid YAML") unless points
235
+ halt(403, "Failed to load :save_to location as valid points file") unless points[:points]
236
+
237
+ begin
238
+ new_point = point_from_json(request.body.read)
239
+
240
+ index = points[:points].find_index{|p| p[:title] == new_point[:title]}
241
+
242
+ if index
243
+ points[:points][index] = new_point
244
+ result = '{"status":"success","message":"%s has been updated"}' % h(new_point[:title])
211
245
  else
212
- point[i.intern] = data[i]
246
+ points[:points] << new_point
247
+ result = '{"status":"success","message":"%s has been saved"}' % h(new_point[:title])
213
248
  end
249
+
250
+ File.open(save_file, "w") do |f|
251
+ f.puts YAML.dump(points)
252
+ end
253
+ rescue Exception
254
+ result = '{"status":"message": "Failed to save point: %s"}' % h($!.to_s)
214
255
  end
215
256
 
216
- point[:type] = data["type"].intern
257
+ result
258
+ end
259
+
260
+ post '/points/validate' do
261
+ protected!
262
+
263
+ content_type :"application/json"
264
+
265
+ point = point_from_json(request.body.read)
217
266
 
218
267
  result = {"yaml" => YAML.dump([point]).lines.to_a[1..-1].join,
219
268
  "html" => erb(:"_point_comment", :layout => false, :locals => {:point => point})}
@@ -1,3 +1,3 @@
1
1
  module Travlrmap
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/views/geolocate.erb CHANGED
@@ -114,6 +114,47 @@
114
114
  }
115
115
  }
116
116
 
117
+ function selectText(element) {
118
+ var doc = document;
119
+ var text = doc.getElementById(element);
120
+
121
+ if (doc.body.createTextRange) { // ms
122
+ var range = doc.body.createTextRange();
123
+ range.moveToElementText(text);
124
+ range.select();
125
+ } else if (window.getSelection) { // moz, opera, webkit
126
+ var selection = window.getSelection();
127
+ var range = doc.createRange();
128
+ range.selectNodeContents(text);
129
+ selection.removeAllRanges();
130
+ selection.addRange(range);
131
+ }
132
+ }
133
+
134
+ function pointFromForm() {
135
+ var point = {};
136
+ point.type = document.getElementById('point_type').value
137
+ point.title = document.getElementById('point_title').value
138
+ point.comment = document.getElementById('point_comment').value
139
+ point.country = document.getElementById('point_country').value
140
+ point.date = document.getElementById('point_date').value
141
+ point.href = document.getElementById('point_href').value
142
+ point.linktext = document.getElementById('point_linktext').value
143
+ point.linkimg = document.getElementById('point_linkimg').value
144
+ point.lon = marker.position.lng();
145
+ point.lat = marker.position.lat();
146
+
147
+ return point;
148
+ }
149
+
150
+ function savePoint() {
151
+ var point = pointFromForm();
152
+
153
+ $.post("/points/save", JSON.stringify(point), function(data) {
154
+ alert(data.message);
155
+ });
156
+ }
157
+
117
158
  $(document).ready(function() {
118
159
  $('#point_date').datepicker({
119
160
  format: 'yyyy-mm-dd',
@@ -150,20 +191,15 @@
150
191
  });
151
192
  });
152
193
 
194
+ document.getElementById('preview_yaml').style.cursor = 'pointer';
195
+ document.getElementById('preview_yaml').onclick = function() {
196
+ selectText('preview_yaml');
197
+ };
198
+
153
199
  $('#point_info').submit(function(e) {
154
200
  e.preventDefault();
155
201
 
156
- var point = {};
157
- point.type = document.getElementById('point_type').value
158
- point.title = document.getElementById('point_title').value
159
- point.comment = document.getElementById('point_comment').value
160
- point.country = document.getElementById('point_country').value
161
- point.date = document.getElementById('point_date').value
162
- point.href = document.getElementById('point_href').value
163
- point.linktext = document.getElementById('point_linktext').value
164
- point.linkimg = document.getElementById('point_linkimg').value
165
- point.lon = marker.position.lng();
166
- point.lat = marker.position.lat();
202
+ var point = pointFromForm();
167
203
 
168
204
  document.getElementById('preview_html').innerHTML = "Validating point and fetching preview...";
169
205
  document.getElementById('preview_yaml').innerHTML = "";
@@ -206,6 +242,9 @@
206
242
  </div>
207
243
  <div class="modal-footer">
208
244
  <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
245
+ <% if @map[:authenticate] && @map[:save_to] %>
246
+ <button type="button" class="btn btn-primary" onclick="savePoint()" data-dismiss="modal">Save</button>
247
+ <% end %>
209
248
  </div>
210
249
  </div>
211
250
  </div>
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: travlrmap
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 1.1.0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - R.I.Pienaar
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2015-01-11 00:00:00 +00:00
18
+ date: 2015-01-13 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency