crowi.js 25 KB

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