buttons.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. require(['gitbook', 'jquery'], function(gitbook, $) {
  2. var SITES = {
  3. 'facebook': {
  4. 'label': 'Facebook',
  5. 'icon': 'fa fa-facebook',
  6. 'onClick': function(e) {
  7. e.preventDefault();
  8. window.open('http://www.facebook.com/sharer/sharer.php?s=100&p[url]='+encodeURIComponent(location.href));
  9. }
  10. },
  11. 'twitter': {
  12. 'label': 'Twitter',
  13. 'icon': 'fa fa-twitter',
  14. 'onClick': function(e) {
  15. e.preventDefault();
  16. window.open('http://twitter.com/home?status='+encodeURIComponent(document.title+' '+location.href));
  17. }
  18. },
  19. 'google': {
  20. 'label': 'Google+',
  21. 'icon': 'fa fa-google-plus',
  22. 'onClick': function(e) {
  23. e.preventDefault();
  24. window.open('https://plus.google.com/share?url='+encodeURIComponent(location.href));
  25. }
  26. },
  27. 'weibo': {
  28. 'label': 'Weibo',
  29. 'icon': 'fa fa-weibo',
  30. 'onClick': function(e) {
  31. e.preventDefault();
  32. window.open('http://service.weibo.com/share/share.php?content=utf-8&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title));
  33. }
  34. },
  35. 'instapaper': {
  36. 'label': 'Instapaper',
  37. 'icon': 'fa fa-instapaper',
  38. 'onClick': function(e) {
  39. e.preventDefault();
  40. window.open('http://www.instapaper.com/text?u='+encodeURIComponent(location.href));
  41. }
  42. },
  43. 'vk': {
  44. 'label': 'VK',
  45. 'icon': 'fa fa-vk',
  46. 'onClick': function(e) {
  47. e.preventDefault();
  48. window.open('http://vkontakte.ru/share.php?url='+encodeURIComponent(location.href));
  49. }
  50. }
  51. };
  52. gitbook.events.bind('start', function(e, config) {
  53. var opts = config.sharing;
  54. // Create dropdown menu
  55. var menu = $.map(opts.all, function(id) {
  56. var site = SITES[id];
  57. return {
  58. text: site.label,
  59. onClick: site.onClick
  60. };
  61. });
  62. // Create main button with dropdown
  63. if (menu.length > 0) {
  64. gitbook.toolbar.createButton({
  65. icon: 'fa fa-share-alt',
  66. label: 'Share',
  67. position: 'right',
  68. dropdown: [menu]
  69. });
  70. }
  71. // Direct actions to share
  72. $.each(SITES, function(sideId, site) {
  73. if (!opts[sideId]) return;
  74. gitbook.toolbar.createButton({
  75. icon: site.icon,
  76. label: site.text,
  77. position: 'right',
  78. onClick: site.onClick
  79. });
  80. });
  81. });
  82. });