crowi.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* eslint-disable react/jsx-filename-extension */
  2. require('jquery.cookie');
  3. require('./thirdparty-js/waves');
  4. const Crowi = {};
  5. if (!window) {
  6. window = {};
  7. }
  8. window.Crowi = Crowi;
  9. /**
  10. * set 'data-caret-line' attribute that will be processed when 'shown.bs.tab' event fired
  11. * @param {number} line
  12. */
  13. Crowi.setCaretLineData = function(line) {
  14. const pageEditorDom = document.querySelector('#page-editor');
  15. pageEditorDom.setAttribute('data-caret-line', line);
  16. };
  17. /**
  18. * invoked when;
  19. *
  20. * 1. 'shown.bs.tab' event fired
  21. */
  22. Crowi.setCaretLineAndFocusToEditor = function() {
  23. // get 'data-caret-line' attributes
  24. const pageEditorDom = document.querySelector('#page-editor');
  25. if (pageEditorDom == null) {
  26. return;
  27. }
  28. const { appContainer } = window;
  29. const editorContainer = appContainer.getContainer('EditorContainer');
  30. const line = pageEditorDom.getAttribute('data-caret-line') || 0;
  31. editorContainer.setCaretLine(+line);
  32. // reset data-caret-line attribute
  33. pageEditorDom.removeAttribute('data-caret-line');
  34. // focus
  35. editorContainer.focusToEditor();
  36. };
  37. // original: middleware.swigFilter
  38. Crowi.userPicture = function(user) {
  39. if (!user) {
  40. return '/images/icons/user.svg';
  41. }
  42. return user.image || '/images/icons/user.svg';
  43. };
  44. Crowi.modifyScrollTop = function() {
  45. const offset = 10;
  46. const hash = window.location.hash;
  47. if (hash === '') {
  48. return;
  49. }
  50. const pageHeader = document.querySelector('#page-header');
  51. if (!pageHeader) {
  52. return;
  53. }
  54. const pageHeaderRect = pageHeader.getBoundingClientRect();
  55. const sectionHeader = Crowi.findSectionHeader(hash);
  56. if (sectionHeader === null) {
  57. return;
  58. }
  59. let timeout = 0;
  60. if (window.scrollY === 0) {
  61. timeout = 200;
  62. }
  63. setTimeout(() => {
  64. const sectionHeaderRect = sectionHeader.getBoundingClientRect();
  65. if (sectionHeaderRect.top >= pageHeaderRect.bottom) {
  66. return;
  67. }
  68. window.scrollTo(0, (window.scrollY - pageHeaderRect.height - offset));
  69. }, timeout);
  70. };
  71. Crowi.initClassesByOS = function() {
  72. // add classes to cmd-key by OS
  73. const platform = navigator.platform.toLowerCase();
  74. const isMac = (platform.indexOf('mac') > -1);
  75. document.querySelectorAll('.system-version .cmd-key').forEach((element) => {
  76. if (isMac) {
  77. element.classList.add('mac');
  78. }
  79. else {
  80. element.classList.add('win');
  81. }
  82. });
  83. document.querySelectorAll('#shortcuts-modal .cmd-key').forEach((element) => {
  84. if (isMac) {
  85. element.classList.add('mac');
  86. }
  87. else {
  88. element.classList.add('win', 'key-longer');
  89. }
  90. });
  91. };
  92. Crowi.findHashFromUrl = function(url) {
  93. let match;
  94. /* eslint-disable no-cond-assign */
  95. if (match = url.match(/#(.+)$/)) {
  96. return `#${match[1]}`;
  97. }
  98. /* eslint-enable no-cond-assign */
  99. return '';
  100. };
  101. Crowi.findSectionHeader = function(hash) {
  102. if (hash.length === 0) {
  103. return;
  104. }
  105. // omit '#'
  106. const id = hash.replace('#', '');
  107. // don't use jQuery and document.querySelector
  108. // because hash may containe Base64 encoded strings
  109. const elem = document.getElementById(id);
  110. if (elem != null && elem.tagName.match(/h\d+/i)) { // match h1, h2, h3...
  111. return elem;
  112. }
  113. return null;
  114. };
  115. Crowi.unhighlightSelectedSection = function(hash) {
  116. const elem = Crowi.findSectionHeader(hash);
  117. if (elem != null) {
  118. elem.classList.remove('highlighted');
  119. }
  120. };
  121. Crowi.highlightSelectedSection = function(hash) {
  122. const elem = Crowi.findSectionHeader(hash);
  123. if (elem != null) {
  124. elem.classList.add('highlighted');
  125. }
  126. };
  127. $(() => {
  128. const appContainer = window.appContainer;
  129. const config = appContainer.getConfig();
  130. const pageId = $('#content-main').data('page-id');
  131. // const revisionId = $('#content-main').data('page-revision-id');
  132. // const revisionCreatedAt = $('#content-main').data('page-revision-created');
  133. // const currentUser = $('#content-main').data('current-user');
  134. const isSeen = $('#content-main').data('page-is-seen');
  135. const isSavedStatesOfTabChanges = config.isSavedStatesOfTabChanges;
  136. $('[data-toggle="popover"]').popover();
  137. $('[data-toggle="tooltip"]').tooltip();
  138. $('[data-tooltip-stay]').tooltip('show');
  139. $('#toggle-crowi-sidebar').click((e) => {
  140. const $body = $('body');
  141. if ($body.hasClass('aside-hidden')) {
  142. $body.removeClass('aside-hidden');
  143. $.cookie('aside-hidden', 0, { expires: 30, path: '/' });
  144. }
  145. else {
  146. $body.addClass('aside-hidden');
  147. $.cookie('aside-hidden', 1, { expires: 30, path: '/' });
  148. }
  149. return false;
  150. });
  151. if ($.cookie('aside-hidden') === 1) {
  152. $('body').addClass('aside-hidden');
  153. }
  154. $('.copy-link').on('click', function() {
  155. $(this).select();
  156. });
  157. if (pageId) {
  158. if (!isSeen) {
  159. $.post('/_api/pages.seen', { page_id: pageId }, (res) => {
  160. // ignore unless response has error
  161. if (res.ok && res.seenUser) {
  162. $('#content-main').data('page-is-seen', 1);
  163. }
  164. });
  165. }
  166. } // end if pageId
  167. // focus to editor when 'shown.bs.tab' event fired
  168. $('a[href="#edit"]').on('shown.bs.tab', (e) => {
  169. Crowi.setCaretLineAndFocusToEditor();
  170. });
  171. });
  172. window.addEventListener('load', (e) => {
  173. const { appContainer } = window;
  174. // do nothing if user is guest
  175. if (appContainer.currentUser == null) {
  176. return;
  177. }
  178. // hash on page
  179. if (window.location.hash) {
  180. const navigationContainer = appContainer.getContainer('NavigationContainer');
  181. if ((window.location.hash === '#edit' || window.location.hash === '#edit-form') && $('.tab-pane#edit').length > 0) {
  182. navigationContainer.setEditorMode('builtin');
  183. $('a[data-toggle="tab"][href="#edit"]').tab('show');
  184. $('body').addClass('on-edit');
  185. $('body').addClass('builtin-editor');
  186. // focus
  187. Crowi.setCaretLineAndFocusToEditor();
  188. }
  189. else if (window.location.hash === '#hackmd' && $('.tab-pane#hackmd').length > 0) {
  190. navigationContainer.setEditorMode('hackmd');
  191. $('a[data-toggle="tab"][href="#hackmd"]').tab('show');
  192. $('body').addClass('on-edit');
  193. $('body').addClass('hackmd');
  194. }
  195. else if (window.location.hash === '#revision-history' && $('.tab-pane#revision-history').length > 0) {
  196. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  197. }
  198. }
  199. });
  200. window.addEventListener('load', (e) => {
  201. const crowi = window.crowi;
  202. if (crowi && crowi.users && crowi.users.length !== 0) {
  203. const totalUsers = crowi.users.length;
  204. const $listLiker = $('.page-list-liker');
  205. $listLiker.each((i, liker) => {
  206. const count = $(liker).data('count') || 0;
  207. if (count / totalUsers > 0.05) {
  208. $(liker).addClass('popular-page-high');
  209. // 5%
  210. }
  211. else if (count / totalUsers > 0.02) {
  212. $(liker).addClass('popular-page-mid');
  213. // 2%
  214. }
  215. else if (count / totalUsers > 0.005) {
  216. $(liker).addClass('popular-page-low');
  217. // 0.5%
  218. }
  219. });
  220. const $listSeer = $('.page-list-seer');
  221. $listSeer.each((i, seer) => {
  222. const count = $(seer).data('count') || 0;
  223. if (count / totalUsers > 0.10) {
  224. // 10%
  225. $(seer).addClass('popular-page-high');
  226. }
  227. else if (count / totalUsers > 0.05) {
  228. // 5%
  229. $(seer).addClass('popular-page-mid');
  230. }
  231. else if (count / totalUsers > 0.02) {
  232. // 2%
  233. $(seer).addClass('popular-page-low');
  234. }
  235. });
  236. }
  237. Crowi.highlightSelectedSection(window.location.hash);
  238. Crowi.modifyScrollTop();
  239. Crowi.initClassesByOS();
  240. });
  241. window.addEventListener('hashchange', (e) => {
  242. Crowi.unhighlightSelectedSection(Crowi.findHashFromUrl(e.oldURL));
  243. Crowi.highlightSelectedSection(Crowi.findHashFromUrl(e.newURL));
  244. Crowi.modifyScrollTop();
  245. // hash on page
  246. if (window.location.hash) {
  247. if (window.location.hash === '#edit') {
  248. $('a[data-toggle="tab"][href="#edit"]').tab('show');
  249. }
  250. else if (window.location.hash === '#hackmd') {
  251. $('a[data-toggle="tab"][href="#hackmd"]').tab('show');
  252. }
  253. else if (window.location.hash === '#revision-history') {
  254. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  255. }
  256. }
  257. else {
  258. $('a[data-toggle="tab"][href="#revision-body"]').tab('show');
  259. }
  260. });
  261. // adjust min-height of page for print temporarily
  262. window.onbeforeprint = function() {
  263. $('#page-wrapper').css('min-height', '0px');
  264. };