common.ui.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. $(function()
  2. {
  3. $('#log').on('click', '.btn-close', closeRelation);
  4. window.onScmChange();
  5. setTimeout(function()
  6. {
  7. $('#monacoTabs').on('show.zui.tab', function(e, tab)
  8. {
  9. if($(e.target).hasClass('monaco-dropmenu')) return;
  10. $('#monacoTree .listitem').removeClass('selected');
  11. if(!tab || !tab[tab.length - 1] || typeof tab[tab.length - 1] != 'string') return;
  12. const fileKey = tab[tab.length - 1].substring(5).replace(/-/g, '=');
  13. $('li[z-key="' + fileKey + '"] .listitem').addClass('selected');
  14. });
  15. }, 1000);
  16. });
  17. /**
  18. * 切换仓库。
  19. * Swtich repo.
  20. *
  21. * @param int $repoID
  22. * @param string $module
  23. * @param string $method
  24. * @access public
  25. * @return void
  26. */
  27. window.switchRepo = function(repoID, module, method)
  28. {
  29. if(typeof(eventKeyCode) == 'undefined') eventKeyCode = 0;
  30. if(eventKeyCode > 0 && eventKeyCode != 13) return false;
  31. /* The project id is a string, use it as the project model. */
  32. if(isNaN(repoID))
  33. {
  34. $.cookie.set('projectMode', repoID, {expires:config.cookieLife, path:config.webRoot});
  35. repoID = 0;
  36. }
  37. if(method != 'settings') method ="browse";
  38. link = createLink(module, method, 'repoID=' + repoID);
  39. location.href=link;
  40. }
  41. /**
  42. * 切换分支。
  43. * Switch branch for git.
  44. *
  45. * @param string $branchID
  46. * @access public
  47. * @return void
  48. */
  49. window.switchBranch = function(branchID)
  50. {
  51. $.cookie.set('repoBranch', branchID, {expires:config.cookieLife, path:config.webRoot});
  52. $.cookie.set('repoRefresh', 1, {expires:config.cookieLife, path:config.webRoot});
  53. location.href=location.href;
  54. }
  55. var distance = 0;
  56. /**
  57. * 左右切换关联信息。
  58. * Aarrow tabs area.
  59. *
  60. * @param string domID
  61. * @param number shift 1|-1
  62. * @param bool hideRightBtn
  63. * @access public
  64. * @return void
  65. */
  66. window.arrowTabs = function(domID, shift, hideRightBtn)
  67. {
  68. if($('#' + domID).html() == '') return;
  69. var hasParent = $('#' + domID + ' > .btn-left').length;
  70. var $leftBtn = hasParent ? $('#' + domID + ' > .btn-left') : $('.btn-left');
  71. var $rightBtn = hasParent ? $('#' + domID + ' > .btn-right') : $('.btn-right');
  72. $leftBtn.show();
  73. $rightBtn.show();
  74. if(hideRightBtn) $rightBtn.hide();
  75. var tabItemWidth = 0;
  76. const $tabs = $('#' + domID + ' > .tabs-header > .nav-tabs')[0];
  77. if($tabs) tabItemWidth = $tabs.scrollWidth;
  78. var tabsWidth = $('#' + domID)[0].clientWidth;
  79. if($('#' + domID + ' .close-bugs').length) tabsWidth = tabsWidth * 0.7;
  80. if(tabItemWidth <= tabsWidth)
  81. {
  82. $leftBtn.hide();
  83. $rightBtn.hide();
  84. $tabs.style.transform = 'translateX(0px)';
  85. return;
  86. }
  87. distance += tabsWidth * shift * 0.2;
  88. if(distance > 0) distance = 0;
  89. if(distance == 0)
  90. {
  91. $leftBtn.hide();
  92. }
  93. if((tabItemWidth + distance) <= tabsWidth * 0.75)
  94. {
  95. $rightBtn.hide();
  96. return arrowTabs(domID, 1, true);
  97. }
  98. if(domID == 'monacoTabs' && distance < -60) distance = distance + 60;
  99. $tabs.style.transform = 'translateX('+ distance +'px)';
  100. }
  101. /**
  102. * 关闭关联信息tab。
  103. * Close commit relations.
  104. *
  105. * @access public
  106. * @return void
  107. */
  108. window.closeRelation = function()
  109. {
  110. $('#relationTabs ul li').remove();
  111. $('#relationTabs .tab-content .tab-pane').remove();
  112. $('.history').html('');
  113. $('#log').data('line', 0);
  114. $('#log').hide();
  115. $('#codeContainer').css('height', codeHeight);
  116. $('#related').css('height', 0);
  117. };
  118. /**
  119. * 获取tabs内容高度。
  120. * Get tab-content height.
  121. *
  122. * @access public
  123. * @return void
  124. */
  125. window.getIframeHeight = function()
  126. {
  127. if(typeof(iframeHeight) != 'undefined' && iframeHeight) return iframeHeight;
  128. var windowHeight = $(window).height();
  129. var headerHeight = parseInt($('#header').height());
  130. var mainNavbar = parseInt($('#mainNavbar').height());
  131. var featureBar = parseInt($('#featureBar').height());
  132. var tabsbar = parseInt($('.nav-tabs').height());
  133. var mainMenuHeight = parseInt($('#mainMenu').css('padding-top')) + parseInt($('#mainMenu').css('padding-bottom'));
  134. var appTabsHeight = parseInt($('#appTabs').height());
  135. var appsBarHeight = parseInt($('#appsBar').height());
  136. featureBar = featureBar ? featureBar : 0;
  137. appsBarHeight = appsBarHeight ? appsBarHeight : 0;
  138. appTabsHeight = appTabsHeight ? appTabsHeight : 0;
  139. mainMenuHeight = mainMenuHeight ? mainMenuHeight : 0;
  140. mainNavbar = mainNavbar ? mainNavbar : 0;
  141. iframeHeight = windowHeight - headerHeight - appsBarHeight - appTabsHeight - mainMenuHeight - mainNavbar - tabsbar - 20 - featureBar;
  142. return iframeHeight;
  143. }
  144. /**
  145. * 获取左侧边栏高度。
  146. * Get sidebar height.
  147. *
  148. * @access public
  149. * @return void
  150. */
  151. window.getSidebarHeight = function()
  152. {
  153. if(typeof sidebarHeight != 'undefined' && sidebarHeight) return sidebarHeight;
  154. var windowHeight = $(window).height();
  155. var headerHeight = parseInt($('#header').height());
  156. var mainNavbar = parseInt($('#mainNavbar').height());
  157. var featureBar = parseInt($('#featureBar').height());
  158. var repoMenu = parseInt($('#repoBranchDropMenu').height());
  159. var mainMenuHeight = parseInt($('#mainMenu').css('padding-top')) + parseInt($('#mainMenu').css('padding-bottom'));
  160. var appTabsHeight = parseInt($('#appTabs').height());
  161. var appsBarHeight = parseInt($('#appsBar').height());
  162. repoMenu = repoMenu ? repoMenu : 0;
  163. featureBar = featureBar ? featureBar : 0;
  164. appsBarHeight = appsBarHeight ? appsBarHeight : 0;
  165. appTabsHeight = appTabsHeight ? appTabsHeight : 0;
  166. mainMenuHeight = mainMenuHeight ? mainMenuHeight : 0;
  167. mainNavbar = mainNavbar ? mainNavbar : 0;
  168. sidebarHeight = windowHeight - headerHeight - appsBarHeight - appTabsHeight - mainMenuHeight - mainNavbar - repoMenu - featureBar;
  169. return sidebarHeight;
  170. }
  171. /**
  172. * 查找选中元素所有的父元素及选中元素的id。
  173. * Find parent nodes and item id of selected tree item.
  174. *
  175. * @access public
  176. * @return string
  177. */
  178. window.findItemInTreeItems = function(list, key, level) {
  179. for (const item of list) {
  180. if(level === 0)
  181. {
  182. parentTree = [item.key];
  183. }
  184. if (item.id === key) return item;
  185. if (item.children && item.children.length > 0)
  186. {
  187. const findedItem = findItemInTreeItems(item.children, key);
  188. if (findedItem)
  189. {
  190. parentTree.push(item.key);
  191. return findedItem;
  192. }
  193. }
  194. }
  195. }
  196. /**
  197. * 展开树状结构。
  198. * Expand tree node.
  199. *
  200. * @access public
  201. * @return void
  202. */
  203. window.expandTree = function()
  204. {
  205. const treeObj = $('#monacoTree').parent().data('zui.Tree');
  206. if(treeObj == undefined || treeObj.$ == undefined) return;
  207. for (const key of parentTree) treeObj.$.expand(key);
  208. }
  209. window.onHostChange = function()
  210. {
  211. const host = $('[name=serviceHost]').val();
  212. if(!host) return false;
  213. toggleLoading('#serviceProject', true);
  214. const $picker = $('#serviceProject').zui('picker');
  215. $picker.$.clear();
  216. $.getJSON($.createLink('repo', 'ajaxGetProjects', "host=" + host), function(items)
  217. {
  218. $picker.render({items: items});
  219. toggleLoading('#serviceProject', false);
  220. });
  221. }
  222. window.onProjectChange = function()
  223. {
  224. const $picker = $('#serviceProject').zui('picker');
  225. const selections = $picker.$.state.selections;
  226. const serviceProject = selections.length > 0 ? selections[0].text : '';
  227. if(!serviceProject)
  228. {
  229. $('#name').val('');
  230. return;
  231. }
  232. else
  233. {
  234. $('#name').val(serviceProject);
  235. }
  236. }
  237. /**
  238. * Changed SCM.
  239. *
  240. * @param string $scm
  241. * @access public
  242. * @return void
  243. */
  244. window.onScmChange = function()
  245. {
  246. if(typeof scmList == 'undefined' || !scmList) return;
  247. var scm = $('[name=SCM]').val();
  248. if(!scm)
  249. {
  250. for(i in scmList)
  251. {
  252. scm = i;
  253. break;
  254. }
  255. }
  256. (scm == 'Git') ? $('.tips-git').removeClass('hidden') : $('.tips-git').addClass('hidden');
  257. if(scm != 'Subversion')
  258. {
  259. $('.account-fields').addClass('hidden');
  260. $('#path').attr('placeholder', pathGitTip);
  261. $('#client').attr('placeholder', clientGitTip);
  262. if(!client) $('#client').val('/usr/bin/git');
  263. }
  264. else
  265. {
  266. $('.account-fields').removeClass('hidden');
  267. $('#path').attr('placeholder', pathSvnTip);
  268. $('#client').attr('placeholder', clientSvnTip);
  269. if(!client) $('#client').val('/usr/bin/svn');
  270. }
  271. if(scm == 'Git' || scm == 'Subversion')
  272. {
  273. $('.service').toggle(false);
  274. $('.hide-service').toggle(true);
  275. }
  276. else
  277. {
  278. $('.service').toggle(true);
  279. if(scm == 'Gitea' || scm == 'Gogs')
  280. {
  281. $('.hide-service').each(function()
  282. {
  283. if(!$(this).hasClass('hide-git')) $(this).toggle(true);
  284. });
  285. $('.hide-git').toggle(false);
  286. }
  287. else
  288. {
  289. $('.hide-service').toggle(false);
  290. }
  291. var url = $.createLink('repo', 'ajaxGetHosts', "scm=" + scm);
  292. $.getJSON(url, function(data)
  293. {
  294. const $hostPicker = $('#serviceHost').zui('picker');
  295. $hostPicker.render({items: data});
  296. $hostPicker.$.clear();
  297. });
  298. }
  299. }
  300. /**
  301. * On acl change event.
  302. *
  303. * @param event $event
  304. * @access public
  305. * @return void
  306. */
  307. window.onAclChange = function(event)
  308. {
  309. const acl = $(event.target).val();
  310. if(acl == 'private' || acl == 'custom')
  311. {
  312. $('#whitelist').removeClass('hidden');
  313. }
  314. else
  315. {
  316. $('#whitelist').addClass('hidden');
  317. }
  318. }
  319. /**
  320. * 点击左侧菜单打开详情tab。
  321. * Open new tab when click tree item.
  322. *
  323. * @access public
  324. * @return void
  325. */
  326. window.treeClick = function(info)
  327. {
  328. if(info.item.items && (info.item.items.length > 0 || info.item.items.url)) return;
  329. $('#monacoTree .listitem').removeClass('selected');
  330. $('li[z-key="' + info.item.id + '"] .listitem').addClass('selected');
  331. openTab(info.item.id, info.item.text);
  332. arrowTabs('monacoTabs', -2);
  333. }
  334. window.searchList = function()
  335. {
  336. $.getLib(config.webRoot + 'js/misc/base64.js', {root: false}, function()
  337. {
  338. const dom = new DOMParser().parseFromString($('#keyword').val(), "text/html");
  339. const keyword = dom.body.textContent;
  340. loadPage(searchUrl.replace('%s', encodeURIComponent(Base64.encode(keyword))));
  341. });
  342. }