]> BookStack Code Mirror - bookstack/blob - resources/assets/js/pages/page-show.js
Added comment reply and delete confirmation.
[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     // 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");
114         isFixed = true;
115     }
116     // Function to un-fix the tree back into position
117     function unstickTree() {
118         $sidebar.css('width', 'auto');
119         $sidebar.removeClass("fixed");
120         isFixed = false;
121     }
122     // Checks if the tree stickiness state should change
123     function checkTreeStickiness(skipCheck) {
124         let shouldBeFixed = $window.scrollTop() > headerHeight;
125         if (shouldBeFixed && (!isFixed || skipCheck)) {
126             stickTree();
127         } else if (!shouldBeFixed && (isFixed || skipCheck)) {
128             unstickTree();
129         }
130     }
131     // The event ran when the window scrolls
132     function windowScrollEvent() {
133         checkTreeStickiness(false);
134     }
135
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);
141     }
142
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);
148         } else {
149             $window.off('scroll', windowScrollEvent);
150             unstickTree();
151         }
152     });
153 };
154
155 module.exports = setupPageShow;