/*
HTML5STICKY (http://github.com/sarfraznawaz2005/HTML5Sticky)
================================================================
Author : Sarfraz Ahmed (sarfraznawaz2005@gmail.com)
Twitter : @sarfraznawaz
Blog : http://sarfraznawaz.wordpress.com/
LICENSE : MIT
================================================================
*/
var stickywidth = 200; // width of sticky note (can't be less than 200)
var stickyheight = 200; // height of sticky note (can't be less than 200)
var max_notes = 500; // maximum number of notes one can store
var allowed_tags = '
Text here...
')); // 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'}); if (! $("#removenotes").is(':visible')){ $('#removenotes').slideDown('slow'); } //$(stickynote).find('h2').attr('contentEditable', true); //$(stickynote).find('p').attr('contentEditable', true); // add utility buttons //html5sticky.addUtilityButtons(stickynote); // scroll to newly added sticky note $('html, body').animate({ scrollTop:$(stickynote).offset().top }); // store note info local storage if (Modernizr.localstorage){ localStorage.setItem(nindex, nindex); localStorage.setItem(nindex + '|pos', parseInt($(stickynote).offset().left, 10) + '|' + parseInt($(stickynote).offset().top, 10)); localStorage.setItem(nindex + '|text', $(stickynote).find('h2').text() + '|' + $(stickynote).find('p').text()); localStorage.setItem(nindex + '|bgcolor', bgcolor); localStorage.setItem(nindex + '|dated', dated + '|' + html5sticky.getISODateTime(dateStr)); } else { html5sticky.nohtml5(); } }; // no html5 ? html5sticky.nohtml5 = function(){ html5sticky.showMessage('#FFE16B', 'black', 'Your browser does not support html5, please upgrade your browser.'); }; // adds utility buttons on note footer html5sticky.addUtilityButtons = function(el){ $el = $(el); $el.append(''); $('.icons-footer').append('')
.css({
position:'absolute',
bottom:10,
left:15
})
.appendTo($clone);
var tags = allowed_tags;
tags = tags.replace('
', '');
// remove ending tags
tags = tags.match(/<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)/ig);
$('.tip').text($('.tip').text() + tags.join(' '));
};
// get all notes
html5sticky.getNotes = function(){
if (Modernizr.localstorage){
for(var i = 1; i <= max_notes; i++){
if (localStorage.getItem(i + 'stickynote')){
//console.log(i + 'stickynote');
note_index = i;
var stickynote, bgcolor, htext, ptext, pleft, ptop, temp_array;
// get color and rotation level
bgcolor = localStorage.getItem(i + 'stickynote' + '|bgcolor');
// get position info
temp_array = localStorage.getItem(i + 'stickynote' + '|pos').split('|');
pleft = temp_array[0];
ptop = temp_array[1];
// get text info
temp_array = localStorage.getItem(i + 'stickynote' + '|text').split('|');
htext = temp_array[0];
ptext = temp_array[1];
stickynote = $('').appendTo($('#main'));
// add tape to stickynote
html5sticky.addPin(stickynote);
$(stickynote).append($('Sticky Note
'));
$(stickynote).append($('Text here...
'));
// append identifier
$(stickynote).append($(''));
//$(stickynote).find('h2').attr('contentEditable', true);
//$(stickynote).find('p').attr('contentEditable', true);
$(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 + 9) + 'px'});
// add utility buttons
//html5sticky.addUtilityButtons(stickynote);
}
}
if (! $('.note_common').length > 0){
$('#removenotes').hide();
}
}
else {
html5sticky.nohtml5();
}
};
// email note
html5sticky.emailNote = function(){
};
// share note
html5sticky.shareNote = function(){
};
// shrink notes
html5sticky.shrink = function(){
var height = 0 + parseInt($('.note_common:first').find('h2').height(), 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){
if (!$('#smsg').is(':visible'))
{
$('html, body').animate({
scrollTop: 0
}, 500, function(){
if (!$('#smsg').length)
{
$(''+msg+'').appendTo($('body')).css({
position:'absolute',
top:0,
left:3,
width:'98%',
height:'30px',
lineHeight:'30px',
background:bgcolor,
color:color,
zIndex:1000,
padding:'10px',
fontWeight:'bold',
textAlign:'center',
opacity:0.9,
margin:'auto',
display:'none'
}).slideDown('show');
setTimeout(function(){
$('#smsg').animate({'width':'hide'}, function(){
$('#smsg').remove();
});
}, 4000);
}
});
}
};
// get random color
html5sticky.getColor = function(){
var text = "";
var possible = "0123456789";
text += possible.charAt(Math.floor(Math.random() * possible.length));
return 'stickynote' + text;
};
// get random animation string
html5sticky.getAnimation = function(hideAnimation){
var words = new Array();
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
var rnd = Math.ceil(Math.random() * 3);
return words[rnd];
};
// add pin to note
html5sticky.addPin = function(el){
var close = $('');
var tag = $('
');
$(close).css({
position:'absolute',
top:-15,
right:-15
}).prependTo($(el));
$(tag).css({
position:'absolute',
zIndex:99,
top:-30,
left:parseInt(stickywidth / 2, 10) - 10
}).prependTo($(el));
};
// enlarge note for editing
html5sticky.enlargeNote = function(el){
$this = $(el);
// get initial position
var posLeft = parseInt($(el).offset().left, 10);
var posTop = parseInt($(el).offset().top, 10);
// create overlay
$('').css({
position:'fixed',
background:'transparent',
background:'rgba(0,0,0,0.5)',
top:'0',
left:'0',
width:'100%',
height:'100%',
zIndex:'100'
}).appendTo($('body'));
$clone = $(el).clone().removeClass('note_common').addClass('bignote').appendTo($('#overlay'));
// remove the pin
$clone.find($('img[src*="pin.png"]').closest('div')).hide();
// change delete button title
$clone.find($('img[src*="delete.png"]').closest('div')).hide();
$($clone).css({
position:'absolute',
zIndex: 500,
cursor:'default',
paddingTop:'5px',
top:posTop,
left:posLeft,
width:'600px',
height:'400px',
top:'50%',
left:'50%',
display:'none',
marginLeft:'-300px',
marginTop:'-200px'
});
$($clone)[html5sticky.getAnimation()](400);
// add date and time info
var dateStr = '', dateAgo = '';
if (Modernizr.localstorage){
var identifier = html5sticky.getIdentifier($(el));
var dateTime = localStorage.getItem(identifier + '|dated');
var timeImg = '
';
dateStr = dateTime.split('|')[0];
dateAgo = prettyDate(dateTime.split('|')[1]);
dateStr = (dateStr.length > 0) ? 'Created on: ' + dateStr : '';
dateAgo = (dateAgo.length > 0) ? ' (' + dateAgo + ')' : '';
timeImg = (dateStr.length > 0) ? timeImg : '';
}
else {
html5sticky.nohtml5();
}
$('').prependTo($clone);
$('.timeago').css({fontSize: '12px', fontFamily:'tahoma'}).html(timeImg + ' ' + dateStr + dateAgo);
$('.timeago').after('');
// hide the utility buttons
$($clone).find('.icons-footer').hide();
// make content editable
html5sticky.editNote($clone, el);
};
html5sticky.setup = function(){
var fontFamily = $('.note_common p').css('font-family');
// Architects font needs to have lesser line height / spacing for lists
if (typeof fontFamily != 'undefined' && fontFamily.indexOf('Architects') >= 0){
$('.note_common ul').css({lineHeight:'10px', padding:'0px', margin:'0px'});
$('.note_common ol').css({lineHeight:'0px', padding:'0px', margin:'0px'});
}
// sticky notes can't be less than 200x200
if (stickywidth < 200){
stickywidth = 200;
}
if (stickyheight < 200){
stickyheight = 200;
}
};
// http://phpjs.org/functions/strip_tags:535
html5sticky.stripTags = function(input, allowed) {
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase ()
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
// jQuery Play
$(function(){
// initial setup
html5sticky.setup();
// get any saved notes on page load
html5sticky.getNotes();
// add note
$('#addnote').click(function(){
html5sticky.addNote();
return false;
});
// delete all notes
$('#removenotes').click(function(){
html5sticky.deleteAllNotes();
return false;
});
// delete note
$('.delete_stickynote').live('click', function(){
html5sticky.deleteNote($(this));
return false;
});
// close enlarged note
$('.close_stickynote').live('click', function(){
html5sticky.closeNote($(this));
return false;
});
// save the note
$('.save_stickynote').live('click', function(){
html5sticky.saveNote($(this));
return false;
});
// enlarge the note
$(".note_common").live("click", function(event) {
html5sticky.enlargeNote($(this));
});
// shrink the notes
$('#shrink').click(function(event) {
html5sticky.shrink();
});
// expand the notes
$('#expand').click(function(event) {
html5sticky.expand();
});
// allow escape to close big note
$(document).keyup(function(e) {
if (e.keyCode == "27") {
$('#overlay').remove();
$('.bignore').remove();
}
});
});