]> BookStack Code Mirror - bookstack/blob - resources/assets/js/pages/page-show.js
Refactored the code to first check for the permissions before sorting the book.
[bookstack] / resources / assets / js / pages / page-show.js
1 const Clipboard = require("clipboard");
2 const Code = require('../code');
3
4 let setupPageShow = window.setupPageShow = function (pageId) {
5
6     Code.highlight();
7
8     if (!pageId) return;
9
10     // Set up pointer
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 = '';
17
18     // Select all contents on input click
19     $pointer.on('click', 'input', function (e) {
20         $(this).select();
21         e.stopPropagation();
22     });
23
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();
30     });
31
32     // Set up clipboard
33     let clipboard = new Clipboard('#pointer button');
34
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;
40
41         $pointer.detach();
42         pointerShowing = false;
43     });
44
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;
48
49         $pointer.find('input').val(inputText);
50     }
51
52     // Show pointer when selecting a single block of tagged content
53     $('.page-content [id^="bkmrk"]').on('mouseup keyup', function (e) {
54         e.stopPropagation();
55         let selection = window.getSelection();
56         if (selection.toString().length === 0) return;
57
58         // Show pointer and set link
59         let $elem = $(this);
60         pointerSectionId = $elem.attr('id');
61         updatePointerContent();
62
63         $elem.before($pointer);
64         $pointer.show();
65         pointerShowing = true;
66
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 + '%');
72
73         isSelection = true;
74         setTimeout(() => {
75             isSelection = false;
76         }, 100);
77     });
78
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);
85         } else {
86             $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
87         }
88     }
89
90     // Check the hash on load
91     if (window.location.hash) {
92         let text = window.location.hash.replace(/\%20/g, ' ').substr(1);
93         goToText(text);
94     }
95
96     // Sidebar page nav click event
97     $('.sidebar-page-nav').on('click', 'a', event => {
98         goToText(event.target.getAttribute('href').substr(1));
99     });
100
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
106     // Check the page is scrollable and the content is taller than the tree
107     let pageScrollable = ($(document).height() > $window.height()) && ($sidebar.height() < $('.page-content').height());
108
109     // Get current tree's width and header height
110     let headerHeight = $("#header").height() + $(".toolbar").height();
111     let isFixed = $window.scrollTop() > headerHeight;
112
113     // Fix the tree as a sidebar
114     function stickTree() {
115         $sidebar.width($bookTreeParent.width() + 15);
116         $sidebar.addClass("fixed");
117         isFixed = true;
118     }
119
120     // Un-fix the tree back into position
121     function unstickTree() {
122         $sidebar.css('width', 'auto');
123         $sidebar.removeClass("fixed");
124         isFixed = false;
125     }
126
127     // Checks if the tree stickiness state should change
128     function checkTreeStickiness(skipCheck) {
129         let shouldBeFixed = $window.scrollTop() > headerHeight;
130         if (shouldBeFixed && (!isFixed || skipCheck)) {
131             stickTree();
132         } else if (!shouldBeFixed && (isFixed || skipCheck)) {
133             unstickTree();
134         }
135     }
136     // The event ran when the window scrolls
137     function windowScrollEvent() {
138         checkTreeStickiness(false);
139     }
140
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);
146     }
147
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);
153         } else {
154             $window.off('scroll', windowScrollEvent);
155             unstickTree();
156         }
157     });
158
159
160     // Check if support is present for IntersectionObserver
161     if ('IntersectionObserver' in window &&
162         'IntersectionObserverEntry' in window &&
163         'intersectionRatio' in window.IntersectionObserverEntry.prototype) {
164         addPageHighlighting();
165     }
166
167     function addPageHighlighting() {
168       let pageNav = document.querySelector('.sidebar-page-nav');
169
170       // fetch all the headings.
171       let headings = document.querySelector('.page-content').querySelectorAll('h1, h2, h3, h4, h5, h6');
172       // if headings are present, add observers.
173       if (headings.length > 0 && pageNav !== null) {
174           addNavObserver(headings);
175       }
176
177       function addNavObserver(headings) {
178           // Setup the intersection observer.
179           let intersectOpts = {
180               rootMargin: '0px 0px 0px 0px',
181               threshold: 1.0
182           };
183           let pageNavObserver = new IntersectionObserver(headingVisibilityChange, intersectOpts);
184
185           // observe each heading
186           for (let i = 0; i !== headings.length; ++i) {
187               pageNavObserver.observe(headings[i]);
188           }
189       }
190
191       function headingVisibilityChange(entries, observer) {
192           for (let i = 0; i < entries.length; i++) {
193               let currentEntry = entries[i];
194               let isVisible = (currentEntry.intersectionRatio === 1);
195               toggleAnchorHighlighting(currentEntry.target.id, isVisible);
196           }
197       }
198
199         function toggleAnchorHighlighting(elementId, shouldHighlight) {
200             let anchorsToHighlight = pageNav.querySelectorAll('a[href="#' + elementId + '"]');
201             for (let i = 0; i < anchorsToHighlight.length; i++) {
202                 // Change below to use classList.toggle when IE support is dropped.
203                 if (shouldHighlight) {
204                     anchorsToHighlight[i].classList.add('current-heading');
205                 } else {
206                     anchorsToHighlight[i].classList.remove('current-heading');
207                 }
208             }
209         }
210     }
211 };
212
213 module.exports = setupPageShow;