From 954ce2009bf9602e13a148d224e6e22b43b87be4 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Mon, 7 Jan 2013 19:43:12 -0500 Subject: [PATCH] got editing of synapses working. Also tidied up selection code. --- app/assets/javascripts/Jit/graphsettings.js | 76 +++++++++++++++++---- app/controllers/synapses_controller.rb | 27 +++++--- app/views/synapses/create.js.erb | 3 +- 3 files changed, 82 insertions(+), 24 deletions(-) diff --git a/app/assets/javascripts/Jit/graphsettings.js b/app/assets/javascripts/Jit/graphsettings.js index e95dd01f..9a18d410 100644 --- a/app/assets/javascripts/Jit/graphsettings.js +++ b/app/assets/javascripts/Jit/graphsettings.js @@ -408,26 +408,78 @@ var nodeSettings = { } } +function editEdge(edge, e) { + deselectEdge(edge); //so the label is missing while editing + if (!document.getElementById('edit_synapse')) { + var edit_div = document.createElement('div'); + edit_div.setAttribute('id', 'edit_synapse'); + edit_div.setAttribute('class', 'best_in_place best_in_place_desc'); + edit_div.setAttribute('data-object', 'synapse'); + edit_div.setAttribute('data-attribute', 'desc'); + edit_div.setAttribute('data-type', 'input'); + //TODO how to get blank data-nil + edit_div.setAttribute('data-nil', ' '); + $('.main .wrapper').append(edit_div); + }//if + + $('#edit_synapse').attr('data-url', '/synapses/' + edge.getData("id")); + $('#edit_synapse').html(edge.getData("desc")); + $('#edit_synapse').css('position', 'absolute'); + $('#edit_synapse').css('left', e.clientX); + $('#edit_synapse').css('top', e.clientY); + + $('#edit_synapse').bind("ajax:success", function() { + var desc = $(this).html(); + $('#edit_synapse').hide(); + edge.setData("desc", desc); + selectEdge(edge); + Mconsole.plot(); + }); + + $('#edit_synapse').focusout(function() { + $('#edit_synapse').hide(); + }); + + //css stuff above moves it, this activates it + $('#edit_synapse').click(); + $('#edit_synapse input').focus(); + $('#edit_synapse').show(); +} + function selectEdgeOnClickHandler(adj, e) { + //editing overrides everything else + if (e.altKey) { + editEdge(adj, e); + return; + } + var showDesc = adj.getData("showDesc"); - if (showDesc) { + if (showDesc && e.shiftKey) { + //deselecting an edge with shift deselectEdge(adj); - Mconsole.plot(); - } - if (!e.shiftKey) { - for (var i = 0; i < selectedEdges.length; i += 1) { - var edge = selectedEdges[i]; - deselectEdge(edge); - } - } - if (!showDesc) { + } else if (!showDesc && e.shiftKey) { + //selecting an edge with shift + selectEdge(adj); + } else if (showDesc && !e.shiftKey) { + //deselecting an edge without shift - unselect all + deselectAllEdges(); + } else if (!showDesc && !e.shiftKey) { + //selecting an edge without shift - unselect all but new one + deselectAllEdges(); selectEdge(adj); - Mconsole.plot(); } + + Mconsole.plot(); }//selectEdgeOnClickHandler +function deselectAllEdges() { + for (var i = 0; i < selectedEdges.length; i += 1) { + var edge = selectedEdges[i]; + deselectEdge(edge); + } +} + function selectNodeOnClickHandler(node, e) { - //set final styles if (!e.shiftKey) { Mconsole.graph.eachNode(function (n) { diff --git a/app/controllers/synapses_controller.rb b/app/controllers/synapses_controller.rb index 3662deaa..37148792 100644 --- a/app/controllers/synapses_controller.rb +++ b/app/controllers/synapses_controller.rb @@ -100,20 +100,27 @@ class SynapsesController < ApplicationController # PUT synapses/:id def update - @current = current_user + @current = current_user @synapse = Synapse.find(params[:id]).authorize_to_edit(@current) - if @synapse - @synapse.desc = params[:synapse][:desc] - @synapse.category = params[:synapse][:category] - @synapse.topic1 = Topic.find(params[:node1_id][:node1]) - @synapse.topic2 = Topic.find(params[:node2_id][:node2]) - @synapse.permission = params[:synapse][:permission] - @synapse.save + if @synapse + if params[:synapse] + @synapse.desc = params[:synapse][:desc] if params[:synapse][:desc] + @synapse.category = params[:synapse][:category] if params[:synapse][:category] + @synapse.permission = params[:synapse][:permission] if params[:synapse][:permission] + end + if params[:node1_id] and params[:node1_id][:node1] + @synapse.topic1 = Topic.find(params[:node1_id][:node1]) + end + if params[:node1_id] and params[:node1_id][:node1] + @synapse.topic2 = Topic.find(params[:node2_id][:node2]) + end + @synapse.save end - respond_with(@user, location: synapse_url(@synapse)) do |format| - end + respond_with @synapse + #respond_with(@user, location: synapse_url(@synapse)) do |format| + #end end # POST mappings/:map_id/:synapse_id/removefrommap diff --git a/app/views/synapses/create.js.erb b/app/views/synapses/create.js.erb index 2fed16ad..122367eb 100644 --- a/app/views/synapses/create.js.erb +++ b/app/views/synapses/create.js.erb @@ -10,7 +10,6 @@ if ( Mconsole != null) { temp2 = Mconsole.graph.getNode(<%= @synapse.topic2.id %>); Mconsole.graph.addAdjacence(temp1, temp2, {}); temp = Mconsole.graph.getAdjacence(temp1.id, temp2.id); - console.log(temp); temp.setDataset('start', { lineWidth: 0.4 }); @@ -20,7 +19,7 @@ if ( Mconsole != null) { var d = new Array(<%= @synapse.node1_id.to_s() %>, <%= @synapse.node2_id.to_s() %>); temp.setDataset('current', { desc: '<%= @synapse.desc %>', - showDesc: true, + showDesc: false, //will be changed by selectEdge category: '<%= @synapse.category %>', id: '<%= @synapse.id %>', userid: '<%= @synapse.user.id %>',