From 4242841d865549b37b38193b23a7129f8faa990f Mon Sep 17 00:00:00 2001
From: Devin Howard <devin@callysto.com>
Date: Sat, 15 Dec 2012 19:46:39 -0500
Subject: [PATCH] totally unweildy and not working implementation of node
 adding by double clicking. It works all right though

---
 app/assets/javascripts/Jit/graphsettings.js | 32 ++++++++++++---------
 app/assets/stylesheets/application.css      | 11 +++++--
 app/controllers/items_controller.rb         | 18 ++++++++----
 app/views/items/_new.html.erb               | 16 ++---------
 app/views/items/create.js.erb               |  5 ++--
 app/views/layouts/application.html.erb      |  6 ++++
 6 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/app/assets/javascripts/Jit/graphsettings.js b/app/assets/javascripts/Jit/graphsettings.js
index 0248841a..25f24fcf 100644
--- a/app/assets/javascripts/Jit/graphsettings.js
+++ b/app/assets/javascripts/Jit/graphsettings.js
@@ -129,6 +129,7 @@ function graphSettings(type) {
 			
 			// add some events to the label
 			nameContainer.onmouseover = function(){
+                if (node.id == 0) return;
 				$('.showcard').css('display','none');
 				$('.name').css('display','block');
 				$('.name.item_' + node.id).css('display','none');
@@ -328,7 +329,7 @@ function graphSettings(type) {
 
 
 // defining custom node type	
-var nodeSettings = {  
+var nodeSettings = {
 	  'customNode': {  
 		  'render': function (node, canvas) {		  			  
 			  var pos = node.pos.getc(true),
@@ -427,20 +428,25 @@ function selectNodeOnClickHandler(node) {
 
 //for the canvasDoubleClickHandler function
 var canvasDoubleClickHandlerObject = new Object();
-canvasDoubleClickHandlerObject.stored_timestamp = 0;
+canvasDoubleClickHandlerObject.storedTime = 0;
 
 function canvasDoubleClickHandler(e) {
-  var TOLERANCE = 1000; //1 second
+   console.log(e);
+   var TOLERANCE = 300; //0.3 seconds
 
-  //grab the location and timestamp of the click
-  var stored_timestamp = canvasDoubleClickHandlerObject.stored_timestamp;
-  var now = Date.now(); //not compatible with IE8 FYI
+   //grab the location and timestamp of the click
+   var storedTime = canvasDoubleClickHandlerObject.storedTime;
+   var now = Date.now(); //not compatible with IE8 FYI
 
-  if (now - stored_timestamp < TOLERANCE) {
-    //pop up node creation :)
-    $('#new_item').fadeIn('fast');
-    //NOTE: we have e.x, e.y so use them!!
-  } else {
-    canvasDoubleClickHandlerObject.stored_timestamp = now;
-  }
+   if (now - storedTime < TOLERANCE) {
+      //pop up node creation :)
+      document.getElementById('new_item').style.left = e.x + "px";
+      document.getElementById('new_item').style.top = e.y + "px";
+      $('#item_x').val(e.x);
+      $('#item_y').val(e.y);
+      $('#new_item').fadeIn('fast');
+      $('#item_name').focus();
+   } else {
+      canvasDoubleClickHandlerObject.storedTime = now;
+   }
 }//canvasDoubleClickHandler
diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css
index 9d05327d..b9e8ffc3 100644
--- a/app/assets/stylesheets/application.css
+++ b/app/assets/stylesheets/application.css
@@ -33,14 +33,21 @@ h2 {display:block; text-align:center; font-family: "vinyl",sans-serif; backgroun
 a {color:#2d6a5d; text-decoration:none;}
 .clearfloat {clear:both;}
 
-.new_session, .new_user, .new_item, .new_synapse, .new_map, .edit_user, .edit_item, .edit_synapse, .edit_map, .invite { display: block; width: 350px; position:absolute; left:50%; top:0; margin:200px 0 0 -195px; background: url('bg.png'); padding: 20px; border-radius: 15px; color: #000; border:2px solid #000; }
+.new_session, .new_user, .new_synapse, .new_map, .edit_user, .edit_item, .edit_synapse, .edit_map, .invite { display: block; width: 350px; position:absolute; left:50%; top:0; margin:200px 0 0 -195px; background: url('bg.png'); padding: 20px; border-radius: 15px; color: #000; border:2px solid #000; }
 
 .invite p { margin:10px 0; }
 .invite strong { text-align:center; display:block; color:#67AF9F; }
 
-.anypage .new_item, .anypage .new_synapse { display: none; position:absolute; left:50%; top:0; margin:200px 0 0 -195px; border:2px solid #000; }
+.anypage .new_synapse { display: none; position:absolute; left:50%; top:0; margin:200px 0 0 -195px; border:2px solid #000; }
 #closenewtopic, #closenewsynapse { position:absolute; top: 3px; right:3px; }
 
+.anypage .new_item {
+  display: none;
+  position: absolute;
+  background: url('bg.png');
+  border: 2px solid #000;
+}
+
 
 label, select, input, textarea { display:block; }
 
diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb
index e79c39f3..fed86c2e 100644
--- a/app/controllers/items_controller.rb
+++ b/app/controllers/items_controller.rb
@@ -63,14 +63,20 @@ class ItemsController < ApplicationController
 	else
 		@item = Item.new()
 		@item.name = params[:item][:name]
-		@item.desc = params[:item][:desc]
-		@item.link = params[:item][:link]
-		@item.permission = params[:item][:permission]
-		@item.item_category = ItemCategory.find(params[:category])
+		@item.desc = ""
+		@item.link = ""
+		@item.permission = 'commons'
+        @item.item_category = ItemCategory.all.first
+		#@item.item_category = ItemCategory.find(params[:category])
 		@item.user = @user
-		
+
 		@item.save
     end		
+
+    @position = Hash.new()
+    @position.x = params[:item][:x]
+    @position.y = params[:item][:y]
+    @position.save
 	
 	@mapping = Mapping.new()
 	if params[:item][:map]
@@ -85,7 +91,7 @@ class ItemsController < ApplicationController
     
     respond_to do |format|
       format.html { respond_with(@user, location: user_item_url(@user, @item)) }
-      format.js { respond_with(@item, @mapping) }
+      format.js { respond_with(@item, @mapping, @position) }
     end
     
   end
diff --git a/app/views/items/_new.html.erb b/app/views/items/_new.html.erb
index f3ab866d..fd0d38eb 100644
--- a/app/views/items/_new.html.erb
+++ b/app/views/items/_new.html.erb
@@ -1,17 +1,7 @@
 <div class="anypage">
 <%= form_for Item.new, url: user_items_url(user), remote: true do |form| %>
-  <button id="closenewtopic" onclick="$('#new_item').fadeOut('fast'); $('#new_item')[0].reset(); return false;">close</button>
-  <h3>Add Topic</h3>
-  <label for="category">Category</label>
-  <%= select_tag "category", options_from_collection_for_select(ItemCategory.order("name ASC").all, "id", "name") %>
-  <label for="item_name">Title</label>
   <%= form.text_field :name %>
-  <label for="item_desc">Description</label>
-  <%= form.text_area :desc, class: "description", :rows => 5 %>
-  <label for="item_link">Link</label>
-  <%= form.text_field :link, class: "link" %>
-  <label for="item_permission">Permission</label>
-  <%= form.select(:permission, options_for_select(['commons', 'public', 'private'])) %>
-  <%= form.submit "Add Topic", class: "addTopic", id: "addTopic" %>
+  <%= form.hidden_field :x, :value => 0 %>
+  <%= form.hidden_field :y, :value => 0 %>
 <% end %>
-</div>
\ No newline at end of file
+</div>
diff --git a/app/views/items/create.js.erb b/app/views/items/create.js.erb
index c0e95bdd..6979ded2 100644
--- a/app/views/items/create.js.erb
+++ b/app/views/items/create.js.erb
@@ -3,7 +3,8 @@ $('#new_item')[0].reset();
 
 if(viewMode == "graph") {
 	  var newnode = <%= @item.self_as_json.html_safe %>;
-		
+      var x = <%= position.x %>;
+      var y = <%= position.y %>;
 		
 		if (Mconsole != null) {
       Mconsole.graph.addNode(newnode);
@@ -44,4 +45,4 @@ else {
   alert('wrong!');
 	$('#cards').prepend('<%= escape_javascript(render(@item)) %>');
 	$(".scroll").mCustomScrollbar(); 
-}
\ No newline at end of file
+}
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 77b3bc68..b5358c87 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -2,6 +2,12 @@
 <html>
 <head>
   <title>Metamaps</title>
+  <% if authenticated? %>
+    <script type="text/javascript">
+      var userid = "<%= user.id %>";
+      var username = "<%= user.name %>";
+    </script>
+  <% end %>
   <%= stylesheet_link_tag    "application", :media => "all" %>
   <%= javascript_include_tag "application" %>
   <script type="text/javascript" src="//use.typekit.net/tki2nyo.js"></script>