aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Florisson2014-04-28 17:30:15 +0200
committerMarc Florisson2014-04-28 17:30:15 +0200
commit9663dcb4d35e75a4046a14014e504ea9fa92abba (patch)
tree1b93612caa04d920d63f917c5ec6866da6b73b22
parent0b2f62d9a0744f5687249d3e6212c22c67b08181 (diff)
downloadchouette-core-9663dcb4d35e75a4046a14014e504ea9fa92abba.tar.bz2
fix delete add reorder stop points
-rw-r--r--app/views/routes/_form.html.erb54
-rw-r--r--app/views/routes/_stop_point_fields.html.erb8
2 files changed, 48 insertions, 14 deletions
diff --git a/app/views/routes/_form.html.erb b/app/views/routes/_form.html.erb
index 76bba9068..12556faa9 100644
--- a/app/views/routes/_form.html.erb
+++ b/app/views/routes/_form.html.erb
@@ -11,21 +11,21 @@
<% end %>
<div id="stop_points">
<%= form.semantic_fields_for :stop_points, :include_id => false, :label => "TOTO" do |p| %>
- <%= render "stop_point_fields", :f => p %>
+ <%= render "stop_point_fields", :f => p, :test1 => "oiuoiu" %>
<% end %>
<div class="links">
<%= link_to_add_association t("routes.actions.add_stop_point"), form, :stop_points, :class => 'add_stop_point' %>
- </div>
- </div>
+ </div>
+ </div>
<%= form.actions do %>
<%= form.action :submit, :as => :button %>
<%= form.action :cancel, :as => :link %>
<% end %>
<% end %>
-
+
<script>
-var stop_point_ids = [];
+var stop_point_ids = [];
var order_position;
order_position = function() {
@@ -33,6 +33,17 @@ order_position = function() {
$(this).val(index);
});
};
+var read_stop_point_ids = function(){
+ $('#stop_points input[type="hidden"][class="stop_point_id"]').map(function() {
+ return $(this).val();
+ });
+}
+var write_stop_point_ids = function(){
+ $('#stop_points input[type="hidden"][class="stop_point_id"]').each(function(index) {
+ $(this).attr("value", stop_point_ids[index])
+ });
+}
+
$('#stop_points').sortable({
axis: 'y',
@@ -45,8 +56,8 @@ order_position = function() {
start: function( event, ui ) {
stop_point_ids = $('#stop_points input[type="hidden"][class="stop_point_id"]').map(function() {
return $(this).val();
- }); },
- update: function( event, ui ) {
+ }); },
+ update: function( event, ui ) {
$('#stop_points input[type="hidden"][class="stop_point_id"]').each(function(index) {
$(this).attr("value", stop_point_ids[index])
});
@@ -54,14 +65,37 @@ order_position = function() {
}
});
-$('#stop_points').bind("cocoon:after-insert", function(event) {
+$('#stop_points').bind("cocoon:after-insert", function(event, insertedItem) {
+ //$( ".stop_point" ).last().val( );
+
+ var new_stop_count = $('div.nested-fields.stop_point').size();
+ var cocoonId = insertedItem.find("input.new_stop_point").attr("id").match( /route_stop_points_attributes_(\d+)_stop_area_id/)[1];
+ insertedItem.find('input').each( function(index,e){
+ var old = $(e).attr("name");
+ console.log(old);
+ if (old!=undefined) {
+ $(e).attr("name", old.replace( cocoonId, new_stop_count - 1));
+ }
+ });
+
+ console.log( "cocoonId="+cocoonId+", new_stop_count="+new_stop_count);
order_position();
});
-$('#stop_points').bind("cocoon:after-remove", function(event) {
+$('#stop_points').on("cocoon:before-remove", function() {
+ stop_point_ids = $('#stop_points input[type="hidden"][class="stop_point_id"]').map(function() {
+ return $(this).val();
+ });
+ //var stop_point_ids = read_stop_point_ids();
+ console.log( "before-remove");
+ console.log( stop_point_ids);
+});
+
+$('#stop_points').on("cocoon:after-remove", function() {
+ write_stop_point_ids();
order_position();
});
-
+
</script>
diff --git a/app/views/routes/_stop_point_fields.html.erb b/app/views/routes/_stop_point_fields.html.erb
index 690a2285a..26219a2e7 100644
--- a/app/views/routes/_stop_point_fields.html.erb
+++ b/app/views/routes/_stop_point_fields.html.erb
@@ -3,9 +3,9 @@
<%= link_to_remove_association "<i class='fa fa-trash-o'></i>".html_safe, f %>
<span class="handle" alt="<%= t('stop_points.index.move') %>" title="<%= t('stop_points.index.move') %>" ><i class='fa fa-arrows'></i></span>
<% if f.object.stop_area.nil? %>
- <%= f.input :id, :as => :hidden, :input_html => { :class => "stop_point_id", :value => Time.now.to_i } %>
+ <%= f.input :id, :as => :hidden, :input_html => { :class => "stop_point_id", :value => "" } %>
<%= f.input :position, :as => :hidden, :input_html => { :class => "position" } %>
- <%= f.input :stop_area_id, :label => false, :input_html => { :class => "new_stop_point stop_area_id", :"data-pre" => [].to_json } %>
+ <%= f.input :stop_area_id, :label => false, :wrapper_html => { :class => (f.object.valid? ? "" : "error") }, :input_html => { :class => "new_stop_point stop_area_id", :"data-pre" => [].to_json } %>
<script>
$(function() {
$( ".new_stop_point" ).last().tokenInput('<%= referential_stop_point_areas_path(@referential, :format => :json) %>',
@@ -16,11 +16,11 @@
hintText: '<%= t('search_hint') %>',
noResultsText: '<%= t('no_result_text') %>',
searchingText: '<%= t('searching_term') %>' }
- );
+ );
});
</script>
<% else %>
- <%= f.input :id, :as => :hidden, :input_html => { :class => "stop_point_id" } %>
+ <%= f.input :id, :as => :hidden, :input_html => { :class => "stop_point_id" } %>
<%= f.input :position, :as => :hidden, :input_html => { :class => "position" } %>
<%= f.input :stop_area_id, :as => :hidden, :input_html => { :class => "stop_area_id"} %>
<%= f.object.stop_area.name %>