1 const Clipboard = require("clipboard");
2 const Code = require('../code');
4 let setupPageShow = window.setupPageShow = function (pageId) {
11 let $pointer = $('#pointer').detach();
12 let pointerShowing = false;
13 let $pointerInner = $pointer.children('div.pointer').first();
14 let isSelection = false;
15 let pointerModeLink = true;
16 let pointerSectionId = '';
18 // Select all contents on input click
19 $pointer.on('click', 'input', function (e) {
24 // Pointer mode toggle
25 $pointer.on('click', 'span.icon', event => {
26 let $icon = $(event.currentTarget);
27 pointerModeLink = !pointerModeLink;
28 $icon.html(pointerModeLink ? '<i class="zmdi zmdi-link"></i>' : '<i class="zmdi zmdi-square-down"></i>');
29 updatePointerContent();
33 let clipboard = new Clipboard('#pointer button');
35 // Hide pointer when clicking away
36 $(document.body).find('*').on('click focus', event => {
37 if (!pointerShowing || isSelection) return;
38 let target = $(event.target);
39 if (target.is('.zmdi') || $(event.target).closest('#pointer').length === 1) return;
42 pointerShowing = false;
45 function updatePointerContent() {
46 let inputText = pointerModeLink ? window.baseUrl(`/link/${pageId}#${pointerSectionId}`) : `{{@${pageId}#${pointerSectionId}}}`;
47 if (pointerModeLink && inputText.indexOf('http') !== 0) inputText = window.location.protocol + "//" + window.location.host + inputText;
49 $pointer.find('input').val(inputText);
52 // Show pointer when selecting a single block of tagged content
53 $('.page-content [id^="bkmrk"]').on('mouseup keyup', function (e) {
55 let selection = window.getSelection();
56 if (selection.toString().length === 0) return;
58 // Show pointer and set link
60 pointerSectionId = $elem.attr('id');
61 updatePointerContent();
63 $elem.before($pointer);
65 pointerShowing = true;
67 // Set pointer to sit near mouse-up position
68 let pointerLeftOffset = (e.pageX - $elem.offset().left - ($pointerInner.width() / 2));
69 if (pointerLeftOffset < 0) pointerLeftOffset = 0;
70 let pointerLeftOffsetPercent = (pointerLeftOffset / $elem.width()) * 100;
71 $pointerInner.css('left', pointerLeftOffsetPercent + '%');
79 // Go to, and highlight if necessary, the specified text.
80 function goToText(text) {
81 let idElem = document.getElementById(text);
82 $('.page-content [data-highlighted]').attr('data-highlighted', '').css('background-color', '');
83 if (idElem !== null) {
84 window.scrollAndHighlight(idElem);
86 $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
90 // Check the hash on load
91 if (window.location.hash) {
92 let text = window.location.hash.replace(/\%20/g, ' ').substr(1);
96 // Sidebar page nav click event
97 $('.sidebar-page-nav').on('click', 'a', event => {
98 goToText(event.target.getAttribute('href').substr(1));
101 // Make the sidebar stick in view on scroll
102 let $window = $(window);
103 let $sidebar = $("#sidebar .scroll-body");
104 let $bookTreeParent = $sidebar.parent();
105 // Check the page is scrollable and the content is taller than the tree
106 let pageScrollable = ($(document).height() > $window.height()) && ($sidebar.height() < $('.page-content').height());
107 // Get current tree's width and header height
108 let headerHeight = $("#header").height() + $(".toolbar").height();
109 let isFixed = $window.scrollTop() > headerHeight;
110 // Function to fix the tree as a sidebar
111 function stickTree() {
112 $sidebar.width($bookTreeParent.width() + 15);
113 $sidebar.addClass("fixed");
116 // Function to un-fix the tree back into position
117 function unstickTree() {
118 $sidebar.css('width', 'auto');
119 $sidebar.removeClass("fixed");
122 // Checks if the tree stickiness state should change
123 function checkTreeStickiness(skipCheck) {
124 let shouldBeFixed = $window.scrollTop() > headerHeight;
125 if (shouldBeFixed && (!isFixed || skipCheck)) {
127 } else if (!shouldBeFixed && (isFixed || skipCheck)) {
131 // The event ran when the window scrolls
132 function windowScrollEvent() {
133 checkTreeStickiness(false);
136 // If the page is scrollable and the window is wide enough listen to scroll events
137 // and evaluate tree stickiness.
138 if (pageScrollable && $window.width() > 1000) {
139 $window.on('scroll', windowScrollEvent);
140 checkTreeStickiness(true);
143 // Handle window resizing and switch between desktop/mobile views
144 $window.on('resize', event => {
145 if (pageScrollable && $window.width() > 1000) {
146 $window.on('scroll', windowScrollEvent);
147 checkTreeStickiness(true);
149 $window.off('scroll', windowScrollEvent);
155 module.exports = setupPageShow;