crowi.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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.handleKeyCtrlSlashHandler = (event) => {
  72. // show modal to create a page
  73. $('#shortcuts-modal').modal('toggle');
  74. event.preventDefault();
  75. };
  76. Crowi.initClassesByOS = function() {
  77. // add classes to cmd-key by OS
  78. const platform = navigator.platform.toLowerCase();
  79. const isMac = (platform.indexOf('mac') > -1);
  80. document.querySelectorAll('.system-version .cmd-key').forEach((element) => {
  81. if (isMac) {
  82. element.classList.add('mac');
  83. }
  84. else {
  85. element.classList.add('win');
  86. }
  87. });
  88. document.querySelectorAll('#shortcuts-modal .cmd-key').forEach((element) => {
  89. if (isMac) {
  90. element.classList.add('mac');
  91. }
  92. else {
  93. element.classList.add('win', 'key-longer');
  94. }
  95. });
  96. };
  97. Crowi.findHashFromUrl = function(url) {
  98. let match;
  99. /* eslint-disable no-cond-assign */
  100. if (match = url.match(/#(.+)$/)) {
  101. return `#${match[1]}`;
  102. }
  103. /* eslint-enable no-cond-assign */
  104. return '';
  105. };
  106. Crowi.findSectionHeader = function(hash) {
  107. if (hash.length === 0) {
  108. return;
  109. }
  110. // omit '#'
  111. const id = hash.replace('#', '');
  112. // don't use jQuery and document.querySelector
  113. // because hash may containe Base64 encoded strings
  114. const elem = document.getElementById(id);
  115. if (elem != null && elem.tagName.match(/h\d+/i)) { // match h1, h2, h3...
  116. return elem;
  117. }
  118. return null;
  119. };
  120. Crowi.unhighlightSelectedSection = function(hash) {
  121. const elem = Crowi.findSectionHeader(hash);
  122. if (elem != null) {
  123. elem.classList.remove('highlighted');
  124. }
  125. };
  126. Crowi.highlightSelectedSection = function(hash) {
  127. const elem = Crowi.findSectionHeader(hash);
  128. if (elem != null) {
  129. elem.classList.add('highlighted');
  130. }
  131. };
  132. $(() => {
  133. const appContainer = window.appContainer;
  134. const config = appContainer.getConfig();
  135. const pageId = $('#content-main').data('page-id');
  136. // const revisionId = $('#content-main').data('page-revision-id');
  137. // const revisionCreatedAt = $('#content-main').data('page-revision-created');
  138. // const currentUser = $('#content-main').data('current-user');
  139. const isSeen = $('#content-main').data('page-is-seen');
  140. const isSavedStatesOfTabChanges = config.isSavedStatesOfTabChanges;
  141. $('[data-toggle="popover"]').popover();
  142. $('[data-toggle="tooltip"]').tooltip();
  143. $('[data-tooltip-stay]').tooltip('show');
  144. $('#toggle-crowi-sidebar').click((e) => {
  145. const $body = $('body');
  146. if ($body.hasClass('aside-hidden')) {
  147. $body.removeClass('aside-hidden');
  148. $.cookie('aside-hidden', 0, { expires: 30, path: '/' });
  149. }
  150. else {
  151. $body.addClass('aside-hidden');
  152. $.cookie('aside-hidden', 1, { expires: 30, path: '/' });
  153. }
  154. return false;
  155. });
  156. if ($.cookie('aside-hidden') === 1) {
  157. $('body').addClass('aside-hidden');
  158. }
  159. $('.copy-link').on('click', function() {
  160. $(this).select();
  161. });
  162. if (pageId) {
  163. if (!isSeen) {
  164. $.post('/_api/pages.seen', { page_id: pageId }, (res) => {
  165. // ignore unless response has error
  166. if (res.ok && res.seenUser) {
  167. $('#content-main').data('page-is-seen', 1);
  168. }
  169. });
  170. }
  171. // presentation
  172. let presentaionInitialized = false;
  173. const $b = $('body');
  174. $(document).on('click', '.toggle-presentation', function(e) {
  175. const $a = $(this);
  176. e.preventDefault();
  177. $b.toggleClass('overlay-on');
  178. if (!presentaionInitialized) {
  179. presentaionInitialized = true;
  180. $('<iframe />').attr({
  181. src: $a.attr('href'),
  182. }).appendTo($('#presentation-container'));
  183. }
  184. }).on('click', '.fullscreen-layer', () => {
  185. $b.toggleClass('overlay-on');
  186. });
  187. } // end if pageId
  188. // tab changing handling
  189. $('a[href="#revision-body"]').on('show.bs.tab', () => {
  190. const navigationContainer = appContainer.getContainer('NavigationContainer');
  191. navigationContainer.setEditorMode(null);
  192. });
  193. $('a[href="#edit"]').on('show.bs.tab', () => {
  194. const navigationContainer = appContainer.getContainer('NavigationContainer');
  195. navigationContainer.setEditorMode('builtin');
  196. $('body').addClass('on-edit');
  197. $('body').addClass('builtin-editor');
  198. });
  199. $('a[href="#edit"]').on('hide.bs.tab', () => {
  200. $('body').removeClass('on-edit');
  201. $('body').removeClass('builtin-editor');
  202. });
  203. $('a[href="#hackmd"]').on('show.bs.tab', () => {
  204. const navigationContainer = appContainer.getContainer('NavigationContainer');
  205. navigationContainer.setEditorMode('hackmd');
  206. $('body').addClass('on-edit');
  207. $('body').addClass('hackmd');
  208. });
  209. $('a[href="#hackmd"]').on('hide.bs.tab', () => {
  210. $('body').removeClass('on-edit');
  211. $('body').removeClass('hackmd');
  212. });
  213. // hash handling
  214. if (isSavedStatesOfTabChanges) {
  215. $('a[data-toggle="tab"][href="#revision-history"]').on('show.bs.tab', () => {
  216. window.location.hash = '#revision-history';
  217. window.history.replaceState('', 'History', '#revision-history');
  218. });
  219. $('a[data-toggle="tab"][href="#edit"]').on('show.bs.tab', () => {
  220. window.location.hash = '#edit';
  221. window.history.replaceState('', 'Edit', '#edit');
  222. });
  223. $('a[data-toggle="tab"][href="#hackmd"]').on('show.bs.tab', () => {
  224. window.location.hash = '#hackmd';
  225. window.history.replaceState('', 'HackMD', '#hackmd');
  226. });
  227. $('a[data-toggle="tab"][href="#revision-body"]').on('show.bs.tab', () => {
  228. // couln't solve https://github.com/weseek/crowi-plus/issues/119 completely -- 2017.07.03 Yuki Takei
  229. window.location.hash = '#';
  230. window.history.replaceState('', '', window.location.href);
  231. });
  232. }
  233. else {
  234. $('a[data-toggle="tab"][href="#revision-history"]').on('show.bs.tab', () => {
  235. window.history.replaceState('', 'History', '#revision-history');
  236. });
  237. $('a[data-toggle="tab"][href="#edit"]').on('show.bs.tab', () => {
  238. window.history.replaceState('', 'Edit', '#edit');
  239. });
  240. $('a[data-toggle="tab"][href="#hackmd"]').on('show.bs.tab', () => {
  241. window.history.replaceState('', 'HackMD', '#hackmd');
  242. });
  243. $('a[data-toggle="tab"][href="#revision-body"]').on('show.bs.tab', () => {
  244. window.history.replaceState('', '', window.location.href.replace(window.location.hash, ''));
  245. });
  246. // replace all href="#edit" link behaviors
  247. $(document).on('click', 'a[href="#edit"]', () => {
  248. window.location.replace('#edit');
  249. });
  250. }
  251. // focus to editor when 'shown.bs.tab' event fired
  252. $('a[href="#edit"]').on('shown.bs.tab', (e) => {
  253. Crowi.setCaretLineAndFocusToEditor();
  254. });
  255. });
  256. window.addEventListener('load', (e) => {
  257. const { appContainer } = window;
  258. // do nothing if user is guest
  259. if (appContainer.currentUser == null) {
  260. return;
  261. }
  262. // hash on page
  263. if (window.location.hash) {
  264. const navigationContainer = appContainer.getContainer('NavigationContainer');
  265. if ((window.location.hash === '#edit' || window.location.hash === '#edit-form') && $('.tab-pane#edit').length > 0) {
  266. navigationContainer.setEditorMode('builtin');
  267. $('a[data-toggle="tab"][href="#edit"]').tab('show');
  268. $('body').addClass('on-edit');
  269. $('body').addClass('builtin-editor');
  270. // focus
  271. Crowi.setCaretLineAndFocusToEditor();
  272. }
  273. else if (window.location.hash === '#hackmd' && $('.tab-pane#hackmd').length > 0) {
  274. navigationContainer.setEditorMode('hackmd');
  275. $('a[data-toggle="tab"][href="#hackmd"]').tab('show');
  276. $('body').addClass('on-edit');
  277. $('body').addClass('hackmd');
  278. }
  279. else if (window.location.hash === '#revision-history' && $('.tab-pane#revision-history').length > 0) {
  280. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  281. }
  282. }
  283. });
  284. window.addEventListener('load', (e) => {
  285. const crowi = window.crowi;
  286. if (crowi && crowi.users && crowi.users.length !== 0) {
  287. const totalUsers = crowi.users.length;
  288. const $listLiker = $('.page-list-liker');
  289. $listLiker.each((i, liker) => {
  290. const count = $(liker).data('count') || 0;
  291. if (count / totalUsers > 0.05) {
  292. $(liker).addClass('popular-page-high');
  293. // 5%
  294. }
  295. else if (count / totalUsers > 0.02) {
  296. $(liker).addClass('popular-page-mid');
  297. // 2%
  298. }
  299. else if (count / totalUsers > 0.005) {
  300. $(liker).addClass('popular-page-low');
  301. // 0.5%
  302. }
  303. });
  304. const $listSeer = $('.page-list-seer');
  305. $listSeer.each((i, seer) => {
  306. const count = $(seer).data('count') || 0;
  307. if (count / totalUsers > 0.10) {
  308. // 10%
  309. $(seer).addClass('popular-page-high');
  310. }
  311. else if (count / totalUsers > 0.05) {
  312. // 5%
  313. $(seer).addClass('popular-page-mid');
  314. }
  315. else if (count / totalUsers > 0.02) {
  316. // 2%
  317. $(seer).addClass('popular-page-low');
  318. }
  319. });
  320. }
  321. Crowi.highlightSelectedSection(window.location.hash);
  322. Crowi.modifyScrollTop();
  323. Crowi.initClassesByOS();
  324. });
  325. window.addEventListener('hashchange', (e) => {
  326. Crowi.unhighlightSelectedSection(Crowi.findHashFromUrl(e.oldURL));
  327. Crowi.highlightSelectedSection(Crowi.findHashFromUrl(e.newURL));
  328. Crowi.modifyScrollTop();
  329. // hash on page
  330. if (window.location.hash) {
  331. if (window.location.hash === '#edit') {
  332. $('a[data-toggle="tab"][href="#edit"]').tab('show');
  333. }
  334. else if (window.location.hash === '#hackmd') {
  335. $('a[data-toggle="tab"][href="#hackmd"]').tab('show');
  336. }
  337. else if (window.location.hash === '#revision-history') {
  338. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  339. }
  340. }
  341. else {
  342. $('a[data-toggle="tab"][href="#revision-body"]').tab('show');
  343. }
  344. });
  345. window.addEventListener('keydown', (event) => {
  346. const target = event.target;
  347. // ignore when target dom is input
  348. const inputPattern = /^input|textinput|textarea$/i;
  349. if (inputPattern.test(target.tagName) || target.isContentEditable) {
  350. return;
  351. }
  352. switch (event.key) {
  353. case '/':
  354. if (event.ctrlKey || event.metaKey) {
  355. Crowi.handleKeyCtrlSlashHandler(event);
  356. }
  357. break;
  358. default:
  359. }
  360. });
  361. // adjust min-height of page for print temporarily
  362. window.onbeforeprint = function() {
  363. $('#page-wrapper').css('min-height', '0px');
  364. };