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