From ce6af625d2a5f5261a71869063283ba4b4de5424 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 12 Nov 2015 17:34:12 +0100 Subject: [PATCH] - Save polygon from carte on procedure - Display save polygon from database --- app/assets/javascripts/carte.js | 47 +++++++------ app/assets/stylesheets/carte.scss | 67 ++++++++++++++++++- app/controllers/users/carte_controller.rb | 3 +- app/views/users/carte/show.html.haml | 8 +-- ...1112151918_add_json_lat_lngs_to_dossier.rb | 5 ++ db/schema.rb | 3 +- spec/views/users/carte/show.html.haml_spec.rb | 4 +- 7 files changed, 103 insertions(+), 34 deletions(-) create mode 100644 db/migrate/20151112151918_add_json_lat_lngs_to_dossier.rb diff --git a/app/assets/javascripts/carte.js b/app/assets/javascripts/carte.js index c66a6ed82..a7f713831 100644 --- a/app/assets/javascripts/carte.js +++ b/app/assets/javascripts/carte.js @@ -1,5 +1,3 @@ -//récupération de la position de l'entreprise - function initCarto() { OSM = L.tileLayer("http://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png", { attribution: '© OpenStreetMap' @@ -13,11 +11,35 @@ function initCarto() { layers: [OSM] }); - freeDraw = new L.FreeDraw({ + var freeDraw = new L.FreeDraw({ mode: L.FreeDraw.MODES.CREATE }); map.addLayer(freeDraw); + + $.each($.parseJSON($("#json_latlngs").val()), function(i, val){ + freeDraw.createPolygon(val); + }); + + add_event_freeDraw(freeDraw); +} + +function add_event_freeDraw(freeDraw){ + freeDraw.on('markers', function (e){ + $("#json_latlngs").val(JSON.stringify(e.latLngs)); + }); + + $("#new").on('click', function (e){ + freeDraw.setMode(L.FreeDraw.MODES.CREATE); + }); + + $("#edit").on('click', function (e){ + freeDraw.setMode(L.FreeDraw.MODES.EDIT); + }); + + $("#delete").on('click', function (e){ + freeDraw.setMode(L.FreeDraw.MODES.DELETE); + }); } function get_position() { @@ -32,23 +54,4 @@ function get_position() { }); return position; -} - -function get_ref_dossier() { - $.post("http://apicarto.coremaps.com/api/v1/datastore", { - contentType: "application/json", - dataType: 'json', - geom: JSON.stringify(window.featureCollection.features[0]), - ascyn: false - }).done(function (data) { - $("#ref_dossier").val(data.reference); - }); -} - -function submit_check_draw(e) { - if (window.location.href.indexOf('carte') > -1 && window.featureCollection.features.length == 0) { - $("#flash_message").html('
Un dessin est obligatoire.
'); - e.preventDefault(); - return false; - } } \ No newline at end of file diff --git a/app/assets/stylesheets/carte.scss b/app/assets/stylesheets/carte.scss index 4e0251c10..91a507fdc 100644 --- a/app/assets/stylesheets/carte.scss +++ b/app/assets/stylesheets/carte.scss @@ -6,18 +6,79 @@ table { font-size: 13; } -#map.mode-create { - cursor: crosshair; + +#map section.overlay { + position: absolute; + top: 0; + left: 0; + pointer-events: none; + box-shadow: inset -100px 0 100px -100px rgba(0, 0, 0, .25); + width: 100%; + height: 100%; + z-index: 2001; } -svg.tracer { +#map.mode-create { + cursor: crosshair !important; +} + +#map g path { + transition: all 0.25s; + stroke-width: 4px; + stroke-opacity: 1; + stroke: #D7217E; + position: absolute; + z-index: 1001; + fill: #D7217E; + fill-opacity: .75; + -webkit-filter: none; +} + +#map.mode-delete path { + cursor: no-drop !important; +} + +#map.mode-delete path:hover { + fill: #4d4d4d !important; +} + +#map div.polygon-elbow { + -webkit-transition: opacity .25s; + box-shadow: 0 0 0 2px white, 0 0 10px rgba(0, 0, 0, .35); + border: 5px solid #D7217E; + border-radius: 10px; + transition: opacity 0.25s; + cursor: move; + opacity: 0; + pointer-events: none; + box-sizing: border-box; + width: 0 !important; + height: 0 !important; +} + +#map div.polygon-elbow.non-polygon { + opacity: 0 !important; + pointer-events: none !important; + border: 5px solid darkgray; +} + +#map.mode-edit div.polygon-elbow { + opacity: 1; + pointer-events: all; +} + +#map svg.tracer { position: absolute; top: 0; left: 0; width: 100%; height: 100%; + z-index: 2001; + pointer-events: none; } + + .info { padding: 6px 8px; font: 14px/16px Arial, Helvetica, sans-serif; diff --git a/app/controllers/users/carte_controller.rb b/app/controllers/users/carte_controller.rb index a5486003c..645d75183 100644 --- a/app/controllers/users/carte_controller.rb +++ b/app/controllers/users/carte_controller.rb @@ -10,9 +10,9 @@ class Users::CarteController < UsersController def save_ref_api_carto dossier = current_user_dossier + dossier.update_attributes(json_latlngs: params[:json_latlngs]) if dossier.draft? - #dossier.update_attributes(ref_dossier_carto: params[:ref_dossier]) redirect_to url_for(controller: :description, action: :show, dossier_id: params[:dossier_id]) else commentaire_params = { @@ -23,7 +23,6 @@ class Users::CarteController < UsersController commentaire = Commentaire.new commentaire_params commentaire.save redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: params[:dossier_id]) - end end diff --git a/app/views/users/carte/show.html.haml b/app/views/users/carte/show.html.haml index a77c016ff..f9be74a5a 100644 --- a/app/views/users/carte/show.html.haml +++ b/app/views/users/carte/show.html.haml @@ -3,11 +3,11 @@ %br .content{style:'margin-bottom:60px'} - %button.btn.btn-sm.btn-success{type:'button', disabled: 'disabled'} Nouveau + %button#new.btn.btn-sm.btn-success{type:'button'} Nouveau \- - %button.btn.btn-sm.btn-info{type:'button', disabled: 'disabled'} Editer + %button#edit.btn.btn-sm.btn-info{type:'button'} Editer \- - %button.btn.btn-sm.btn-danger{type:'button', disabled: 'disabled'} Supprimer + %button#delete.btn.btn-sm.btn-danger{type:'button'} Supprimer %br %br @@ -15,7 +15,7 @@ = form_tag(url_for({controller: :carte, action: :save_ref_api_carto, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST') do %br - %input{type: 'hidden', value: '', name: 'ref_dossier', id: 'ref_dossier'} + %input{type: 'hidden', value: "#{@dossier.json_latlngs}", name: 'json_latlngs', id: 'json_latlngs'} -if @dossier.draft? =render partial: '/layouts/etape_suivante' diff --git a/db/migrate/20151112151918_add_json_lat_lngs_to_dossier.rb b/db/migrate/20151112151918_add_json_lat_lngs_to_dossier.rb new file mode 100644 index 000000000..9aac8a6a9 --- /dev/null +++ b/db/migrate/20151112151918_add_json_lat_lngs_to_dossier.rb @@ -0,0 +1,5 @@ +class AddJsonLatLngsToDossier < ActiveRecord::Migration + def change + add_column :dossiers, :json_latlngs, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 8b1c8c784..ee00a5dc0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151110091451) do +ActiveRecord::Schema.define(version: 20151112151918) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -66,6 +66,7 @@ ActiveRecord::Schema.define(version: 20151110091451) do t.datetime "updated_at", default: '2015-09-22 09:25:29' t.string "state" t.integer "user_id" + t.text "json_latlngs" end add_index "dossiers", ["procedure_id"], name: "index_dossiers_on_procedure_id", using: :btree diff --git a/spec/views/users/carte/show.html.haml_spec.rb b/spec/views/users/carte/show.html.haml_spec.rb index d4df8349d..564585c11 100644 --- a/spec/views/users/carte/show.html.haml_spec.rb +++ b/spec/views/users/carte/show.html.haml_spec.rb @@ -22,8 +22,8 @@ describe 'users/carte/show.html.haml', type: :view do end context 'présence des inputs hidden' do - it 'stockage de la référence du dossie de l\'API carto' do - expect(rendered).to have_selector('input[type=hidden][id=ref_dossier][name=ref_dossier]') + it 'stockage du json des polygons dessinés' do + expect(rendered).to have_selector('input[type=hidden][id=json_latlngs][name=json_latlngs]') end end