-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstickynotes.js
212 lines (177 loc) · 5.92 KB
/
stickynotes.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
$(function() {
whiteboard = new Whiteboard('#whiteboard');
});
Whiteboard = (function() {
var WhiteboardConstructor;
WhiteboardConstructor = function(selector) {
if (false === (this instanceof Whiteboard)) {
return new Whiteboard(selector);
}
this.$whiteboard = $(selector || 'body').first();
this.$whiteboard.addClass("whiteboard-wrapper");
this.$editor = $('<textarea>').attr('id', 'editor');
this.noteColors = {
98: '#ccf', //blue
99: '#cff', //cyan
103: '#cfc', //green
109: '#fce', //magenta
111: '#fec', //orange
112: '#fcf', //purple
114: '#fcc', //red
116: '#cff', //teal
119: '#fff', //white
121: '#ffc' //yellow
};
this.$currentNote = undefined;
this.$pinnedNote = undefined;
this.$editingNote = undefined;
this.zIndex = 1000;
this.registerEventHandlers();
this.registerInputHandlers();
this.registerNoteEventHandlers();
};
WhiteboardConstructor.prototype.registerEventHandlers = function() {
var that = this;
this.$whiteboard.click(function(e) {
// If we're currently editing submit note before creating
if (typeof that.$editingNote !== 'undefined') {
that.submitNote();
}
var new_note = that.makeNote(e);
that.editNote(new_note);
});
$('body').keypress(function(e) {
console.log(e.which);
if (typeof that.$currentNote !== 'undefined' & typeof that.$editingNote === 'undefined') {
console.log('changing color: ' + that.noteColors[e.which]);
that.$currentNote.css('background-color', that.noteColors[e.which]);
}
});
};
WhiteboardConstructor.prototype.registerInputHandlers = function() {
var that = this;
this.$whiteboard.on('keydown', '#editor', function(e) {
switch (e.which) {
case 13:
that.submitNote();
break;
case 27:
that.cancelNote();
break;
}
});
// Rejigger font size down to 18pt
this.$whiteboard.on('keyup', '#editor', function(e) {
while (e.currentTarget.scrollHeight > e.currentTarget.offsetHeight && parseInt($(e.currentTarget).css('font-size')) > 20) {
var newFontSize = parseInt($(e.currentTarget).css('font-size'))-1;
$(e.currentTarget).css('font-size', newFontSize);
}
});
};
WhiteboardConstructor.prototype.registerNoteEventHandlers = function() {
var that = this;
// Register mouseover for color changes
this.$whiteboard.on('mouseover', '.note', function(e) {
that.$currentNote = $(e.currentTarget);
console.log('mouseover');
});
// Register mouseout to not allow color changes
this.$whiteboard.on('mouseout', '.note', function(e) {
that.$currentNote = undefined;
console.log('mouseout');
});
// Register offset position for drag start
this.$whiteboard.on('mousedown', '.note', function(e) {
that.$pinnedNote = $(e.currentTarget);
that.$pinnedNote.xOffset = e.offsetX;
that.$pinnedNote.yOffset = e.offsetY;
console.log('mousedown');
});
this.$whiteboard.on('click', '.note', function(e) {
console.log('click');
if (typeof that.$currentNote !== 'undefined') {
if (!that.$pinnedNote.moved) {
that.editNote(that.$pinnedNote);
}
delete that.$pinnedNote.moved;
that.$pinnedNote = undefined;
return false;
}
});
this.$whiteboard.mousemove(function(e) {
if (typeof that.$pinnedNote !== 'undefined') {
that.$pinnedNote.moved = true;
that.$pinnedNote.css({
'z-index': ++that.zIndex,
'top': e.pageY - that.$pinnedNote.yOffset,
'left': e.pageX - that.$pinnedNote.xOffset
});
that.$editor.css({
'z-index': that.zIndex,
'top': e.pageY - that.$pinnedNote.yOffset,
'left': e.pageX - that.$pinnedNote.xOffset
});
}
});
this.$whiteboard.on('dblclick', '.note', function(e) {
$(e.currentTarget).remove();
return false;
});
};
WhiteboardConstructor.prototype.editNote = function(note) {
if (note.children('textarea').length > 0) {
//already editing this note
return;
}
this.$editingNote = note;
this.$editingNote.previousVal = this.$editingNote.text();
this.$editingNote.html('');
this.$editor.appendTo(this.$editingNote);
this.$editor.css({
top: this.$editingNote.offset().top,
left: this.$editingNote.offset().left,
width: this.$editingNote.width(),
height: this.$editingNote.height(),
'font-size': this.$editingNote.css('font-size')
}).val(this.$editingNote.previousVal).show().focus().select();
};
WhiteboardConstructor.prototype.uneditNote = function() {
this.$editor.hide().val('').appendTo('body');
this.$editingNote = undefined;
};
WhiteboardConstructor.prototype.submitNote = function() {
console.log('writing: ' + this.$editor.val());
this.$editingNote.html(this.$editor.val());
this.$editingNote.css('font-size', this.$editor.css('font-size'));
this.uneditNote();
};
WhiteboardConstructor.prototype.cancelNote = function() {
this.$editingNote.html(this.$editingNote.previousVal || '');
this.uneditNote();
};
WhiteboardConstructor.prototype.makeNote = function(e) {
$('#instructions').hide();
var note_size = 150;
var note_color = '#ffc';
var note_template = $('<div>');
note_template.addClass('note');
note_template.css({
'width': note_size,
'height': note_size,
'background-color': note_color,
'position': 'absolute',
'font-size': '48px',
'z-index': ++this.zIndex
});
var note = note_template.clone();
note.css({
top: e.pageY - note.height()/2,
left: e.pageX - note.width()/2
});
this.$whiteboard.append(note);
note.previousVal = '';
this.$currentNote = note;
return note;
};
return WhiteboardConstructor;
}());