browse.ui.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. var revisionMap = {};
  2. var checkedIds = [];
  3. window.renderCell = function(result, {col, row})
  4. {
  5. if(col.name === 'name')
  6. {
  7. const iconHtml = '<i class="icon card-icon mr-2 icon-' + (row.data.kind == 'dir' ? 'folder text-warning' : 'file-text-alt') + '"></i>';
  8. result[0] = {html: iconHtml + '<a href="' + row.data.link + '" data-app="' + appTab + '">' + row.data.name + '</a>', className: row.data.account ? '' : 'hidden'};
  9. return result;
  10. }
  11. if(col.name === 'originalComment')
  12. {
  13. result[0] = {html:'<span class="repo-comment">' + row.data.comment + '</span>'};
  14. return result;
  15. }
  16. return result;
  17. };
  18. window.afterRender = function()
  19. {
  20. if(repo.SCM != 'Gitlab') return;
  21. const dtable = $('#table-repo-browse').zui('dtable');
  22. if(!dtable) return;
  23. const oldData = dtable.options.data;;
  24. if(oldData.length == 0) return;
  25. // 如果正在加载提交信息或已经加载提交信息,直接返回
  26. if(!dtable.repoID || dtable.repoID != repo.id || !dtable.branch || dtable.branch != base64BranchID || !dtable.path || dtable.path != path)
  27. {
  28. dtable.isLoadingCommits = false;
  29. dtable.isLoadedAllCommits = false;
  30. }
  31. dtable.repoID = repo.id;
  32. dtable.path = path;
  33. dtable.branch = base64BranchID;
  34. if(dtable.isLoadingCommits || dtable.isLoadedAllCommits) return;
  35. // 设置正在加载提交信息
  36. dtable.isLoadingCommits = true;
  37. // 获取下一个需要加载提交信息的行
  38. const nextCommitRowIndex = oldData.findIndex(row => !row.revision);
  39. if(nextCommitRowIndex < 0) {
  40. // 如果没有需要加载提交信息的行,设置已经加载提交信息
  41. dtable.isLoadedAllCommits = true;
  42. return;
  43. }
  44. const nextCommitRow = oldData[nextCommitRowIndex];
  45. $.post(
  46. $.createLink('repo', 'ajaxGetFileCommitInfo'),
  47. {repoID: repo.id, branch: branch, path: nextCommitRow.path}
  48. ).then(rowData =>
  49. {
  50. const commit = JSON.parse(rowData);
  51. // 取消设置正在加载提交信息
  52. dtable.isLoadingCommits = false;
  53. // 合并行数据和提交信息
  54. oldData[nextCommitRowIndex].date = commit.authoredDate;
  55. oldData[nextCommitRowIndex].account = commit.author ? commit.author.username : commit.authorName;
  56. oldData[nextCommitRowIndex].comment = commit.comment;
  57. oldData[nextCommitRowIndex].revision = commit.sha;
  58. // 重新渲染表格
  59. dtable.render({data: oldData});
  60. });
  61. }
  62. /**
  63. * commit表格渲染跳转链接。
  64. * Render jump link of version.
  65. *
  66. * @access public
  67. * @return void
  68. */
  69. window.renderCommentCell = function(result, {col, row})
  70. {
  71. if(col.name === 'revision')
  72. {
  73. result[0] = {html:'<a href="' + row.data.link + '" data-app="' + appTab + '">' + row.data.revision + '</a>', style:{flexDirection:"column"}};
  74. return result;
  75. }
  76. if(col.name === 'originalComment')
  77. {
  78. result[0] = {html:'<span class="repo-comment">' + row.data.comment + '</span>', style:{flexDirection:"column"}};
  79. return result;
  80. }
  81. return result;
  82. };
  83. /* Open download page when downZip btn click. */
  84. window.downloadZip = function()
  85. {
  86. var link = $.createLink('repo', 'downloadCode', 'repoID=' + repo.id + '&branch=' + branch);
  87. $.ajaxSubmit({url: link});
  88. }
  89. /* Refresh page when repo changed. */
  90. $('#repo-select').on('change', function()
  91. {
  92. var index = $('#repo-select').prop('selectedIndex');
  93. if(menus[index - 1].url != undefined)
  94. {
  95. openUrl(menus[index - 1].url, {app: appTab});
  96. }
  97. })
  98. /**
  99. * 当选中两行时禁用其他行。
  100. * Disable checkable attribution when checked rows equal 2.
  101. *
  102. * @param object changes
  103. * @access public
  104. * @return void
  105. */
  106. window.checkedChange = function()
  107. {
  108. checkedIds = getCurrentCheckedIds();
  109. if(checkedIds.length < 2)
  110. {
  111. $('.btn-diff').addClass('disabled')
  112. }
  113. else
  114. {
  115. $('.btn-diff').removeClass('disabled')
  116. }
  117. }
  118. /**
  119. * 跳转比较差异页面。
  120. * Redirect to diff page.
  121. *
  122. * @access public
  123. * @return void
  124. */
  125. window.diffClick = function()
  126. {
  127. var checkedIds = getCurrentCheckedIds();
  128. if(checkedIds.length < 2) return;
  129. var newDiffLink = diffLink.replace('{oldRevision}', revisionMap[checkedIds[1]]);
  130. newDiffLink = newDiffLink.replace('{newRevision}', revisionMap[checkedIds[0]]);
  131. $.cookie.set('sideRepoSelected', checkedIds.join(','), {expires:config.cookieLife, path:config.webRoot})
  132. openUrl(newDiffLink, {app: appTab});
  133. }
  134. /**
  135. * 当选中数量等于2,则禁用其他所有行。
  136. * When the selected row equals 2, disable all other rows.
  137. *
  138. * @param int rowID
  139. * @access public
  140. * @return bool
  141. */
  142. window.canRowCheckable = function(rowID)
  143. {
  144. const dtable = zui.DTable.query('#repo-comments-table');
  145. if(dtable == undefined) return;
  146. var data = dtable.$.props.data;
  147. if(data.length == 0) return true;
  148. initRevisionMap(data);
  149. var currentCheckedIds = getCurrentCheckedIds();
  150. if(currentCheckedIds.length < 2) return true;
  151. if(currentCheckedIds.indexOf(rowID) == -1) return 'disabled'
  152. return true;
  153. }
  154. /**
  155. * 检测revisionMap是否跟当前页面数据一致,不一致重新生成。
  156. * Regenerate revisionMap when revisionMap is not in current list.
  157. *
  158. * @param array
  159. * @access public
  160. * @return void
  161. */
  162. function initRevisionMap(data)
  163. {
  164. if(revisionMap[data[data.length - 1].id] !== undefined && revisionMap[data[0].id] !== undefined) return;
  165. revisionMap = {};
  166. for (var i = 0; i < data.length; i++) revisionMap[data[i].id] = data[i].revision;
  167. /* Check rows where id in cookie. */
  168. setTimeout(function()
  169. {
  170. checkColInCurrentPage()
  171. }, 100);
  172. }
  173. /**
  174. * 选中当前页码对应的行。
  175. * Select the rows to the current page.
  176. *
  177. * @access public
  178. * @return void
  179. */
  180. function checkColInCurrentPage()
  181. {
  182. const dtable = zui.DTable.query('#repo-comments-table');
  183. const checkedIds = $.cookie.get('sideRepoSelected') ? $.cookie.get('sideRepoSelected').split(',') : [];
  184. var currentCheckedIds = [];
  185. if(revisionMap[checkedIds[0]]) currentCheckedIds.push(checkedIds[0]);
  186. if(revisionMap[checkedIds[1]]) currentCheckedIds.push(checkedIds[1]);
  187. dtable.$.toggleCheckRows(Object.keys(revisionMap), false);
  188. if(currentCheckedIds.length > 0)
  189. {
  190. dtable.$.toggleCheckRows(currentCheckedIds, true);
  191. }
  192. else
  193. {
  194. dtable.$.toggleCheckRows(Object.keys(revisionMap).slice(0, 2), true);
  195. }
  196. window.checkedChange();
  197. }
  198. /**
  199. * 获取当前页码选中的行。
  200. * Get checked rows in current page.
  201. *
  202. * @access public
  203. * @return array
  204. */
  205. function getCurrentCheckedIds()
  206. {
  207. const dtable = zui.DTable.query('#repo-comments-table');
  208. if(dtable.$ == undefined) return [];
  209. var checkedIds = dtable.$.getChecks();
  210. var currentCheckedIds = [];
  211. for (var i = 0; i < checkedIds.length; i++)
  212. {
  213. if(revisionMap[checkedIds[i]]) currentCheckedIds.push(checkedIds[i]);
  214. }
  215. if(currentCheckedIds.length > 2) currentCheckedIds = currentCheckedIds.slice(0, 2);
  216. return currentCheckedIds;
  217. }
  218. window.copyLink = function(dom)
  219. {
  220. var copyText = $(dom).parent().parent().find('input');
  221. copyText[0].select();
  222. document.execCommand("Copy");
  223. copyText[0].selectionStart = copyText[0].selectionEnd;
  224. copyText[0].blur();
  225. zui.Messager.show({
  226. type: 'success',
  227. content: copied,
  228. time: 2000
  229. });
  230. }
  231. $(function()
  232. {
  233. if(base64BranchID) $.get($.createLink('repo', 'ajaxSyncBranchCommit', 'repoID=' + repo.id + '&branch=' + base64BranchID));
  234. });