crowi.js 25 KB

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