/**
* 笔记管理工具
* @author zxlie
*/
let stickywidth = 220; // width of sticky note (can't be less than 200)
let stickyheight = 200; // height of sticky note (can't be less than 200)
let max_notes = 10000; // maximum number of notes one can store
let allowed_tags = '
';
let html5sticky = {};
const STICKYNOTES_ALLKEYS = 'stickynotes|allkeys';
const STICKYNOTES_FOLDERS = 'stickynotes|folders';
const STICKYNOTES_SELECTED_FOLDER = 'stickynotes|selected|folder';
// add a note
html5sticky.addNote = function () {
// count total present notes
let tnotes = $('.note_common').length;
if (tnotes === max_notes) {
html5sticky.showMessage('#FFE16B', 'black', '当前便签笔记已经足够多了,不能再添加了!');
return false;
}
// unique localstorage identifier for this sticky note
let nindex = 'stickynote_' + (new Date * 1);
let dated = getDateTime();
let dateStr = new Date();
// get random color
let bgcolor = html5sticky.getColor();
let folderId = html5sticky.getCurrentFolder()[1];
let stickynote = $('').appendTo($('#main'));
// add tape to stickynote
html5sticky.addPin(stickynote);
$(stickynote).append($('
' + dated + '
'));
$(stickynote).append($(''));
// append identifier
$(stickynote).append($(''));
// set width and height of the sticky note
$('.note_common').css({width: stickywidth + 'px', height: stickyheight + 'px'});
$('.note_common p').css({height: (stickyheight - 60) + 'px', width: (stickywidth + 9) + 'px'});
// scroll to newly added sticky note
$('html, body').animate({
scrollTop: $(stickynote).offset().top
});
// 先存key,再存数据
let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
allKeys.push(nindex + '|text');
allKeys.push(nindex + '|bgcolor');
allKeys.push(nindex + '|dated');
allKeys.push(nindex + '|folderid');
localStorage.setItem(STICKYNOTES_ALLKEYS, allKeys.join(','));
// 存数据
localStorage.setItem(nindex + '|text', $(stickynote).find('h2').text() + '|' + $(stickynote).find('p').text());
localStorage.setItem(nindex + '|bgcolor', bgcolor);
localStorage.setItem(nindex + '|dated', dated + '|' + getISODateTime(dateStr));
localStorage.setItem(nindex + '|folderid', folderId);
html5sticky.enlargeNote(stickynote);
html5sticky.updateNotesCountForFolder();
};
// save note
html5sticky.saveNote = function (el) {
let identifier = html5sticky.getIdentifier($(el));
let htext = html5sticky.stripTags($(el).closest('.bignote').find('.hedit')[0].value, allowed_tags);
let ptext = html5sticky.stripTags($(el).closest('.bignote').find('.pedit')[0].value, allowed_tags);
ptext = ptext.replace(/\r?\n/g, ' ');
localStorage.setItem(identifier + '|text', htext + '|' + ptext);
let theNoteEl = $('[id^=idf_' + identifier + ']').closest('.note_common');
theNoteEl.find('h2').text(htext);
theNoteEl.find('p').html(ptext);
let oldFolder = localStorage.getItem(identifier + '|folderid') || 0;
let newFolder = $(el).closest('.bignote').find('.fedit')[0].value;
localStorage.setItem(identifier + '|folderid', newFolder);
html5sticky.closeNote(el);
html5sticky.showMessage('#9BED87', 'black', '笔记保存成功!');
// 发生了文件夹变更
if (String(oldFolder) !== String(newFolder)) {
theNoteEl.fadeOut('slow', function () {
theNoteEl.remove();
if (!$(".note_common").length > 0) {
$('#removenotes').slideUp('slow');
}
});
html5sticky.updateNotesCountForFolder();
}
};
html5sticky.updateNotesCountForFolder = function () {
// 刷新folder计数
let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
let folders = html5sticky.loadFolders();
Object.keys(folders).forEach(f => {
let counter = allKeys.filter(key => /\|folderid/.test(key) && (String(folders[f]) === (localStorage.getItem(key) || '0'))).length;
$('#f_' + folders[f]).find('i').text('(' + counter + ')');
});
};
// get note identifier
html5sticky.getIdentifier = function (el) {
if (!el) {
return 'stickynote_' + (new Date * 1 + Math.floor(Math.random() * 10));
}
let identifier = $(el).closest('.bignote').find('[id^=idf_]').attr('id');
if (typeof identifier == 'undefined' || identifier == null) {
identifier = $(el).closest('.note_common').find('[id^=idf_]').attr('id');
}
if (typeof identifier != 'undefined') {
identifier = identifier.replace('idf_', '');
return identifier;
}
else {
return false;
}
};
html5sticky.deleteNoteById = function (identifier) {
localStorage.removeItem(identifier);
localStorage.removeItem(identifier + '|text');
localStorage.removeItem(identifier + '|bgcolor');
localStorage.removeItem(identifier + '|dated');
localStorage.removeItem(identifier + '|folderid');
let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
['text', 'bgcolor', 'dated', 'folderid'].forEach(function (item) {
let id = identifier + '|' + item;
allKeys.indexOf(id) > -1 && allKeys.splice(allKeys.indexOf(id), 1);
});
localStorage.setItem(STICKYNOTES_ALLKEYS, allKeys.join(','));
};
// delete note
html5sticky.deleteNote = function (el) {
if (confirm('确定要删除这个便签笔记吗,一旦删除则不可恢复,请三思?')) {
let identifier = html5sticky.getIdentifier($(el));
html5sticky.deleteNoteById(identifier);
$(el).closest('.note_common').fadeOut('slow', function () {
$(el).closest('.note_common').remove();
});
}
};
// delete all notes
html5sticky.deleteAllNotes = function () {
if (confirm('建议删除之前先【全部导出】,否则一旦删除则不可恢复,请三思?')) {
$('.note_common').fadeOut('slow', function () {
$('.note_common').remove();
let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
allKeys.forEach(function (key) {
localStorage.removeItem(key);
});
localStorage.removeItem(STICKYNOTES_ALLKEYS);
html5sticky.deleteAllFolders();
location.reload(true);
});
}
};
// close big note
html5sticky.closeNote = function (el) {
$(el).closest('.bignote')[html5sticky.getAnimation(true)]('slow', function () {
$('#overlay').remove();
});
};
// edit note
html5sticky.editNote = function ($clone, el) {
let ptext = $clone.find('p').html();
ptext = ptext.replace(/( | )/g, '\n');
$clone.find('p').replaceWith('');
$clone.find('.pedit')
.val(ptext)
.css({
'marginTop': '5px',
'resize': 'none',
'outline': 'none'
})
.addClass('inset')
.width('568px')
.height('280px');
// make content editable
let htext = $clone.find('h2').text();
$clone.find('h2').replaceWith('');
$('.hedit').addClass('inset').val(html5sticky.stripTags(htext, allowed_tags)).width(250);
// folder-list
let folders = html5sticky.loadFolders();
let folderid = localStorage.getItem($clone.find('span[id^="idf_"]').attr('id').replace('idf_', '') + '|folderid') || '0';
$('.hedit').after('');
// put in Close button
$(`关闭`)
.css({
position: 'absolute',
top: 5,
right: 5,
color: '#000'
})
.appendTo($clone);
// put in Save button
$(`保存`)
.css({
position: 'absolute',
top: 5,
right: 40,
color: '#000'
})
.appendTo($clone);
};
html5sticky.getNotesByFolderId = function (folderId) {
// load notes
let allKeys = (localStorage.getItem(STICKYNOTES_ALLKEYS) || '').split(',');
let result = [];
allKeys.forEach(key => {
if (!/\|text/.test(key)) {
return false;
}
let id = key.replace('|text', '');
// 按照folder id寻找对应目录下的便签
let folderid = localStorage.getItem(id + '|folderid') || '0';
if (String(folderId) !== folderid) {
return false;
}
result.push(id);
});
return result;
};
// load all notes
html5sticky.loadNotes = function (folderId) {
let mainEl = $('#main').html('');
let notes = html5sticky.getNotesByFolderId(folderId);
notes.forEach(id => {
let stickynote, bgcolor, htext, ptext, temp_array;
// get color and rotation level
bgcolor = localStorage.getItem(id + '|bgcolor');
// get text info
temp_array = localStorage.getItem(id + '|text').split('|');
htext = temp_array[0];
ptext = temp_array[1];
stickynote = $('').appendTo(mainEl);
html5sticky.addPin(stickynote);
$(stickynote).append($(''));
$(stickynote).append($(''));
// append identifier
$(stickynote).append($(''));
$(stickynote).find('h2').text(html5sticky.stripTags(htext, allowed_tags));
$(stickynote).find('p').html(html5sticky.stripTags(ptext, allowed_tags));
// set width and height of the sticky note
$('.note_common').css({width: stickywidth + 'px', height: stickyheight + 'px'});
$('.note_common p').css({height: (stickyheight - 60) + 'px', width: (stickywidth - 24) + 'px'});
});
$('#f_' + folderId).find('i').text('(' + notes.length + ')');
};
// collapse notes
html5sticky.collapse = function () {
let height = parseInt($('.note_common:first').find('h2').height() || 0, 10) + 'px';
$('.note_common').animate({height: height}, function () {
$('.note_common').find('p').hide();
});
};
// expand notes
html5sticky.expand = function () {
$('.note_common').animate({height: stickyheight}, function () {
$('.note_common').find('p').fadeIn('slow');
});
};
// share note
html5sticky.showMessage = function (bgcolor, color, msg, callback) {
if (!$('#smsg').is(':visible')) {
$('html, body').animate({
scrollTop: 0
}, 500, function () {
if (!$('#smsg').length) {
$('
' + msg + '
').appendTo($('body')).css({
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: '40px',
lineHeight: '40px',
background: bgcolor,
color: color,
zIndex: 1000,
fontWeight: 'bold',
textAlign: 'center',
opacity: 0.9,
margin: 'auto',
display: 'none'
}).slideDown('show');
setTimeout(function () {
$('#smsg').animate({'width': 'hide'}, function () {
$('#smsg').remove();
callback && callback();
});
}, 2000);
}
});
}
};
// get random color
html5sticky.getColor = function () {
let text = "";
let possible = "0123456789";
text += possible.charAt(Math.floor(Math.random() * possible.length));
return 'stickynote' + text;
};
// get random animation string
html5sticky.getAnimation = function (hideAnimation) {
let words = [];
if (typeof hideAnimation !== 'undefined') {
words[1] = "hide";
words[2] = "fadeOut";
words[3] = "slideUp";
}
else {
words[1] = "show";
words[2] = "fadeIn";
words[3] = "slideDown";
}
// Generate a random number between 1 and 3
let rnd = Math.ceil(Math.random() * 3);
return words[rnd];
};
// add pin to note
html5sticky.addPin = function (el) {
let close = $(`