Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.js 71KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  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. // Give new write/preview data-tab name to distinguish from others
  522. var $editContentForm = $editContentZone.find('.ui.comment.form');
  523. var $tabMenu = $editContentForm.find('.tabular.menu');
  524. $tabMenu.attr('data-write', $editContentZone.data('write'));
  525. $tabMenu.attr('data-preview', $editContentZone.data('preview'));
  526. $tabMenu.find('.write.item').attr('data-tab', $editContentZone.data('write'));
  527. $tabMenu.find('.preview.item').attr('data-tab', $editContentZone.data('preview'));
  528. $editContentForm.find('.write.segment').attr('data-tab', $editContentZone.data('write'));
  529. $editContentForm.find('.preview.segment').attr('data-tab', $editContentZone.data('preview'));
  530. initCommentPreviewTab($editContentForm);
  531. $editContentZone.find('.cancel.button').click(function () {
  532. $renderContent.show();
  533. $editContentZone.hide();
  534. });
  535. $editContentZone.find('.save.button').click(function () {
  536. $renderContent.show();
  537. $editContentZone.hide();
  538. $.post($editContentZone.data('update-url'), {
  539. "_csrf": csrf,
  540. "content": $textarea.val(),
  541. "context": $editContentZone.data('context')
  542. },
  543. function (data) {
  544. if (data.length == 0) {
  545. $renderContent.html($('#no-content').html());
  546. } else {
  547. $renderContent.html(data.content);
  548. emojify.run($renderContent[0]);
  549. $('pre code', $renderContent[0]).each(function (i, block) {
  550. hljs.highlightBlock(block);
  551. });
  552. }
  553. });
  554. });
  555. } else {
  556. $textarea = $segment.find('textarea');
  557. }
  558. // Show write/preview tab and copy raw content as needed
  559. $editContentZone.show();
  560. $renderContent.hide();
  561. if ($textarea.val().length == 0) {
  562. $textarea.val($rawContent.text());
  563. }
  564. $textarea.focus();
  565. return false;
  566. });
  567. // Delete comment
  568. $('.delete-comment').click(function () {
  569. var $this = $(this);
  570. if (confirm($this.data('locale'))) {
  571. $.post($this.data('url'), {
  572. "_csrf": csrf
  573. }).success(function () {
  574. $('#' + $this.data('comment-id')).remove();
  575. });
  576. }
  577. return false;
  578. });
  579. // Change status
  580. var $statusButton = $('#status-button');
  581. $('#comment-form .edit_area').keyup(function () {
  582. if ($(this).val().length == 0) {
  583. $statusButton.text($statusButton.data('status'))
  584. } else {
  585. $statusButton.text($statusButton.data('status-and-comment'))
  586. }
  587. });
  588. $statusButton.click(function () {
  589. $('#status').val($statusButton.data('status-val'));
  590. $('#comment-form').submit();
  591. });
  592. // Pull Request merge button
  593. var $mergeButton = $('.merge-button > button');
  594. $mergeButton.on('click', function(e) {
  595. e.preventDefault();
  596. $('.' + $(this).data('do') + '-fields').show();
  597. $(this).parent().hide();
  598. });
  599. $('.merge-button > .dropdown').dropdown({
  600. onChange: function (text, value, $choice) {
  601. if ($choice.data('do')) {
  602. $mergeButton.find('.button-text').text($choice.text());
  603. $mergeButton.data('do', $choice.data('do'));
  604. }
  605. }
  606. });
  607. $('.merge-cancel').on('click', function(e) {
  608. e.preventDefault();
  609. $(this).closest('.form').hide();
  610. $mergeButton.parent().show();
  611. });
  612. initReactionSelector();
  613. }
  614. // Diff
  615. if ($('.repository.diff').length > 0) {
  616. var $counter = $('.diff-counter');
  617. if ($counter.length >= 1) {
  618. $counter.each(function (i, item) {
  619. var $item = $(item);
  620. var addLine = $item.find('span[data-line].add').data("line");
  621. var delLine = $item.find('span[data-line].del').data("line");
  622. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  623. $item.find(".bar .add").css("width", addPercent + "%");
  624. });
  625. }
  626. }
  627. // Quick start and repository home
  628. $('#repo-clone-ssh').click(function () {
  629. $('.clone-url').text($(this).data('link'));
  630. $('#repo-clone-url').val($(this).data('link'));
  631. $(this).addClass('blue');
  632. $('#repo-clone-https').removeClass('blue');
  633. localStorage.setItem('repo-clone-protocol', 'ssh');
  634. });
  635. $('#repo-clone-https').click(function () {
  636. $('.clone-url').text($(this).data('link'));
  637. $('#repo-clone-url').val($(this).data('link'));
  638. $(this).addClass('blue');
  639. $('#repo-clone-ssh').removeClass('blue');
  640. localStorage.setItem('repo-clone-protocol', 'https');
  641. });
  642. $('#repo-clone-url').click(function () {
  643. $(this).select();
  644. });
  645. // Pull request
  646. if ($('.repository.compare.pull').length > 0) {
  647. initFilterSearchDropdown('.choose.branch .dropdown');
  648. }
  649. // Branches
  650. if ($('.repository.settings.branches').length > 0) {
  651. initFilterSearchDropdown('.protected-branches .dropdown');
  652. $('.enable-protection, .enable-whitelist').change(function () {
  653. if (this.checked) {
  654. $($(this).data('target')).removeClass('disabled');
  655. } else {
  656. $($(this).data('target')).addClass('disabled');
  657. }
  658. });
  659. }
  660. }
  661. function initRepositoryCollaboration() {
  662. console.log('initRepositoryCollaboration');
  663. // Change collaborator access mode
  664. $('.access-mode.menu .item').click(function () {
  665. var $menu = $(this).parent();
  666. $.post($menu.data('url'), {
  667. "_csrf": csrf,
  668. "uid": $menu.data('uid'),
  669. "mode": $(this).data('value')
  670. })
  671. });
  672. }
  673. function initTeamSettings() {
  674. // Change team access mode
  675. $('.organization.new.team input[name=permission]').change(function () {
  676. var val = $('input[name=permission]:checked', '.organization.new.team').val()
  677. if (val === 'admin') {
  678. $('.organization.new.team .team-units').hide();
  679. } else {
  680. $('.organization.new.team .team-units').show();
  681. }
  682. });
  683. }
  684. function initWikiForm() {
  685. var $editArea = $('.repository.wiki textarea#edit_area');
  686. if ($editArea.length > 0) {
  687. new SimpleMDE({
  688. autoDownloadFontAwesome: false,
  689. element: $editArea[0],
  690. forceSync: true,
  691. previewRender: function (plainText, preview) { // Async method
  692. setTimeout(function () {
  693. // FIXME: still send render request when return back to edit mode
  694. $.post($editArea.data('url'), {
  695. "_csrf": csrf,
  696. "mode": "gfm",
  697. "context": $editArea.data('context'),
  698. "text": plainText
  699. },
  700. function (data) {
  701. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  702. emojify.run($('.editor-preview')[0]);
  703. $('.editor-preview').autolink();
  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. $('.markdown').autolink();
  1408. $('.issue-checkbox').click(function() {
  1409. var numChecked = $('.issue-checkbox').children('input:checked').length;
  1410. if (numChecked > 0) {
  1411. $('#issue-filters').hide();
  1412. $('#issue-actions').show();
  1413. } else {
  1414. $('#issue-filters').show();
  1415. $('#issue-actions').hide();
  1416. }
  1417. });
  1418. $('.issue-action').click(function () {
  1419. var action = this.dataset.action
  1420. var elementId = this.dataset.elementId
  1421. var issueIDs = $('.issue-checkbox').children('input:checked').map(function() {
  1422. return this.dataset.issueId;
  1423. }).get().join();
  1424. var url = this.dataset.url
  1425. updateIssuesMeta(url, action, issueIDs, elementId, function() {
  1426. location.reload();
  1427. });
  1428. });
  1429. buttonsClickOnEnter();
  1430. searchUsers();
  1431. searchRepositories();
  1432. initCommentForm();
  1433. initInstall();
  1434. initRepository();
  1435. initWikiForm();
  1436. initEditForm();
  1437. initEditor();
  1438. initOrganization();
  1439. initWebhook();
  1440. initAdmin();
  1441. initCodeView();
  1442. initVueApp();
  1443. initTeamSettings();
  1444. initCtrlEnterSubmit();
  1445. initNavbarContentToggle();
  1446. // Repo clone url.
  1447. if ($('#repo-clone-url').length > 0) {
  1448. switch (localStorage.getItem('repo-clone-protocol')) {
  1449. case 'ssh':
  1450. if ($('#repo-clone-ssh').click().length === 0) {
  1451. $('#repo-clone-https').click();
  1452. }
  1453. break;
  1454. default:
  1455. $('#repo-clone-https').click();
  1456. break;
  1457. }
  1458. }
  1459. var routes = {
  1460. 'div.user.settings': initUserSettings,
  1461. 'div.repository.settings.collaboration': initRepositoryCollaboration
  1462. };
  1463. var selector;
  1464. for (selector in routes) {
  1465. if ($(selector).length > 0) {
  1466. routes[selector]();
  1467. break;
  1468. }
  1469. }
  1470. });
  1471. function changeHash(hash) {
  1472. if (history.pushState) {
  1473. history.pushState(null, null, hash);
  1474. }
  1475. else {
  1476. location.hash = hash;
  1477. }
  1478. }
  1479. function deSelect() {
  1480. if (window.getSelection) {
  1481. window.getSelection().removeAllRanges();
  1482. } else {
  1483. document.selection.empty();
  1484. }
  1485. }
  1486. function selectRange($list, $select, $from) {
  1487. $list.removeClass('active');
  1488. if ($from) {
  1489. var a = parseInt($select.attr('rel').substr(1));
  1490. var b = parseInt($from.attr('rel').substr(1));
  1491. var c;
  1492. if (a != b) {
  1493. if (a > b) {
  1494. c = a;
  1495. a = b;
  1496. b = c;
  1497. }
  1498. var classes = [];
  1499. for (var i = a; i <= b; i++) {
  1500. classes.push('.L' + i);
  1501. }
  1502. $list.filter(classes.join(',')).addClass('active');
  1503. changeHash('#L' + a + '-' + 'L' + b);
  1504. return
  1505. }
  1506. }
  1507. $select.addClass('active');
  1508. changeHash('#' + $select.attr('rel'));
  1509. }
  1510. $(function () {
  1511. if ($('.user.signin').length > 0) return;
  1512. $('form').areYouSure();
  1513. // Parse SSH Key
  1514. $("#ssh-key-content").on('change paste keyup',function(){
  1515. var arrays = $(this).val().split(" ");
  1516. var $title = $("#ssh-key-title")
  1517. if ($title.val() === "" && arrays.length === 3 && arrays[2] !== "") {
  1518. $title.val(arrays[2]);
  1519. }
  1520. });
  1521. });
  1522. function showDeletePopup() {
  1523. var $this = $(this);
  1524. var filter = "";
  1525. if ($this.attr("id")) {
  1526. filter += "#" + $this.attr("id")
  1527. }
  1528. $('.delete.modal' + filter).modal({
  1529. closable: false,
  1530. onApprove: function() {
  1531. if ($this.data('type') == "form") {
  1532. $($this.data('form')).submit();
  1533. return;
  1534. }
  1535. $.post($this.data('url'), {
  1536. "_csrf": csrf,
  1537. "id": $this.data("id")
  1538. }).done(function(data) {
  1539. window.location.href = data.redirect;
  1540. });
  1541. }
  1542. }).modal('show');
  1543. return false;
  1544. }
  1545. function initVueComponents(){
  1546. var vueDelimeters = ['${', '}'];
  1547. Vue.component('repo-search', {
  1548. delimiters: vueDelimeters,
  1549. props: {
  1550. searchLimit: {
  1551. type: Number,
  1552. default: 10
  1553. },
  1554. suburl: {
  1555. type: String,
  1556. required: true
  1557. },
  1558. uid: {
  1559. type: Number,
  1560. required: true
  1561. },
  1562. organizations: {
  1563. type: Array,
  1564. default: []
  1565. },
  1566. isOrganization: {
  1567. type: Boolean,
  1568. default: true
  1569. },
  1570. canCreateOrganization: {
  1571. type: Boolean,
  1572. default: false
  1573. },
  1574. organizationsTotalCount: {
  1575. type: Number,
  1576. default: 0
  1577. },
  1578. moreReposLink: {
  1579. type: String,
  1580. default: ''
  1581. }
  1582. },
  1583. data: function() {
  1584. return {
  1585. tab: 'repos',
  1586. repos: [],
  1587. reposTotalCount: 0,
  1588. reposFilter: 'all',
  1589. searchQuery: '',
  1590. isLoading: false,
  1591. repoTypes: {
  1592. 'all': {
  1593. count: 0,
  1594. searchMode: '',
  1595. },
  1596. 'forks': {
  1597. count: 0,
  1598. searchMode: 'fork',
  1599. },
  1600. 'mirrors': {
  1601. count: 0,
  1602. searchMode: 'mirror',
  1603. },
  1604. 'sources': {
  1605. count: 0,
  1606. searchMode: 'source',
  1607. },
  1608. 'collaborative': {
  1609. count: 0,
  1610. searchMode: 'collaborative',
  1611. },
  1612. }
  1613. }
  1614. },
  1615. computed: {
  1616. showMoreReposLink: function() {
  1617. return this.repos.length > 0 && this.repos.length < this.repoTypes[this.reposFilter].count;
  1618. },
  1619. searchURL: function() {
  1620. 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' : '');
  1621. },
  1622. repoTypeCount: function() {
  1623. return this.repoTypes[this.reposFilter].count;
  1624. }
  1625. },
  1626. mounted: function() {
  1627. this.searchRepos(this.reposFilter);
  1628. var self = this;
  1629. Vue.nextTick(function() {
  1630. self.$refs.search.focus();
  1631. });
  1632. },
  1633. methods: {
  1634. changeTab: function(t) {
  1635. this.tab = t;
  1636. },
  1637. changeReposFilter: function(filter) {
  1638. this.reposFilter = filter;
  1639. this.repos = [];
  1640. this.repoTypes[filter].count = 0;
  1641. this.searchRepos(filter);
  1642. },
  1643. showRepo: function(repo, filter) {
  1644. switch (filter) {
  1645. case 'sources':
  1646. return repo.owner.id == this.uid && !repo.mirror && !repo.fork;
  1647. case 'forks':
  1648. return repo.owner.id == this.uid && !repo.mirror && repo.fork;
  1649. case 'mirrors':
  1650. return repo.mirror;
  1651. case 'collaborative':
  1652. return repo.owner.id != this.uid && !repo.mirror;
  1653. default:
  1654. return true;
  1655. }
  1656. },
  1657. searchRepos: function(reposFilter) {
  1658. var self = this;
  1659. this.isLoading = true;
  1660. var searchedMode = this.repoTypes[reposFilter].searchMode;
  1661. var searchedURL = this.searchURL;
  1662. var searchedQuery = this.searchQuery;
  1663. $.getJSON(searchedURL, function(result, textStatus, request) {
  1664. if (searchedURL == self.searchURL) {
  1665. self.repos = result.data;
  1666. var count = request.getResponseHeader('X-Total-Count');
  1667. if (searchedQuery === '' && searchedMode === '') {
  1668. self.reposTotalCount = count;
  1669. }
  1670. self.repoTypes[reposFilter].count = count;
  1671. }
  1672. }).always(function() {
  1673. if (searchedURL == self.searchURL) {
  1674. self.isLoading = false;
  1675. }
  1676. });
  1677. },
  1678. repoClass: function(repo) {
  1679. if (repo.fork) {
  1680. return 'octicon octicon-repo-forked';
  1681. } else if (repo.mirror) {
  1682. return 'octicon octicon-repo-clone';
  1683. } else if (repo.private) {
  1684. return 'octicon octicon-lock';
  1685. } else {
  1686. return 'octicon octicon-repo';
  1687. }
  1688. }
  1689. }
  1690. })
  1691. }
  1692. function initCtrlEnterSubmit() {
  1693. $(".js-quick-submit").keydown(function(e) {
  1694. if (((e.ctrlKey && !e.altKey) || e.metaKey) && (e.keyCode == 13 || e.keyCode == 10)) {
  1695. $(this).closest("form").submit();
  1696. }
  1697. });
  1698. }
  1699. function initVueApp() {
  1700. var el = document.getElementById('app');
  1701. if (!el) {
  1702. return;
  1703. }
  1704. initVueComponents();
  1705. new Vue({
  1706. delimiters: ['${', '}'],
  1707. el: el,
  1708. data: {
  1709. searchLimit: document.querySelector('meta[name=_search_limit]').content,
  1710. suburl: document.querySelector('meta[name=_suburl]').content,
  1711. uid: document.querySelector('meta[name=_context_uid]').content,
  1712. },
  1713. });
  1714. }
  1715. function timeAddManual() {
  1716. $('.mini.modal')
  1717. .modal({
  1718. duration: 200,
  1719. onApprove: function() {
  1720. $('#add_time_manual_form').submit();
  1721. }
  1722. }).modal('show')
  1723. ;
  1724. }
  1725. function toggleStopwatch() {
  1726. $("#toggle_stopwatch_form").submit();
  1727. }
  1728. function cancelStopwatch() {
  1729. $("#cancel_stopwatch_form").submit();
  1730. }
  1731. function initFilterBranchTagDropdown(selector) {
  1732. $(selector).each(function() {
  1733. var $dropdown = $(this);
  1734. var $data = $dropdown.find('.data');
  1735. var data = {
  1736. items: [],
  1737. mode: $data.data('mode'),
  1738. searchTerm: '',
  1739. noResults: '',
  1740. canCreateBranch: false,
  1741. menuVisible: false,
  1742. active: 0
  1743. };
  1744. $data.find('.item').each(function() {
  1745. data.items.push({
  1746. name: $(this).text(),
  1747. url: $(this).data('url'),
  1748. branch: $(this).hasClass('branch'),
  1749. tag: $(this).hasClass('tag'),
  1750. selected: $(this).hasClass('selected')
  1751. });
  1752. });
  1753. $data.remove();
  1754. new Vue({
  1755. delimiters: ['${', '}'],
  1756. el: this,
  1757. data: data,
  1758. beforeMount: function () {
  1759. var vm = this;
  1760. this.noResults = vm.$el.getAttribute('data-no-results');
  1761. this.canCreateBranch = vm.$el.getAttribute('data-can-create-branch') === 'true';
  1762. document.body.addEventListener('click', function(event) {
  1763. if (vm.$el.contains(event.target)) {
  1764. return;
  1765. }
  1766. if (vm.menuVisible) {
  1767. Vue.set(vm, 'menuVisible', false);
  1768. }
  1769. });
  1770. },
  1771. watch: {
  1772. menuVisible: function(visible) {
  1773. if (visible) {
  1774. this.focusSearchField();
  1775. }
  1776. }
  1777. },
  1778. computed: {
  1779. filteredItems: function() {
  1780. var vm = this;
  1781. var items = vm.items.filter(function (item) {
  1782. return ((vm.mode === 'branches' && item.branch)
  1783. || (vm.mode === 'tags' && item.tag))
  1784. && (!vm.searchTerm
  1785. || item.name.toLowerCase().indexOf(vm.searchTerm.toLowerCase()) >= 0);
  1786. });
  1787. vm.active = (items.length === 0 && vm.showCreateNewBranch ? 0 : -1);
  1788. return items;
  1789. },
  1790. showNoResults: function() {
  1791. return this.filteredItems.length === 0
  1792. && !this.showCreateNewBranch;
  1793. },
  1794. showCreateNewBranch: function() {
  1795. var vm = this;
  1796. if (!this.canCreateBranch || !vm.searchTerm || vm.mode === 'tags') {
  1797. return false;
  1798. }
  1799. return vm.items.filter(function (item) {
  1800. return item.name.toLowerCase() === vm.searchTerm.toLowerCase()
  1801. }).length === 0;
  1802. }
  1803. },
  1804. methods: {
  1805. selectItem: function(item) {
  1806. var prev = this.getSelected();
  1807. if (prev !== null) {
  1808. prev.selected = false;
  1809. }
  1810. item.selected = true;
  1811. window.location.href = item.url;
  1812. },
  1813. createNewBranch: function() {
  1814. if (!this.showCreateNewBranch) {
  1815. return;
  1816. }
  1817. this.$refs.newBranchForm.submit();
  1818. },
  1819. focusSearchField: function() {
  1820. var vm = this;
  1821. Vue.nextTick(function() {
  1822. vm.$refs.searchField.focus();
  1823. });
  1824. },
  1825. getSelected: function() {
  1826. for (var i = 0, j = this.items.length; i < j; ++i) {
  1827. if (this.items[i].selected)
  1828. return this.items[i];
  1829. }
  1830. return null;
  1831. },
  1832. getSelectedIndexInFiltered: function() {
  1833. for (var i = 0, j = this.filteredItems.length; i < j; ++i) {
  1834. if (this.filteredItems[i].selected)
  1835. return i;
  1836. }
  1837. return -1;
  1838. },
  1839. scrollToActive: function() {
  1840. var el = this.$refs['listItem' + this.active];
  1841. if (!el || el.length === 0) {
  1842. return;
  1843. }
  1844. if (Array.isArray(el)) {
  1845. el = el[0];
  1846. }
  1847. var cont = this.$refs.scrollContainer;
  1848. if (el.offsetTop < cont.scrollTop) {
  1849. cont.scrollTop = el.offsetTop;
  1850. }
  1851. else if (el.offsetTop + el.clientHeight > cont.scrollTop + cont.clientHeight) {
  1852. cont.scrollTop = el.offsetTop + el.clientHeight - cont.clientHeight;
  1853. }
  1854. },
  1855. keydown: function(event) {
  1856. var vm = this;
  1857. if (event.keyCode === 40) {
  1858. // arrow down
  1859. event.preventDefault();
  1860. if (vm.active === -1) {
  1861. vm.active = vm.getSelectedIndexInFiltered();
  1862. }
  1863. if (vm.active + (vm.showCreateNewBranch ? 0 : 1) >= vm.filteredItems.length) {
  1864. return;
  1865. }
  1866. vm.active++;
  1867. vm.scrollToActive();
  1868. }
  1869. if (event.keyCode === 38) {
  1870. // arrow up
  1871. event.preventDefault();
  1872. if (vm.active === -1) {
  1873. vm.active = vm.getSelectedIndexInFiltered();
  1874. }
  1875. if (vm.active <= 0) {
  1876. return;
  1877. }
  1878. vm.active--;
  1879. vm.scrollToActive();
  1880. }
  1881. if (event.keyCode == 13) {
  1882. // enter
  1883. event.preventDefault();
  1884. if (vm.active >= vm.filteredItems.length) {
  1885. vm.createNewBranch();
  1886. } else if (vm.active >= 0) {
  1887. vm.selectItem(vm.filteredItems[vm.active]);
  1888. }
  1889. }
  1890. if (event.keyCode == 27) {
  1891. // escape
  1892. event.preventDefault();
  1893. vm.menuVisible = false;
  1894. }
  1895. }
  1896. }
  1897. });
  1898. });
  1899. }
  1900. $(".commit-button").click(function() {
  1901. $(this).parent().find('.commit-body').toggle();
  1902. });
  1903. function initNavbarContentToggle() {
  1904. var content = $('#navbar');
  1905. var toggle = $('#navbar-expand-toggle');
  1906. var isExpanded = false;
  1907. toggle.click(function() {
  1908. isExpanded = !isExpanded;
  1909. if (isExpanded) {
  1910. content.addClass('shown');
  1911. toggle.addClass('active');
  1912. }
  1913. else {
  1914. content.removeClass('shown');
  1915. toggle.removeClass('active');
  1916. }
  1917. });
  1918. }