2 // Configure ZeroClipboard
3 var zeroClipBoard = require('zeroclipboard');
5 swfPath: '/ZeroClipboard.swf'
8 window.setupPageShow = module.exports = function (pageId) {
11 var $pointer = $('#pointer').detach();
12 var $pointerInner = $pointer.children('div.pointer').first();
13 var isSelection = false;
15 // Select all contents on input click
16 $pointer.on('click', 'input', function (e) {
21 // Set up copy-to-clipboard
22 new zeroClipBoard($pointer.find('button').first()[0]);
24 // Hide pointer when clicking away
25 $(document.body).find('*').on('click focus', function (e) {
31 // Show pointer when selecting a single block of tagged content
32 $('.page-content [id^="bkmrk"]').on('mouseup keyup', function (e) {
34 var selection = window.getSelection();
35 if (selection.toString().length === 0) return;
37 // Show pointer and set link
39 var link = window.location.protocol + "//" + window.location.host + '/link/' + pageId + '#' + $elem.attr('id');
40 $pointer.find('input').val(link);
41 $pointer.find('button').first().attr('data-clipboard-text', link);
42 $elem.before($pointer);
45 // Set pointer to sit near mouse-up position
46 var pointerLeftOffset = (e.pageX - $elem.offset().left - ($pointerInner.width() / 2));
47 if (pointerLeftOffset < 0) pointerLeftOffset = 0;
48 var pointerLeftOffsetPercent = (pointerLeftOffset / $elem.width()) * 100;
49 $pointerInner.css('left', pointerLeftOffsetPercent + '%');
57 // Go to, and highlight if necessary, the specified text.
58 function goToText(text) {
59 var idElem = $('.page-content #' + text).first();
60 if (idElem.length !== 0) {
61 idElem.smoothScrollTo();
62 idElem.css('background-color', 'rgba(244, 249, 54, 0.25)');
64 $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
68 // Check the hash on load
69 if (window.location.hash) {
70 var text = window.location.hash.replace(/\%20/g, ' ').substr(1);
74 // Make the book-tree sidebar stick in view on scroll
75 var $window = $(window);
76 var $bookTree = $(".book-tree");
77 // Check the page is scrollable and the content is taller than the tree
78 var pageScrollable = ($(document).height() > $window.height()) && ($bookTree.height() < $('.page-content').height());
79 // Get current tree's width and header height
80 var headerHeight = $("#header").height() + $(".toolbar").height();
81 var isFixed = $window.scrollTop() > headerHeight;
82 var bookTreeWidth = $bookTree.width();
83 // Function to fix the tree as a sidebar
84 function stickTree() {
85 $bookTree.width(bookTreeWidth + 48 + 15);
86 $bookTree.addClass("fixed");
89 // Function to un-fix the tree back into position
90 function unstickTree() {
91 $bookTree.css('width', 'auto');
92 $bookTree.removeClass("fixed");
95 // Checks if the tree stickiness state should change
96 function checkTreeStickiness(skipCheck) {
97 var shouldBeFixed = $window.scrollTop() > headerHeight;
98 if (shouldBeFixed && (!isFixed || skipCheck)) {
100 } else if (!shouldBeFixed && (isFixed || skipCheck)) {
104 // If the page is scrollable and the window is wide enough listen to scroll events
105 // and evaluate tree stickiness.
106 if (pageScrollable && $window.width() > 1000) {
107 $window.scroll(function() {
108 checkTreeStickiness(false);
110 checkTreeStickiness(true);