ajaxgeteditorcontent.ui.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var blames = null;
  2. var codeHeight = 0;
  3. window.onMouseDown = function(obj)
  4. {
  5. if(!obj.target.position) return;
  6. showBlameAndRelation(obj.target.position.lineNumber);
  7. }
  8. window.getRevision = function(blames, line)
  9. {
  10. let blame = blames[line];
  11. if(!blame) return false;
  12. let p_line = parseInt(line);
  13. while(!blame.revision)
  14. {
  15. p_line--;
  16. blame = blames[p_line];
  17. }
  18. return blame;
  19. }
  20. /**
  21. * 显示关联信息弹窗。
  22. * Show blame info and relations.
  23. *
  24. * @param int line
  25. * @access public
  26. * @return void
  27. */
  28. function showBlameAndRelation(line)
  29. {
  30. if(!blames) return;
  31. if(pageType == 'diff') line = diffContent.line.new[line -1];
  32. const blame = window.getRevision(blames, line);
  33. if(!blame) return;
  34. if($('#log').data('line') == line) return;
  35. var time = blame.time != 'unknown' ? blame.time : '';
  36. var user = blame.committer != 'unknown' ? blame.committer : '';
  37. var version = blame.revision.toString().substring(0, 10);
  38. var content = blameTmpl.replace('%line', line).replace('%time', time).replace('%name', user).replace('%version', version).replace('%comment', blame.message);
  39. $('.history').html(content);
  40. $('#log').data('line', line);
  41. $('#log').css('display', 'flex');
  42. getRelation(blame.revision);
  43. }
  44. /**
  45. * 获取关联信息。
  46. * Get relation by commit.
  47. *
  48. * @param string $commit
  49. * @access public
  50. * @return void
  51. */
  52. window.getRelation = function(commit)
  53. {
  54. $('.table-empty-tip').show();
  55. $('#codeContainer').css('height', codeHeight / 5 * 3);
  56. var relatedHeight = codeHeight / 5 * 2 - $('#log').height() - 10;
  57. $('#related').css('height', relatedHeight);
  58. setTimeout(() => {
  59. var tabsHeight = $('#relationTabs .nav-tabs').height();
  60. if(!tabsHeight) tabsHeight = 32;
  61. $('#relationTabs .tab-content').css('height', relatedHeight - tabsHeight - 8);
  62. }, 500);
  63. $('#relationTabs ul li').remove();
  64. $('#relationTabs .tab-content .tab-pane').remove();
  65. $.post($.createLink('repo', 'ajaxGetCommitRelation', 'repoID=' + repoID + '&commit=' + commit), function(data)
  66. {
  67. var titleList = JSON.parse(data).titleList;
  68. $.each(titleList, function(i, titleObj)
  69. {
  70. openTab(titleObj);
  71. });
  72. if(titleList.length > 0)
  73. {
  74. $('.table-empty-tip').hide();
  75. $('#related button').show();
  76. $('#relationTabs ul li a').first().trigger('click');
  77. }
  78. else
  79. {
  80. if(!canLinkStory && !canLinkBug && !canLinkTask) $('#related button').hide();
  81. }
  82. arrowTabs('relationTabs', 1);
  83. });
  84. globalCommit = commit;
  85. var linkStory = $.createLink('repo', 'linkStory', 'repoID=' + repoID + '&commit=' + commit, '', true);
  86. var linkBug = $.createLink('repo', 'linkBug', 'repoID=' + repoID + '&commit=' + commit, '', true);
  87. var linkTask = $.createLink('repo', 'linkTask', 'repoID=' + repoID + '&commit=' + commit, '', true);
  88. $('#linkStory').data('link', linkStory);
  89. $('#linkBug').data('link', linkBug);
  90. $('#linkTask').data('link', linkTask);
  91. $('#related').show();
  92. }
  93. /**
  94. * 打开关联信息tab。
  95. * Open tab.
  96. *
  97. * @param object $titleObj
  98. * @access public
  99. * @return void
  100. */
  101. function openTab(titleObj)
  102. {
  103. var tabTemplate = `<li class="nav-item"><a class="font-medium" href="{href}" data-toggle="tab"><span><i class="icon icon-{prefixIcon}"></i>{title}</span></a><a title="{unlinkTitle}" class="unlinks" data-link="{unlinkHref}"><i class="icon icon-unlink"></i></a></li>`;
  104. var eleId = titleObj.type + '-' + titleObj.id;
  105. var prefixIcon = titleObj.type == 'story' ? 'lightbulb' : (titleObj.type == 'task' ? 'check-sign' : 'bug');
  106. var unlinkHref = canUnlinkObject ? $.createLink('repo', 'unlink', 'repoID=' + repoID + '&commit=' + globalCommit + '&objectID=' + titleObj.type + '&objectType=' + titleObj.id) : '';
  107. var tabHtml = tabTemplate.replace('{title}', titleObj.title)
  108. .replace('{unlinkHref}', unlinkHref)
  109. .replace('{unlinkTitle}', unlinkTitle)
  110. .replace('{href}', '#related-' + eleId)
  111. .replace('{prefixIcon}', prefixIcon);
  112. $('#relationTabs > .tabs-header > ul').append(tabHtml);
  113. var height = getRelationTabHeight();
  114. $('#relationTabs .tab-content').append("<div id='related-" + eleId + "' class='tab-pane active in'><iframe class='repo-iframe' src='" + $.createLink('repo', 'ajaxGetRelationInfo', 'objectID=' + titleObj.id + '&objectType=' + titleObj.type) + "' width='100%' height='" + height + "'></iframe></div>")
  115. }
  116. /**
  117. * 获取关联信息弹窗高度。
  118. * Get relation tab height.
  119. *
  120. * @param object $titleObj
  121. * @access public
  122. * @return void
  123. */
  124. function getRelationTabHeight()
  125. {
  126. var relatedHeight = $('#related').height();
  127. relatedHeight = parseInt(relatedHeight) ? parseInt(relatedHeight) : 0;
  128. return relatedHeight - 52;
  129. }
  130. /**
  131. * 修改比对差异方式。
  132. * Update diff editor inline style.
  133. *
  134. * @param bool display
  135. * @access public
  136. * @return void
  137. */
  138. window.updateEditorInline = function(display)
  139. {
  140. modifiedEditor.updateOptions({renderSideBySide: display});
  141. }
  142. /**
  143. * 获取提交信息。
  144. * Show commit info.
  145. *
  146. * @access public
  147. * @return void
  148. */
  149. function showCommitInfo()
  150. {
  151. var link = $.createLink('repo', 'ajaxGetCommitInfo');
  152. var data = {
  153. repoID : repoID,
  154. entry : encodePath,
  155. revision : revision,
  156. sourceRevision: sourceRevision,
  157. line : 0,
  158. returnType : 'json'
  159. };
  160. $.post(link, data, function(res)
  161. {
  162. res = JSON.parse(res);
  163. blames = res.blames;
  164. })
  165. }
  166. /**
  167. * 初始化页面。
  168. * Init page.
  169. *
  170. * @access public
  171. * @return void
  172. */
  173. /* 初始化数据 */
  174. function initPage()
  175. {
  176. codeHeight = $.cookie.get('codeContainerHeight');
  177. $('#codeContainer').css('height', $.cookie.get('codeContainerHeight'));
  178. $('.btn-left').on('click', function() {arrowTabs('relationTabs', 1);});
  179. $('.btn-right').on('click', function() {arrowTabs('relationTabs', -2);});
  180. $('#linkStory, #linkBug, #linkTask').on('click', function()
  181. {
  182. var link = $(this).data('link');
  183. parent.loadLinkPage(link);
  184. });
  185. $('#relationTabs').off('click', '.unlinks').on('click', '.unlinks', function()
  186. {
  187. var link = $(this).data('link');
  188. $.post(link, function(data)
  189. {
  190. data = JSON.parse(data);
  191. if(data.result)
  192. {
  193. getRelation(data.revision);
  194. }
  195. else
  196. {
  197. alert(data.message);
  198. }
  199. })
  200. })
  201. $('#relationTabs').on('onOpen', function(event, tab)
  202. {
  203. $('#tab-nav-item-' + tab.id).attr('title', tab.title);
  204. var relatedHeight = codeHeight / 5 * 2 - $('#log').height() - 45;
  205. $('#relationTabs iframe').css('height', relatedHeight);
  206. });
  207. /* Get file commits. */
  208. showCommitInfo();
  209. }
  210. $(function()
  211. {
  212. setTimeout(() => {
  213. initPage();
  214. }, 200);
  215. });
  216. window.downloadCode = function()
  217. {
  218. var url = $(this).data('link');
  219. window.open(url, '_self');
  220. }