crowi.js 12 KB

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