2 // Configure ZeroClipboard
3 const Clipboard = require("clipboard");
5 let setupPageShow = 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 $idElem.addClass('anim').addClass('selectFade').css('background-color', '');
87 $idElem.removeClass('selectFade');
91 $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
95 // Check the hash on load
96 if (window.location.hash) {
97 let text = window.location.hash.replace(/\%20/g, ' ').substr(1);
101 // Sidebar page nav click event
102 $('.sidebar-page-nav').on('click', 'a', event => {
103 goToText(event.target.getAttribute('href').substr(1));
106 // Make the book-tree sidebar stick in view on scroll
107 let $window = $(window);
108 let $bookTree = $(".book-tree");
109 let $bookTreeParent = $bookTree.parent();
110 // Check the page is scrollable and the content is taller than the tree
111 let pageScrollable = ($(document).height() > $window.height()) && ($bookTree.height() < $('.page-content').height());
112 // Get current tree's width and header height
113 let headerHeight = $("#header").height() + $(".toolbar").height();
114 let isFixed = $window.scrollTop() > headerHeight;
115 // Function to fix the tree as a sidebar
116 function stickTree() {
117 $bookTree.width($bookTreeParent.width() + 15);
118 $bookTree.addClass("fixed");
121 // Function to un-fix the tree back into position
122 function unstickTree() {
123 $bookTree.css('width', 'auto');
124 $bookTree.removeClass("fixed");
127 // Checks if the tree stickiness state should change
128 function checkTreeStickiness(skipCheck) {
129 let shouldBeFixed = $window.scrollTop() > headerHeight;
130 if (shouldBeFixed && (!isFixed || skipCheck)) {
132 } else if (!shouldBeFixed && (isFixed || skipCheck)) {
136 // The event ran when the window scrolls
137 function windowScrollEvent() {
138 checkTreeStickiness(false);
141 // If the page is scrollable and the window is wide enough listen to scroll events
142 // and evaluate tree stickiness.
143 if (pageScrollable && $window.width() > 1000) {
144 $window.on('scroll', windowScrollEvent);
145 checkTreeStickiness(true);
148 // Handle window resizing and switch between desktop/mobile views
149 $window.on('resize', event => {
150 if (pageScrollable && $window.width() > 1000) {
151 $window.on('scroll', windowScrollEvent);
152 checkTreeStickiness(true);
154 $window.off('scroll', windowScrollEvent);
161 module.exports = setupPageShow;