crowi.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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 = {
  363. markdown,
  364. parsedHTML,
  365. currentPagePath: decodeURIComponent(location.pathname)
  366. };
  367. // process interceptors for pre rendering
  368. crowi.interceptorManager.process('preRender', context) // process with the context
  369. // render HTML with jQuery
  370. .then(() => {
  371. $('#revision-body-content').html(context.parsedHTML);
  372. $('.template-create-button').on('click', function() {
  373. var path = $(this).data('path');
  374. var templateId = $(this).data('template');
  375. var template = $('#' + templateId).html();
  376. crowi.saveDraft(path, template);
  377. top.location.href = path;
  378. });
  379. Crowi.correctHeaders('#revision-body-content');
  380. Crowi.revisionToc('#revision-body-content', '#revision-toc');
  381. Promise.resolve($('#revision-body-content'));
  382. })
  383. // process interceptors for post rendering
  384. .then((bodyElement) => {
  385. context = Object.assign(context, {bodyElement})
  386. return crowi.interceptorManager.process('postRender', context);
  387. });
  388. }
  389. // header
  390. var $header = $('#page-header');
  391. if ($header.length > 0) {
  392. var headerHeight = $header.outerHeight(true);
  393. $('.header-wrap').css({height: (headerHeight + 16) + 'px'});
  394. $header.affix({
  395. offset: {
  396. top: function() {
  397. return headerHeight + 86; // (54 header + 16 header padding-top + 16 content padding-top)
  398. }
  399. }
  400. });
  401. $('[data-affix-disable]').on('click', function(e) {
  402. var $elm = $($(this).data('affix-disable'));
  403. $(window).off('.affix');
  404. $elm.removeData('affix').removeClass('affix affix-top affix-bottom');
  405. return false;
  406. });
  407. }
  408. // omg
  409. function createCommentHTML(revision, creator, comment, commentedAt) {
  410. var $comment = $('<div>');
  411. var $commentImage = $('<img class="picture picture-rounded">')
  412. .attr('src', Crowi.userPicture(creator));
  413. var $commentCreator = $('<div class="page-comment-creator">')
  414. .text(creator.username);
  415. var $commentRevision = $('<a class="page-comment-revision label">')
  416. .attr('href', '?revision=' + revision)
  417. .text(revision.substr(0,8));
  418. if (revision !== revisionId) {
  419. $commentRevision.addClass('label-default');
  420. } else {
  421. $commentRevision.addClass('label-primary');
  422. }
  423. var $commentMeta = $('<div class="page-comment-meta">')
  424. .text(commentedAt + ' ')
  425. .append($commentRevision);
  426. var $commentBody = $('<div class="page-comment-body">')
  427. .html(comment.replace(/(\r\n|\r|\n)/g, '<br>'));
  428. var $commentMain = $('<div class="page-comment-main">')
  429. .append($commentCreator)
  430. .append($commentBody)
  431. .append($commentMeta)
  432. $comment.addClass('page-comment');
  433. if (creator._id === currentUser) {
  434. $comment.addClass('page-comment-me');
  435. }
  436. if (revision !== revisionId) {
  437. $comment.addClass('page-comment-old');
  438. }
  439. $comment
  440. .append($commentImage)
  441. .append($commentMain);
  442. return $comment;
  443. }
  444. // get comments
  445. var $pageCommentList = $('.page-comments-list');
  446. var $pageCommentListNewer = $('#page-comments-list-newer');
  447. var $pageCommentListCurrent = $('#page-comments-list-current');
  448. var $pageCommentListOlder = $('#page-comments-list-older');
  449. var hasNewer = false;
  450. var hasOlder = false;
  451. $.get('/_api/comments.get', {page_id: pageId}, function(res) {
  452. if (res.ok) {
  453. var comments = res.comments;
  454. $.each(comments, function(i, comment) {
  455. var commentContent = createCommentHTML(comment.revision, comment.creator, comment.comment, comment.createdAt);
  456. if (comment.revision == revisionId) {
  457. $pageCommentListCurrent.append(commentContent);
  458. } else {
  459. if (Date.parse(comment.createdAt)/1000 > revisionCreatedAt) {
  460. $pageCommentListNewer.append(commentContent);
  461. hasNewer = true;
  462. } else {
  463. $pageCommentListOlder.append(commentContent);
  464. hasOlder = true;
  465. }
  466. }
  467. });
  468. }
  469. }).fail(function(data) {
  470. }).always(function() {
  471. if (!hasNewer) {
  472. $('.page-comments-list-toggle-newer').hide();
  473. }
  474. if (!hasOlder) {
  475. $pageCommentListOlder.addClass('collapse');
  476. $('.page-comments-list-toggle-older').hide();
  477. }
  478. });
  479. // post comment event
  480. $('#page-comment-form').on('submit', function() {
  481. var $button = $('#comment-form-button');
  482. $button.attr('disabled', 'disabled');
  483. $.post('/_api/comments.add', $(this).serialize(), function(data) {
  484. $button.prop('disabled', false);
  485. if (data.ok) {
  486. var comment = data.comment;
  487. $pageCommentList.prepend(createCommentHTML(comment.revision, comment.creator, comment.comment, comment.createdAt));
  488. $('#comment-form-comment').val('');
  489. $('#comment-form-message').text('');
  490. } else {
  491. $('#comment-form-message').text(data.error);
  492. }
  493. }).fail(function(data) {
  494. if (data.status !== 200) {
  495. $('#comment-form-message').text(data.statusText);
  496. }
  497. });
  498. return false;
  499. });
  500. // attachment
  501. var $pageAttachmentList = $('.page-attachments ul');
  502. $.get('/_api/attachments.list', {page_id: pageId}, function(res) {
  503. if (!res.ok) {
  504. return ;
  505. }
  506. var attachments = res.attachments;
  507. if (attachments.length > 0) {
  508. $.each(attachments, function(i, file) {
  509. $pageAttachmentList.append(
  510. '<li><a href="' + file.fileUrl + '">' + (file.originalName || file.fileName) + '</a> <span class="label label-default">' + file.fileFormat + '</span></li>'
  511. );
  512. })
  513. } else {
  514. $('.page-attachments').remove();
  515. }
  516. });
  517. // bookmark
  518. var $bookmarkButton = $('#bookmark-button');
  519. $.get('/_api/bookmarks.get', {page_id: pageId}, function(res) {
  520. if (res.ok) {
  521. if (res.bookmark) {
  522. MarkBookmarked();
  523. }
  524. }
  525. });
  526. $bookmarkButton.click(function() {
  527. var bookmarked = $bookmarkButton.data('bookmarked');
  528. var token = $bookmarkButton.data('csrftoken');
  529. if (!bookmarked) {
  530. $.post('/_api/bookmarks.add', {_csrf: token, page_id: pageId}, function(res) {
  531. if (res.ok && res.bookmark) {
  532. MarkBookmarked();
  533. }
  534. });
  535. } else {
  536. $.post('/_api/bookmarks.remove', {_csrf: token, page_id: pageId}, function(res) {
  537. if (res.ok) {
  538. MarkUnBookmarked();
  539. }
  540. });
  541. }
  542. return false;
  543. });
  544. function MarkBookmarked()
  545. {
  546. $('i', $bookmarkButton)
  547. .removeClass('fa-star-o')
  548. .addClass('fa-star');
  549. $bookmarkButton.data('bookmarked', 1);
  550. }
  551. function MarkUnBookmarked()
  552. {
  553. $('i', $bookmarkButton)
  554. .removeClass('fa-star')
  555. .addClass('fa-star-o');
  556. $bookmarkButton.data('bookmarked', 0);
  557. }
  558. // Like
  559. var $likeButton = $('.like-button');
  560. var $likeCount = $('#like-count');
  561. $likeButton.click(function() {
  562. var liked = $likeButton.data('liked');
  563. var token = $likeButton.data('csrftoken');
  564. if (!liked) {
  565. $.post('/_api/likes.add', {_csrf: token, page_id: pageId}, function(res) {
  566. if (res.ok) {
  567. MarkLiked();
  568. }
  569. });
  570. } else {
  571. $.post('/_api/likes.remove', {_csrf: token, page_id: pageId}, function(res) {
  572. if (res.ok) {
  573. MarkUnLiked();
  574. }
  575. });
  576. }
  577. return false;
  578. });
  579. var $likerList = $("#liker-list");
  580. var likers = $likerList.data('likers');
  581. if (likers && likers.length > 0) {
  582. // FIXME: user data cache
  583. $.get('/_api/users.list', {user_ids: likers}, function(res) {
  584. // ignore unless response has error
  585. if (res.ok) {
  586. AddToLikers(res.users);
  587. }
  588. });
  589. }
  590. function AddToLikers (users) {
  591. $.each(users, function(i, user) {
  592. $likerList.append(CreateUserLinkWithPicture(user));
  593. });
  594. }
  595. function MarkLiked()
  596. {
  597. $likeButton.addClass('active');
  598. $likeButton.data('liked', 1);
  599. $likeCount.text(parseInt($likeCount.text()) + 1);
  600. }
  601. function MarkUnLiked()
  602. {
  603. $likeButton.removeClass('active');
  604. $likeButton.data('liked', 0);
  605. $likeCount.text(parseInt($likeCount.text()) - 1);
  606. }
  607. if (!isSeen) {
  608. $.post('/_api/pages.seen', {page_id: pageId}, function(res) {
  609. // ignore unless response has error
  610. if (res.ok && res.seenUser) {
  611. $('#content-main').data('page-is-seen', 1);
  612. }
  613. });
  614. }
  615. function CreateUserLinkWithPicture (user) {
  616. var $userHtml = $('<a>');
  617. $userHtml.data('user-id', user._id);
  618. $userHtml.attr('href', '/user/' + user.username);
  619. $userHtml.attr('title', user.name);
  620. var $userPicture = $('<img class="picture picture-xs picture-rounded">');
  621. $userPicture.attr('alt', user.name);
  622. $userPicture.attr('src', Crowi.userPicture(user));
  623. $userHtml.append($userPicture);
  624. return $userHtml;
  625. }
  626. // presentation
  627. var presentaionInitialized = false
  628. , $b = $('body');
  629. $(document).on('click', '.toggle-presentation', function(e) {
  630. var $a = $(this);
  631. e.preventDefault();
  632. $b.toggleClass('overlay-on');
  633. if (!presentaionInitialized) {
  634. presentaionInitialized = true;
  635. $('<iframe />').attr({
  636. src: $a.attr('href')
  637. }).appendTo($('#presentation-container'));
  638. }
  639. }).on('click', '.fullscreen-layer', function() {
  640. $b.toggleClass('overlay-on');
  641. });
  642. //
  643. var me = $('body').data('me');
  644. var socket = io();
  645. socket.on('page edited', function (data) {
  646. if (data.user._id != me
  647. && data.page.path == pagePath) {
  648. $('#notifPageEdited').show();
  649. $('#notifPageEdited .edited-user').html(data.user.name);
  650. }
  651. });
  652. } // end if pageId
  653. // hash handling
  654. $('a[data-toggle="tab"][href="#revision-history"]').on('show.bs.tab', function() {
  655. window.history.pushState('', 'History', '#revision-history');
  656. });
  657. $('a[data-toggle="tab"][href="#edit-form"]').on('show.bs.tab', function() {
  658. window.history.pushState('', 'Edit', '#edit-form');
  659. });
  660. $('a[data-toggle="tab"][href="#revision-body"]').on('show.bs.tab', function() {
  661. window.history.pushState('', '', location.href.replace(location.hash, ''));
  662. });
  663. });
  664. Crowi.getRevisionBodyContent = function() {
  665. return $('#revision-body-content').html();
  666. }
  667. Crowi.replaceRevisionBodyContent = function(html) {
  668. $('#revision-body-content').html(html);
  669. }
  670. Crowi.findHashFromUrl = function(url)
  671. {
  672. var match;
  673. if (match = url.match(/#(.+)$/)) {
  674. return '#' + match[1];
  675. }
  676. return "";
  677. }
  678. Crowi.unhighlightSelectedSection = function(hash)
  679. {
  680. if (!hash || hash == "" || !hash.match(/^#head.+/)) {
  681. // とりあえず head* だけ (検索結果ページで副作用出た
  682. return true;
  683. }
  684. $(hash).removeClass('highlighted');
  685. }
  686. Crowi.highlightSelectedSection = function(hash)
  687. {
  688. if (!hash || hash == "" || !hash.match(/^#head.+/)) {
  689. // とりあえず head* だけ (検索結果ページで副作用出た
  690. return true;
  691. }
  692. $(hash).addClass('highlighted');
  693. }
  694. window.addEventListener('load', function(e) {
  695. // hash on page
  696. if (location.hash) {
  697. if (location.hash == '#edit-form') {
  698. $('a[data-toggle="tab"][href="#edit-form"]').tab('show');
  699. }
  700. if (location.hash == '#revision-history') {
  701. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  702. }
  703. }
  704. if (crowi && crowi.users || crowi.users.length == 0) {
  705. var totalUsers = crowi.users.length;
  706. var $listLiker = $('.page-list-liker');
  707. $listLiker.each(function(i, liker) {
  708. var count = $(liker).data('count') || 0;
  709. if (count/totalUsers > 0.05) {
  710. $(liker).addClass('popular-page-high');
  711. // 5%
  712. } else if (count/totalUsers > 0.02) {
  713. $(liker).addClass('popular-page-mid');
  714. // 2%
  715. } else if (count/totalUsers > 0.005) {
  716. $(liker).addClass('popular-page-low');
  717. // 0.5%
  718. }
  719. });
  720. var $listSeer = $('.page-list-seer');
  721. $listSeer.each(function(i, seer) {
  722. var count = $(seer).data('count') || 0;
  723. if (count/totalUsers > 0.10) {
  724. // 10%
  725. $(seer).addClass('popular-page-high');
  726. } else if (count/totalUsers > 0.05) {
  727. // 5%
  728. $(seer).addClass('popular-page-mid');
  729. } else if (count/totalUsers > 0.02) {
  730. // 2%
  731. $(seer).addClass('popular-page-low');
  732. }
  733. });
  734. }
  735. Crowi.highlightSelectedSection(location.hash);
  736. Crowi.modifyScrollTop();
  737. });
  738. window.addEventListener('hashchange', function(e) {
  739. Crowi.unhighlightSelectedSection(Crowi.findHashFromUrl(e.oldURL));
  740. Crowi.highlightSelectedSection(Crowi.findHashFromUrl(e.newURL));
  741. Crowi.modifyScrollTop();
  742. // hash on page
  743. if (location.hash) {
  744. if (location.hash == '#edit-form') {
  745. $('a[data-toggle="tab"][href="#edit-form"]').tab('show');
  746. }
  747. if (location.hash == '#revision-history') {
  748. $('a[data-toggle="tab"][href="#revision-history"]').tab('show');
  749. }
  750. }
  751. if (location.hash == '' || location.hash.match(/^#head.+/)) {
  752. $('a[data-toggle="tab"][href="#revision-body"]').tab('show');
  753. }
  754. });