-
Notifications
You must be signed in to change notification settings - Fork 1
/
boardz.js
175 lines (141 loc) · 5.41 KB
/
boardz.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Boards = new Meteor.Collection("boards");
function randomString() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 32;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
function slug(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;";
var to = "aaaaaeeeeeiiiiooooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
};
if (Meteor.is_client) {
Template.main.content = function() {
return Template[Session.get('page')]();
};
Template.menu.page_is = function(page) {
return Session.equals('page', page);
};
Template.home.boards = function() {
var boards = Boards.find({}, {sort: {title: 1}}).fetch();
return boards;
};
Template.boards.board = function() {
var name = Session.get('board-name');
return Boards.findOne({name: name}) || {};
};
Template.home.events = {
'click .add-board': function() {
$("#create-board-form").slideDown('slow');
$('.create-board-form .board-title').focus();
},
'click .create-board-form .close': function() {
$("#create-board-form").slideUp('slow');
},
'submit .create-board-form': function(event) {
event.preventDefault();
var title = $('input[name="board-title"]').val(),
name = slug(title);
Boards.insert({
title: title,
name: name
});
}
};
Template.boards.init_sortable = function() {
Meteor.setTimeout(function() {
var name = Session.get('board-name'),
board = Boards.findOne({name: name});
$('.sortable').ready(function() {
$( ".sortable" ).sortable({
revert: 100,
connectWith: ".sortable"
});
});
$('.sortable').bind("sortstop", function(event, ui) {
var cards = (board.todo || []).concat(board.doing || []).concat(board.done || []),
ids = {
todo: $.map($('.sortable.todo .card'), function(item){ return item.dataset.id }),
doing: $.map($('.sortable.doing .card'), function(item){ return item.dataset.id }),
done: $.map($('.sortable.done .card'), function(item){ return item.dataset.id }),
};
board.todo = cards.filter(function(item) {
return ids.todo.indexOf(item.id) > -1;
}).sort(function (a, b) {
return ids.todo.indexOf(a.id) - ids.todo.indexOf(b.id);
});
board.doing = cards.filter(function(item) {
return ids.doing.indexOf(item.id) > -1;
}).sort(function (a, b) {
return ids.doing.indexOf(a.id) - ids.doing.indexOf(b.id);
});
board.done = cards.filter(function(item) {
return ids.done.indexOf(item.id) > -1;
}).sort(function (a, b) {
return ids.done.indexOf(a.id) - ids.done.indexOf(b.id);
});
Boards.update({_id: board._id}, board);
});
$('.editable').editable(function(value, settings) {
if(!$.trim(value)){
return;
}
var id = $(this).parents('.card').data('id'),
column = $(this).parents('.sortable').data('column');
board[column] = board[column].map(function(item) {
if( item.id == id ){
item.title = value;
}
return item;
});
Boards.update({_id: board._id}, board);
return value;
});
}, 200);
};
Template.boards.events = {
'click .add-card': function(event) {
var name = Session.get('board-name'),
board = Boards.findOne({name: name}),
column = event.target.dataset.column;
board[column] = board[column] || [];
board[column].push({
id: randomString(),
title: 'New card'
});
Boards.update({_id: board._id}, board);
}
};
Session.set('page', 'home');
Meteor.startup(function() {
Router({
'/': function() {
Session.set('page', 'home');
},
'/about': function() {
Session.set('page', 'about');
},
'/boards/:name': function(name) {
Session.set('page', 'boards');
Session.set('board-name', name);
}
}).init();
});
}
if (Meteor.is_server) {
Meteor.startup(function () {});
}