crowi.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. contentHtml = this.expandImage(contentHtml);
  144. contentHtml = this.link(contentHtml);
  145. var $body = this.$revisionBody;
  146. // Using async version of marked
  147. marked(contentHtml, {}, function (err, content) {
  148. if (err) {
  149. throw err;
  150. }
  151. $body.html(content);
  152. });
  153. },
  154. link: function (content) {
  155. return content
  156. //.replace(/\s(https?:\/\/[\S]+)/g, ' <a href="$1">$1</a>') // リンク
  157. .replace(/\s<((\/[^>]+?){2,})>/g, ' <a href="$1">$1</a>') // ページ間リンク: <> でかこまれてて / から始まり、 / が2個以上
  158. ;
  159. },
  160. expandImage: function (content) {
  161. return content.replace(/\s(https?:\/\/[\S]+\.(jpg|jpeg|gif|png))/g, ' <a href="$1"><img src="$1" class="auto-expanded-image"></a>');
  162. }
  163. };
  164. Crowi.renderer = function (contentText, revisionBody) {
  165. var $revisionBody = revisionBody || $('#revision-body-content');
  166. this.contentText = contentText;
  167. this.$revisionBody = $revisionBody;
  168. this.format = 'markdown'; // とりあえず
  169. this.renderer = Crowi.getRendererType();
  170. this.renderer.$revisionBody = this.$revisionBody;
  171. };
  172. Crowi.renderer.prototype = {
  173. render: function() {
  174. this.renderer.render(this.contentText);
  175. }
  176. };
  177. // original: middleware.swigFilter
  178. Crowi.userPicture = function (user) {
  179. if (!user) {
  180. return '/images/userpicture.png';
  181. }
  182. if (user.image && user.image != '/images/userpicture.png') {
  183. return user.image;
  184. } else if (user.fbId) {
  185. return '//graph.facebook.com/' + user.fbId + '/picture?size=square';
  186. } else {
  187. return '/images/userpicture.png';
  188. }
  189. };
  190. CrowiSearcher = function(path, $el) {
  191. this.$el = $el;
  192. this.path = path;
  193. this.searchResult = {};
  194. };
  195. CrowiSearcher.prototype.querySearch = function(keyword, option) {
  196. };
  197. CrowiSearcher.prototype.search = function(keyword) {
  198. var option = {};
  199. this.querySearch(keyword, option);
  200. this.$el.html(this.render());
  201. };
  202. CrowiSearcher.prototype.render = function() {
  203. return $('<div>');
  204. };
  205. $(function() {
  206. var pageId = $('#content-main').data('page-id');
  207. var revisionId = $('#content-main').data('page-revision-id');
  208. var revisionCreatedAt = $('#content-main').data('page-revision-created');
  209. var currentUser = $('#content-main').data('current-user');
  210. var isSeen = $('#content-main').data('page-is-seen');
  211. Crowi.linkPath();
  212. $('[data-toggle="tooltip"]').tooltip();
  213. $('[data-tooltip-stay]').tooltip('show');
  214. $('a[data-toggle="tab"][href="#edit-form"]').on('show.bs.tab', function() {
  215. $('.content-main').addClass('on-edit');
  216. });
  217. $('a[data-toggle="tab"][href="#edit-form"]').on('hide.bs.tab', function() {
  218. $('.content-main').removeClass('on-edit');
  219. });
  220. $('.copy-link').on('click', function () {
  221. $(this).select();
  222. });
  223. $('#createMemo').on('shown.bs.modal', function (e) {
  224. $('#memoName').focus();
  225. });
  226. $('#createMemoForm').submit(function(e)
  227. {
  228. var prefix = $('[name=memoNamePrefix]', this).val();
  229. var name = $('[name=memoName]', this).val();
  230. if (name === '') {
  231. prefix = prefix.slice(0, -1);
  232. }
  233. top.location.href = prefix + name;
  234. return false;
  235. });
  236. $('#renamePage').on('shown.bs.modal', function (e) {
  237. $('#newPageName').focus();
  238. });
  239. $('#renamePageForm').submit(function(e) {
  240. $.ajax({
  241. type: 'POST',
  242. url: '/_api/pages.rename',
  243. data: $('#renamePageForm').serialize(),
  244. dataType: 'json'
  245. }).done(function(res) {
  246. if (!res.ok) {
  247. $('#newPageNameCheck').html('<i class="fa fa-times-circle"></i> ' + res.error);
  248. $('#newPageNameCheck').addClass('alert-danger');
  249. } else {
  250. var page = res.page;
  251. var path = $('#pagePath').html();
  252. $('#newPageNameCheck').removeClass('alert-danger');
  253. $('#newPageNameCheck').html('<img src="/images/loading_s.gif"> 移動しました。移動先にジャンプします。');
  254. setTimeout(function() {
  255. top.location.href = page.path + '?renamed=' + path;
  256. }, 1000);
  257. }
  258. });
  259. return false;
  260. });
  261. $('#create-portal-button').on('click', function(e) {
  262. $('.portal').removeClass('hide');
  263. $('.content-main').addClass('on-edit');
  264. $('.portal a[data-toggle="tab"][href="#edit-form"]').tab('show');
  265. var path = $('.content-main').data('path');
  266. if (path != '/' && $('.content-main').data('page-id') == '') {
  267. var upperPage = path.substr(0, path.length - 1);
  268. $.get('/_api/pages.get', {path: upperPage}, function(res) {
  269. if (res.ok && res.page) {
  270. $('#portal-warning-modal').modal('show');
  271. }
  272. });
  273. }
  274. });
  275. $('#portal-form-close').on('click', function(e) {
  276. $('.portal').addClass('hide');
  277. $('.content-main').removeClass('on-edit');
  278. return false;
  279. });
  280. // list-link
  281. $('.page-list-link').each(function() {
  282. var $link = $(this);
  283. var text = $link.text();
  284. var path = $link.data('path');
  285. var shortPath = $link.data('short-path');
  286. $link.html(path.replace(new RegExp(shortPath + '(/)?$'), '<strong>' + shortPath + '$1</strong>'));
  287. });
  288. if (pageId) {
  289. // if page exists
  290. var $rawTextOriginal = $('#raw-text-original');
  291. if ($rawTextOriginal.length > 0) {
  292. var renderer = new Crowi.renderer($('#raw-text-original').html());
  293. renderer.render();
  294. Crowi.correctHeaders('#revision-body-content');
  295. Crowi.revisionToc('#revision-body-content', '#revision-toc');
  296. }
  297. // header
  298. var $header = $('#page-header');
  299. if ($header.length > 0) {
  300. var headerHeight = $header.outerHeight(true);
  301. $('.header-wrap').css({height: (headerHeight + 16) + 'px'});
  302. $header.affix({
  303. offset: {
  304. top: function() {
  305. return headerHeight + 86; // (54 header + 16 header padding-top + 16 content padding-top)
  306. }
  307. }
  308. });
  309. $('[data-affix-disable]').on('click', function(e) {
  310. $elm = $($(this).data('affix-disable'));
  311. $(window).off('.affix');
  312. $elm.removeData('affix').removeClass('affix affix-top affix-bottom');
  313. return false;
  314. });
  315. }
  316. // omg
  317. function createCommentHTML(revision, creator, comment, commentedAt) {
  318. var $comment = $('<div>');
  319. var $commentImage = $('<img class="picture picture-rounded">')
  320. .attr('src', Crowi.userPicture(creator));
  321. var $commentCreator = $('<div class="page-comment-creator">')
  322. .text(creator.username);
  323. var $commentRevision = $('<a class="page-comment-revision label">')
  324. .attr('href', '?revision=' + revision)
  325. .text(revision.substr(0,8));
  326. if (revision !== revisionId) {
  327. $commentRevision.addClass('label-default');
  328. } else {
  329. $commentRevision.addClass('label-primary');
  330. }
  331. var $commentMeta = $('<div class="page-comment-meta">')
  332. .text(commentedAt + ' ')
  333. .append($commentRevision);
  334. var $commentBody = $('<div class="page-comment-body">')
  335. .html(comment.replace(/(\r\n|\r|\n)/g, '<br>'));
  336. var $commentMain = $('<div class="page-comment-main">')
  337. .append($commentCreator)
  338. .append($commentBody)
  339. .append($commentMeta)
  340. $comment.addClass('page-comment');
  341. if (creator._id === currentUser) {
  342. $comment.addClass('page-comment-me');
  343. }
  344. if (revision !== revisionId) {
  345. $comment.addClass('page-comment-old');
  346. }
  347. $comment
  348. .append($commentImage)
  349. .append($commentMain);
  350. return $comment;
  351. }
  352. // get comments
  353. var $pageCommentList = $('.page-comments-list');
  354. var $pageCommentListNewer = $('#page-comments-list-newer');
  355. var $pageCommentListCurrent = $('#page-comments-list-current');
  356. var $pageCommentListOlder = $('#page-comments-list-older');
  357. var hasNewer = false;
  358. var hasOlder = false;
  359. $.get('/_api/comments.get', {page_id: pageId}, function(res) {
  360. if (res.ok) {
  361. var comments = res.comments;
  362. $.each(comments, function(i, comment) {
  363. var commentContent = createCommentHTML(comment.revision, comment.creator, comment.comment, comment.createdAt);
  364. if (comment.revision == revisionId) {
  365. $pageCommentListCurrent.append(commentContent);
  366. } else {
  367. if (Date.parse(comment.createdAt)/1000 > revisionCreatedAt) {
  368. $pageCommentListNewer.append(commentContent);
  369. hasNewer = true;
  370. } else {
  371. $pageCommentListOlder.append(commentContent);
  372. hasOlder = true;
  373. }
  374. }
  375. });
  376. }
  377. }).fail(function(data) {
  378. }).always(function() {
  379. if (!hasNewer) {
  380. $('.page-comments-list-toggle-newer').hide();
  381. }
  382. if (!hasOlder) {
  383. $pageCommentListOlder.addClass('collapse');
  384. $('.page-comments-list-toggle-older').hide();
  385. }
  386. });
  387. // post comment event
  388. $('#page-comment-form').on('submit', function() {
  389. $button = $('#commenf-form-button');
  390. $button.attr('disabled', 'disabled');
  391. $.post('/_api/comments.add', $(this).serialize(), function(data) {
  392. $button.removeAttr('disabled');
  393. if (data.ok) {
  394. var comment = data.comment;
  395. $pageCommentList.prepend(createCommentHTML(comment.revision, comment.creator, comment.comment, comment.createdAt));
  396. $('#comment-form-comment').val('');
  397. $('#comment-form-message').text('');
  398. } else {
  399. $('#comment-form-message').text(data.error);
  400. }
  401. }).fail(function(data) {
  402. if (data.status !== 200) {
  403. $('#comment-form-message').text(data.statusText);
  404. }
  405. });
  406. return false;
  407. });
  408. // attachment
  409. var $pageAttachmentList = $('.page-attachments ul');
  410. $.get('/_api/attachment/page/' + pageId, function(res) {
  411. var attachments = res.data.attachments;
  412. if (attachments.length > 0) {
  413. $.each(attachments, function(i, file) {
  414. $pageAttachmentList.append(
  415. '<li><a href="' + file.fileUrl + '">' + (file.originalName || file.fileName) + '</a> <span class="label label-default">' + file.fileFormat + '</span></li>'
  416. );
  417. })
  418. } else {
  419. $('.page-attachments').remove();
  420. }
  421. });
  422. // bookmark
  423. var $bookmarkButton = $('#bookmark-button');
  424. $.get('/_api/bookmarks.get', {page_id: pageId}, function(res) {
  425. if (res.ok) {
  426. if (res.bookmark) {
  427. MarkBookmarked();
  428. }
  429. }
  430. });
  431. $bookmarkButton.click(function() {
  432. var bookmarked = $bookmarkButton.data('bookmarked');
  433. if (!bookmarked) {
  434. $.post('/_api/bookmarks.add', {page_id: pageId}, function(res) {
  435. if (res.ok && res.bookmark) {
  436. MarkBookmarked();
  437. }
  438. });
  439. } else {
  440. $.post('/_api/bookmarks.remove', {page_id: pageId}, function(res) {
  441. if (res.ok) {
  442. MarkUnBookmarked();
  443. }
  444. });
  445. }
  446. return false;
  447. });
  448. function MarkBookmarked()
  449. {
  450. $('i', $bookmarkButton)
  451. .removeClass('fa-star-o')
  452. .addClass('fa-star');
  453. $bookmarkButton.data('bookmarked', 1);
  454. }
  455. function MarkUnBookmarked()
  456. {
  457. $('i', $bookmarkButton)
  458. .removeClass('fa-star')
  459. .addClass('fa-star-o');
  460. $bookmarkButton.data('bookmarked', 0);
  461. }
  462. // Like
  463. var $likeButton = $('#like-button');
  464. var $likeCount = $('#like-count');
  465. $likeButton.click(function() {
  466. var liked = $likeButton.data('liked');
  467. if (!liked) {
  468. $.post('/_api/likes.add', {page_id: pageId}, function(res) {
  469. if (res.ok) {
  470. MarkLiked();
  471. }
  472. });
  473. } else {
  474. $.post('/_api/likes.remove', {page_id: pageId}, function(res) {
  475. if (res.ok) {
  476. MarkUnLiked();
  477. }
  478. });
  479. }
  480. return false;
  481. });
  482. var $likerList = $("#liker-list");
  483. var likers = $likerList.data('likers');
  484. if (likers && likers.length > 0) {
  485. // FIXME: user data cache
  486. $.get('/_api/users.list', {user_ids: likers}, function(res) {
  487. // ignore unless response has error
  488. if (res.ok) {
  489. AddToLikers(res.users);
  490. }
  491. });
  492. }
  493. function AddToLikers (users) {
  494. $.each(users, function(i, user) {
  495. $likerList.append(CreateUserLinkWithPicture(user));
  496. });
  497. }
  498. function MarkLiked()
  499. {
  500. $likeButton.addClass('active');
  501. $likeButton.data('liked', 1);
  502. $likeCount.text(parseInt($likeCount.text()) + 1);
  503. }
  504. function MarkUnLiked()
  505. {
  506. $likeButton.removeClass('active');
  507. $likeButton.data('liked', 0);
  508. $likeCount.text(parseInt($likeCount.text()) - 1);
  509. }
  510. if (!isSeen) {
  511. $.post('/_api/pages.seen', {page_id: pageId}, function(res) {
  512. // ignore unless response has error
  513. if (res.ok && res.seenUser) {
  514. $('#content-main').data('page-is-seen', 1);
  515. }
  516. });
  517. }
  518. var $seenUserList = $("#seen-user-list");
  519. var seenUsers = $seenUserList.data('seen-users');
  520. var seenUsersArray = seenUsers.split(',');
  521. if (seenUsers && seenUsersArray.length > 0 && seenUsersArray.length <= 10) {
  522. // FIXME: user data cache
  523. $.get('/_api/users.list', {user_ids: seenUsers}, function(res) {
  524. // ignore unless response has error
  525. if (res.ok) {
  526. AddToSeenUser(res.users);
  527. }
  528. });
  529. }
  530. function CreateUserLinkWithPicture (user) {
  531. var $userHtml = $('<a>');
  532. $userHtml.data('user-id', user._id);
  533. $userHtml.attr('href', '/user/' + user.username);
  534. $userHtml.attr('title', user.name);
  535. var $userPicture = $('<img class="picture picture-xs picture-rounded">');
  536. $userPicture.attr('alt', user.name);
  537. $userPicture.attr('src', Crowi.userPicture(user));
  538. $userHtml.append($userPicture);
  539. return $userHtml;
  540. }
  541. function AddToSeenUser (users) {
  542. $.each(users, function(i, user) {
  543. $seenUserList.append(CreateUserLinkWithPicture(user));
  544. });
  545. }
  546. // History Diff
  547. var allRevisionIds = [];
  548. $.each($('.diff-view'), function() {
  549. allRevisionIds.push($(this).data('revisionId'));
  550. });
  551. $('.diff-view').on('click', function(e) {
  552. e.preventDefault();
  553. var getBeforeRevisionId = function(revisionId) {
  554. var currentPos = $.inArray(revisionId, allRevisionIds);
  555. if (currentPos < 0) {
  556. return false;
  557. }
  558. var beforeRevisionId = allRevisionIds[currentPos + 1];
  559. if (typeof beforeRevisionId === 'undefined') {
  560. return false;
  561. }
  562. return beforeRevisionId;
  563. };
  564. var revisionId = $(this).data('revisionId');
  565. var beforeRevisionId = getBeforeRevisionId(revisionId);
  566. var $diffDisplay = $('#diff-display-' + revisionId);
  567. var $diffIcon = $('#diff-icon-' + revisionId);
  568. if ($diffIcon.hasClass('fa-arrow-circle-right')) {
  569. $diffIcon.removeClass('fa-arrow-circle-right');
  570. $diffIcon.addClass('fa-arrow-circle-down');
  571. } else {
  572. $diffIcon.removeClass('fa-arrow-circle-down');
  573. $diffIcon.addClass('fa-arrow-circle-right');
  574. }
  575. if (beforeRevisionId === false) {
  576. $diffDisplay.text('差分はありません');
  577. $diffDisplay.slideToggle();
  578. } else {
  579. var revisionIds = revisionId + ',' + beforeRevisionId;
  580. $.ajax({
  581. type: 'GET',
  582. url: '/_api/revisions.list?revision_ids=' + revisionIds,
  583. dataType: 'json'
  584. }).done(function(res) {
  585. var currentText = res[0].body;
  586. var previousText = res[1].body;
  587. $diffDisplay.text('');
  588. var diff = jsdiff.diffLines(previousText, currentText);
  589. diff.forEach(function(part) {
  590. var color = part.added ? 'green' : part.removed ? 'red' : 'grey';
  591. var $span = $('<span>');
  592. $span.css('color', color);
  593. $span.text(part.value);
  594. $diffDisplay.append($span);
  595. });
  596. $diffDisplay.slideToggle();
  597. });
  598. }
  599. });
  600. // default open
  601. $('.diff-view').each(function(i, diffView) {
  602. if (i < 2) {
  603. $(diffView).click();
  604. }
  605. });
  606. }
  607. // for search
  608. });