-
Notifications
You must be signed in to change notification settings - Fork 1
/
post.it.js
187 lines (180 loc) · 5.05 KB
/
post.it.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
176
177
178
179
180
181
182
183
184
185
186
187
(function () {
var root = this;
if (typeof exports !== 'undefined') {
root._ = require('underscore')._;
root.Backbone = require('backbone');
root.postit = exports;
}
else {
root.postit = root.postit || {};
}
GUID = function () { var res = [], hv, rgx = new RegExp('[2345]'); for (var i = 0; i < 8; i++) { hv = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1); if (rgx.exec(i.toString()) != null) { if (i == 3) { hv = '6' + hv.substr(1, 3); } res.push('-'); } res.push(hv.toUpperCase()); } return res.join(''); }
/* MODELS */
postit.Note = root.Backbone.Model.extend({
defaults: function() {
return {
text: 'New note...',
rotation: -30 + parseInt(60 * Math.random())
};
},
initialize: function() {
if(!this.has('id')) {
this.set({ id: GUID() });
}
this.bind('change', function() {
if(this.room) {
this.room.setNote(this.toJSON(), true);
}
}, this)
}
});
postit.NotesCollection = root.Backbone.Collection.extend({
model: postit.Room
});
postit.Room = Backbone.Model.extend({
defaults: function() {
return {
roomId: 'default',
socket: null,
light: false,
notes: new postit.NotesCollection,
};
},
initialize: function() {
var model = this;
this.get('socket').on('connection', function(client) {
client.emit('lights' + (model.get('lights') ? 'On' : 'Off'));
var clientsConnected = function() {
return _(client.manager.connected).keys().length;
};
model.get('notes').each(function(note) {
client.emit('note', note.toJSON());
});
client.emit('clientsConnected', clientsConnected());
client.broadcast.emit('clientsConnected', clientsConnected());
client.on('disconnect', function() {
client.broadcast.emit('clientsConnected', clientsConnected());
});
model.initializeEvents(client);
});
model.initializeEvents(this.get('socket'));
},
initializeEvents: function(socket) {
var model = this;
socket.on('lightsOn', function() {
model.set({ lights: true });
if(socket.manager) {
socket.broadcast.emit('lightsOn');
}
});
socket.on('lightsOff', function() {
model.set({ lights: false });
if(socket.manager) {
socket.broadcast.emit('lightsOff');
}
});
socket.on('clientsConnected', function(data) {
model.set({ clientsConnected: data });
});
socket.on('note', function(data) {
var note = model.setNote(data);
if(socket.manager) {
socket.broadcast.emit('note', note.toJSON());
}
});
},
toggleLights: function() {
this.set({ lights: !this.get('lights') });
this.get('socket').emit('lights' + (this.get('lights') ? 'On' : 'Off'));
},
setNote: function(data, broadcast) {
var model = this;
var note = model.get('notes').get(data.id);
if(!note) {
note = new postit.Note(data);
note.room = model;
model.get('notes').add(note);
}
note.set(data, { silent: broadcast });
if(broadcast) {
this.get('socket').emit('note', note.toJSON());
}
return note;
}
});
/* VIEWS */
postit.NoteView = root.Backbone.View.extend({
tagName: 'div',
className: 'note',
initialize: function() {
this.model.bind('change', function() {
this.update();
}, this);
},
render: function(edit) {
var model = this.model;
var el = $(this.el);
var text = $('<div class="text" />').appendTo(el);
this.update();
el.draggable({
stop: function(e) {
console.log(el.position());
model.set(el.position());
}
}).dblclick(function(e) {
e.stopPropagation();
text.attr({ contentEditable: 'true' });
text.get(0).focus();
document.execCommand('selectAll', false, null);
});
text.blur(function() {
model.set({ text: text.html() });
});
if(edit) {
setTimeout(function() {
text.trigger('dblclick');
}, 1)
}
return this;
},
update: function() {
$(this.el).css({
position: 'absolute',
'-webkit-transform': 'rotate(' + this.model.get('rotation') + 'deg)',
left: this.model.get('left') + 'px',
top: this.model.get('top') + 'px'
})
.find('.text').html(this.model.get('text'));
}
});
postit.RoomView = Backbone.View.extend({
el: '#room',
initialize: function() {
var model = this.model;
$(this.el).click(function(e) {
var w = $('.background').width() / 680;
var h = $('.background').height() / 545;
if(e.pageX > 540 * w && e.pageX < 610 * w && e.pageY > 110 * h && e.pageY < 165 * h) {
model.toggleLights();
}
});
var edit = false;
$(this.el).dblclick(function(e) {
edit = true;
model.setNote({ left: e.pageX, top: e.pageY }, true);
});
model.bind('change:lights', function() {
$('.light').toggleClass('on', model.get('lights'));
});
model.bind('change:clientsConnected', function(data) {
$('.clientsConnected').html(model.get('clientsConnected'));
});
model.get('notes').bind('add', function(note) {
var view = new postit.NoteView({ model: note });
$(this.el).append(view.render(edit).el);
edit = false;
}, this);
model.bind('destroy', this.remove, this);
},
});
})(this);