|
//FROM http://ad1987.blogspot.com/2010/04/howto-create-facebook-status-message.html |
|
function stripHTML(source){ |
|
var strippedText = source.replace(/<\/?[^>]+(>|$)/g, ""); |
|
return strippedText |
|
} |
|
function replaceURLWithHTMLLinks(source) { |
|
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; |
|
return source.replace(exp,"<a href='$1' target='_blank'>$1</a>"); |
|
} |
|
|
|
// Deal With the shares |
|
function add_to_db(message){ |
|
d = {}; |
|
d['message'] = message; |
|
$.ajax({ |
|
type: 'POST', |
|
url: '/ajax', |
|
data: d, |
|
success: function(json){ if (window.console && window.console.firebug) console.log(json.error);}, |
|
dataType: 'json' |
|
}); |
|
} |
|
|
|
//Add the message to to the share list |
|
function add_to_shares(message, embed){ |
|
var post = '<li><p>'+replaceURLWithHTMLLinks(message)+'</p>'; |
|
if (embed != null) |
|
post += embed; |
|
post += '</li>'; |
|
$('#shares').prepend(post); |
|
add_to_db(message); |
|
} |
|
|
|
$(document).ready(function() { |
|
//Bind Handler |
|
$('#sharebox .sharebutton').bind('click', function(e){ |
|
e.preventDefault(); |
|
var text = stripHTML($('#sharebox .sharecontent').val()); |
|
|
|
//If there is no content in the box just return. |
|
if (!text || !text.replace(/\s/g, '')) |
|
return false |
|
|
|
// Find all the urls in the post |
|
var exp = /(\bhttp:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; |
|
var urls = text.match(exp); |
|
// We only want one link. |
|
if (urls != null && urls.length > 1){ |
|
return false |
|
}else if(urls != null){ |
|
//Call Embedly |
|
$.embedly(urls[0], {maxWidth: 480, wrapElement: 'div', method : "afterParent"}, |
|
function(oembed){ |
|
if (oembed != null) |
|
add_to_shares(text, oembed.code); |
|
else |
|
add_to_shares(text) |
|
}); |
|
} else { |
|
//No Url just add the text. |
|
add_to_shares(text, null); |
|
} |
|
}); |
|
//Deal with the links already on the page. |
|
$('#shares').embedly({maxWidth: 480, wrapElement: 'div', method : "afterParent" }); |
|
}); |