-
Notifications
You must be signed in to change notification settings - Fork 0
/
stickyNotes.app.js
322 lines (300 loc) · 8.18 KB
/
stickyNotes.app.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* StickyNotes v1.0 in PHP & jQuery & AngularJS by zairwolf
*
* Source: https://github.com/zairwolf/stickyNotes/blob/master/stickyNotes.app.js
*
* Author: Hai Zheng @ https://www.linkedin.com/in/zairwolf/
*
*/
var noteApp = angular.module('noteApp', []);
//filter is for content html show
noteApp.filter('html', ['$sce', function($sce) {
return function(text) {
return $sce.trustAsHtml(text);
};
}]);
noteApp.controller('noteCtrl', ['$scope', '$interval', 'stickyNotes', function($scope, $interval, stickyNotes) {
$scope.textShadow = 'white 1px 1px 0px';
$scope.notes = [];
$scope.md5 = '';
$scope.add = function(){
$scope.notes.push(stickyNotes.add());
}
//interval read notes from json
$interval(function(){
stickyNotes.getNotes($scope.md5).then(function(data){
if(data.md5){
if($('[noteid=-1]').length) data.notes.push(stickyNotes.add());
$scope.notes = data.notes;
$scope.md5 = data.md5;
}
});
}, 1000);
}]);
//config button
noteApp.directive('config', function() {
return {
restrict: 'A',
link: function(scope, element) {
element.bind("click", function(e) {
var thisNote = $(this).closest('.PIApostit');
thisNote.children('.back').show();
thisNote.addClass('flip', function() {
$(this).children('.front').hide();
});
//minicolor
thisNote.find('[bgColor]').minicolors({
change: function(hex) {
thisNote.css('background-color', hex);
}
});
thisNote.find('[color]').minicolors({
change: function(hex) {
thisNote.css('color', hex);
}
});
});
}
};
});
//delete button
noteApp.directive('delete', function() {
return {
restrict: 'A',
link: function(scope, element) {
element.bind("click", function(e) {
$(this).next().show();
});
}
};
});
//cancel delete button
noteApp.directive('canceldelete', function() {
return {
restrict: 'A',
link: function(scope, element) {
element.bind("click", function(e) {
$(this).closest('.ui-widget').hide();
});
}
};
});
//sure delete button
noteApp.directive('suredelete', ['stickyNotes', function(stickyNotes) {
return {
restrict: 'A',
link: function(scope, element) {
element.bind("click", function(e) {
var thisNote = element.closest('.PIApostit');
var id = thisNote.attr('noteid');
stickyNotes.delNote(id);
if (id == -1) thisNote.remove();
});
}
};
}]);
//close button
noteApp.directive('close', ['stickyNotes', function(stickyNotes) {
return {
restrict: 'A',
link: function(scope, element) {
element.bind("click", function(e) {
var thisNote = element.closest('.PIApostit');
var id = thisNote.attr('noteid');
//hide back
$(this).closest('.PIApostit').children('.front').show();
$(this).closest('.PIApostit').removeClass('flip', function() {
$(this).children('.back').hide();
});
//save cfg
if (id == -1) return; //new note save cfg when save content
var data = {
'id': id
};
data['bgColor'] = thisNote.find('[bgColor]').val();
data['color'] = thisNote.find('[color]').val();
data['textshadow'] = thisNote.find('[textshadow]').is(':checked') ? 1 : 0;
stickyNotes.setNote(data);
});
}
};
}]);
//content edit
noteApp.directive('contenteditable', ['stickyNotes', function(stickyNotes) { //NOTE: contenteditable is a HTML5 attribute
return {
restrict: 'A', // only activate on element attribute
link: function(scope, element, attrs) {
//auto resize
element.on('keyup paste', function() {
element.resizable({
animate: false,
helper: 'ui-resizable-helper',
stop: function() {
var thisNote = element.closest('.PIApostit');
var posY = thisNote.css('left'),
posX = thisNote.css('top'),
divWidth = thisNote.width(),
divHeight = thisNote.find('.PIAeditable').height();
console.log(divHeight);
// minDivHeight = options.minHeight;
// if (divHeight >= minDivHeight) {
// divHeight += 50;
// options.height = divHeight;
// element.css('height', divHeight);
// if ($.ui) {
// if (options.resizable) {
// element.resizable({
// minHeight: divHeight
// });
// }
// }
// } else if (divHeight < minDivHeight) {
// options.height = minDivHeight;
// minDivHeight += 50;
// element.css('height', minDivHeight);
// }
}
});
});
// Listen for change events to enable binding
element.on('blur', function() {
scope.$evalAsync(read);
});
// Write data to the model
function read() {
var html = element.html();
// When we clear the content editable the browser leaves a <br> behind
// If strip-br attribute is provided then we strip this out
if (attrs.stripBr && html == '<br>') {
html = '';
}
if (html) {
var thisNote = element.closest('.PIApostit');
var id = thisNote.attr('noteid');
var data = {
'id': id
};
data['content'] = html;
if (id == -1) {
data['left'] = thisNote.css('left');
data['top'] = thisNote.css('top');
data['width'] = thisNote.css('width');
data['height'] = thisNote.css('height');
//add cfg for new note
data['bgColor'] = thisNote.find('[bgColor]').val();
data['color'] = thisNote.find('[color]').val();
data['textshadow'] = thisNote.find('[textshadow]').is(':checked') ? 1 : 0;
}
var newId = stickyNotes.setNote(data);
if (id == -1 && newId) thisNote.attr('noteid', newId); //change new note's id
}
}
}
};
}]);
//draggable attribute
noteApp.directive('draggable', ['stickyNotes', function(stickyNotes) {
return {
restrict: 'A',
link: function(scope, element) {
element.draggable({
handle: ".PIAtoolbar",
scroll: false,
start: function() {
//all others notes go to back, this note goes to front
$('.PIApostit').css('z-index', function() {
return $(this).css('z-index') - 1;
});
element.css('z-index', 9999);
},
stop: function() {
var id = element.closest('.PIApostit').attr('noteid');
if (id == -1) return; //new note save cfg @saveCon
//save position
var data = {
'id': id
};
data['left'] = element.css('left');
data['top'] = element.css('top');
stickyNotes.setNote(data);
}
});
}
};
}]);
//service
noteApp.factory('stickyNotes', function($http) {
var hai = {};
hai.add = function() {
var noteNewElm = $('[noteid=-1]');
if (!noteNewElm.length) return {
'id': '-1',
'left': '50px',
'top': '50px',
'width': '210px',
'height': '220px',
'color': '#333333',
'zindex': '9999',
'textShadow': '1',
'bgColor': '#fffc7f',
'user': '',
'date': 'Now',
};
//return existing new notes
var noteExisting = {
'id': '-1',
'left': noteNewElm.css('left'),
'top': noteNewElm.css('top'),
'width': noteNewElm.css('width'),
'height': noteNewElm.css('height'),
'color': noteNewElm.css('color'),
'zindex': noteNewElm.css('z-index'),
'textShadow': $('#noteCon-1').css('text-shadow') ? 1 : 0,
'bgColor': noteNewElm.css('background-color'),
'user': '',
'date': 'Now',
'content': $('#noteCon-1').html()
};
return noteExisting;
};
hai.loading = function(id) {
$('[noteid=' + id + ']').find('[status]').show();
};
hai.loaded = function(id) {
$('[noteid=' + id + ']').find('[status]').hide();
};
hai.getNotes = function(md5) {
return $http.get(_requestUrl + 'action=getNotes&md5=' + md5).then(function(response) {
return response.data;
});
};
//save data to php
hai.setNote = function(data) {
var id = data.id;
hai.loading(id);
var q = $http({
url: _requestUrl + 'action=setNote',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: $.param(data)
});
return q.then(function(response) {
hai.loaded(id);
if (response.data.error) alert(response.data.errorinfo);
return response.data.id;
}, function(response) {
hai.loaded(id);
alert('error');
return false;
});
};
//delete note
hai.delNote = function(id) {
return $http.get(_requestUrl + 'action=delNote&id=' + id).then(function(response) {
return response.data;
});
}
return hai;
});