contentshare.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (function(){
  2. $.fn.share = function(options) {
  3. var option = $.extend($.fn.share.defaults,options);
  4. $.extend($.fn.share,{
  5. init : function(shareable) {
  6. var self = this;
  7. $.fn.share.defaults.shareable = shareable;
  8. $.fn.share.defaults.shareable.on('mouseup',function(){
  9. self.popOver();
  10. });
  11. $.fn.share.defaults.shareable.on('mousedown',function(){
  12. self.destroy();
  13. });
  14. },
  15. getContent : function() {
  16. var current_url = window.location.href
  17. var selected_text = this.getSelection('string').substring(0,option.maxLength-(current_url.length+option.author_name.length+7));
  18. var text = encodeURIComponent('\"'+selected_text+'\" '+'--@'+option.author_name+' '+current_url)
  19. return '<a onclick="window.open(\''+option.shareLink+text+'\',\'_'+option.target+'\',\'location=yes,height=570,width=520,scrollbars=yes,status=yes\')"><i class="fa fa-twitter fa-lg"/></a>';
  20. },
  21. getSelection : function(share) {
  22. if(window.getSelection){
  23. return (share=='string')?String(window.getSelection().getRangeAt(0)).replace(/\s{2,}/g, ' '):window.getSelection().getRangeAt(0);
  24. }
  25. else if(document.selection){
  26. return (share=='string')?document.selection.createRange().text.replace(/\s{2,}/g, ' '):document.selection.createRange();
  27. }
  28. },
  29. popOver : function() {
  30. this.destroy();
  31. if(this.getSelection('string').length < option.minLength)
  32. return;
  33. var data = this.getContent();
  34. var range = this.getSelection();
  35. var newNode = document.createElement("mark");
  36. range.surroundContents(newNode);
  37. $('mark').addClass(option.className);
  38. $('.'+option.className).popover({trigger:'manual', placement: option.placement, html: true
  39. , content:function(){
  40. return data;
  41. }
  42. });
  43. $('.'+option.className).popover('show');
  44. },
  45. destroy : function(){
  46. $('.'+option.className).popover('hide');
  47. $('mark').contents().unwrap();
  48. $('mark').remove();
  49. }
  50. });
  51. $.fn.share.init(this);
  52. };
  53. $.fn.share.defaults = {
  54. shareLink : "http://twitter.com/intent/tweet?text=",
  55. minLength : 5,
  56. maxLength : 140,
  57. target : "blank",
  58. className : "share",
  59. placement : "top",
  60. };
  61. }());