]> BookStack Code Mirror - bookstack/blob - resources/assets/js/pages/page-show.js
Updated all application urls to allow path prefix.
[bookstack] / resources / assets / js / pages / page-show.js
1 "use strict";
2 // Configure ZeroClipboard
3 var zeroClipBoard = require('zeroclipboard');
4 zeroClipBoard.config({
5     swfPath: window.baseUrl('/ZeroClipboard.swf')
6 });
7
8 window.setupPageShow = module.exports = function (pageId) {
9
10     // Set up pointer
11     var $pointer = $('#pointer').detach();
12     var $pointerInner = $pointer.children('div.pointer').first();
13     var isSelection = false;
14
15     // Select all contents on input click
16     $pointer.on('click', 'input', function (e) {
17         $(this).select();
18         e.stopPropagation();
19     });
20
21     // Set up copy-to-clipboard
22     new zeroClipBoard($pointer.find('button').first()[0]);
23
24     // Hide pointer when clicking away
25     $(document.body).find('*').on('click focus', function (e) {
26         if (!isSelection) {
27             $pointer.detach();
28         }
29     });
30
31     // Show pointer when selecting a single block of tagged content
32     $('.page-content [id^="bkmrk"]').on('mouseup keyup', function (e) {
33         e.stopPropagation();
34         var selection = window.getSelection();
35         if (selection.toString().length === 0) return;
36
37         // Show pointer and set link
38         var $elem = $(this);
39         let link = window.baseUrl('/link/' + pageId + '#' + $elem.attr('id'));
40         if (link.indexOf('http') !== 0) link = window.location.protocol + "//" + window.location.host + link;
41         $pointer.find('input').val(link);
42         $pointer.find('button').first().attr('data-clipboard-text', link);
43         $elem.before($pointer);
44         $pointer.show();
45
46         // Set pointer to sit near mouse-up position
47         var pointerLeftOffset = (e.pageX - $elem.offset().left - ($pointerInner.width() / 2));
48         if (pointerLeftOffset < 0) pointerLeftOffset = 0;
49         var pointerLeftOffsetPercent = (pointerLeftOffset / $elem.width()) * 100;
50         $pointerInner.css('left', pointerLeftOffsetPercent + '%');
51
52         isSelection = true;
53         setTimeout(() => {
54             isSelection = false;
55         }, 100);
56     });
57
58     // Go to, and highlight if necessary, the specified text.
59     function goToText(text) {
60         var idElem = $('.page-content #' + text).first();
61         if (idElem.length !== 0) {
62             idElem.smoothScrollTo();
63             idElem.css('background-color', 'rgba(244, 249, 54, 0.25)');
64         } else {
65             $('.page-content').find(':contains("' + text + '")').smoothScrollTo();
66         }
67     }
68
69     // Check the hash on load
70     if (window.location.hash) {
71         var text = window.location.hash.replace(/\%20/g, ' ').substr(1);
72         goToText(text);
73     }
74
75     // Make the book-tree sidebar stick in view on scroll
76     var $window = $(window);
77     var $bookTree = $(".book-tree");
78     var $bookTreeParent = $bookTree.parent();
79     // Check the page is scrollable and the content is taller than the tree
80     var pageScrollable = ($(document).height() > $window.height()) && ($bookTree.height() < $('.page-content').height());
81     // Get current tree's width and header height
82     var headerHeight = $("#header").height() + $(".toolbar").height();
83     var isFixed = $window.scrollTop() > headerHeight;
84     // Function to fix the tree as a sidebar
85     function stickTree() {
86         $bookTree.width($bookTreeParent.width() + 15);
87         $bookTree.addClass("fixed");
88         isFixed = true;
89     }
90     // Function to un-fix the tree back into position
91     function unstickTree() {
92         $bookTree.css('width', 'auto');
93         $bookTree.removeClass("fixed");
94         isFixed = false;
95     }
96     // Checks if the tree stickiness state should change
97     function checkTreeStickiness(skipCheck) {
98         var shouldBeFixed = $window.scrollTop() > headerHeight;
99         if (shouldBeFixed && (!isFixed || skipCheck)) {
100             stickTree();
101         } else if (!shouldBeFixed && (isFixed || skipCheck)) {
102             unstickTree();
103         }
104     }
105     // The event ran when the window scrolls
106     function windowScrollEvent() {
107         checkTreeStickiness(false);
108     }
109
110     // If the page is scrollable and the window is wide enough listen to scroll events
111     // and evaluate tree stickiness.
112     if (pageScrollable && $window.width() > 1000) {
113         $window.on('scroll', windowScrollEvent);
114         checkTreeStickiness(true);
115     }
116
117     // Handle window resizing and switch between desktop/mobile views
118     $window.on('resize', event => {
119         if (pageScrollable && $window.width() > 1000) {
120             $window.on('scroll', windowScrollEvent);
121             checkTreeStickiness(true);
122         } else {
123             $window.off('scroll', windowScrollEvent);
124             unstickTree();
125         }
126     });
127
128 };