diff.ui.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. var iframeHeight = 0;
  2. var sidebarHeight = 0;
  3. var tabTemp;
  4. var diffAppose = $.cookie.get('renderSideBySide') == 'true';
  5. /* Close tab. */
  6. window.closeTab = function(dom)
  7. {
  8. const eleId = $(dom).parent().attr('href');
  9. const tabsEle = $(dom).parent().parent().parent();
  10. const isActive = $(dom).parent().hasClass('active');
  11. $(dom).parent().parent().remove();
  12. $(eleId).remove();
  13. $('#' + eleId.substring(5)).parent().removeClass('selected');
  14. if(isActive) tabsEle.children().last().find('a').trigger('click');
  15. if(!tabsEle.children().length) $('.monaco-dropmenu').addClass('hidden');
  16. const fileKey = eleId.substring(5).replace(/-/g, '=');
  17. $('li[z-key="' + fileKey + '"] .listitem').removeClass('selected');
  18. }
  19. $(function()
  20. {
  21. setTimeout(function()
  22. {
  23. if(typeof currentFile == 'undefined') return;
  24. var fileAsId = currentFile.replace(/=/g, '-');
  25. /* Resize moaco height. */
  26. $('#monacoTree').css('height', getSidebarHeight() - 8 + 'px');
  27. /* Init tab template. */
  28. if(!tabTemp) tabTemp = $('#monacoTabs ul li').first().clone();
  29. /* Load default tab content. */
  30. var height = getIframeHeight();
  31. $.cookie.set('repoCodePath', file, {expires:config.cookieLife, path:config.webRoot});
  32. $('#tab-' + fileAsId).html("<iframe class='repo-iframe' src='" + $.createLink('repo', 'ajaxGetDiffEditorContent', urlParams.replace('%s', '')) + "' width='100%' height='" + height + "' scrolling='no'></iframe>")
  33. /* Select default tree item. */
  34. const currentElement = findItemInTreeItems(tree, fileAsId, 0);
  35. expandTree();
  36. if(currentElement != undefined) setTimeout(() =>
  37. {
  38. $('#' + currentElement.id).parent().addClass('selected');
  39. }, 100);
  40. $('.btn-left').on('click', function() {arrowTabs('monacoTabs', 1);});
  41. $('.btn-right').on('click', function() {arrowTabs('monacoTabs', -2);});
  42. }, 300);
  43. });
  44. window.downloadCode = function()
  45. {
  46. var url = $(event.target).closest('.repoDownload-code').data('link');
  47. var activeFilePath = $('#monacoTabs .nav-item .active').attr('href').substring(5).replace(/-/g, '=');
  48. window.open(url.replace('{path}', activeFilePath), '_self');
  49. return;
  50. }
  51. /**
  52. * Change code encoding.
  53. *
  54. * @param string encoding
  55. * @access public
  56. * @return void
  57. */
  58. function changeEncoding(encoding)
  59. {
  60. $('#encoding').val(encoding);
  61. $('#encoding').parents('form').submit();
  62. }
  63. /**
  64. * Html code decode.
  65. *
  66. * @param string str
  67. * @access public
  68. * @return string
  69. */
  70. function htmlspecialchars_decode(str){
  71. str = str.replace(/&amp;/g, '&');
  72. str = str.replace(/&lt;/g, '<');
  73. str = str.replace(/&gt;/g, '>');
  74. str = str.replace(/&quot;/g, "''");
  75. str = str.replace(/&#039;/g, "'");
  76. return str;
  77. }
  78. /**
  79. * Get diffs by file name.
  80. *
  81. * @param string fileName
  82. * @access public
  83. * @return object
  84. */
  85. window.getDiffs = function(fileName)
  86. {
  87. if(fileName.indexOf('./') === 0) fileName = fileName.substring(2);
  88. var result = {
  89. 'code': {'new': '', 'old': ''},
  90. 'line': {'new': [], 'old': []}
  91. };
  92. const newContent = [];
  93. const oldContent = [];
  94. $.each(diffs, function(i, diff)
  95. {
  96. if(diff.fileName == fileName)
  97. {
  98. if(!diff.contents || typeof diff.contents[0].lines != 'object') return result;
  99. $.each(diff.contents, function(k, content)
  100. {
  101. var lines = content.lines;
  102. $.each(lines, function(l, code)
  103. {
  104. if(code.type == 'all' || code.type == 'new')
  105. {
  106. newContent.push(htmlspecialchars_decode(code.line.substring(1)));
  107. result.line.new.push(parseInt(code.newlc));
  108. }
  109. if(code.type == 'all' || code.type == 'old')
  110. {
  111. oldContent.push(htmlspecialchars_decode(code.line.substring(1)));
  112. result.line.old.push(parseInt(code.oldlc));
  113. }
  114. })
  115. })
  116. result.code.new = newContent.join('\n');
  117. result.code.old = oldContent.join('\n');
  118. return result;
  119. }
  120. });
  121. return result;
  122. }
  123. /**
  124. * 打开新tab。
  125. * Open new tab.
  126. *
  127. * @access public
  128. * @return void
  129. */
  130. function openTab(entry, name)
  131. {
  132. var eleId = 'tab-' + entry.replace(/=/g, '-');
  133. var element = document.getElementById(eleId);
  134. if (element)
  135. {
  136. $("a[href='" + '#' + eleId + "']").trigger('click');
  137. return;
  138. }
  139. var newTab = tabTemp.clone();
  140. newTab.find('a').attr('href', '#' + eleId);
  141. newTab.find('span').text(name);
  142. $('#monacoTabs .nav-tabs').append(newTab);
  143. var height = getIframeHeight();
  144. $.cookie.set('repoCodePath', entry, {expires:config.cookieLife, path:config.webRoot});
  145. $('#monacoTabs .tab-content').append("<div id='" + eleId + "' class='tab-pane active in'><iframe class='repo-iframe' src='" + $.createLink('repo', 'ajaxGetDiffEditorContent', urlParams.replace('%s', '')) + "' width='100%' height='" + height + "' scrolling='no'></iframe></div>")
  146. if($('.monaco-dropmenu').attr('class').indexOf('hidden')) $('.monaco-dropmenu').removeClass('hidden');
  147. setTimeout(() => {
  148. $("a[href='" + '#' + eleId + "']").trigger('click');
  149. updateEditorInline('#' + eleId);
  150. }, 100);
  151. }
  152. function updateEditorInline(eleId)
  153. {
  154. $.cookie.set('renderSideBySide', diffAppose, {expires:config.cookieLife, path:config.webRoot});
  155. if(typeof $(eleId + ' iframe')[0].contentWindow.updateEditorInline == 'function')
  156. {
  157. $(eleId + ' iframe')[0].contentWindow.updateEditorInline(diffAppose);
  158. }
  159. }
  160. $('#monacoTabs .nav-item a').on('click', function()
  161. {
  162. var eleId = $(this).attr('href');
  163. $(eleId + ' iframe')[0].contentWindow.updateEditorInline(diffAppose);
  164. });
  165. $(document).ready(function()
  166. {
  167. if(diffAppose)
  168. {
  169. $('.dropdown-menu #inline').show();
  170. $('.dropdown-menu #appose').hide();
  171. }
  172. else
  173. {
  174. $('.dropdown-menu #appose').show();
  175. $('.dropdown-menu #inline').hide();
  176. }
  177. $('.btn-left').on('click', function() {arrowTabs('monacoTabs', 1);});
  178. $('.btn-right').on('click', function() {arrowTabs('monacoTabs', -2);});
  179. });
  180. window.inlineAppose = function()
  181. {
  182. $('.inline-appose').hide();
  183. diffAppose = !diffAppose;
  184. if(diffAppose)
  185. {
  186. $('.dropdown-menu #inline').show();
  187. }
  188. else
  189. {
  190. $('.dropdown-menu #appose').show();
  191. }
  192. var tabID = $('#monacoTabs .nav-item .active').attr('href');
  193. updateEditorInline(tabID);
  194. return;
  195. }
  196. window.changeDiff = function()
  197. {
  198. var source = $('#oldRevision').val();
  199. var target = $('#newRevision').val();
  200. if(source && target)
  201. {
  202. $('#oldRevision').val(target);
  203. $('#newRevision').val(source);
  204. window.goDiff();
  205. }
  206. }
  207. /**
  208. * 在当前页面用modal加载链接。
  209. * Load link object page.
  210. *
  211. * @param string $link
  212. * @access public
  213. * @return void
  214. */
  215. window.loadLinkPage = function(link)
  216. {
  217. $('#linkObject').attr('href', link);
  218. $('#linkObject').trigger('click');
  219. }
  220. $('body').off('click', '.dropmenu-tree .dropmenu-item').on('click', '.dropmenu-tree .dropmenu-item', function()
  221. {
  222. var branchOrTag = $(this).find('.listitem').data('value');
  223. var url = $(this).find('.listitem').data('url');
  224. if(url != 'javascript:;') return;
  225. var domID = $('#source button.dropmenu-btn').hasClass('focus') ? 'oldRevision' : 'newRevision';
  226. $('#' + domID).val(branchOrTag);
  227. $('#isBranchOrTag').val('1');
  228. $('.pick-container').empty();
  229. if(domID == 'oldRevision')
  230. {
  231. $('#source button.dropmenu-btn').removeClass('focus');
  232. $('#source button.dropmenu-btn .text').text(branchOrTag);
  233. }
  234. else
  235. {
  236. $('#target button.dropmenu-btn').removeClass('focus');
  237. $('#target button.dropmenu-btn .text').text(branchOrTag);
  238. }
  239. })
  240. /**
  241. * 触发diff检查。
  242. * Trigger diff.
  243. *
  244. * @access public
  245. * @return viod
  246. */
  247. window.goDiff = function()
  248. {
  249. var oldRevision = $('#oldRevision').val();
  250. var newRevision = $('#newRevision').val();
  251. var isBranchOrTag = $('#isBranchOrTag').val();
  252. if(!oldRevision || !newRevision)
  253. {
  254. (repo.SCM != 'Subversion') ? zui.Modal.alert(repoLang.error.needTwoVersion) : zui.Modal.alert(repoLang.error.emptyVersion);
  255. return false;
  256. }
  257. if(repo.SCM == 'Subversion')
  258. {
  259. var intRe = /^\d+$/;
  260. if((intRe.test(oldRevision) == false && oldRevision != '^') || (intRe.test(newRevision) == false && newRevision != '^'))
  261. {
  262. zui.Modal.alert(repoLang.error.versionError);
  263. return false;
  264. }
  265. }
  266. if(oldRevision == newRevision)
  267. {
  268. zui.Modal.alert(repoLang.error.differentVersions);
  269. return false;
  270. }
  271. if(isBranchOrTag == '1')
  272. {
  273. oldRevision = btoa(encodeURIComponent(oldRevision));
  274. newRevision = btoa(encodeURIComponent(newRevision));
  275. }
  276. var url = $.createLink('repo', 'diff', 'repoID=' + repo.id + '&objectID=' + objectID + '&entry=&oldRevision=' + oldRevision + '&newRevision=' +newRevision + '&showBug=0&encoding=&isBranchOrTag=' + isBranchOrTag);
  277. loadPage(url);
  278. }