crowi.js 12 KB

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