2 // Configure ZeroClipboard
3 import Clipboard from "clipboard";
5 export default window.setupPageShow = function (pageId) {
8 let $pointer = $('#pointer').detach();
9 let pointerShowing = false;
10 let $pointerInner = $pointer.children('div.pointer').first();
11 let isSelection = false;
12 let pointerModeLink = true;
13 let pointerSectionId = '';
15 // Select all contents on input click
16 $pointer.on('click', 'input', function (e) {
21 // Pointer mode toggle
22 $pointer.on('click', 'span.icon', event => {
23 let $icon = $(event.currentTarget);
24 pointerModeLink = !pointerModeLink;
25 $icon.html(pointerModeLink ? '<i class="zmdi zmdi-link"></i>' : '<i class="zmdi zmdi-square-down"></i>');
26 updatePointerContent();
30 let clipboard = new Clipboard('#pointer button');
32 // Hide pointer when clicking away
33 $(document.body).find('*').on('click focus', event => {
34 if (!pointerShowing || isSelection) return;
35 let target = $(event.target);
36 if (target.is('.zmdi') || $(event.target).closest('#pointer').length === 1) return;
39 pointerShowing = false;
42 function updatePointerContent() {
43 let inputText = pointerModeLink ? window.baseUrl(`/link/${pageId}#${pointerSectionId}`) : `{{@${pageId}#${pointerSectionId}}}`;
44 if (pointerModeLink && inputText.indexOf('http') !== 0) inputText = window.location.protocol + "//" + window.location.host + inputText;
46 $pointer.find('input').val(inputText);
49 // Show pointer when selecting a single block of tagged content
50 $('.page-content [id^="bkmrk"]').on('mouseup keyup', function (e) {
52 let selection = window.getSelection();
53 if (selection.toString().length === 0) return;
55 // Show pointer and set link
57 pointerSectionId = $elem.attr('id');
58 updatePointerContent();
60 $elem.before($pointer);
62 pointerShowing = true;
64 // Set pointer to sit near mouse-up position
65 let pointerLeftOffset = (e.pageX - $elem.offset().left - ($pointerInner.width() / 2));
66 if (pointerLeftOffset < 0) pointerLeftOffset = 0;
67 let pointerLeftOffsetPercent = (pointerLeftOffset / $elem.width()) * 100;
68 $pointerInner.css('left', pointerLeftOffsetPercent + '%');
76 // Go to, and highlight if necessary, the specified text.
77 function goToText(text) {
78 let idElem = document.getElementById(text);
79 $('.page-content [data-highlighted]').attr('data-highlighted', '').css('background-color', '');
80 if (idElem !== null) {
81 let $idElem = $(idElem);
82 let color = $('#custom-styles').attr('data-color-light');
83 $idElem.css('background-color', color).attr('data-highlighted', 'true').smoothScrollTo();
85 $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
89 // Check the hash on load
90 if (window.location.hash) {
91 let text = window.location.hash.replace(/\%20/g, ' ').substr(1);
95 // Sidebar page nav click event
96 $('.sidebar-page-nav').on('click', 'a', event => {
97 goToText(event.target.getAttribute('href').substr(1));
100 // Make the book-tree sidebar stick in view on scroll
101 let $window = $(window);
102 let $bookTree = $(".book-tree");
103 let $bookTreeParent = $bookTree.parent();
104 // Check the page is scrollable and the content is taller than the tree
105 let pageScrollable = ($(document).height() > $window.height()) && ($bookTree.height() < $('.page-content').height());
106 // Get current tree's width and header height
107 let headerHeight = $("#header").height() + $(".toolbar").height();
108 let isFixed = $window.scrollTop() > headerHeight;
109 // Function to fix the tree as a sidebar
110 function stickTree() {
111 $bookTree.width($bookTreeParent.width() + 15);
112 $bookTree.addClass("fixed");
115 // Function to un-fix the tree back into position
116 function unstickTree() {
117 $bookTree.css('width', 'auto');
118 $bookTree.removeClass("fixed");
121 // Checks if the tree stickiness state should change
122 function checkTreeStickiness(skipCheck) {
123 let shouldBeFixed = $window.scrollTop() > headerHeight;
124 if (shouldBeFixed && (!isFixed || skipCheck)) {
126 } else if (!shouldBeFixed && (isFixed || skipCheck)) {
130 // The event ran when the window scrolls
131 function windowScrollEvent() {
132 checkTreeStickiness(false);
135 // If the page is scrollable and the window is wide enough listen to scroll events
136 // and evaluate tree stickiness.
137 if (pageScrollable && $window.width() > 1000) {
138 $window.on('scroll', windowScrollEvent);
139 checkTreeStickiness(true);
142 // Handle window resizing and switch between desktop/mobile views
143 $window.on('resize', event => {
144 if (pageScrollable && $window.width() > 1000) {
145 $window.on('scroll', windowScrollEvent);
146 checkTreeStickiness(true);
148 $window.off('scroll', windowScrollEvent);