您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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