-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlunchbag.js
130 lines (104 loc) · 3.75 KB
/
lunchbag.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// a list of lists
Lists = new Meteor.Collection('lists');
// the list's schema
function List(id, title, owner, items) {
this.id = id;
this.title = title;
this.items = items;
this.owner = owner;
}
function log() {
if (typeof console !== 'undefined')
console.log(arguments);
}
if (Meteor.isClient) {
Template.hello.greeting = function () {
return "Welcome to lunchbag.";
};
Template.hello.events({
'click #createList' : function () {
console.log('create list clicked.');
// template data, if any, is available in 'this'
// create the list
var list = new List(0, 'List Title', 'me', [{
text: "Item 1"
}]);
var found = Lists.findOne(list.id);
if (found === undefined) Lists.insert(list);
var fragmentRenderer = function () {
return Template.list(Lists.findOne({id: list.id}));
}
$(Meteor.render(fragmentRenderer)).appendTo(".colB");
}
});
Template.list.events({
'click #addItem' : function () {
this.items = this.items || [];
this.items.push({
text: document.getElementById("newItem").value
});
Lists.update({id: this.id}, this);
},
'click .listContainer .title': function (obj) {
log("You clicked on the title of the list.");
var list = this;
var $target = $(obj.target);
$target.html("<input type='text' value='"
+ $target.html() + "' class='textbox' />");
$target.find("input")
.css('width', '90%')
.focus();
$target.find("input").bind("blur", function (evt) {
var $parent = $(this).parent();
if (list.title != $(this).val()) {
list.title = $(this).val();
Lists.update({id: list.id}, list);
}
$parent.html($(this).val());
});
},
'click .listItems > li': function (obj) {
var list = this;
var $target = $(obj.target);
var $siblings = $target.siblings();
$siblings.removeClass("selected");
if ($target.hasClass("selected")
&& !$target.hasClass("editing")) {
$target.addClass("editing");
$target.html("<input type='text' value='"
+ $target.html() + "' class='textbox' />");
$target.find("input").focus();
$target.find("input").bind("blur", function (evt) {
var $parent = $(this).parent();
var index = $parent.index();
if (list.items[index].text != $(this).val()) {
list.items[index].text = $(this).val();
Lists.update({id: list.id}, list);
}
$parent.html($(this).val());
$parent.removeClass("editing");
});
}
$(obj.target).addClass("selected");
},
'click #deleteItem': function () {
if ((".listItems .selected").length > 0) {
var index = $(".listItems").find(".selected").index();
log(index);
if (index == -1) return;
this.items.splice(index, 1);
log(this.items);
Lists.update({id: this.id}, this);
}
},
'click #clearList': function () {
this.items = [];
Lists.update({id: this.id}, this);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// server startup code here.
});
}