crowi.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /* jshint browser: true, jquery: true */
  2. /* Author: Sotaro KARASAWA <sotarok@crocos.co.jp>
  3. */
  4. var io = require('socket.io-client');
  5. require('bootstrap-sass');
  6. require('jquery.cookie');
  7. var Crowi = {};
  8. if (!window) {
  9. window = {};
  10. }
  11. window.Crowi = Crowi;
  12. Crowi.createErrorView = function(msg) {
  13. $('#main').prepend($('<p class="alert-message error">' + msg + '</p>'));
  14. };
  15. Crowi.correctHeaders = function(contentId) {
  16. // h1 ~ h6 の id 名を補正する
  17. var $content = $(contentId || '#revision-body-content');
  18. var i = 0;
  19. $('h1,h2,h3,h4,h5,h6', $content).each(function(idx, elm) {
  20. var id = 'head' + i++;
  21. $(this).attr('id', id);
  22. $(this).addClass('revision-head');
  23. $(this).append('<span class="revision-head-link"><a href="#' + id +'"><i class="fa fa-link"></i></a></span>');
  24. });
  25. };
  26. Crowi.revisionToc = function(contentId, tocId) {
  27. var $content = $(contentId || '#revision-body-content');
  28. var $tocId = $(tocId || '#revision-toc');
  29. var $tocContent = $('<div id="revision-toc-content" class="revision-toc-content collapse"></div>');
  30. $tocId.append($tocContent);
  31. $('h1', $content).each(function(idx, elm) {
  32. var id = $(this).attr('id');
  33. var title = $(this).text();
  34. var selector = '#' + id + ' ~ h2:not(#' + id + ' ~ h1 ~ h2)';
  35. var $toc = $('<ul></ul>');
  36. var $tocLi = $('<li><a href="#' + id +'">' + title + '</a></li>');
  37. $tocContent.append($toc);
  38. $toc.append($tocLi);
  39. $(selector).each(function()
  40. {
  41. var id2 = $(this).attr('id');
  42. var title2 = $(this).text();
  43. var selector2 = '#' + id2 + ' ~ h3:not(#' + id2 + ' ~ h2 ~ h3)';
  44. var $toc2 = $('<ul></ul>');
  45. var $tocLi2 = $('<li><a href="#' + id2 +'">' + title2 + '</a></li>');
  46. $tocLi.append($toc2);
  47. $toc2.append($tocLi2);
  48. $(selector2).each(function()
  49. {
  50. var id3 = $(this).attr('id');
  51. var title3 = $(this).text();
  52. var $toc3 = $('<ul></ul>');
  53. var $tocLi3 = $('<li><a href="#' + id3 +'">' + title3 + '</a></li>');
  54. $tocLi2.append($toc3);
  55. $toc3.append($tocLi3);
  56. });
  57. });
  58. });
  59. };
  60. Crowi.escape = function(s) {
  61. s = s.replace(/&/g, '&amp;')
  62. .replace(/</g, '&lt;')
  63. .replace(/>/g, '&gt;')
  64. .replace(/'/g, '&#39;')
  65. .replace(/"/g, '&quot;')
  66. ;
  67. return s;
  68. };
  69. Crowi.unescape = function(s) {
  70. s = s.replace(/&nbsp;/g, ' ')
  71. .replace(/&amp;/g, '&')
  72. .replace(/&lt;/g, '<')
  73. .replace(/&gt;/g, '>')
  74. .replace(/&#39;/g, '\'')
  75. .replace(/&quot;/g, '"')
  76. ;
  77. return s;
  78. };
  79. // original: middleware.swigFilter
  80. Crowi.userPicture = function (user) {
  81. if (!user) {
  82. return '/images/userpicture.png';
  83. }
  84. if (user.image && user.image != '/images/userpicture.png') {
  85. return user.image;
  86. } else {
  87. return '/images/userpicture.png';
  88. }
  89. };
  90. Crowi.modifyScrollTop = function() {
  91. var offset = 10;
  92. var hash = window.location.hash;
  93. if (hash === "") {
  94. return;
  95. }
  96. var pageHeader = document.querySelector('#page-header');
  97. if (!pageHeader) {
  98. return;
  99. }
  100. var pageHeaderRect = pageHeader.getBoundingClientRect();
  101. var sectionHeader = document.querySelector(hash);
  102. if (sectionHeader === null) {
  103. return;
  104. }
  105. var timeout = 0;
  106. if (window.scrollY === 0) {
  107. timeout = 200;
  108. }
  109. setTimeout(function() {
  110. var sectionHeaderRect = sectionHeader.getBoundingClientRect();
  111. if (sectionHeaderRect.top >= pageHeaderRect.bottom) {
  112. return;
  113. }
  114. window.scrollTo(0, (window.scrollY - pageHeaderRect.height - offset));
  115. }, timeout);
  116. }
  117. $(function() {
  118. var pageId = $('#content-main').data('page-id');
  119. var revisionId = $('#content-main').data('page-revision-id');
  120. var revisionCreatedAt = $('#content-main').data('page-revision-created');
  121. var currentUser = $('#content-main').data('current-user');
  122. var isSeen = $('#content-main').data('page-is-seen');
  123. var pagePath= $('#content-main').data('path');
  124. var isEnabledLineBreaks = $('#content-main').data('linebreaks-enabled');
  125. // generate options obj
  126. var rendererOptions = {
  127. // see: https://www.npmjs.com/package/marked
  128. marked: {
  129. breaks: isEnabledLineBreaks
  130. }
  131. };
  132. $('[data-toggle="popover"]').popover();
  133. $('[data-toggle="tooltip"]').tooltip();
  134. $('[data-tooltip-stay]').tooltip('show');
  135. $('#toggle-sidebar').click(function(e) {
  136. var $mainContainer = $('.main-container');
  137. if ($mainContainer.hasClass('aside-hidden')) {
  138. $('.main-container').removeClass('aside-hidden');
  139. $.cookie('aside-hidden', 0, { expires: 30, path: '/' });
  140. } else {
  141. $mainContainer.addClass('aside-hidden');
  142. $.cookie('aside-hidden', 1, { expires: 30, path: '/' });
  143. }
  144. return false;
  145. });
  146. if ($.cookie('aside-hidden') == 1) {
  147. $('.main-container').addClass('aside-hidden');
  148. }
  149. $('.copy-link').on('click', function () {
  150. $(this).select();
  151. });
  152. $('#create-page').on('shown.bs.modal', function (e) {
  153. var input2Width = $('#create-page-today .col-xs-10').outerWidth() - 1;
  154. var newWidth = input2Width
  155. - $('#create-page-today .page-today-prefix').outerWidth()
  156. - $('#create-page-today .page-today-input1').outerWidth()
  157. - $('#create-page-today .page-today-suffix').outerWidth()
  158. - 40
  159. ;
  160. $('#create-page-today .form-control.page-today-input2').css({width: newWidth}).focus();
  161. });
  162. $('#create-page-today').submit(function(e) {
  163. var prefix1 = $('input.page-today-input1', this).data('prefix');
  164. var input1 = $('input.page-today-input1', this).val();
  165. var prefix2 = $('input.page-today-input2', this).data('prefix');
  166. var input2 = $('input.page-today-input2', this).val();
  167. if (input1 === '') {
  168. prefix1 = 'メモ';
  169. }
  170. if (input2 === '') {
  171. prefix2 = prefix2.slice(0, -1);
  172. }
  173. top.location.href = prefix1 + input1 + prefix2 + input2;
  174. return false;
  175. });
  176. $('#create-page-under-tree').submit(function(e) {
  177. var name = $('input', this).val();
  178. if (!name.match(/^\//)) {
  179. name = '/' + name;
  180. }
  181. if (name.match(/.+\/$/)) {
  182. name = name.substr(0, name.length - 1);
  183. }
  184. top.location.href = name;
  185. return false;
  186. });
  187. // rename
  188. $('#renamePage').on('shown.bs.modal', function (e) {
  189. $('#newPageName').focus();
  190. });
  191. $('#renamePageForm, #unportalize-form').submit(function(e) {
  192. $.ajax({
  193. type: 'POST',
  194. url: '/_api/pages.rename',
  195. data: $(this).serialize(),
  196. dataType: 'json'
  197. }).done(function(res) {
  198. if (!res.ok) {
  199. $('#newPageNameCheck').html('<i class="fa fa-times-circle"></i> ' + res.error);
  200. $('#newPageNameCheck').addClass('alert-danger');
  201. } else {
  202. var page = res.page;
  203. $('#newPageNameCheck').removeClass('alert-danger');
  204. //$('#newPageNameCheck').html('<img src="/images/loading_s.gif"> 移動しました。移動先にジャンプします。');
  205. // fix
  206. $('#newPageNameCheck').html('<img src="/images/loading_s.gif"> Page moved! Redirecting to new page location.');
  207. setTimeout(function() {
  208. top.location.href = page.path + '?renamed=' + pagePath;
  209. }, 1000);
  210. }
  211. });
  212. return false;
  213. });
  214. // delete
  215. $('#delete-page-form').submit(function(e) {
  216. $.ajax({
  217. type: 'POST',
  218. url: '/_api/pages.remove',
  219. data: $('#delete-page-form').serialize(),
  220. dataType: 'json'
  221. }).done(function(res) {
  222. if (!res.ok) {
  223. $('#delete-errors').html('<i class="fa fa-times-circle"></i> ' + res.error);
  224. $('#delete-errors').addClass('alert-danger');
  225. } else {
  226. var page = res.page;
  227. top.location.href = page.path;
  228. }
  229. });
  230. return false;
  231. });
  232. $('#revert-delete-page-form').submit(function(e) {
  233. $.ajax({
  234. type: 'POST',
  235. url: '/_api/pages.revertRemove',
  236. data: $('#revert-delete-page-form').serialize(),
  237. dataType: 'json'
  238. }).done(function(res) {
  239. if (!res.ok) {
  240. $('#delete-errors').html('<i class="fa fa-times-circle"></i> ' + res.error);
  241. $('#delete-errors').addClass('alert-danger');
  242. } else {
  243. var page = res.page;
  244. top.location.href = page.path;
  245. }
  246. });
  247. return false;
  248. });
  249. $('#create-portal-button').on('click', function(e) {
  250. $('.portal').removeClass('hide');
  251. $('.content-main').addClass('on-edit');
  252. $('.portal a[data-toggle="tab"][href="#edit-form"]').tab('show');
  253. var path = $('.content-main').data('path');
  254. if (path != '/' && $('.content-main').data('page-id') == '') {
  255. var upperPage = path.substr(0, path.length - 1);
  256. $.get('/_api/pages.get', {path: upperPage}, function(res) {
  257. if (res.ok && res.page) {
  258. $('#portal-warning-modal').modal('show');
  259. }
  260. });
  261. }
  262. });
  263. $('#portal-form-close').on('click', function(e) {
  264. $('.portal').addClass('hide');
  265. $('.content-main').removeClass('on-edit');
  266. return false;
  267. });
  268. // list-link
  269. $('.page-list-link').each(function() {
  270. var $link = $(this);
  271. var text = $link.text();
  272. var path = $link.data('path');
  273. var shortPath = new String($link.data('short-path'));
  274. var escape = function(s) {
  275. return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  276. };
  277. path = Crowi.escape(path);
  278. var pattern = escape(Crowi.escape(shortPath)) + '(/)?$';
  279. $link.html(path.replace(new RegExp(pattern), '<strong>' + shortPath + '$1</strong>'));
  280. });
  281. // for list page
  282. $('a[data-toggle="tab"][href="#view-timeline"]').on('show.bs.tab', function() {
  283. var isShown = $('#view-timeline').data('shown');
  284. if (isShown == 0) {
  285. $('#view-timeline .timeline-body').each(function()
  286. {
  287. var id = $(this).attr('id');
  288. var contentId = '#' + id + ' > script';
  289. var revisionBody = '#' + id + ' .revision-body';
  290. var revisionPath = '#' + id + ' .revision-path';
  291. var markdown = Crowi.unescape($(contentId).html());
  292. var parsedHTML = crowiRenderer.render(markdown, rendererOptions);
  293. $(revisionBody).html(parsedHTML);
  294. $('.template-create-button', revisionBody).on('click', function() {
  295. var path = $(this).data('path');
  296. var templateId = $(this).data('template');
  297. var template = $('#' + templateId).html();
  298. crowi.saveDraft(path, template);
  299. top.location.href = path;
  300. });
  301. });
  302. $('#view-timeline').data('shown', 1);
  303. }
  304. });
  305. // login
  306. $('#register').on('click', function() {
  307. $('#login-dialog').addClass('to-flip');
  308. return false;
  309. });
  310. $('#login').on('click', function() {
  311. $('#login-dialog').removeClass('to-flip');
  312. return false;
  313. });
  314. $('#register-form input[name="registerForm[username]"]').change(function(e) {
  315. var username = $(this).val();
  316. $('#input-group-username').removeClass('has-error');
  317. $('#help-block-username').html("");
  318. $.getJSON('/_api/check_username', {username: username}, function(json) {
  319. if (!json.valid) {
  320. $('#help-block-username').html('<i class="fa fa-warning"></i> This User ID is not available.<br>');
  321. $('#input-group-username').addClass('has-error');
  322. }
  323. });
  324. });
  325. if (pageId) {
  326. // if page exists
  327. var $rawTextOriginal = $('#raw-text-original');
  328. if ($rawTextOriginal.length > 0) {
  329. var markdown = Crowi.unescape($('#raw-text-original').html());
  330. // create context object
  331. var context = {
  332. markdown,
  333. currentPagePath: decodeURIComponent(location.pathname)
  334. };
  335. crowi.interceptorManager.process('preRender', context)
  336. .then(() => crowi.interceptorManager.process('prePreProcess', context))
  337. .then(() => {
  338. context.markdown = crowiRenderer.preProcess(context.markdown);
  339. })
  340. .then(() => crowi.interceptorManager.process('postPreProcess', context))
  341. .then(() => {
  342. var parsedHTML = crowiRenderer.render(context.markdown, rendererOptions);
  343. context.parsedHTML = parsedHTML;
  344. Promise.resolve(context);
  345. })
  346. .then(() => crowi.interceptorManager.process('postRender', context))
  347. .then(() => crowi.interceptorManager.process('preRenderHtml', context))
  348. // render HTML with jQuery
  349. .then(() => {
  350. $('#revision-body-content').html(context.parsedHTML);
  351. $('.template-create-button').on('click', function() {
  352. var path = $(this).data('path');
  353. var templateId = $(this).data('template');
  354. var template = $('#' + templateId).html();
  355. crowi.saveDraft(path, template);
  356. top.location.href = path;
  357. });
  358. Crowi.correctHeaders('#revision-body-content');
  359. Crowi.revisionToc('#revision-body-content', '#revision-toc');
  360. Promise.resolve($('#revision-body-content'));
  361. })
  362. // process interceptors for post rendering
  363. .then((bodyElement) => {
  364. context = Object.assign(context, {bodyElement})
  365. return crowi.interceptorManager.process('postRenderHtml', context);
  366. });
  367. }
  368. // header
  369. var $header = $('#page-header');
  370. if ($header.length > 0) {
  371. var headerHeight = $header.outerHeight(true);
  372. $('.header-wrap').css({height: (headerHeight + 16) + 'px'});
  373. $header.affix({
  374. offset: {
  375. top: function() {
  376. return headerHeight + 86; // (54 header + 16 header padding-top + 16 content padding-top)
  377. }
  378. }
  379. });
  380. $('[data-affix-disable]').on('click', function(e) {
  381. var $elm = $($(this).data('affix-disable'));
  382. $(window).off('.affix');
  383. $elm.removeData('affix').removeClass('affix affix-top affix-bottom');
  384. return false;
  385. });
  386. }
  387. // omg
  388. function createCommentHTML(revision, creator, comment, commentedAt) {
  389. var $comment = $('<div>');
  390. var $commentImage = $('<img class="picture picture-rounded">')
  391. .attr('src', Crowi.userPicture(creator));
  392. var $commentCreator = $('<div class="page-comment-creator">')
  393. .text(creator.username);
  394. var $commentRevision = $('<a class="page-comment-revision label">')
  395. .attr('href', '?revision=' + revision)
  396. .text(revision.substr(0,8));
  397. if (revision !== revisionId) {
  398. $commentRevision.addClass('label-default');
  399. } else {
  400. $commentRevision.addClass('label-primary');
  401. }
  402. var $commentMeta = $('<div class="page-comment-meta">')
  403. .text(commentedAt + ' ')
  404. .append($commentRevision);
  405. var $commentBody = $('<div class="page-comment-body">')
  406. .html(comment.replace(/(\r\n|\r|\n)/g, '<br>'));
  407. var $commentMain = $('<div class="page-comment-main">')
  408. .append($commentCreator)
  409. .append($commentBody)
  410. .append($commentMeta)
  411. $comment.addClass('page-comment');
  412. if (creator._id === currentUser) {
  413. $comment.addClass('page-comment-me');
  414. }
  415. if (revision !== revisionId) {
  416. $comment.addClass('page-comment-old');
  417. }
  418. $comment
  419. .append($commentImage)
  420. .append($commentMain);
  421. return $comment;
  422. }
  423. // get comments
  424. var $pageCommentList = $('.page-comments-list');
  425. var $pageCommentListNewer = $('#page-comments-list-newer');
  426. var $pageCommentListCurrent = $('#page-comments-list-current');
  427. var $pageCommentListOlder = $('#page-comments-list-older');
  428. var hasNewer = false;
  429. var hasOlder = false;
  430. $.get('/_api/comments.get', {page_id: pageId}, function(res) {
  431. if (res.ok) {
  432. var comments = res.comments;
  433. $.each(comments, function(i, comment) {
  434. var commentContent = createCommentHTML(comment.revision, comment.creator, comment.comment, comment.createdAt);
  435. if (comment.revision == revisionId) {
  436. $pageCommentListCurrent.append(commentContent);
  437. } else {
  438. if (Date.parse(comment.createdAt)/1000 > revisionCreatedAt) {
  439. $pageCommentListNewer.append(commentContent);
  440. hasNewer = true;
  441. } else {
  442. $pageCommentListOlder.append(commentContent);
  443. hasOlder = true;
  444. }
  445. }
  446. });
  447. }
  448. }).fail(function(data) {
  449. }).always(function() {
  450. if (!hasNewer) {
  451. $('.page-comments-list-toggle-newer').hide();
  452. }
  453. if (!hasOlder) {
  454. $pageCommentListOlder.addClass('collapse');
  455. $('.page-comments-list-toggle-older').hide();
  456. }
  457. });
  458. // post comment event
  459. $('#page-comment-form').on('submit', function() {
  460. var $button = $('#comment-form-button');
  461. $button.attr('disabled', 'disabled');
  462. $.post('/_api/comments.add', $(this).serialize(), function(data) {
  463. $button.prop('disabled', false);
  464. if (data.ok) {
  465. var comment = data.comment;
  466. $pageCommentList.prepend(createCommentHTML(comment.revision, comment.creator, comment.comment, comment.createdAt));
  467. $('#comment-form-comment').val('');
  468. $('#comment-form-message').text('');
  469. } else {
  470. $('#comment-form-message').text(data.error);
  471. }
  472. }).fail(function(data) {
  473. if (data.status !== 200) {
  474. $('#comment-form-message').text(data.statusText);
  475. }
  476. });
  477. return false;
  478. });
  479. // attachment
  480. var $pageAttachmentList = $('.page-attachments ul');
  481. $.get('/_api/attachments.list', {page_id: pageId}, function(res) {
  482. if (!res.ok) {
  483. return ;
  484. }
  485. var attachments = res.attachments;
  486. if (attachments.length > 0) {
  487. $.each(attachments, function(i, file) {
  488. $pageAttachmentList.append(
  489. '<li><a href="' + file.fileUrl + '">' + (file.originalName || file.fileName) + '</a> <span class="label label-default">' + file.fileFormat + '</span></li>'
  490. );
  491. })
  492. } else {
  493. $('.page-attachments').remove();
  494. }
  495. });
  496. // bookmark
  497. var $bookmarkButton = $('#bookmark-button');
  498. $.get('/_api/bookmarks.get', {page_id: pageId}, function(res) {
  499. if (res.ok) {
  500. if (res.bookmark) {
  501. MarkBookmarked();
  502. }
  503. }
  504. });
  505. $bookmarkButton.click(function() {
  506. var bookmarked = $bookmarkButton.data('bookmarked');
  507. var token = $bookmarkButton.data('csrftoken');
  508. if (!bookmarked) {
  509. $.post('/_api/bookmarks.add', {_csrf: token, page_id: pageId}, function(res) {
  510. if (res.ok && res.bookmark) {
  511. MarkBookmarked();
  512. }
  513. });
  514. } else {
  515. $.post('/_api/bookmarks.remove', {_csrf: token, page_id: pageId}, function(res) {
  516. if (res.ok) {
  517. MarkUnBookmarked();
  518. }
  519. });
  520. }
  521. return false;
  522. });
  523. function MarkBookmarked()
  524. {
  525. $('i', $bookmarkButton)
  526. .removeClass('fa-star-o')
  527. .addClass('fa-star');
  528. $bookmarkButton.data('bookmarked', 1);
  529. }
  530. function MarkUnBookmarked()
  531. {
  532. $('i', $bookmarkButton)
  533. .removeClass('fa-star')
  534. .addClass('fa-star-o');
  535. $bookmarkButton.data('bookmarked', 0);
  536. }
  537. // Like
  538. var $likeButton = $('.like-button');
  539. var $likeCount = $('#like-count');
  540. $likeButton.click(function() {
  541. var liked = $likeButton.data('liked');
  542. var token = $likeButton.data('csrftoken');
  543. if (!liked) {
  544. $.post('/_api/likes.add', {_csrf: token, page_id: pageId}, function(res) {
  545. if (res.ok) {
  546. MarkLiked();
  547. }
  548. });
  549. } else {
  550. $.post('/_api/likes.remove', {_csrf: token, page_id: pageId}, function(res) {
  551. if (res.ok) {
  552. MarkUnLiked();
  553. }
  554. });
  555. }
  556. return false;
  557. });
  558. var $likerList = $("#liker-list");
  559. var likers = $likerList.data('likers');
  560. if (likers && likers.length > 0) {
  561. // FIXME: user data cache
  562. $.get('/_api/users.list', {user_ids: likers}, function(res) {
  563. // ignore unless response has error
  564. if (res.ok) {
  565. AddToLikers(res.users);
  566. }
  567. });
  568. }
  569. function AddToLikers (users) {
  570. $.each(users, function(i, user) {
  571. $likerList.append(CreateUserLinkWithPicture(user));
  572. });
  573. }
  574. function MarkLiked()
  575. {
  576. $likeButton.addClass('active');
  577. $likeButton.data('liked', 1);
  578. $likeCount.text(parseInt($likeCount.text()) + 1);
  579. }
  580. function MarkUnLiked()
  581. {
  582. $likeButton.removeClass('active');
  583. $likeButton.data('liked', 0);
  584. $likeCount.text(parseInt($likeCount.text()) - 1);
  585. }
  586. if (!isSeen) {
  587. $.post('/_api/pages.seen', {page_id: pageId}, function(res) {
  588. // ignore unless response has error
  589. if (res.ok && res.seenUser) {
  590. $('#content-main').data('page-is-seen', 1);
  591. }
  592. });
  593. }
  594. function CreateUserLinkWithPicture (user) {
  595. var $userHtml = $('<a>');
  596. $userHtml.data('user-id', user._id);
  597. $userHtml.attr('href', '/user/' + user.username);
  598. $userHtml.attr('title', user.name);
  599. var $userPicture = $('<img class="picture picture-xs picture-rounded">');
  600. $userPicture.attr('alt', user.name);
  601. $userPicture.attr('src', Crowi.userPicture(user));
  602. $userHtml.append($userPicture);
  603. return $userHtml;
  604. }
  605. // presentation
  606. var presentaionInitialized = false
  607. , $b = $('body');
  608. $(document).on('click', '.toggle-presentation', function(e) {
  609. var $a = $(this);
  610. e.preventDefault();
  611. $b.toggleClass('overlay-on');
  612. if (!presentaionInitialized) {
  613. presentaionInitialized = true;
  614. $('<iframe />').attr({
  615. src: $a.attr('href')
  616. }).appendTo($('#presentation-container'));
  617. }
  618. }).on('click', '.fullscreen-layer', function() {
  619. $b.toggleClass('overlay-on');
  620. });
  621. //
  622. var me = $('body').data('me');
  623. var socket = io();
  624. socket.on('page edited', function (data) {
  625. if (data.user._id != me
  626. && data.page.path == pagePath) {
  627. $('#notifPageEdited').show();
  628. $('#notifPageEdited .edited-user').html(data.user.name);
  629. }
  630. });
  631. } // end if pageId
  632. // hash handling
  633. $('a[data-toggle="tab"][href="#revision-history"]').on('show.bs.tab', function() {
  634. window.history.pushState('', 'History', '#revision-history');
  635. });
  636. $('a[data-toggle="tab"][href="#edit-form"]').on('show.bs.tab', function() {
  637. window.history.pushState('', 'Edit', '#edit-form');
  638. });
  639. $('a[data-toggle="tab"][href="#revision-body"]').on('show.bs.tab', function() {
  640. window.history.pushState('', '', location.href.replace(location.hash, ''));
  641. });
  642. });
  643. Crowi.getRevisionBodyContent = function() {
  644. return $('#revision-body-content').html();
  645. }
  646. Crowi.replaceRevisionBodyContent = function(html) {
  647. $('#revision-body-content').html(html);
  648. }
  649. Crowi.findHashFromUrl = function(url)
  650. {
  651. var match;
  652. if (match = url.match(/#(.+)$/)) {
  653. return '#' + match[1];
  654. }
  655. return "";
  656. }
  657. Crowi.unhighlightSelectedSection = function(hash)
  658. {
  659. if (!hash || hash == "" || !hash.match(/^#head.+/)) {
  660. // とりあえず head* だけ (検索結果ページで副作用出た
  661. return true;
  662. }
  663. $(hash).removeClass('highlighted');
  664. }
  665. Crowi.highlightSelectedSection = function(hash)
  666. {
  667. if (!hash || hash == "" || !hash.match(/^#head.+/)) {
  668. // とりあえず head* だけ (検索結果ページで副作用出た
  669. return true;
  670. }
  671. $(hash).addClass('highlighted');
  672. }
  673. window.addEventListener('load', function(e) {
  674. // hash on page
  675. if (location.hash) {
  676. if (location.hash == '#edit-form') {
  677. $('a[data-toggle="tab"][href="#edit-form"]').tab('show');
  678. }
  679. if (location.hash == '#revision-history') {
  680. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  681. }
  682. }
  683. if (crowi && crowi.users || crowi.users.length == 0) {
  684. var totalUsers = crowi.users.length;
  685. var $listLiker = $('.page-list-liker');
  686. $listLiker.each(function(i, liker) {
  687. var count = $(liker).data('count') || 0;
  688. if (count/totalUsers > 0.05) {
  689. $(liker).addClass('popular-page-high');
  690. // 5%
  691. } else if (count/totalUsers > 0.02) {
  692. $(liker).addClass('popular-page-mid');
  693. // 2%
  694. } else if (count/totalUsers > 0.005) {
  695. $(liker).addClass('popular-page-low');
  696. // 0.5%
  697. }
  698. });
  699. var $listSeer = $('.page-list-seer');
  700. $listSeer.each(function(i, seer) {
  701. var count = $(seer).data('count') || 0;
  702. if (count/totalUsers > 0.10) {
  703. // 10%
  704. $(seer).addClass('popular-page-high');
  705. } else if (count/totalUsers > 0.05) {
  706. // 5%
  707. $(seer).addClass('popular-page-mid');
  708. } else if (count/totalUsers > 0.02) {
  709. // 2%
  710. $(seer).addClass('popular-page-low');
  711. }
  712. });
  713. }
  714. Crowi.highlightSelectedSection(location.hash);
  715. Crowi.modifyScrollTop();
  716. });
  717. window.addEventListener('hashchange', function(e) {
  718. Crowi.unhighlightSelectedSection(Crowi.findHashFromUrl(e.oldURL));
  719. Crowi.highlightSelectedSection(Crowi.findHashFromUrl(e.newURL));
  720. Crowi.modifyScrollTop();
  721. // hash on page
  722. if (location.hash) {
  723. if (location.hash == '#edit-form') {
  724. $('a[data-toggle="tab"][href="#edit-form"]').tab('show');
  725. }
  726. if (location.hash == '#revision-history') {
  727. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  728. }
  729. }
  730. if (location.hash == '' || location.hash.match(/^#head.+/)) {
  731. $('a[data-toggle="tab"][href="#revision-body"]').tab('show');
  732. }
  733. });