/* eslint-disable react/jsx-filename-extension */ require('jquery.cookie'); require('./thirdparty-js/waves'); const Crowi = {}; if (!window) { window = {}; } window.Crowi = Crowi; /** * set 'data-caret-line' attribute that will be processed when 'shown.bs.tab' event fired * @param {number} line */ Crowi.setCaretLineData = function(line) { const pageEditorDom = document.querySelector('#page-editor'); pageEditorDom.setAttribute('data-caret-line', line); }; /** * invoked when; * * 1. 'shown.bs.tab' event fired */ Crowi.setCaretLineAndFocusToEditor = function() { // get 'data-caret-line' attributes const pageEditorDom = document.querySelector('#page-editor'); if (pageEditorDom == null) { return; } const { appContainer } = window; const editorContainer = appContainer.getContainer('EditorContainer'); const line = pageEditorDom.getAttribute('data-caret-line') || 0; editorContainer.setCaretLine(+line); // reset data-caret-line attribute pageEditorDom.removeAttribute('data-caret-line'); // focus editorContainer.focusToEditor(); }; // original: middleware.swigFilter Crowi.userPicture = function(user) { if (!user) { return '/images/icons/user.svg'; } return user.image || '/images/icons/user.svg'; }; Crowi.modifyScrollTop = function() { const offset = 10; const hash = window.location.hash; if (hash === '') { return; } const pageHeader = document.querySelector('#page-header'); if (!pageHeader) { return; } const pageHeaderRect = pageHeader.getBoundingClientRect(); const sectionHeader = Crowi.findSectionHeader(hash); if (sectionHeader === null) { return; } let timeout = 0; if (window.scrollY === 0) { timeout = 200; } setTimeout(() => { const sectionHeaderRect = sectionHeader.getBoundingClientRect(); if (sectionHeaderRect.top >= pageHeaderRect.bottom) { return; } window.scrollTo(0, (window.scrollY - pageHeaderRect.height - offset)); }, timeout); }; Crowi.handleKeyCtrlSlashHandler = (event) => { // show modal to create a page $('#shortcuts-modal').modal('toggle'); event.preventDefault(); }; Crowi.initClassesByOS = function() { // add classes to cmd-key by OS const platform = navigator.platform.toLowerCase(); const isMac = (platform.indexOf('mac') > -1); document.querySelectorAll('.system-version .cmd-key').forEach((element) => { if (isMac) { element.classList.add('mac'); } else { element.classList.add('win'); } }); document.querySelectorAll('#shortcuts-modal .cmd-key').forEach((element) => { if (isMac) { element.classList.add('mac'); } else { element.classList.add('win', 'key-longer'); } }); }; Crowi.findHashFromUrl = function(url) { let match; /* eslint-disable no-cond-assign */ if (match = url.match(/#(.+)$/)) { return `#${match[1]}`; } /* eslint-enable no-cond-assign */ return ''; }; Crowi.findSectionHeader = function(hash) { if (hash.length === 0) { return; } // omit '#' const id = hash.replace('#', ''); // don't use jQuery and document.querySelector // because hash may containe Base64 encoded strings const elem = document.getElementById(id); if (elem != null && elem.tagName.match(/h\d+/i)) { // match h1, h2, h3... return elem; } return null; }; Crowi.unhighlightSelectedSection = function(hash) { const elem = Crowi.findSectionHeader(hash); if (elem != null) { elem.classList.remove('highlighted'); } }; Crowi.highlightSelectedSection = function(hash) { const elem = Crowi.findSectionHeader(hash); if (elem != null) { elem.classList.add('highlighted'); } }; $(() => { const appContainer = window.appContainer; const config = appContainer.getConfig(); const pageId = $('#content-main').data('page-id'); // const revisionId = $('#content-main').data('page-revision-id'); // const revisionCreatedAt = $('#content-main').data('page-revision-created'); // const currentUser = $('#content-main').data('current-user'); const isSeen = $('#content-main').data('page-is-seen'); const isSavedStatesOfTabChanges = config.isSavedStatesOfTabChanges; $('[data-toggle="popover"]').popover(); $('[data-toggle="tooltip"]').tooltip(); $('[data-tooltip-stay]').tooltip('show'); $('#toggle-crowi-sidebar').click((e) => { const $body = $('body'); if ($body.hasClass('aside-hidden')) { $body.removeClass('aside-hidden'); $.cookie('aside-hidden', 0, { expires: 30, path: '/' }); } else { $body.addClass('aside-hidden'); $.cookie('aside-hidden', 1, { expires: 30, path: '/' }); } return false; }); if ($.cookie('aside-hidden') === 1) { $('body').addClass('aside-hidden'); } $('.copy-link').on('click', function() { $(this).select(); }); if (pageId) { if (!isSeen) { $.post('/_api/pages.seen', { page_id: pageId }, (res) => { // ignore unless response has error if (res.ok && res.seenUser) { $('#content-main').data('page-is-seen', 1); } }); } // presentation let presentaionInitialized = false; const $b = $('body'); $(document).on('click', '.toggle-presentation', function(e) { const $a = $(this); e.preventDefault(); $b.toggleClass('overlay-on'); if (!presentaionInitialized) { presentaionInitialized = true; $('