You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 72KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  1. 'use strict';
  2. var csrf;
  3. var suburl;
  4. function initCommentPreviewTab($form) {
  5. var $tabMenu = $form.find('.tabular.menu');
  6. $tabMenu.find('.item').tab();
  7. $tabMenu.find('.item[data-tab="' + $tabMenu.data('preview') + '"]').click(function () {
  8. var $this = $(this);
  9. $.post($this.data('url'), {
  10. "_csrf": csrf,
  11. "mode": "gfm",
  12. "context": $this.data('context'),
  13. "text": $form.find('.tab.segment[data-tab="' + $tabMenu.data('write') + '"] textarea').val()
  14. },
  15. function (data) {
  16. var $previewPanel = $form.find('.tab.segment[data-tab="' + $tabMenu.data('preview') + '"]');
  17. $previewPanel.html(data);
  18. emojify.run($previewPanel[0]);
  19. $('pre code', $previewPanel[0]).each(function (i, block) {
  20. hljs.highlightBlock(block);
  21. });
  22. }
  23. );
  24. });
  25. buttonsClickOnEnter();
  26. }
  27. var previewFileModes;
  28. function initEditPreviewTab($form) {
  29. var $tabMenu = $form.find('.tabular.menu');
  30. $tabMenu.find('.item').tab();
  31. var $previewTab = $tabMenu.find('.item[data-tab="' + $tabMenu.data('preview') + '"]');
  32. if ($previewTab.length) {
  33. previewFileModes = $previewTab.data('preview-file-modes').split(',');
  34. $previewTab.click(function () {
  35. var $this = $(this);
  36. $.post($this.data('url'), {
  37. "_csrf": csrf,
  38. "mode": "gfm",
  39. "context": $this.data('context'),
  40. "text": $form.find('.tab.segment[data-tab="' + $tabMenu.data('write') + '"] textarea').val()
  41. },
  42. function (data) {
  43. var $previewPanel = $form.find('.tab.segment[data-tab="' + $tabMenu.data('preview') + '"]');
  44. $previewPanel.html(data);
  45. emojify.run($previewPanel[0]);
  46. $('pre code', $previewPanel[0]).each(function (i, block) {
  47. hljs.highlightBlock(block);
  48. });
  49. }
  50. );
  51. });
  52. }
  53. }
  54. function initEditDiffTab($form) {
  55. var $tabMenu = $form.find('.tabular.menu');
  56. $tabMenu.find('.item').tab();
  57. $tabMenu.find('.item[data-tab="' + $tabMenu.data('diff') + '"]').click(function () {
  58. var $this = $(this);
  59. $.post($this.data('url'), {
  60. "_csrf": csrf,
  61. "context": $this.data('context'),
  62. "content": $form.find('.tab.segment[data-tab="' + $tabMenu.data('write') + '"] textarea').val()
  63. },
  64. function (data) {
  65. var $diffPreviewPanel = $form.find('.tab.segment[data-tab="' + $tabMenu.data('diff') + '"]');
  66. $diffPreviewPanel.html(data);
  67. emojify.run($diffPreviewPanel[0]);
  68. }
  69. );
  70. });
  71. }
  72. function initEditForm() {
  73. if ($('.edit.form').length == 0) {
  74. return;
  75. }
  76. initEditPreviewTab($('.edit.form'));
  77. initEditDiffTab($('.edit.form'));
  78. }
  79. function initBranchSelector() {
  80. var $selectBranch = $('.ui.select-branch')
  81. var $branchMenu = $selectBranch.find('.reference-list-menu');
  82. $branchMenu.find('.item:not(.no-select)').click(function () {
  83. var selectedValue = $(this).data('id');
  84. $($(this).data('id-selector')).val(selectedValue);
  85. $selectBranch.find('.ui .branch-name').text(selectedValue);
  86. });
  87. $selectBranch.find('.reference.column').click(function () {
  88. $selectBranch.find('.scrolling.reference-list-menu').css('display', 'none');
  89. $selectBranch.find('.reference .text').removeClass('black');
  90. $($(this).data('target')).css('display', 'block');
  91. $(this).find('.text').addClass('black');
  92. return false;
  93. });
  94. }
  95. function updateIssuesMeta(url, action, issueIds, elementId, afterSuccess) {
  96. $.ajax({
  97. type: "POST",
  98. url: url,
  99. data: {
  100. "_csrf": csrf,
  101. "action": action,
  102. "issue_ids": issueIds,
  103. "id": elementId
  104. },
  105. success: afterSuccess
  106. })
  107. }
  108. function initReactionSelector(parent) {
  109. var reactions = '';
  110. if (!parent) {
  111. parent = $(document);
  112. reactions = '.reactions > ';
  113. }
  114. parent.find(reactions + 'a.label').popup({'position': 'bottom left', 'metadata': {'content': 'title', 'title': 'none'}});
  115. parent.find('.select-reaction > .menu > .item, ' + reactions + 'a.label').on('click', function(e){
  116. var vm = this;
  117. e.preventDefault();
  118. if ($(this).hasClass('disabled')) return;
  119. var actionURL = $(this).hasClass('item') ?
  120. $(this).closest('.select-reaction').data('action-url') :
  121. $(this).data('action-url');
  122. var url = actionURL + '/' + ($(this).hasClass('blue') ? 'unreact' : 'react');
  123. $.ajax({
  124. type: 'POST',
  125. url: url,
  126. data: {
  127. '_csrf': csrf,
  128. 'content': $(this).data('content')
  129. }
  130. }).done(function(resp) {
  131. if (resp && (resp.html || resp.empty)) {
  132. var content = $(vm).closest('.content');
  133. var react = content.find('.segment.reactions');
  134. if (react.length > 0) {
  135. react.remove();
  136. }
  137. if (!resp.empty) {
  138. react = $('<div class="ui attached segment reactions"></div>');
  139. var attachments = content.find('.segment.bottom:first');
  140. if (attachments.length > 0) {
  141. react.insertBefore(attachments);
  142. } else {
  143. react.appendTo(content);
  144. }
  145. react.html(resp.html);
  146. var hasEmoji = react.find('.has-emoji');
  147. for (var i = 0; i < hasEmoji.length; i++) {
  148. emojify.run(hasEmoji.get(i));
  149. }
  150. react.find('.dropdown').dropdown();
  151. initReactionSelector(react);
  152. }
  153. }
  154. });
  155. });
  156. }
  157. function initCommentForm() {
  158. if ($('.comment.form').length == 0) {
  159. return
  160. }
  161. initBranchSelector();
  162. initCommentPreviewTab($('.comment.form'));
  163. // Labels
  164. var $list = $('.ui.labels.list');
  165. var $noSelect = $list.find('.no-select');
  166. var $labelMenu = $('.select-label .menu');
  167. var hasLabelUpdateAction = $labelMenu.data('action') == 'update';
  168. $('.select-label').dropdown('setting', 'onHide', function(){
  169. if (hasLabelUpdateAction) {
  170. location.reload();
  171. }
  172. });
  173. $labelMenu.find('.item:not(.no-select)').click(function () {
  174. if ($(this).hasClass('checked')) {
  175. $(this).removeClass('checked');
  176. $(this).find('.octicon').removeClass('octicon-check');
  177. if (hasLabelUpdateAction) {
  178. updateIssuesMeta(
  179. $labelMenu.data('update-url'),
  180. "detach",
  181. $labelMenu.data('issue-id'),
  182. $(this).data('id')
  183. );
  184. }
  185. } else {
  186. $(this).addClass('checked');
  187. $(this).find('.octicon').addClass('octicon-check');
  188. if (hasLabelUpdateAction) {
  189. updateIssuesMeta(
  190. $labelMenu.data('update-url'),
  191. "attach",
  192. $labelMenu.data('issue-id'),
  193. $(this).data('id')
  194. );
  195. }
  196. }
  197. var labelIds = [];
  198. $(this).parent().find('.item').each(function () {
  199. if ($(this).hasClass('checked')) {
  200. labelIds.push($(this).data('id'));
  201. $($(this).data('id-selector')).removeClass('hide');
  202. } else {
  203. $($(this).data('id-selector')).addClass('hide');
  204. }
  205. });
  206. if (labelIds.length == 0) {
  207. $noSelect.removeClass('hide');
  208. } else {
  209. $noSelect.addClass('hide');
  210. }
  211. $($(this).parent().data('id')).val(labelIds.join(","));
  212. return false;
  213. });
  214. $labelMenu.find('.no-select.item').click(function () {
  215. if (hasLabelUpdateAction) {
  216. updateIssuesMeta(
  217. $labelMenu.data('update-url'),
  218. "clear",
  219. $labelMenu.data('issue-id'),
  220. ""
  221. );
  222. }
  223. $(this).parent().find('.item').each(function () {
  224. $(this).removeClass('checked');
  225. $(this).find('.octicon').removeClass('octicon-check');
  226. });
  227. $list.find('.item').each(function () {
  228. $(this).addClass('hide');
  229. });
  230. $noSelect.removeClass('hide');
  231. $($(this).parent().data('id')).val('');
  232. });
  233. function selectItem(select_id, input_id) {
  234. var $menu = $(select_id + ' .menu');
  235. var $list = $('.ui' + select_id + '.list');
  236. var hasUpdateAction = $menu.data('action') == 'update';
  237. $menu.find('.item:not(.no-select)').click(function () {
  238. $(this).parent().find('.item').each(function () {
  239. $(this).removeClass('selected active')
  240. });
  241. $(this).addClass('selected active');
  242. if (hasUpdateAction) {
  243. updateIssuesMeta(
  244. $menu.data('update-url'),
  245. "",
  246. $menu.data('issue-id'),
  247. $(this).data('id'),
  248. function() { location.reload(); }
  249. );
  250. }
  251. switch (input_id) {
  252. case '#milestone_id':
  253. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  254. $(this).text() + '</a>');
  255. break;
  256. case '#assignee_id':
  257. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  258. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  259. $(this).text() + '</a>');
  260. }
  261. $('.ui' + select_id + '.list .no-select').addClass('hide');
  262. $(input_id).val($(this).data('id'));
  263. });
  264. $menu.find('.no-select.item').click(function () {
  265. $(this).parent().find('.item:not(.no-select)').each(function () {
  266. $(this).removeClass('selected active')
  267. });
  268. if (hasUpdateAction) {
  269. updateIssuesMeta(
  270. $menu.data('update-url'),
  271. "",
  272. $menu.data('issue-id'),
  273. $(this).data('id'),
  274. function() { location.reload(); }
  275. );
  276. }
  277. $list.find('.selected').html('');
  278. $list.find('.no-select').removeClass('hide');
  279. $(input_id).val('');
  280. });
  281. }
  282. // Milestone and assignee
  283. selectItem('.select-milestone', '#milestone_id');
  284. selectItem('.select-assignee', '#assignee_id');
  285. }
  286. function initInstall() {
  287. if ($('.install').length == 0) {
  288. return;
  289. }
  290. if ($('#db_host').val()=="") {
  291. $('#db_host').val("127.0.0.1:3306");
  292. $('#db_user').val("gitea");
  293. $('#db_name').val("gitea");
  294. }
  295. // Database type change detection.
  296. $("#db_type").change(function () {
  297. var sqliteDefault = 'data/gitea.db';
  298. var tidbDefault = 'data/gitea_tidb';
  299. var dbType = $(this).val();
  300. if (dbType === "SQLite3" || dbType === "TiDB") {
  301. $('#sql_settings').hide();
  302. $('#pgsql_settings').hide();
  303. $('#sqlite_settings').show();
  304. if (dbType === "SQLite3" && $('#db_path').val() == tidbDefault) {
  305. $('#db_path').val(sqliteDefault);
  306. } else if (dbType === "TiDB" && $('#db_path').val() == sqliteDefault) {
  307. $('#db_path').val(tidbDefault);
  308. }
  309. return;
  310. }
  311. var dbDefaults = {
  312. "MySQL": "127.0.0.1:3306",
  313. "PostgreSQL": "127.0.0.1:5432",
  314. "MSSQL": "127.0.0.1:1433"
  315. };
  316. $('#sqlite_settings').hide();
  317. $('#sql_settings').show();
  318. $('#pgsql_settings').toggle(dbType === "PostgreSQL");
  319. $.each(dbDefaults, function(type, defaultHost) {
  320. if ($('#db_host').val() == defaultHost) {
  321. $('#db_host').val(dbDefaults[dbType]);
  322. return false;
  323. }
  324. });
  325. });
  326. // TODO: better handling of exclusive relations.
  327. $('#offline-mode input').change(function () {
  328. if ($(this).is(':checked')) {
  329. $('#disable-gravatar').checkbox('check');
  330. $('#federated-avatar-lookup').checkbox('uncheck');
  331. }
  332. });
  333. $('#disable-gravatar input').change(function () {
  334. if ($(this).is(':checked')) {
  335. $('#federated-avatar-lookup').checkbox('uncheck');
  336. } else {
  337. $('#offline-mode').checkbox('uncheck');
  338. }
  339. });
  340. $('#federated-avatar-lookup input').change(function () {
  341. if ($(this).is(':checked')) {
  342. $('#disable-gravatar').checkbox('uncheck');
  343. $('#offline-mode').checkbox('uncheck');
  344. }
  345. });
  346. $('#enable-openid-signin input').change(function () {
  347. if ($(this).is(':checked')) {
  348. if ( $('#disable-registration input').is(':checked') ) {
  349. } else {
  350. $('#enable-openid-signup').checkbox('check');
  351. }
  352. } else {
  353. $('#enable-openid-signup').checkbox('uncheck');
  354. }
  355. });
  356. $('#disable-registration input').change(function () {
  357. if ($(this).is(':checked')) {
  358. $('#enable-captcha').checkbox('uncheck');
  359. $('#enable-openid-signup').checkbox('uncheck');
  360. } else {
  361. $('#enable-openid-signup').checkbox('check');
  362. }
  363. });
  364. $('#enable-captcha input').change(function () {
  365. if ($(this).is(':checked')) {
  366. $('#disable-registration').checkbox('uncheck');
  367. }
  368. });
  369. }
  370. function initRepository() {
  371. if ($('.repository').length == 0) {
  372. return;
  373. }
  374. function initFilterSearchDropdown(selector) {
  375. var $dropdown = $(selector);
  376. $dropdown.dropdown({
  377. fullTextSearch: true,
  378. selectOnKeydown: false,
  379. onChange: function (text, value, $choice) {
  380. if ($choice.data('url')) {
  381. window.location.href = $choice.data('url');
  382. }
  383. },
  384. message: {noResults: $dropdown.data('no-results')}
  385. });
  386. }
  387. // File list and commits
  388. if ($('.repository.file.list').length > 0 ||
  389. ('.repository.commits').length > 0) {
  390. initFilterBranchTagDropdown('.choose.reference .dropdown');
  391. }
  392. // Wiki
  393. if ($('.repository.wiki.view').length > 0) {
  394. initFilterSearchDropdown('.choose.page .dropdown');
  395. }
  396. // Options
  397. if ($('.repository.settings.options').length > 0) {
  398. $('#repo_name').keyup(function () {
  399. var $prompt = $('#repo-name-change-prompt');
  400. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  401. $prompt.show();
  402. } else {
  403. $prompt.hide();
  404. }
  405. });
  406. // Enable or select internal/external wiki system and issue tracker.
  407. $('.enable-system').change(function () {
  408. if (this.checked) {
  409. $($(this).data('target')).removeClass('disabled');
  410. if (!$(this).data('context')) $($(this).data('context')).addClass('disabled');
  411. } else {
  412. $($(this).data('target')).addClass('disabled');
  413. if (!$(this).data('context')) $($(this).data('context')).removeClass('disabled');
  414. }
  415. });
  416. $('.enable-system-radio').change(function () {
  417. if (this.value == 'false') {
  418. $($(this).data('target')).addClass('disabled');
  419. if (typeof $(this).data('context') !== 'undefined') $($(this).data('context')).removeClass('disabled');
  420. } else if (this.value == 'true') {
  421. $($(this).data('target')).removeClass('disabled');
  422. if (typeof $(this).data('context') !== 'undefined') $($(this).data('context')).addClass('disabled');
  423. }
  424. });
  425. }
  426. // Labels
  427. if ($('.repository.labels').length > 0) {
  428. // Create label
  429. var $newLabelPanel = $('.new-label.segment');
  430. $('.new-label.button').click(function () {
  431. $newLabelPanel.show();
  432. });
  433. $('.new-label.segment .cancel').click(function () {
  434. $newLabelPanel.hide();
  435. });
  436. $('.color-picker').each(function () {
  437. $(this).minicolors();
  438. });
  439. $('.precolors .color').click(function () {
  440. var color_hex = $(this).data('color-hex');
  441. $('.color-picker').val(color_hex);
  442. $('.minicolors-swatch-color').css("background-color", color_hex);
  443. });
  444. $('.edit-label-button').click(function () {
  445. $('#label-modal-id').val($(this).data('id'));
  446. $('.edit-label .new-label-input').val($(this).data('title'));
  447. $('.edit-label .color-picker').val($(this).data('color'));
  448. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  449. $('.edit-label.modal').modal({
  450. onApprove: function () {
  451. $('.edit-label.form').submit();
  452. }
  453. }).modal('show');
  454. return false;
  455. });
  456. }
  457. // Milestones
  458. if ($('.repository.milestones').length > 0) {
  459. }
  460. if ($('.repository.new.milestone').length > 0) {
  461. var $datepicker = $('.milestone.datepicker');
  462. $datepicker.datetimepicker({
  463. lang: $datepicker.data('lang'),
  464. inline: true,
  465. timepicker: false,
  466. startDate: $datepicker.data('start-date'),
  467. formatDate: 'Y-m-d',
  468. onSelectDate: function (ct) {
  469. $('#deadline').val(ct.dateFormat('Y-m-d'));
  470. }
  471. });
  472. $('#clear-date').click(function () {
  473. $('#deadline').val('');
  474. return false;
  475. });
  476. }
  477. // Issues
  478. if ($('.repository.view.issue').length > 0) {
  479. // Edit issue title
  480. var $issueTitle = $('#issue-title');
  481. var $editInput = $('#edit-title-input input');
  482. var editTitleToggle = function () {
  483. $issueTitle.toggle();
  484. $('.not-in-edit').toggle();
  485. $('#edit-title-input').toggle();
  486. $('.in-edit').toggle();
  487. $editInput.focus();
  488. return false;
  489. };
  490. $('#edit-title').click(editTitleToggle);
  491. $('#cancel-edit-title').click(editTitleToggle);
  492. $('#save-edit-title').click(editTitleToggle).click(function () {
  493. if ($editInput.val().length == 0 ||
  494. $editInput.val() == $issueTitle.text()) {
  495. $editInput.val($issueTitle.text());
  496. return false;
  497. }
  498. $.post($(this).data('update-url'), {
  499. "_csrf": csrf,
  500. "title": $editInput.val()
  501. },
  502. function (data) {
  503. $editInput.val(data.title);
  504. $issueTitle.text(data.title);
  505. location.reload();
  506. });
  507. return false;
  508. });
  509. // Edit issue or comment content
  510. $('.edit-content').click(function () {
  511. var $segment = $(this).parent().parent().parent().next();
  512. var $editContentZone = $segment.find('.edit-content-zone');
  513. var $renderContent = $segment.find('.render-content');
  514. var $rawContent = $segment.find('.raw-content');
  515. var $textarea;
  516. // Setup new form
  517. if ($editContentZone.html().length == 0) {
  518. $editContentZone.html($('#edit-content-form').html());
  519. $textarea = $segment.find('textarea');
  520. issuesTribute.attach($textarea.get());
  521. emojiTribute.attach($textarea.get());
  522. // Give new write/preview data-tab name to distinguish from others
  523. var $editContentForm = $editContentZone.find('.ui.comment.form');
  524. var $tabMenu = $editContentForm.find('.tabular.menu');
  525. $tabMenu.attr('data-write', $editContentZone.data('write'));
  526. $tabMenu.attr('data-preview', $editContentZone.data('preview'));
  527. $tabMenu.find('.write.item').attr('data-tab', $editContentZone.data('write'));
  528. $tabMenu.find('.preview.item').attr('data-tab', $editContentZone.data('preview'));
  529. $editContentForm.find('.write.segment').attr('data-tab', $editContentZone.data('write'));
  530. $editContentForm.find('.preview.segment').attr('data-tab', $editContentZone.data('preview'));
  531. initCommentPreviewTab($editContentForm);
  532. $editContentZone.find('.cancel.button').click(function () {
  533. $renderContent.show();
  534. $editContentZone.hide();
  535. });
  536. $editContentZone.find('.save.button').click(function () {
  537. $renderContent.show();
  538. $editContentZone.hide();
  539. $.post($editContentZone.data('update-url'), {
  540. "_csrf": csrf,
  541. "content": $textarea.val(),
  542. "context": $editContentZone.data('context')
  543. },
  544. function (data) {
  545. if (data.length == 0) {
  546. $renderContent.html($('#no-content').html());
  547. } else {
  548. $renderContent.html(data.content);
  549. emojify.run($renderContent[0]);
  550. $('pre code', $renderContent[0]).each(function (i, block) {
  551. hljs.highlightBlock(block);
  552. });
  553. }
  554. });
  555. });
  556. } else {
  557. $textarea = $segment.find('textarea');
  558. }
  559. // Show write/preview tab and copy raw content as needed
  560. $editContentZone.show();
  561. $renderContent.hide();
  562. if ($textarea.val().length == 0) {
  563. $textarea.val($rawContent.text());
  564. }
  565. $textarea.focus();
  566. return false;
  567. });
  568. // Delete comment
  569. $('.delete-comment').click(function () {
  570. var $this = $(this);
  571. if (confirm($this.data('locale'))) {
  572. $.post($this.data('url'), {
  573. "_csrf": csrf
  574. }).success(function () {
  575. $('#' + $this.data('comment-id')).remove();
  576. });
  577. }
  578. return false;
  579. });
  580. // Change status
  581. var $statusButton = $('#status-button');
  582. $('#comment-form .edit_area').keyup(function () {
  583. if ($(this).val().length == 0) {
  584. $statusButton.text($statusButton.data('status'))
  585. } else {
  586. $statusButton.text($statusButton.data('status-and-comment'))
  587. }
  588. });
  589. $statusButton.click(function () {
  590. $('#status').val($statusButton.data('status-val'));
  591. $('#comment-form').submit();
  592. });
  593. // Pull Request merge button
  594. var $mergeButton = $('.merge-button > button');
  595. $mergeButton.on('click', function(e) {
  596. e.preventDefault();
  597. $('.' + $(this).data('do') + '-fields').show();
  598. $(this).parent().hide();
  599. });
  600. $('.merge-button > .dropdown').dropdown({
  601. onChange: function (text, value, $choice) {
  602. if ($choice.data('do')) {
  603. $mergeButton.find('.button-text').text($choice.text());
  604. $mergeButton.data('do', $choice.data('do'));
  605. }
  606. }
  607. });
  608. $('.merge-cancel').on('click', function(e) {
  609. e.preventDefault();
  610. $(this).closest('.form').hide();
  611. $mergeButton.parent().show();
  612. });
  613. initReactionSelector();
  614. }
  615. // Diff
  616. if ($('.repository.diff').length > 0) {
  617. var $counter = $('.diff-counter');
  618. if ($counter.length >= 1) {
  619. $counter.each(function (i, item) {
  620. var $item = $(item);
  621. var addLine = $item.find('span[data-line].add').data("line");
  622. var delLine = $item.find('span[data-line].del').data("line");
  623. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  624. $item.find(".bar .add").css("width", addPercent + "%");
  625. });
  626. }
  627. }
  628. // Quick start and repository home
  629. $('#repo-clone-ssh').click(function () {
  630. $('.clone-url').text($(this).data('link'));
  631. $('#repo-clone-url').val($(this).data('link'));
  632. $(this).addClass('blue');
  633. $('#repo-clone-https').removeClass('blue');
  634. localStorage.setItem('repo-clone-protocol', 'ssh');
  635. });
  636. $('#repo-clone-https').click(function () {
  637. $('.clone-url').text($(this).data('link'));
  638. $('#repo-clone-url').val($(this).data('link'));
  639. $(this).addClass('blue');
  640. $('#repo-clone-ssh').removeClass('blue');
  641. localStorage.setItem('repo-clone-protocol', 'https');
  642. });
  643. $('#repo-clone-url').click(function () {
  644. $(this).select();
  645. });
  646. // Pull request
  647. if ($('.repository.compare.pull').length > 0) {
  648. initFilterSearchDropdown('.choose.branch .dropdown');
  649. }
  650. // Branches
  651. if ($('.repository.settings.branches').length > 0) {
  652. initFilterSearchDropdown('.protected-branches .dropdown');
  653. $('.enable-protection, .enable-whitelist').change(function () {
  654. if (this.checked) {
  655. $($(this).data('target')).removeClass('disabled');
  656. } else {
  657. $($(this).data('target')).addClass('disabled');
  658. }
  659. });
  660. }
  661. }
  662. function initRepositoryCollaboration() {
  663. console.log('initRepositoryCollaboration');
  664. // Change collaborator access mode
  665. $('.access-mode.menu .item').click(function () {
  666. var $menu = $(this).parent();
  667. $.post($menu.data('url'), {
  668. "_csrf": csrf,
  669. "uid": $menu.data('uid'),
  670. "mode": $(this).data('value')
  671. })
  672. });
  673. }
  674. function initTeamSettings() {
  675. // Change team access mode
  676. $('.organization.new.team input[name=permission]').change(function () {
  677. var val = $('input[name=permission]:checked', '.organization.new.team').val()
  678. if (val === 'admin') {
  679. $('.organization.new.team .team-units').hide();
  680. } else {
  681. $('.organization.new.team .team-units').show();
  682. }
  683. });
  684. }
  685. function initWikiForm() {
  686. var $editArea = $('.repository.wiki textarea#edit_area');
  687. if ($editArea.length > 0) {
  688. new SimpleMDE({
  689. autoDownloadFontAwesome: false,
  690. element: $editArea[0],
  691. forceSync: true,
  692. previewRender: function (plainText, preview) { // Async method
  693. setTimeout(function () {
  694. // FIXME: still send render request when return back to edit mode
  695. $.post($editArea.data('url'), {
  696. "_csrf": csrf,
  697. "mode": "gfm",
  698. "context": $editArea.data('context'),
  699. "text": plainText
  700. },
  701. function (data) {
  702. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  703. emojify.run($('.editor-preview')[0]);
  704. }
  705. );
  706. }, 0);
  707. return "Loading...";
  708. },
  709. renderingConfig: {
  710. singleLineBreaks: false
  711. },
  712. indentWithTabs: false,
  713. tabSize: 4,
  714. spellChecker: false,
  715. toolbar: ["bold", "italic", "strikethrough", "|",
  716. "heading-1", "heading-2", "heading-3", "heading-bigger", "heading-smaller", "|",
  717. "code", "quote", "|",
  718. "unordered-list", "ordered-list", "|",
  719. "link", "image", "table", "horizontal-rule", "|",
  720. "clean-block", "preview", "fullscreen"]
  721. })
  722. }
  723. }
  724. var simpleMDEditor;
  725. var codeMirrorEditor;
  726. // For IE
  727. String.prototype.endsWith = function (pattern) {
  728. var d = this.length - pattern.length;
  729. return d >= 0 && this.lastIndexOf(pattern) === d;
  730. };
  731. // Adding function to get the cursor position in a text field to jQuery object.
  732. (function ($, undefined) {
  733. $.fn.getCursorPosition = function () {
  734. var el = $(this).get(0);
  735. var pos = 0;
  736. if ('selectionStart' in el) {
  737. pos = el.selectionStart;
  738. } else if ('selection' in document) {
  739. el.focus();
  740. var Sel = document.selection.createRange();
  741. var SelLength = document.selection.createRange().text.length;
  742. Sel.moveStart('character', -el.value.length);
  743. pos = Sel.text.length - SelLength;
  744. }
  745. return pos;
  746. }
  747. })(jQuery);
  748. function setSimpleMDE($editArea) {
  749. if (codeMirrorEditor) {
  750. codeMirrorEditor.toTextArea();
  751. codeMirrorEditor = null;
  752. }
  753. if (simpleMDEditor) {
  754. return true;
  755. }
  756. simpleMDEditor = new SimpleMDE({
  757. autoDownloadFontAwesome: false,
  758. element: $editArea[0],
  759. forceSync: true,
  760. renderingConfig: {
  761. singleLineBreaks: false
  762. },
  763. indentWithTabs: false,
  764. tabSize: 4,
  765. spellChecker: false,
  766. previewRender: function (plainText, preview) { // Async method
  767. setTimeout(function () {
  768. // FIXME: still send render request when return back to edit mode
  769. $.post($editArea.data('url'), {
  770. "_csrf": csrf,
  771. "mode": "gfm",
  772. "context": $editArea.data('context'),
  773. "text": plainText
  774. },
  775. function (data) {
  776. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  777. emojify.run($('.editor-preview')[0]);
  778. }
  779. );
  780. }, 0);
  781. return "Loading...";
  782. },
  783. toolbar: ["bold", "italic", "strikethrough", "|",
  784. "heading-1", "heading-2", "heading-3", "heading-bigger", "heading-smaller", "|",
  785. "code", "quote", "|",
  786. "unordered-list", "ordered-list", "|",
  787. "link", "image", "table", "horizontal-rule", "|",
  788. "clean-block", "preview", "fullscreen", "side-by-side"]
  789. });
  790. return true;
  791. }
  792. function setCodeMirror($editArea) {
  793. if (simpleMDEditor) {
  794. simpleMDEditor.toTextArea();
  795. simpleMDEditor = null;
  796. }
  797. if (codeMirrorEditor) {
  798. return true;
  799. }
  800. codeMirrorEditor = CodeMirror.fromTextArea($editArea[0], {
  801. lineNumbers: true
  802. });
  803. codeMirrorEditor.on("change", function (cm, change) {
  804. $editArea.val(cm.getValue());
  805. });
  806. return true;
  807. }
  808. function initEditor() {
  809. $('.js-quick-pull-choice-option').change(function () {
  810. if ($(this).val() == 'commit-to-new-branch') {
  811. $('.quick-pull-branch-name').show();
  812. $('.quick-pull-branch-name input').prop('required',true);
  813. } else {
  814. $('.quick-pull-branch-name').hide();
  815. $('.quick-pull-branch-name input').prop('required',false);
  816. }
  817. });
  818. var $editFilename = $("#file-name");
  819. $editFilename.keyup(function (e) {
  820. var $section = $('.breadcrumb span.section');
  821. var $divider = $('.breadcrumb div.divider');
  822. if (e.keyCode == 8) {
  823. if ($(this).getCursorPosition() == 0) {
  824. if ($section.length > 0) {
  825. var value = $section.last().find('a').text();
  826. $(this).val(value + $(this).val());
  827. $(this)[0].setSelectionRange(value.length, value.length);
  828. $section.last().remove();
  829. $divider.last().remove();
  830. }
  831. }
  832. }
  833. if (e.keyCode == 191) {
  834. var parts = $(this).val().split('/');
  835. for (var i = 0; i < parts.length; ++i) {
  836. var value = parts[i];
  837. if (i < parts.length - 1) {
  838. if (value.length) {
  839. $('<span class="section"><a href="#">' + value + '</a></span>').insertBefore($(this));
  840. $('<div class="divider"> / </div>').insertBefore($(this));
  841. }
  842. }
  843. else {
  844. $(this).val(value);
  845. }
  846. $(this)[0].setSelectionRange(0, 0);
  847. }
  848. }
  849. var parts = [];
  850. $('.breadcrumb span.section').each(function (i, element) {
  851. element = $(element);
  852. if (element.find('a').length) {
  853. parts.push(element.find('a').text());
  854. } else {
  855. parts.push(element.text());
  856. }
  857. });
  858. if ($(this).val())
  859. parts.push($(this).val());
  860. $('#tree_path').val(parts.join('/'));
  861. }).trigger('keyup');
  862. var $editArea = $('.repository.editor textarea#edit_area');
  863. if (!$editArea.length)
  864. return;
  865. var markdownFileExts = $editArea.data("markdown-file-exts").split(",");
  866. var lineWrapExtensions = $editArea.data("line-wrap-extensions").split(",");
  867. $editFilename.on("keyup", function (e) {
  868. var val = $editFilename.val(), m, mode, spec, extension, extWithDot, previewLink, dataUrl, apiCall;
  869. extension = extWithDot = "";
  870. if (m = /.+\.([^.]+)$/.exec(val)) {
  871. extension = m[1];
  872. extWithDot = "." + extension;
  873. }
  874. var info = CodeMirror.findModeByExtension(extension);
  875. previewLink = $('a[data-tab=preview]');
  876. if (info) {
  877. mode = info.mode;
  878. spec = info.mime;
  879. apiCall = mode;
  880. }
  881. else {
  882. apiCall = extension
  883. }
  884. if (previewLink.length && apiCall && previewFileModes && previewFileModes.length && previewFileModes.indexOf(apiCall) >= 0) {
  885. dataUrl = previewLink.data('url');
  886. previewLink.data('url', dataUrl.replace(/(.*)\/.*/i, '$1/' + mode));
  887. previewLink.show();
  888. }
  889. else {
  890. previewLink.hide();
  891. }
  892. // If this file is a Markdown extensions, we will load that editor and return
  893. if (markdownFileExts.indexOf(extWithDot) >= 0) {
  894. if (setSimpleMDE($editArea)) {
  895. return;
  896. }
  897. }
  898. // Else we are going to use CodeMirror
  899. if (!codeMirrorEditor && !setCodeMirror($editArea)) {
  900. return;
  901. }
  902. if (mode) {
  903. codeMirrorEditor.setOption("mode", spec);
  904. CodeMirror.autoLoadMode(codeMirrorEditor, mode);
  905. }
  906. if (lineWrapExtensions.indexOf(extWithDot) >= 0) {
  907. codeMirrorEditor.setOption("lineWrapping", true);
  908. }
  909. else {
  910. codeMirrorEditor.setOption("lineWrapping", false);
  911. }
  912. // get the filename without any folder
  913. var value = $editFilename.val();
  914. if (value.length === 0) {
  915. return;
  916. }
  917. value = value.split('/');
  918. value = value[value.length - 1];
  919. $.getJSON($editFilename.data('ec-url-prefix')+value, function(editorconfig) {
  920. if (editorconfig.indent_style === 'tab') {
  921. codeMirrorEditor.setOption("indentWithTabs", true);
  922. codeMirrorEditor.setOption('extraKeys', {});
  923. } else {
  924. codeMirrorEditor.setOption("indentWithTabs", false);
  925. // required because CodeMirror doesn't seems to use spaces correctly for {"indentWithTabs": false}:
  926. // - https://github.com/codemirror/CodeMirror/issues/988
  927. // - https://codemirror.net/doc/manual.html#keymaps
  928. codeMirrorEditor.setOption('extraKeys', {
  929. Tab: function(cm) {
  930. var spaces = Array(parseInt(cm.getOption("indentUnit")) + 1).join(" ");
  931. cm.replaceSelection(spaces);
  932. }
  933. });
  934. }
  935. codeMirrorEditor.setOption("indentUnit", editorconfig.indent_size || 4);
  936. codeMirrorEditor.setOption("tabSize", editorconfig.tab_width || 4);
  937. });
  938. }).trigger('keyup');
  939. }
  940. function initOrganization() {
  941. if ($('.organization').length == 0) {
  942. return;
  943. }
  944. // Options
  945. if ($('.organization.settings.options').length > 0) {
  946. $('#org_name').keyup(function () {
  947. var $prompt = $('#org-name-change-prompt');
  948. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  949. $prompt.show();
  950. } else {
  951. $prompt.hide();
  952. }
  953. });
  954. }
  955. }
  956. function initUserSettings() {
  957. console.log('initUserSettings');
  958. // Options
  959. if ($('.user.settings.profile').length > 0) {
  960. $('#username').keyup(function () {
  961. var $prompt = $('#name-change-prompt');
  962. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  963. $prompt.show();
  964. } else {
  965. $prompt.hide();
  966. }
  967. });
  968. }
  969. }
  970. function initWebhook() {
  971. if ($('.new.webhook').length == 0) {
  972. return;
  973. }
  974. $('.events.checkbox input').change(function () {
  975. if ($(this).is(':checked')) {
  976. $('.events.fields').show();
  977. }
  978. });
  979. $('.non-events.checkbox input').change(function () {
  980. if ($(this).is(':checked')) {
  981. $('.events.fields').hide();
  982. }
  983. });
  984. // Test delivery
  985. $('#test-delivery').click(function () {
  986. var $this = $(this);
  987. $this.addClass('loading disabled');
  988. $.post($this.data('link'), {
  989. "_csrf": csrf
  990. }).done(
  991. setTimeout(function () {
  992. window.location.href = $this.data('redirect');
  993. }, 5000)
  994. )
  995. });
  996. }
  997. function initAdmin() {
  998. if ($('.admin').length == 0) {
  999. return;
  1000. }
  1001. // New user
  1002. if ($('.admin.new.user').length > 0 ||
  1003. $('.admin.edit.user').length > 0) {
  1004. $('#login_type').change(function () {
  1005. if ($(this).val().substring(0, 1) == '0') {
  1006. $('#login_name').removeAttr('required');
  1007. $('.non-local').hide();
  1008. $('.local').show();
  1009. $('#user_name').focus();
  1010. if ($(this).data('password') == "required") {
  1011. $('#password').attr('required', 'required');
  1012. }
  1013. } else {
  1014. $('#login_name').attr('required', 'required');
  1015. $('.non-local').show();
  1016. $('.local').hide();
  1017. $('#login_name').focus();
  1018. $('#password').removeAttr('required');
  1019. }
  1020. });
  1021. }
  1022. function onSecurityProtocolChange() {
  1023. if ($('#security_protocol').val() > 0) {
  1024. $('.has-tls').show();
  1025. } else {
  1026. $('.has-tls').hide();
  1027. }
  1028. }
  1029. function onOAuth2Change() {
  1030. $('.open_id_connect_auto_discovery_url, .oauth2_use_custom_url').hide();
  1031. $('.open_id_connect_auto_discovery_url input[required]').removeAttr('required');
  1032. var provider = $('#oauth2_provider').val();
  1033. switch (provider) {
  1034. case 'github':
  1035. case 'gitlab':
  1036. $('.oauth2_use_custom_url').show();
  1037. break;
  1038. case 'openidConnect':
  1039. $('.open_id_connect_auto_discovery_url input').attr('required', 'required');
  1040. $('.open_id_connect_auto_discovery_url').show();
  1041. break;
  1042. }
  1043. onOAuth2UseCustomURLChange();
  1044. }
  1045. function onOAuth2UseCustomURLChange() {
  1046. var provider = $('#oauth2_provider').val();
  1047. $('.oauth2_use_custom_url_field').hide();
  1048. $('.oauth2_use_custom_url_field input[required]').removeAttr('required');
  1049. if ($('#oauth2_use_custom_url').is(':checked')) {
  1050. if (!$('#oauth2_token_url').val()) {
  1051. $('#oauth2_token_url').val($('#' + provider + '_token_url').val());
  1052. }
  1053. if (!$('#oauth2_auth_url').val()) {
  1054. $('#oauth2_auth_url').val($('#' + provider + '_auth_url').val());
  1055. }
  1056. if (!$('#oauth2_profile_url').val()) {
  1057. $('#oauth2_profile_url').val($('#' + provider + '_profile_url').val());
  1058. }
  1059. if (!$('#oauth2_email_url').val()) {
  1060. $('#oauth2_email_url').val($('#' + provider + '_email_url').val());
  1061. }
  1062. switch (provider) {
  1063. case 'github':
  1064. $('.oauth2_token_url input, .oauth2_auth_url input, .oauth2_profile_url input, .oauth2_email_url input').attr('required', 'required');
  1065. $('.oauth2_token_url, .oauth2_auth_url, .oauth2_profile_url, .oauth2_email_url').show();
  1066. break;
  1067. case 'gitlab':
  1068. $('.oauth2_token_url input, .oauth2_auth_url input, .oauth2_profile_url input').attr('required', 'required');
  1069. $('.oauth2_token_url, .oauth2_auth_url, .oauth2_profile_url').show();
  1070. $('#oauth2_email_url').val('');
  1071. break;
  1072. }
  1073. }
  1074. }
  1075. // New authentication
  1076. if ($('.admin.new.authentication').length > 0) {
  1077. $('#auth_type').change(function () {
  1078. $('.ldap, .dldap, .smtp, .pam, .oauth2, .has-tls').hide();
  1079. $('.ldap input[required], .dldap input[required], .smtp input[required], .pam input[required], .oauth2 input[required], .has-tls input[required]').removeAttr('required');
  1080. var authType = $(this).val();
  1081. switch (authType) {
  1082. case '2': // LDAP
  1083. $('.ldap').show();
  1084. $('.ldap div.required:not(.dldap) input').attr('required', 'required');
  1085. break;
  1086. case '3': // SMTP
  1087. $('.smtp').show();
  1088. $('.has-tls').show();
  1089. $('.smtp div.required input, .has-tls').attr('required', 'required');
  1090. break;
  1091. case '4': // PAM
  1092. $('.pam').show();
  1093. $('.pam input').attr('required', 'required');
  1094. break;
  1095. case '5': // LDAP
  1096. $('.dldap').show();
  1097. $('.dldap div.required:not(.ldap) input').attr('required', 'required');
  1098. break;
  1099. case '6': // OAuth2
  1100. $('.oauth2').show();
  1101. $('.oauth2 div.required:not(.oauth2_use_custom_url,.oauth2_use_custom_url_field,.open_id_connect_auto_discovery_url) input').attr('required', 'required');
  1102. onOAuth2Change();
  1103. break;
  1104. }
  1105. if (authType == '2' || authType == '5') {
  1106. onSecurityProtocolChange()
  1107. }
  1108. });
  1109. $('#auth_type').change();
  1110. $('#security_protocol').change(onSecurityProtocolChange);
  1111. $('#oauth2_provider').change(onOAuth2Change);
  1112. $('#oauth2_use_custom_url').change(onOAuth2UseCustomURLChange);
  1113. }
  1114. // Edit authentication
  1115. if ($('.admin.edit.authentication').length > 0) {
  1116. var authType = $('#auth_type').val();
  1117. if (authType == '2' || authType == '5') {
  1118. $('#security_protocol').change(onSecurityProtocolChange);
  1119. } else if (authType == '6') {
  1120. $('#oauth2_provider').change(onOAuth2Change);
  1121. $('#oauth2_use_custom_url').change(onOAuth2UseCustomURLChange);
  1122. onOAuth2Change();
  1123. }
  1124. }
  1125. // Notice
  1126. if ($('.admin.notice')) {
  1127. var $detailModal = $('#detail-modal');
  1128. // Attach view detail modals
  1129. $('.view-detail').click(function () {
  1130. $detailModal.find('.content p').text($(this).data('content'));
  1131. $detailModal.modal('show');
  1132. return false;
  1133. });
  1134. // Select actions
  1135. var $checkboxes = $('.select.table .ui.checkbox');
  1136. $('.select.action').click(function () {
  1137. switch ($(this).data('action')) {
  1138. case 'select-all':
  1139. $checkboxes.checkbox('check');
  1140. break;
  1141. case 'deselect-all':
  1142. $checkboxes.checkbox('uncheck');
  1143. break;
  1144. case 'inverse':
  1145. $checkboxes.checkbox('toggle');
  1146. break;
  1147. }
  1148. });
  1149. $('#delete-selection').click(function () {
  1150. var $this = $(this);
  1151. $this.addClass("loading disabled");
  1152. var ids = [];
  1153. $checkboxes.each(function () {
  1154. if ($(this).checkbox('is checked')) {
  1155. ids.push($(this).data('id'));
  1156. }
  1157. });
  1158. $.post($this.data('link'), {
  1159. "_csrf": csrf,
  1160. "ids": ids
  1161. }).done(function () {
  1162. window.location.href = $this.data('redirect');
  1163. });
  1164. });
  1165. }
  1166. }
  1167. function buttonsClickOnEnter() {
  1168. $('.ui.button').keypress(function (e) {
  1169. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  1170. $(this).click();
  1171. });
  1172. }
  1173. function hideWhenLostFocus(body, parent) {
  1174. $(document).click(function (e) {
  1175. var target = e.target;
  1176. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  1177. $(body).hide();
  1178. }
  1179. });
  1180. }
  1181. function searchUsers() {
  1182. var $searchUserBox = $('#search-user-box');
  1183. $searchUserBox.search({
  1184. minCharacters: 2,
  1185. apiSettings: {
  1186. url: suburl + '/api/v1/users/search?q={query}',
  1187. onResponse: function(response) {
  1188. var items = [];
  1189. $.each(response.data, function (i, item) {
  1190. var title = item.login;
  1191. if (item.full_name && item.full_name.length > 0) {
  1192. title += ' (' + item.full_name + ')';
  1193. }
  1194. items.push({
  1195. title: title,
  1196. image: item.avatar_url
  1197. })
  1198. });
  1199. return { results: items }
  1200. }
  1201. },
  1202. searchFields: ['login', 'full_name'],
  1203. showNoResults: false
  1204. });
  1205. }
  1206. function searchRepositories() {
  1207. var $searchRepoBox = $('#search-repo-box');
  1208. $searchRepoBox.search({
  1209. minCharacters: 2,
  1210. apiSettings: {
  1211. url: suburl + '/api/v1/repos/search?q={query}&uid=' + $searchRepoBox.data('uid'),
  1212. onResponse: function(response) {
  1213. var items = [];
  1214. $.each(response.data, function (i, item) {
  1215. items.push({
  1216. title: item.full_name.split("/")[1],
  1217. description: item.full_name
  1218. })
  1219. });
  1220. return { results: items }
  1221. }
  1222. },
  1223. searchFields: ['full_name'],
  1224. showNoResults: false
  1225. });
  1226. }
  1227. function initCodeView() {
  1228. if ($('.code-view .linenums').length > 0) {
  1229. $(document).on('click', '.lines-num span', function (e) {
  1230. var $select = $(this);
  1231. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  1232. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  1233. deSelect();
  1234. });
  1235. $(window).on('hashchange', function (e) {
  1236. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1237. var $list = $('.code-view ol.linenums > li');
  1238. var $first;
  1239. if (m) {
  1240. $first = $list.filter('.' + m[1]);
  1241. selectRange($list, $first, $list.filter('.' + m[2]));
  1242. $("html, body").scrollTop($first.offset().top - 200);
  1243. return;
  1244. }
  1245. m = window.location.hash.match(/^#(L\d+)$/);
  1246. if (m) {
  1247. $first = $list.filter('.' + m[1]);
  1248. selectRange($list, $first);
  1249. $("html, body").scrollTop($first.offset().top - 200);
  1250. }
  1251. }).trigger('hashchange');
  1252. }
  1253. }
  1254. $(document).ready(function () {
  1255. csrf = $('meta[name=_csrf]').attr("content");
  1256. suburl = $('meta[name=_suburl]').attr("content");
  1257. // Show exact time
  1258. $('.time-since').each(function () {
  1259. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  1260. });
  1261. // Semantic UI modules.
  1262. $('.dropdown:not(.custom)').dropdown();
  1263. $('.jump.dropdown').dropdown({
  1264. action: 'hide',
  1265. onShow: function () {
  1266. $('.poping.up').popup('hide');
  1267. }
  1268. });
  1269. $('.slide.up.dropdown').dropdown({
  1270. transition: 'slide up'
  1271. });
  1272. $('.upward.dropdown').dropdown({
  1273. direction: 'upward'
  1274. });
  1275. $('.ui.accordion').accordion();
  1276. $('.ui.checkbox').checkbox();
  1277. $('.ui.progress').progress({
  1278. showActivity: false
  1279. });
  1280. $('.poping.up').popup();
  1281. $('.top.menu .poping.up').popup({
  1282. onShow: function () {
  1283. if ($('.top.menu .menu.transition').hasClass('visible')) {
  1284. return false;
  1285. }
  1286. }
  1287. });
  1288. $('.tabular.menu .item').tab();
  1289. $('.tabable.menu .item').tab();
  1290. $('.toggle.button').click(function () {
  1291. $($(this).data('target')).slideToggle(100);
  1292. });
  1293. // make table <tr> element clickable like a link
  1294. $('tr[data-href]').click(function(event) {
  1295. window.location = $(this).data('href');
  1296. });
  1297. // Highlight JS
  1298. if (typeof hljs != 'undefined') {
  1299. hljs.initHighlightingOnLoad();
  1300. }
  1301. // Dropzone
  1302. var $dropzone = $('#dropzone');
  1303. if ($dropzone.length > 0) {
  1304. // Disable auto discover for all elements:
  1305. Dropzone.autoDiscover = false;
  1306. var filenameDict = {};
  1307. $dropzone.dropzone({
  1308. url: $dropzone.data('upload-url'),
  1309. headers: {"X-Csrf-Token": csrf},
  1310. maxFiles: $dropzone.data('max-file'),
  1311. maxFilesize: $dropzone.data('max-size'),
  1312. acceptedFiles: ($dropzone.data('accepts') === '*/*') ? null : $dropzone.data('accepts'),
  1313. addRemoveLinks: true,
  1314. dictDefaultMessage: $dropzone.data('default-message'),
  1315. dictInvalidFileType: $dropzone.data('invalid-input-type'),
  1316. dictFileTooBig: $dropzone.data('file-too-big'),
  1317. dictRemoveFile: $dropzone.data('remove-file'),
  1318. init: function () {
  1319. this.on("success", function (file, data) {
  1320. filenameDict[file.name] = data.uuid;
  1321. var input = $('<input id="' + data.uuid + '" name="files" type="hidden">').val(data.uuid);
  1322. $('.files').append(input);
  1323. });
  1324. this.on("removedfile", function (file) {
  1325. if (file.name in filenameDict) {
  1326. $('#' + filenameDict[file.name]).remove();
  1327. }
  1328. if ($dropzone.data('remove-url') && $dropzone.data('csrf')) {
  1329. $.post($dropzone.data('remove-url'), {
  1330. file: filenameDict[file.name],
  1331. _csrf: $dropzone.data('csrf')
  1332. });
  1333. }
  1334. })
  1335. }
  1336. });
  1337. }
  1338. // Emojify
  1339. emojify.setConfig({
  1340. img_dir: suburl + '/vendor/plugins/emojify/images',
  1341. ignore_emoticons: true
  1342. });
  1343. var hasEmoji = document.getElementsByClassName('has-emoji');
  1344. for (var i = 0; i < hasEmoji.length; i++) {
  1345. emojify.run(hasEmoji[i]);
  1346. }
  1347. // Clipboard JS
  1348. var clipboard = new Clipboard('.clipboard');
  1349. clipboard.on('success', function (e) {
  1350. e.clearSelection();
  1351. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1352. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  1353. $('#' + e.trigger.getAttribute('id')).popup('show');
  1354. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1355. });
  1356. clipboard.on('error', function (e) {
  1357. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1358. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  1359. $('#' + e.trigger.getAttribute('id')).popup('show');
  1360. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1361. });
  1362. // Helpers.
  1363. $('.delete-button').click(showDeletePopup);
  1364. $('.delete-branch-button').click(showDeletePopup);
  1365. $('.undo-button').click(function() {
  1366. var $this = $(this);
  1367. $.post($this.data('url'), {
  1368. "_csrf": csrf,
  1369. "id": $this.data("id")
  1370. }).done(function(data) {
  1371. window.location.href = data.redirect;
  1372. });
  1373. });
  1374. $('.show-panel.button').click(function () {
  1375. $($(this).data('panel')).show();
  1376. });
  1377. $('.show-modal.button').click(function () {
  1378. $($(this).data('modal')).modal('show');
  1379. });
  1380. $('.delete-post.button').click(function () {
  1381. var $this = $(this);
  1382. $.post($this.data('request-url'), {
  1383. "_csrf": csrf
  1384. }).done(function () {
  1385. window.location.href = $this.data('done-url');
  1386. });
  1387. });
  1388. // Set anchor.
  1389. $('.markdown').each(function () {
  1390. var headers = {};
  1391. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  1392. var node = $(this);
  1393. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, '').replace(/[ ]/g, '-'));
  1394. var name = val;
  1395. if (headers[val] > 0) {
  1396. name = val + '-' + headers[val];
  1397. }
  1398. if (headers[val] == undefined) {
  1399. headers[val] = 1;
  1400. } else {
  1401. headers[val] += 1;
  1402. }
  1403. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1404. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  1405. });
  1406. });
  1407. $('.issue-checkbox').click(function() {
  1408. var numChecked = $('.issue-checkbox').children('input:checked').length;
  1409. if (numChecked > 0) {
  1410. $('#issue-filters').hide();
  1411. $('#issue-actions').show();
  1412. } else {
  1413. $('#issue-filters').show();
  1414. $('#issue-actions').hide();
  1415. }
  1416. });
  1417. $('.issue-action').click(function () {
  1418. var action = this.dataset.action
  1419. var elementId = this.dataset.elementId
  1420. var issueIDs = $('.issue-checkbox').children('input:checked').map(function() {
  1421. return this.dataset.issueId;
  1422. }).get().join();
  1423. var url = this.dataset.url
  1424. updateIssuesMeta(url, action, issueIDs, elementId, function() {
  1425. location.reload();
  1426. });
  1427. });
  1428. buttonsClickOnEnter();
  1429. searchUsers();
  1430. searchRepositories();
  1431. initCommentForm();
  1432. initInstall();
  1433. initRepository();
  1434. initWikiForm();
  1435. initEditForm();
  1436. initEditor();
  1437. initOrganization();
  1438. initWebhook();
  1439. initAdmin();
  1440. initCodeView();
  1441. initVueApp();
  1442. initTeamSettings();
  1443. initCtrlEnterSubmit();
  1444. initNavbarContentToggle();
  1445. // Repo clone url.
  1446. if ($('#repo-clone-url').length > 0) {
  1447. switch (localStorage.getItem('repo-clone-protocol')) {
  1448. case 'ssh':
  1449. if ($('#repo-clone-ssh').click().length === 0) {
  1450. $('#repo-clone-https').click();
  1451. }
  1452. break;
  1453. default:
  1454. $('#repo-clone-https').click();
  1455. break;
  1456. }
  1457. }
  1458. var routes = {
  1459. 'div.user.settings': initUserSettings,
  1460. 'div.repository.settings.collaboration': initRepositoryCollaboration
  1461. };
  1462. var selector;
  1463. for (selector in routes) {
  1464. if ($(selector).length > 0) {
  1465. routes[selector]();
  1466. break;
  1467. }
  1468. }
  1469. });
  1470. function changeHash(hash) {
  1471. if (history.pushState) {
  1472. history.pushState(null, null, hash);
  1473. }
  1474. else {
  1475. location.hash = hash;
  1476. }
  1477. }
  1478. function deSelect() {
  1479. if (window.getSelection) {
  1480. window.getSelection().removeAllRanges();
  1481. } else {
  1482. document.selection.empty();
  1483. }
  1484. }
  1485. function selectRange($list, $select, $from) {
  1486. $list.removeClass('active');
  1487. if ($from) {
  1488. var a = parseInt($select.attr('rel').substr(1));
  1489. var b = parseInt($from.attr('rel').substr(1));
  1490. var c;
  1491. if (a != b) {
  1492. if (a > b) {
  1493. c = a;
  1494. a = b;
  1495. b = c;
  1496. }
  1497. var classes = [];
  1498. for (var i = a; i <= b; i++) {
  1499. classes.push('.L' + i);
  1500. }
  1501. $list.filter(classes.join(',')).addClass('active');
  1502. changeHash('#L' + a + '-' + 'L' + b);
  1503. return
  1504. }
  1505. }
  1506. $select.addClass('active');
  1507. changeHash('#' + $select.attr('rel'));
  1508. }
  1509. $(function () {
  1510. if ($('.user.signin').length > 0) return;
  1511. $('form').areYouSure();
  1512. // Parse SSH Key
  1513. $("#ssh-key-content").on('change paste keyup',function(){
  1514. var arrays = $(this).val().split(" ");
  1515. var $title = $("#ssh-key-title")
  1516. if ($title.val() === "" && arrays.length === 3 && arrays[2] !== "") {
  1517. $title.val(arrays[2]);
  1518. }
  1519. });
  1520. });
  1521. function showDeletePopup() {
  1522. var $this = $(this);
  1523. var filter = "";
  1524. if ($this.attr("id")) {
  1525. filter += "#" + $this.attr("id")
  1526. }
  1527. var dialog = $('.delete.modal' + filter);
  1528. dialog.find('.repo-name').text($this.data('repo-name'));
  1529. dialog.modal({
  1530. closable: false,
  1531. onApprove: function() {
  1532. if ($this.data('type') == "form") {
  1533. $($this.data('form')).submit();
  1534. return;
  1535. }
  1536. $.post($this.data('url'), {
  1537. "_csrf": csrf,
  1538. "id": $this.data("id")
  1539. }).done(function(data) {
  1540. window.location.href = data.redirect;
  1541. });
  1542. }
  1543. }).modal('show');
  1544. return false;
  1545. }
  1546. function initVueComponents(){
  1547. var vueDelimeters = ['${', '}'];
  1548. Vue.component('repo-search', {
  1549. delimiters: vueDelimeters,
  1550. props: {
  1551. searchLimit: {
  1552. type: Number,
  1553. default: 10
  1554. },
  1555. suburl: {
  1556. type: String,
  1557. required: true
  1558. },
  1559. uid: {
  1560. type: Number,
  1561. required: true
  1562. },
  1563. organizations: {
  1564. type: Array,
  1565. default: []
  1566. },
  1567. isOrganization: {
  1568. type: Boolean,
  1569. default: true
  1570. },
  1571. canCreateOrganization: {
  1572. type: Boolean,
  1573. default: false
  1574. },
  1575. organizationsTotalCount: {
  1576. type: Number,
  1577. default: 0
  1578. },
  1579. moreReposLink: {
  1580. type: String,
  1581. default: ''
  1582. }
  1583. },
  1584. data: function() {
  1585. return {
  1586. tab: 'repos',
  1587. repos: [],
  1588. reposTotalCount: 0,
  1589. reposFilter: 'all',
  1590. searchQuery: '',
  1591. isLoading: false,
  1592. repoTypes: {
  1593. 'all': {
  1594. count: 0,
  1595. searchMode: '',
  1596. },
  1597. 'forks': {
  1598. count: 0,
  1599. searchMode: 'fork',
  1600. },
  1601. 'mirrors': {
  1602. count: 0,
  1603. searchMode: 'mirror',
  1604. },
  1605. 'sources': {
  1606. count: 0,
  1607. searchMode: 'source',
  1608. },
  1609. 'collaborative': {
  1610. count: 0,
  1611. searchMode: 'collaborative',
  1612. },
  1613. }
  1614. }
  1615. },
  1616. computed: {
  1617. showMoreReposLink: function() {
  1618. return this.repos.length > 0 && this.repos.length < this.repoTypes[this.reposFilter].count;
  1619. },
  1620. searchURL: function() {
  1621. return this.suburl + '/api/v1/repos/search?uid=' + this.uid + '&q=' + this.searchQuery + '&limit=' + this.searchLimit + '&mode=' + this.repoTypes[this.reposFilter].searchMode + (this.reposFilter !== 'all' ? '&exclusive=1' : '');
  1622. },
  1623. repoTypeCount: function() {
  1624. return this.repoTypes[this.reposFilter].count;
  1625. }
  1626. },
  1627. mounted: function() {
  1628. this.searchRepos(this.reposFilter);
  1629. var self = this;
  1630. Vue.nextTick(function() {
  1631. self.$refs.search.focus();
  1632. });
  1633. },
  1634. methods: {
  1635. changeTab: function(t) {
  1636. this.tab = t;
  1637. },
  1638. changeReposFilter: function(filter) {
  1639. this.reposFilter = filter;
  1640. this.repos = [];
  1641. this.repoTypes[filter].count = 0;
  1642. this.searchRepos(filter);
  1643. },
  1644. showRepo: function(repo, filter) {
  1645. switch (filter) {
  1646. case 'sources':
  1647. return repo.owner.id == this.uid && !repo.mirror && !repo.fork;
  1648. case 'forks':
  1649. return repo.owner.id == this.uid && !repo.mirror && repo.fork;
  1650. case 'mirrors':
  1651. return repo.mirror;
  1652. case 'collaborative':
  1653. return repo.owner.id != this.uid && !repo.mirror;
  1654. default:
  1655. return true;
  1656. }
  1657. },
  1658. searchRepos: function(reposFilter) {
  1659. var self = this;
  1660. this.isLoading = true;
  1661. var searchedMode = this.repoTypes[reposFilter].searchMode;
  1662. var searchedURL = this.searchURL;
  1663. var searchedQuery = this.searchQuery;
  1664. $.getJSON(searchedURL, function(result, textStatus, request) {
  1665. if (searchedURL == self.searchURL) {
  1666. self.repos = result.data;
  1667. var count = request.getResponseHeader('X-Total-Count');
  1668. if (searchedQuery === '' && searchedMode === '') {
  1669. self.reposTotalCount = count;
  1670. }
  1671. self.repoTypes[reposFilter].count = count;
  1672. }
  1673. }).always(function() {
  1674. if (searchedURL == self.searchURL) {
  1675. self.isLoading = false;
  1676. }
  1677. });
  1678. },
  1679. repoClass: function(repo) {
  1680. if (repo.fork) {
  1681. return 'octicon octicon-repo-forked';
  1682. } else if (repo.mirror) {
  1683. return 'octicon octicon-repo-clone';
  1684. } else if (repo.private) {
  1685. return 'octicon octicon-lock';
  1686. } else {
  1687. return 'octicon octicon-repo';
  1688. }
  1689. }
  1690. }
  1691. })
  1692. }
  1693. function initCtrlEnterSubmit() {
  1694. $(".js-quick-submit").keydown(function(e) {
  1695. if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
  1696. $(this).closest("form").submit();
  1697. }
  1698. });
  1699. }
  1700. function initVueApp() {
  1701. var el = document.getElementById('app');
  1702. if (!el) {
  1703. return;
  1704. }
  1705. initVueComponents();
  1706. new Vue({
  1707. delimiters: ['${', '}'],
  1708. el: el,
  1709. data: {
  1710. searchLimit: document.querySelector('meta[name=_search_limit]').content,
  1711. suburl: document.querySelector('meta[name=_suburl]').content,
  1712. uid: document.querySelector('meta[name=_context_uid]').content,
  1713. },
  1714. });
  1715. }
  1716. function timeAddManual() {
  1717. $('.mini.modal')
  1718. .modal({
  1719. duration: 200,
  1720. onApprove: function() {
  1721. $('#add_time_manual_form').submit();
  1722. }
  1723. }).modal('show')
  1724. ;
  1725. }
  1726. function toggleStopwatch() {
  1727. $("#toggle_stopwatch_form").submit();
  1728. }
  1729. function cancelStopwatch() {
  1730. $("#cancel_stopwatch_form").submit();
  1731. }
  1732. function initFilterBranchTagDropdown(selector) {
  1733. $(selector).each(function() {
  1734. var $dropdown = $(this);
  1735. var $data = $dropdown.find('.data');
  1736. var data = {
  1737. items: [],
  1738. mode: $data.data('mode'),
  1739. searchTerm: '',
  1740. noResults: '',
  1741. canCreateBranch: false,
  1742. menuVisible: false,
  1743. active: 0
  1744. };
  1745. $data.find('.item').each(function() {
  1746. data.items.push({
  1747. name: $(this).text(),
  1748. url: $(this).data('url'),
  1749. branch: $(this).hasClass('branch'),
  1750. tag: $(this).hasClass('tag'),
  1751. selected: $(this).hasClass('selected')
  1752. });
  1753. });
  1754. $data.remove();
  1755. new Vue({
  1756. delimiters: ['${', '}'],
  1757. el: this,
  1758. data: data,
  1759. beforeMount: function () {
  1760. var vm = this;
  1761. this.noResults = vm.$el.getAttribute('data-no-results');
  1762. this.canCreateBranch = vm.$el.getAttribute('data-can-create-branch') === 'true';
  1763. document.body.addEventListener('click', function(event) {
  1764. if (vm.$el.contains(event.target)) {
  1765. return;
  1766. }
  1767. if (vm.menuVisible) {
  1768. Vue.set(vm, 'menuVisible', false);
  1769. }
  1770. });
  1771. },
  1772. watch: {
  1773. menuVisible: function(visible) {
  1774. if (visible) {
  1775. this.focusSearchField();
  1776. }
  1777. }
  1778. },
  1779. computed: {
  1780. filteredItems: function() {
  1781. var vm = this;
  1782. var items = vm.items.filter(function (item) {
  1783. return ((vm.mode === 'branches' && item.branch)
  1784. || (vm.mode === 'tags' && item.tag))
  1785. && (!vm.searchTerm
  1786. || item.name.toLowerCase().indexOf(vm.searchTerm.toLowerCase()) >= 0);
  1787. });
  1788. vm.active = (items.length === 0 && vm.showCreateNewBranch ? 0 : -1);
  1789. return items;
  1790. },
  1791. showNoResults: function() {
  1792. return this.filteredItems.length === 0
  1793. && !this.showCreateNewBranch;
  1794. },
  1795. showCreateNewBranch: function() {
  1796. var vm = this;
  1797. if (!this.canCreateBranch || !vm.searchTerm || vm.mode === 'tags') {
  1798. return false;
  1799. }
  1800. return vm.items.filter(function (item) {
  1801. return item.name.toLowerCase() === vm.searchTerm.toLowerCase()
  1802. }).length === 0;
  1803. }
  1804. },
  1805. methods: {
  1806. selectItem: function(item) {
  1807. var prev = this.getSelected();
  1808. if (prev !== null) {
  1809. prev.selected = false;
  1810. }
  1811. item.selected = true;
  1812. window.location.href = item.url;
  1813. },
  1814. createNewBranch: function() {
  1815. if (!this.showCreateNewBranch) {
  1816. return;
  1817. }
  1818. this.$refs.newBranchForm.submit();
  1819. },
  1820. focusSearchField: function() {
  1821. var vm = this;
  1822. Vue.nextTick(function() {
  1823. vm.$refs.searchField.focus();
  1824. });
  1825. },
  1826. getSelected: function() {
  1827. for (var i = 0, j = this.items.length; i < j; ++i) {
  1828. if (this.items[i].selected)
  1829. return this.items[i];
  1830. }
  1831. return null;
  1832. },
  1833. getSelectedIndexInFiltered: function() {
  1834. for (var i = 0, j = this.filteredItems.length; i < j; ++i) {
  1835. if (this.filteredItems[i].selected)
  1836. return i;
  1837. }
  1838. return -1;
  1839. },
  1840. scrollToActive: function() {
  1841. var el = this.$refs['listItem' + this.active];
  1842. if (!el || el.length === 0) {
  1843. return;
  1844. }
  1845. if (Array.isArray(el)) {
  1846. el = el[0];
  1847. }
  1848. var cont = this.$refs.scrollContainer;
  1849. if (el.offsetTop < cont.scrollTop) {
  1850. cont.scrollTop = el.offsetTop;
  1851. }
  1852. else if (el.offsetTop + el.clientHeight > cont.scrollTop + cont.clientHeight) {
  1853. cont.scrollTop = el.offsetTop + el.clientHeight - cont.clientHeight;
  1854. }
  1855. },
  1856. keydown: function(event) {
  1857. var vm = this;
  1858. if (event.keyCode === 40) {
  1859. // arrow down
  1860. event.preventDefault();
  1861. if (vm.active === -1) {
  1862. vm.active = vm.getSelectedIndexInFiltered();
  1863. }
  1864. if (vm.active + (vm.showCreateNewBranch ? 0 : 1) >= vm.filteredItems.length) {
  1865. return;
  1866. }
  1867. vm.active++;
  1868. vm.scrollToActive();
  1869. }
  1870. if (event.keyCode === 38) {
  1871. // arrow up
  1872. event.preventDefault();
  1873. if (vm.active === -1) {
  1874. vm.active = vm.getSelectedIndexInFiltered();
  1875. }
  1876. if (vm.active <= 0) {
  1877. return;
  1878. }
  1879. vm.active--;
  1880. vm.scrollToActive();
  1881. }
  1882. if (event.keyCode == 13) {
  1883. // enter
  1884. event.preventDefault();
  1885. if (vm.active >= vm.filteredItems.length) {
  1886. vm.createNewBranch();
  1887. } else if (vm.active >= 0) {
  1888. vm.selectItem(vm.filteredItems[vm.active]);
  1889. }
  1890. }
  1891. if (event.keyCode == 27) {
  1892. // escape
  1893. event.preventDefault();
  1894. vm.menuVisible = false;
  1895. }
  1896. }
  1897. }
  1898. });
  1899. });
  1900. }
  1901. $(".commit-button").click(function() {
  1902. $(this).parent().find('.commit-body').toggle();
  1903. });
  1904. function initNavbarContentToggle() {
  1905. var content = $('#navbar');
  1906. var toggle = $('#navbar-expand-toggle');
  1907. var isExpanded = false;
  1908. toggle.click(function() {
  1909. isExpanded = !isExpanded;
  1910. if (isExpanded) {
  1911. content.addClass('shown');
  1912. toggle.addClass('active');
  1913. }
  1914. else {
  1915. content.removeClass('shown');
  1916. toggle.removeClass('active');
  1917. }
  1918. });
  1919. }