crowi.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /* jshint browser: true, jquery: true */
  2. /* global FB, marked */
  3. /* Author: Sotaro KARASAWA <sotarok@crocos.co.jp>
  4. */
  5. var hljs = require('highlight.js');
  6. var jsdiff = require('diff');
  7. var marked = require('marked');
  8. var Crowi = {};
  9. if (!window) {
  10. window = {};
  11. }
  12. window.Crowi = Crowi;
  13. Crowi.createErrorView = function(msg) {
  14. $('#main').prepend($('<p class="alert-message error">' + msg + '</p>'));
  15. };
  16. Crowi.linkPath = function(revisionPath) {
  17. var $revisionPath = revisionPath || '#revision-path';
  18. var $title = $($revisionPath);
  19. var pathData = $('#content-main').data('path');
  20. if (!pathData) {
  21. return ;
  22. }
  23. var realPath = pathData.trim();
  24. if (realPath.substr(-1, 1) == '/') {
  25. realPath = realPath.substr(0, realPath.length - 1);
  26. }
  27. var path = '';
  28. var pathHtml = '';
  29. var splittedPath = realPath.split(/\//);
  30. splittedPath.shift();
  31. splittedPath.forEach(function(sub) {
  32. path += '/';
  33. pathHtml += ' <a href="' + path + '">/</a> ';
  34. if (sub) {
  35. path += sub;
  36. pathHtml += '<a href="' + path + '">' + sub + '</a>';
  37. }
  38. });
  39. if (path.substr(-1, 1) != '/') {
  40. path += '/';
  41. pathHtml += ' <a href="' + path + '" class="last-path">/</a>';
  42. }
  43. $title.html(pathHtml);
  44. };
  45. Crowi.correctHeaders = function(contentId) {
  46. // h1 ~ h6 の id 名を補正する
  47. var $content = $(contentId || '#revision-body-content');
  48. var i = 0;
  49. $('h1,h2,h3,h4,h5,h6', $content).each(function(idx, elm) {
  50. var id = 'head' + i++;
  51. $(this).attr('id', id);
  52. $(this).addClass('revision-head');
  53. $(this).append('<span class="revision-head-link"><a href="#' + id +'"><i class="fa fa-link"></i></a></span>');
  54. });
  55. };
  56. Crowi.revisionToc = function(contentId, tocId) {
  57. var $content = $(contentId || '#revision-body-content');
  58. var $tocId = $(tocId || '#revision-toc');
  59. var $tocContent = $('<div id="revision-toc-content" class="revision-toc-content collapse"></div>');
  60. $tocId.append($tocContent);
  61. $('h1', $content).each(function(idx, elm) {
  62. var id = $(this).attr('id');
  63. var title = $(this).text();
  64. var selector = '#' + id + ' ~ h2:not(#' + id + ' ~ h1 ~ h2)';
  65. var $toc = $('<ul></ul>');
  66. var $tocLi = $('<li><a href="#' + id +'">' + title + '</a></li>');
  67. $tocContent.append($toc);
  68. $toc.append($tocLi);
  69. $(selector).each(function()
  70. {
  71. var id2 = $(this).attr('id');
  72. var title2 = $(this).text();
  73. var selector2 = '#' + id2 + ' ~ h3:not(#' + id2 + ' ~ h2 ~ h3)';
  74. var $toc2 = $('<ul></ul>');
  75. var $tocLi2 = $('<li><a href="#' + id2 +'">' + title2 + '</a></li>');
  76. $tocLi.append($toc2);
  77. $toc2.append($tocLi2);
  78. $(selector2).each(function()
  79. {
  80. var id3 = $(this).attr('id');
  81. var title3 = $(this).text();
  82. var $toc3 = $('<ul></ul>');
  83. var $tocLi3 = $('<li><a href="#' + id3 +'">' + title3 + '</a></li>');
  84. $tocLi2.append($toc3);
  85. $toc3.append($tocLi3);
  86. });
  87. });
  88. });
  89. };
  90. Crowi.escape = function(s) {
  91. s = s.replace(/&/g, '&amp;')
  92. .replace(/</g, '&lt;')
  93. .replace(/>/g, '&gt;')
  94. .replace(/'/g, '&#39;')
  95. .replace(/"/g, '&quot;')
  96. ;
  97. return s;
  98. };
  99. Crowi.unescape = function(s) {
  100. s = s.replace(/&nbsp;/g, ' ')
  101. .replace(/&amp;/g, '&')
  102. .replace(/&lt;/g, '<')
  103. .replace(/&gt;/g, '>')
  104. .replace(/&#39;/g, '\'')
  105. .replace(/&quot;/g, '"')
  106. ;
  107. return s;
  108. };
  109. Crowi.getRendererType = function() {
  110. return new Crowi.rendererType.markdown();
  111. };
  112. Crowi.rendererType = {};
  113. Crowi.rendererType.markdown = function(){};
  114. Crowi.rendererType.markdown.prototype = {
  115. render: function(contentText) {
  116. marked.setOptions({
  117. gfm: true,
  118. highlight: function (code, lang, callback) {
  119. var result, hl;
  120. if (lang) {
  121. try {
  122. hl = hljs.highlight(lang, code);
  123. result = hl.value;
  124. } catch (e) {
  125. result = code;
  126. }
  127. } else {
  128. //result = hljs.highlightAuto(code);
  129. //callback(null, result.value);
  130. result = code;
  131. }
  132. return callback(null, result);
  133. },
  134. tables: true,
  135. breaks: true,
  136. pedantic: false,
  137. sanitize: false,
  138. smartLists: true,
  139. smartypants: false,
  140. langPrefix: 'lang-'
  141. });
  142. var contentHtml = Crowi.unescape(contentText);
  143. // TODO 前処理系のプラグイン化
  144. contentHtml = this.preFormatMarkdown(contentHtml);
  145. contentHtml = this.expandImage(contentHtml);
  146. contentHtml = this.link(contentHtml);
  147. var $body = this.$revisionBody;
  148. // Using async version of marked
  149. marked(contentHtml, {}, function (err, content) {
  150. if (err) {
  151. throw err;
  152. }
  153. $body.html(content);
  154. });
  155. },
  156. preFormatMarkdown: function(content){
  157. var x = content
  158. .replace(/^(#{1,})([^\s]+)?(.*)$/gm, '$1 $2$3') // spacer for section
  159. .replace(/^(\s*)(\*|\-)([^\s]+)?(.*)$/gm, '$1$2 $3$4') // spacer for list
  160. .replace(/>[\s]*\n>[\s]*\n/g, '> <br>\n> \n');
  161. return x;
  162. },
  163. link: function (content) {
  164. return content
  165. //.replace(/\s(https?:\/\/[\S]+)/g, ' <a href="$1">$1</a>') // リンク
  166. .replace(/\s<((\/[^>]+?){2,})>/g, ' <a href="$1">$1</a>') // ページ間リンク: <> でかこまれてて / から始まり、 / が2個以上
  167. ;
  168. },
  169. expandImage: function (content) {
  170. return content.replace(/\s(https?:\/\/[\S]+\.(jpg|jpeg|gif|png))/g, ' <a href="$1"><img src="$1" class="auto-expanded-image"></a>');
  171. }
  172. };
  173. Crowi.renderer = function (contentText, revisionBody) {
  174. var $revisionBody = revisionBody || $('#revision-body-content');
  175. this.contentText = contentText;
  176. this.$revisionBody = $revisionBody;
  177. this.format = 'markdown'; // とりあえず
  178. this.renderer = Crowi.getRendererType();
  179. this.renderer.$revisionBody = this.$revisionBody;
  180. };
  181. Crowi.renderer.prototype = {
  182. render: function() {
  183. this.renderer.render(this.contentText);
  184. }
  185. };
  186. // original: middleware.swigFilter
  187. Crowi.userPicture = function (user) {
  188. if (!user) {
  189. return '/images/userpicture.png';
  190. }
  191. if (user.image && user.image != '/images/userpicture.png') {
  192. return user.image;
  193. } else if (user.fbId) {
  194. return '//graph.facebook.com/' + user.fbId + '/picture?size=square';
  195. } else {
  196. return '/images/userpicture.png';
  197. }
  198. };
  199. $(function() {
  200. var pageId = $('#content-main').data('page-id');
  201. var revisionId = $('#content-main').data('page-revision-id');
  202. var revisionCreatedAt = $('#content-main').data('page-revision-created');
  203. var currentUser = $('#content-main').data('current-user');
  204. var isSeen = $('#content-main').data('page-is-seen');
  205. var pagePath= $('#content-main').data('path');
  206. Crowi.linkPath();
  207. $('[data-toggle="tooltip"]').tooltip();
  208. $('[data-tooltip-stay]').tooltip('show');
  209. $('.copy-link').on('click', function () {
  210. $(this).select();
  211. });
  212. $('#createMemo').on('shown.bs.modal', function (e) {
  213. $('#memoName').focus();
  214. });
  215. $('#createMemoForm').submit(function(e)
  216. {
  217. var prefix = $('[name=memoNamePrefix]', this).val();
  218. var name = $('[name=memoName]', this).val();
  219. if (name === '') {
  220. prefix = prefix.slice(0, -1);
  221. }
  222. top.location.href = prefix + name;
  223. return false;
  224. });
  225. $('#renamePage').on('shown.bs.modal', function (e) {
  226. $('#newPageName').focus();
  227. });
  228. $('#renamePageForm').submit(function(e) {
  229. $.ajax({
  230. type: 'POST',
  231. url: '/_api/pages.rename',
  232. data: $('#renamePageForm').serialize(),
  233. dataType: 'json'
  234. }).done(function(res) {
  235. if (!res.ok) {
  236. $('#newPageNameCheck').html('<i class="fa fa-times-circle"></i> ' + res.error);
  237. $('#newPageNameCheck').addClass('alert-danger');
  238. } else {
  239. var page = res.page;
  240. var path = $('#pagePath').html();
  241. $('#newPageNameCheck').removeClass('alert-danger');
  242. $('#newPageNameCheck').html('<img src="/images/loading_s.gif"> 移動しました。移動先にジャンプします。');
  243. setTimeout(function() {
  244. top.location.href = page.path + '?renamed=' + path;
  245. }, 1000);
  246. }
  247. });
  248. return false;
  249. });
  250. $('#create-portal-button').on('click', function(e) {
  251. $('.portal').removeClass('hide');
  252. $('.content-main').addClass('on-edit');
  253. $('.portal a[data-toggle="tab"][href="#edit-form"]').tab('show');
  254. var path = $('.content-main').data('path');
  255. if (path != '/' && $('.content-main').data('page-id') == '') {
  256. var upperPage = path.substr(0, path.length - 1);
  257. $.get('/_api/pages.get', {path: upperPage}, function(res) {
  258. if (res.ok && res.page) {
  259. $('#portal-warning-modal').modal('show');
  260. }
  261. });
  262. }
  263. });
  264. $('#portal-form-close').on('click', function(e) {
  265. $('.portal').addClass('hide');
  266. $('.content-main').removeClass('on-edit');
  267. return false;
  268. });
  269. // list-link
  270. $('.page-list-link').each(function() {
  271. var $link = $(this);
  272. var text = $link.text();
  273. var path = $link.data('path');
  274. var shortPath = $link.data('short-path');
  275. $link.html(path.replace(new RegExp(shortPath + '(/)?$'), '<strong>' + shortPath + '$1</strong>'));
  276. });
  277. if (pageId) {
  278. // if page exists
  279. var $rawTextOriginal = $('#raw-text-original');
  280. if ($rawTextOriginal.length > 0) {
  281. var renderer = new Crowi.renderer($('#raw-text-original').html());
  282. renderer.render();
  283. Crowi.correctHeaders('#revision-body-content');
  284. Crowi.revisionToc('#revision-body-content', '#revision-toc');
  285. }
  286. // header
  287. var $header = $('#page-header');
  288. if ($header.length > 0) {
  289. var headerHeight = $header.outerHeight(true);
  290. $('.header-wrap').css({height: (headerHeight + 16) + 'px'});
  291. $header.affix({
  292. offset: {
  293. top: function() {
  294. return headerHeight + 86; // (54 header + 16 header padding-top + 16 content padding-top)
  295. }
  296. }
  297. });
  298. $('[data-affix-disable]').on('click', function(e) {
  299. $elm = $($(this).data('affix-disable'));
  300. $(window).off('.affix');
  301. $elm.removeData('affix').removeClass('affix affix-top affix-bottom');
  302. return false;
  303. });
  304. }
  305. // omg
  306. function createCommentHTML(revision, creator, comment, commentedAt) {
  307. var $comment = $('<div>');
  308. var $commentImage = $('<img class="picture picture-rounded">')
  309. .attr('src', Crowi.userPicture(creator));
  310. var $commentCreator = $('<div class="page-comment-creator">')
  311. .text(creator.username);
  312. var $commentRevision = $('<a class="page-comment-revision label">')
  313. .attr('href', '?revision=' + revision)
  314. .text(revision.substr(0,8));
  315. if (revision !== revisionId) {
  316. $commentRevision.addClass('label-default');
  317. } else {
  318. $commentRevision.addClass('label-primary');
  319. }
  320. var $commentMeta = $('<div class="page-comment-meta">')
  321. .text(commentedAt + ' ')
  322. .append($commentRevision);
  323. var $commentBody = $('<div class="page-comment-body">')
  324. .html(comment.replace(/(\r\n|\r|\n)/g, '<br>'));
  325. var $commentMain = $('<div class="page-comment-main">')
  326. .append($commentCreator)
  327. .append($commentBody)
  328. .append($commentMeta)
  329. $comment.addClass('page-comment');
  330. if (creator._id === currentUser) {
  331. $comment.addClass('page-comment-me');
  332. }
  333. if (revision !== revisionId) {
  334. $comment.addClass('page-comment-old');
  335. }
  336. $comment
  337. .append($commentImage)
  338. .append($commentMain);
  339. return $comment;
  340. }
  341. // get comments
  342. var $pageCommentList = $('.page-comments-list');
  343. var $pageCommentListNewer = $('#page-comments-list-newer');
  344. var $pageCommentListCurrent = $('#page-comments-list-current');
  345. var $pageCommentListOlder = $('#page-comments-list-older');
  346. var hasNewer = false;
  347. var hasOlder = false;
  348. $.get('/_api/comments.get', {page_id: pageId}, function(res) {
  349. if (res.ok) {
  350. var comments = res.comments;
  351. $.each(comments, function(i, comment) {
  352. var commentContent = createCommentHTML(comment.revision, comment.creator, comment.comment, comment.createdAt);
  353. if (comment.revision == revisionId) {
  354. $pageCommentListCurrent.append(commentContent);
  355. } else {
  356. if (Date.parse(comment.createdAt)/1000 > revisionCreatedAt) {
  357. $pageCommentListNewer.append(commentContent);
  358. hasNewer = true;
  359. } else {
  360. $pageCommentListOlder.append(commentContent);
  361. hasOlder = true;
  362. }
  363. }
  364. });
  365. }
  366. }).fail(function(data) {
  367. }).always(function() {
  368. if (!hasNewer) {
  369. $('.page-comments-list-toggle-newer').hide();
  370. }
  371. if (!hasOlder) {
  372. $pageCommentListOlder.addClass('collapse');
  373. $('.page-comments-list-toggle-older').hide();
  374. }
  375. });
  376. // post comment event
  377. $('#page-comment-form').on('submit', function() {
  378. $button = $('#commenf-form-button');
  379. $button.attr('disabled', 'disabled');
  380. $.post('/_api/comments.add', $(this).serialize(), function(data) {
  381. $button.removeAttr('disabled');
  382. if (data.ok) {
  383. var comment = data.comment;
  384. $pageCommentList.prepend(createCommentHTML(comment.revision, comment.creator, comment.comment, comment.createdAt));
  385. $('#comment-form-comment').val('');
  386. $('#comment-form-message').text('');
  387. } else {
  388. $('#comment-form-message').text(data.error);
  389. }
  390. }).fail(function(data) {
  391. if (data.status !== 200) {
  392. $('#comment-form-message').text(data.statusText);
  393. }
  394. });
  395. return false;
  396. });
  397. // attachment
  398. var $pageAttachmentList = $('.page-attachments ul');
  399. $.get('/_api/attachment/page/' + pageId, function(res) {
  400. var attachments = res.data.attachments;
  401. if (attachments.length > 0) {
  402. $.each(attachments, function(i, file) {
  403. $pageAttachmentList.append(
  404. '<li><a href="' + file.fileUrl + '">' + (file.originalName || file.fileName) + '</a> <span class="label label-default">' + file.fileFormat + '</span></li>'
  405. );
  406. })
  407. } else {
  408. $('.page-attachments').remove();
  409. }
  410. });
  411. // bookmark
  412. var $bookmarkButton = $('#bookmark-button');
  413. $.get('/_api/bookmarks.get', {page_id: pageId}, function(res) {
  414. if (res.ok) {
  415. if (res.bookmark) {
  416. MarkBookmarked();
  417. }
  418. }
  419. });
  420. $bookmarkButton.click(function() {
  421. var bookmarked = $bookmarkButton.data('bookmarked');
  422. if (!bookmarked) {
  423. $.post('/_api/bookmarks.add', {page_id: pageId}, function(res) {
  424. if (res.ok && res.bookmark) {
  425. MarkBookmarked();
  426. }
  427. });
  428. } else {
  429. $.post('/_api/bookmarks.remove', {page_id: pageId}, function(res) {
  430. if (res.ok) {
  431. MarkUnBookmarked();
  432. }
  433. });
  434. }
  435. return false;
  436. });
  437. function MarkBookmarked()
  438. {
  439. $('i', $bookmarkButton)
  440. .removeClass('fa-star-o')
  441. .addClass('fa-star');
  442. $bookmarkButton.data('bookmarked', 1);
  443. }
  444. function MarkUnBookmarked()
  445. {
  446. $('i', $bookmarkButton)
  447. .removeClass('fa-star')
  448. .addClass('fa-star-o');
  449. $bookmarkButton.data('bookmarked', 0);
  450. }
  451. // Like
  452. var $likeButton = $('#like-button');
  453. var $likeCount = $('#like-count');
  454. $likeButton.click(function() {
  455. var liked = $likeButton.data('liked');
  456. if (!liked) {
  457. $.post('/_api/likes.add', {page_id: pageId}, function(res) {
  458. if (res.ok) {
  459. MarkLiked();
  460. }
  461. });
  462. } else {
  463. $.post('/_api/likes.remove', {page_id: pageId}, function(res) {
  464. if (res.ok) {
  465. MarkUnLiked();
  466. }
  467. });
  468. }
  469. return false;
  470. });
  471. var $likerList = $("#liker-list");
  472. var likers = $likerList.data('likers');
  473. if (likers && likers.length > 0) {
  474. // FIXME: user data cache
  475. $.get('/_api/users.list', {user_ids: likers}, function(res) {
  476. // ignore unless response has error
  477. if (res.ok) {
  478. AddToLikers(res.users);
  479. }
  480. });
  481. }
  482. function AddToLikers (users) {
  483. $.each(users, function(i, user) {
  484. $likerList.append(CreateUserLinkWithPicture(user));
  485. });
  486. }
  487. function MarkLiked()
  488. {
  489. $likeButton.addClass('active');
  490. $likeButton.data('liked', 1);
  491. $likeCount.text(parseInt($likeCount.text()) + 1);
  492. }
  493. function MarkUnLiked()
  494. {
  495. $likeButton.removeClass('active');
  496. $likeButton.data('liked', 0);
  497. $likeCount.text(parseInt($likeCount.text()) - 1);
  498. }
  499. if (!isSeen) {
  500. $.post('/_api/pages.seen', {page_id: pageId}, function(res) {
  501. // ignore unless response has error
  502. if (res.ok && res.seenUser) {
  503. $('#content-main').data('page-is-seen', 1);
  504. }
  505. });
  506. }
  507. var $seenUserList = $("#seen-user-list");
  508. var seenUsers = $seenUserList.data('seen-users');
  509. var seenUsersArray = seenUsers.split(',');
  510. if (seenUsers && seenUsersArray.length > 0 && seenUsersArray.length <= 10) {
  511. // FIXME: user data cache
  512. $.get('/_api/users.list', {user_ids: seenUsers}, function(res) {
  513. // ignore unless response has error
  514. if (res.ok) {
  515. AddToSeenUser(res.users);
  516. }
  517. });
  518. }
  519. function CreateUserLinkWithPicture (user) {
  520. var $userHtml = $('<a>');
  521. $userHtml.data('user-id', user._id);
  522. $userHtml.attr('href', '/user/' + user.username);
  523. $userHtml.attr('title', user.name);
  524. var $userPicture = $('<img class="picture picture-xs picture-rounded">');
  525. $userPicture.attr('alt', user.name);
  526. $userPicture.attr('src', Crowi.userPicture(user));
  527. $userHtml.append($userPicture);
  528. return $userHtml;
  529. }
  530. function AddToSeenUser (users) {
  531. $.each(users, function(i, user) {
  532. $seenUserList.append(CreateUserLinkWithPicture(user));
  533. });
  534. }
  535. // History Diff
  536. var allRevisionIds = [];
  537. $.each($('.diff-view'), function() {
  538. allRevisionIds.push($(this).data('revisionId'));
  539. });
  540. $('.diff-view').on('click', function(e) {
  541. e.preventDefault();
  542. var getBeforeRevisionId = function(revisionId) {
  543. var currentPos = $.inArray(revisionId, allRevisionIds);
  544. if (currentPos < 0) {
  545. return false;
  546. }
  547. var beforeRevisionId = allRevisionIds[currentPos + 1];
  548. if (typeof beforeRevisionId === 'undefined') {
  549. return false;
  550. }
  551. return beforeRevisionId;
  552. };
  553. var revisionId = $(this).data('revisionId');
  554. var beforeRevisionId = getBeforeRevisionId(revisionId);
  555. var $diffDisplay = $('#diff-display-' + revisionId);
  556. var $diffIcon = $('#diff-icon-' + revisionId);
  557. if ($diffIcon.hasClass('fa-arrow-circle-right')) {
  558. $diffIcon.removeClass('fa-arrow-circle-right');
  559. $diffIcon.addClass('fa-arrow-circle-down');
  560. } else {
  561. $diffIcon.removeClass('fa-arrow-circle-down');
  562. $diffIcon.addClass('fa-arrow-circle-right');
  563. }
  564. if (beforeRevisionId === false) {
  565. $diffDisplay.text('差分はありません');
  566. $diffDisplay.slideToggle();
  567. } else {
  568. var revisionIds = revisionId + ',' + beforeRevisionId;
  569. $.ajax({
  570. type: 'GET',
  571. url: '/_api/revisions.list?revision_ids=' + revisionIds,
  572. dataType: 'json'
  573. }).done(function(res) {
  574. var currentText = res[0].body;
  575. var previousText = res[1].body;
  576. $diffDisplay.text('');
  577. var diff = jsdiff.diffLines(previousText, currentText);
  578. diff.forEach(function(part) {
  579. var color = part.added ? 'green' : part.removed ? 'red' : 'grey';
  580. var $span = $('<span>');
  581. $span.css('color', color);
  582. $span.text(part.value);
  583. $diffDisplay.append($span);
  584. });
  585. $diffDisplay.slideToggle();
  586. });
  587. }
  588. });
  589. // default open
  590. $('.diff-view').each(function(i, diffView) {
  591. if (i < 2) {
  592. $(diffView).click();
  593. }
  594. });
  595. }
  596. });