-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.js
37 lines (37 loc) · 1.25 KB
/
drag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$(function() {
$("#newItem").keypress(function(e){
if (13!=e.which) return;
makeDrop("#mealBucket", $("#newItem").val());
$("#newItem").select();
}).focus();
$(".droppable, .tddrop").droppable({
hoverClass: "droppableTargetted",
drop: function(event,ui){
ui.draggable.css({left:"auto", top:"auto"});
$(this).append(ui.draggable); }// <-- drop target
});
});
function makeDrop(dest,msg){
var span = $('<div/>',{class: "x", text: "X"})
.mousedown(function(event){$(this).parent().remove();});
$('<div/>', {text: msg})
.draggable({revert: "invalid", helper: 'clone', append: 'body', opacity: 0.5})
.append(span).appendTo(dest);
}
function save(){
data = "";
$(".ui-draggable").each(function(idx,d){
dp = $(d).parent(); //div (ignoring placement)
dpp = $(dp).parent();
dest= !dp.is("td")? "#"+dp.attr('id'):
"#days tr:eq("+dpp.parent().children().index(dpp)+") td:eq("+dpp.children().index(dp)+")";
data+=dest+","+$(d).text().slice(0,-1)+"\n";//without X, with \n
});
localStorage.setItem('recipeSave', data);
}
function load(){
$.map(localStorage['recipeSave'].split('\n'), function(a,b){
c=a.indexOf(',');
makeDrop($(a.substring(0,c)), a.substring(c+1));
});
}