2 // Configure ZeroClipboard
3 var zeroClipBoard = require('zeroclipboard');
5 swfPath: window.baseUrl('/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 let link = window.baseUrl('/link/' + pageId + '#' + $elem.attr('id'));
40 if (link.indexOf('http') !== 0) link = window.location.protocol + "//" + window.location.host + link;
41 $pointer.find('input').val(link);
42 $pointer.find('button').first().attr('data-clipboard-text', link);
43 $elem.before($pointer);
46 // Set pointer to sit near mouse-up position
47 var pointerLeftOffset = (e.pageX - $elem.offset().left - ($pointerInner.width() / 2));
48 if (pointerLeftOffset < 0) pointerLeftOffset = 0;
49 var pointerLeftOffsetPercent = (pointerLeftOffset / $elem.width()) * 100;
50 $pointerInner.css('left', pointerLeftOffsetPercent + '%');
58 // Go to, and highlight if necessary, the specified text.
59 function goToText(text) {
60 var idElem = $('.page-content #' + text).first();
61 if (idElem.length !== 0) {
62 idElem.smoothScrollTo();
63 idElem.css('background-color', 'rgba(244, 249, 54, 0.25)');
65 $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
69 // Check the hash on load
70 if (window.location.hash) {
71 var text = window.location.hash.replace(/\%20/g, ' ').substr(1);
75 // Make the book-tree sidebar stick in view on scroll
76 var $window = $(window);
77 var $bookTree = $(".book-tree");
78 var $bookTreeParent = $bookTree.parent();
79 // Check the page is scrollable and the content is taller than the tree
80 var pageScrollable = ($(document).height() > $window.height()) && ($bookTree.height() < $('.page-content').height());
81 // Get current tree's width and header height
82 var headerHeight = $("#header").height() + $(".toolbar").height();
83 var isFixed = $window.scrollTop() > headerHeight;
84 // Function to fix the tree as a sidebar
85 function stickTree() {
86 $bookTree.width($bookTreeParent.width() + 15);
87 $bookTree.addClass("fixed");
90 // Function to un-fix the tree back into position
91 function unstickTree() {
92 $bookTree.css('width', 'auto');
93 $bookTree.removeClass("fixed");
96 // Checks if the tree stickiness state should change
97 function checkTreeStickiness(skipCheck) {
98 var shouldBeFixed = $window.scrollTop() > headerHeight;
99 if (shouldBeFixed && (!isFixed || skipCheck)) {
101 } else if (!shouldBeFixed && (isFixed || skipCheck)) {
105 // The event ran when the window scrolls
106 function windowScrollEvent() {
107 checkTreeStickiness(false);
110 // If the page is scrollable and the window is wide enough listen to scroll events
111 // and evaluate tree stickiness.
112 if (pageScrollable && $window.width() > 1000) {
113 $window.on('scroll', windowScrollEvent);
114 checkTreeStickiness(true);
117 // Handle window resizing and switch between desktop/mobile views
118 $window.on('resize', event => {
119 if (pageScrollable && $window.width() > 1000) {
120 $window.on('scroll', windowScrollEvent);
121 checkTreeStickiness(true);
123 $window.off('scroll', windowScrollEvent);