plugin-sharing.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. gitbook.require(["gitbook", "lodash", "jQuery"], function(gitbook, _, $) {
  2. var SITES = {
  3. 'github': {
  4. 'label': 'Github',
  5. 'icon': 'fa fa-github',
  6. 'onClick': function(e) {
  7. e.preventDefault();
  8. var repo = $('meta[name="github-repo"]').attr('content');
  9. if (typeof repo === 'undefined') throw("Github repo not defined");
  10. window.open("https://github.com/"+repo);
  11. }
  12. },
  13. 'facebook': {
  14. 'label': 'Facebook',
  15. 'icon': 'fa fa-facebook',
  16. 'onClick': function(e) {
  17. e.preventDefault();
  18. window.open("http://www.facebook.com/sharer/sharer.php?u="+encodeURIComponent(location.href));
  19. }
  20. },
  21. 'twitter': {
  22. 'label': 'Twitter',
  23. 'icon': 'fa fa-twitter',
  24. 'onClick': function(e) {
  25. e.preventDefault();
  26. window.open("http://twitter.com/intent/tweet?text="+document.title+"&url="+encodeURIComponent(location.href)+"&hashtags=rmarkdown,bookdown");
  27. }
  28. },
  29. 'linkedin': {
  30. 'label': 'LinkedIn',
  31. 'icon': 'fa fa-linkedin',
  32. 'onClick': function(e) {
  33. e.preventDefault();
  34. window.open("https://www.linkedin.com/shareArticle?mini=true&url="+encodeURIComponent(location.href)+"&title="+encodeURIComponent(document.title));
  35. }
  36. },
  37. 'weibo': {
  38. 'label': 'Weibo',
  39. 'icon': 'fa fa-weibo',
  40. 'onClick': function(e) {
  41. e.preventDefault();
  42. window.open("http://service.weibo.com/share/share.php?content=utf-8&url="+encodeURIComponent(location.href)+"&title="+encodeURIComponent(document.title));
  43. }
  44. },
  45. 'instapaper': {
  46. 'label': 'Instapaper',
  47. 'icon': 'fa fa-italic',
  48. 'onClick': function(e) {
  49. e.preventDefault();
  50. window.open("http://www.instapaper.com/text?u="+encodeURIComponent(location.href));
  51. }
  52. },
  53. 'vk': {
  54. 'label': 'VK',
  55. 'icon': 'fa fa-vk',
  56. 'onClick': function(e) {
  57. e.preventDefault();
  58. window.open("http://vkontakte.ru/share.php?url="+encodeURIComponent(location.href));
  59. }
  60. }
  61. };
  62. gitbook.events.bind("start", function(e, config) {
  63. var opts = config.sharing;
  64. if (!opts) return;
  65. // Create dropdown menu
  66. var menu = _.chain(opts.all)
  67. .map(function(id) {
  68. var site = SITES[id];
  69. if (!site) return;
  70. return {
  71. text: site.label,
  72. onClick: site.onClick
  73. };
  74. })
  75. .compact()
  76. .value();
  77. // Create main button with dropdown
  78. if (menu.length > 0) {
  79. gitbook.toolbar.createButton({
  80. icon: 'fa fa-share-alt',
  81. label: 'Share',
  82. position: 'right',
  83. dropdown: [menu]
  84. });
  85. }
  86. // Direct actions to share
  87. _.each(SITES, function(site, sideId) {
  88. if (!opts[sideId]) return;
  89. gitbook.toolbar.createButton({
  90. icon: site.icon,
  91. label: site.label,
  92. title: site.label,
  93. position: 'right',
  94. onClick: site.onClick
  95. });
  96. });
  97. });
  98. });