crowi.js 25 KB

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