]> BookStack Code Mirror - bookstack/blob - resources/assets/js/pages/page-show.js
Updated version for v0.15.3
[bookstack] / resources / assets / js / pages / page-show.js
1 "use strict";
2 // Configure ZeroClipboard
3 import Clipboard from "clipboard";
4
5 export default window.setupPageShow = function (pageId) {
6
7     // Set up pointer
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 = '';
14
15     // Select all contents on input click
16     $pointer.on('click', 'input', function (e) {
17         $(this).select();
18         e.stopPropagation();
19     });
20
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();
27     });
28
29     // Set up clipboard
30     let clipboard = new Clipboard('#pointer button');
31
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;
37
38         $pointer.detach();
39         pointerShowing = false;
40     });
41
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;
45
46         $pointer.find('input').val(inputText);
47     }
48
49     // Show pointer when selecting a single block of tagged content
50     $('.page-content [id^="bkmrk"]').on('mouseup keyup', function (e) {
51         e.stopPropagation();
52         let selection = window.getSelection();
53         if (selection.toString().length === 0) return;
54
55         // Show pointer and set link
56         let $elem = $(this);
57         pointerSectionId = $elem.attr('id');
58         updatePointerContent();
59
60         $elem.before($pointer);
61         $pointer.show();
62         pointerShowing = true;
63
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 + '%');
69
70         isSelection = true;
71         setTimeout(() => {
72             isSelection = false;
73         }, 100);
74     });
75
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();
84         } else {
85             $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
86         }
87     }
88
89     // Check the hash on load
90     if (window.location.hash) {
91         let text = window.location.hash.replace(/\%20/g, ' ').substr(1);
92         goToText(text);
93     }
94
95     // Sidebar page nav click event
96     $('.sidebar-page-nav').on('click', 'a', event => {
97         goToText(event.target.getAttribute('href').substr(1));
98     });
99
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");
113         isFixed = true;
114     }
115     // Function to un-fix the tree back into position
116     function unstickTree() {
117         $bookTree.css('width', 'auto');
118         $bookTree.removeClass("fixed");
119         isFixed = false;
120     }
121     // Checks if the tree stickiness state should change
122     function checkTreeStickiness(skipCheck) {
123         let shouldBeFixed = $window.scrollTop() > headerHeight;
124         if (shouldBeFixed && (!isFixed || skipCheck)) {
125             stickTree();
126         } else if (!shouldBeFixed && (isFixed || skipCheck)) {
127             unstickTree();
128         }
129     }
130     // The event ran when the window scrolls
131     function windowScrollEvent() {
132         checkTreeStickiness(false);
133     }
134
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);
140     }
141
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);
147         } else {
148             $window.off('scroll', windowScrollEvent);
149             unstickTree();
150         }
151     });
152
153 };