buttons.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 'github': {
  20. 'label': 'Github',
  21. 'icon': 'fa fa-github',
  22. 'onClick': function(e) {
  23. e.preventDefault();
  24. window.open('https://github.com');
  25. }
  26. },
  27. 'telegram': {
  28. 'label': 'Telegram',
  29. 'icon': 'fa fa-telegram',
  30. 'onClick': function(e) {
  31. e.preventDefault();
  32. window.open('https://t.me');
  33. }
  34. },
  35. 'google': {
  36. 'label': 'Google+',
  37. 'icon': 'fa fa-google-plus',
  38. 'onClick': function(e) {
  39. e.preventDefault();
  40. window.open('https://plus.google.com/share?url='+encodeURIComponent(location.href));
  41. }
  42. },
  43. 'weibo': {
  44. 'label': 'Weibo',
  45. 'icon': 'fa fa-weibo',
  46. 'onClick': function(e) {
  47. e.preventDefault();
  48. window.open('http://service.weibo.com/share/share.php?content=utf-8&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title));
  49. }
  50. },
  51. 'instapaper': {
  52. 'label': 'Instapaper',
  53. 'icon': 'fa fa-instapaper',
  54. 'onClick': function(e) {
  55. e.preventDefault();
  56. window.open('http://www.instapaper.com/text?u='+encodeURIComponent(location.href));
  57. }
  58. },
  59. 'vk': {
  60. 'label': 'VK',
  61. 'icon': 'fa fa-vk',
  62. 'onClick': function(e) {
  63. e.preventDefault();
  64. window.open('http://vkontakte.ru/share.php?url='+encodeURIComponent(location.href));
  65. }
  66. }
  67. };
  68. gitbook.events.bind('start', function(e, config) {
  69. var opts = config.sharing;
  70. // Create dropdown menu
  71. var menu = $.map(opts.all, function(id) {
  72. var site = SITES[id];
  73. return {
  74. text: site.label,
  75. onClick: site.onClick
  76. };
  77. });
  78. // Create main button with dropdown
  79. if (menu.length > 0) {
  80. gitbook.toolbar.createButton({
  81. icon: 'fa fa-share-alt',
  82. label: 'Share',
  83. position: 'right',
  84. dropdown: [menu]
  85. });
  86. }
  87. // Direct actions to share
  88. $.each(SITES, function(sideId, site) {
  89. if (!opts[sideId]) return;
  90. var onClick = site.onClick;
  91. if (sideId === "github" && opts["github_link"] !== undefined && opts["github_link"] !== "") {
  92. onClick = function(e) {
  93. e.preventDefault();
  94. window.open(opts["github_link"]);
  95. }
  96. }
  97. if (sideId === "telegram" && opts["telegram_link"] !== undefined && opts["telegram_link"] !== "") {
  98. onClick = function(e) {
  99. e.preventDefault();
  100. window.open(opts["telegram_link"]);
  101. }
  102. }
  103. gitbook.toolbar.createButton({
  104. icon: site.icon,
  105. label: site.text,
  106. position: 'right',
  107. onClick: onClick
  108. });
  109. });
  110. });
  111. });