gogs.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. 'use strict';
  2. var csrf;
  3. var suburl;
  4. function initCommentPreviewTab($form) {
  5. var $tab_menu = $form.find('.tabular.menu');
  6. $tab_menu.find('.item').tab();
  7. $tab_menu.find('.item[data-tab="' + $tab_menu.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="' + $tab_menu.data('write') + '"] textarea').val()
  14. },
  15. function (data) {
  16. var $preview_tab = $form.find('.tab.segment[data-tab="' + $tab_menu.data('preview') + '"]');
  17. $preview_tab.html(data);
  18. emojify.run($preview_tab[0]);
  19. $('pre code', $preview_tab[0]).each(function (i, block) {
  20. hljs.highlightBlock(block);
  21. });
  22. }
  23. );
  24. });
  25. buttonsClickOnEnter();
  26. }
  27. function initCommentForm() {
  28. if ($('.comment.form').length == 0) {
  29. return
  30. }
  31. initCommentPreviewTab($('.comment.form'));
  32. // Labels
  33. var $list = $('.ui.labels.list');
  34. var $no_select = $list.find('.no-select');
  35. var $label_menu = $('.select-label .menu');
  36. var has_label_update_action = $label_menu.data('action') == 'update';
  37. function updateIssueMeta(url, action, id) {
  38. $.post(url, {
  39. "_csrf": csrf,
  40. "action": action,
  41. "id": id
  42. });
  43. }
  44. $label_menu.find('.item:not(.no-select)').click(function () {
  45. if ($(this).hasClass('checked')) {
  46. $(this).removeClass('checked');
  47. $(this).find('.octicon').removeClass('octicon-check');
  48. if (has_label_update_action) {
  49. updateIssueMeta($label_menu.data('update-url'), "detach", $(this).data('id'));
  50. }
  51. } else {
  52. $(this).addClass('checked');
  53. $(this).find('.octicon').addClass('octicon-check');
  54. if (has_label_update_action) {
  55. updateIssueMeta($label_menu.data('update-url'), "attach", $(this).data('id'));
  56. }
  57. }
  58. var label_ids = "";
  59. $(this).parent().find('.item').each(function () {
  60. if ($(this).hasClass('checked')) {
  61. label_ids += $(this).data('id') + ",";
  62. $($(this).data('id-selector')).removeClass('hide');
  63. } else {
  64. $($(this).data('id-selector')).addClass('hide');
  65. }
  66. });
  67. if (label_ids.length == 0) {
  68. $no_select.removeClass('hide');
  69. } else {
  70. $no_select.addClass('hide');
  71. }
  72. $($(this).parent().data('id')).val(label_ids);
  73. return false;
  74. });
  75. $label_menu.find('.no-select.item').click(function () {
  76. if (has_label_update_action) {
  77. updateIssueMeta($label_menu.data('update-url'), "clear", '');
  78. }
  79. $(this).parent().find('.item').each(function () {
  80. $(this).removeClass('checked');
  81. $(this).find('.octicon').removeClass('octicon-check');
  82. });
  83. $list.find('.item').each(function () {
  84. $(this).addClass('hide');
  85. });
  86. $no_select.removeClass('hide');
  87. $($(this).parent().data('id')).val('');
  88. });
  89. function selectItem(select_id, input_id) {
  90. var $menu = $(select_id + ' .menu');
  91. var $list = $('.ui' + select_id + '.list');
  92. var has_update_action = $menu.data('action') == 'update';
  93. $menu.find('.item:not(.no-select)').click(function () {
  94. $(this).parent().find('.item').each(function () {
  95. $(this).removeClass('selected active')
  96. });
  97. $(this).addClass('selected active');
  98. if (has_update_action) {
  99. updateIssueMeta($menu.data('update-url'), '', $(this).data('id'));
  100. }
  101. switch (input_id) {
  102. case '#milestone_id':
  103. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  104. $(this).text() + '</a>');
  105. break;
  106. case '#assignee_id':
  107. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  108. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  109. $(this).text() + '</a>');
  110. }
  111. $('.ui' + select_id + '.list .no-select').addClass('hide');
  112. $(input_id).val($(this).data('id'));
  113. });
  114. $menu.find('.no-select.item').click(function () {
  115. $(this).parent().find('.item:not(.no-select)').each(function () {
  116. $(this).removeClass('selected active')
  117. });
  118. if (has_update_action) {
  119. updateIssueMeta($menu.data('update-url'), '', '');
  120. }
  121. $list.find('.selected').html('');
  122. $list.find('.no-select').removeClass('hide');
  123. $(input_id).val('');
  124. });
  125. }
  126. // Milestone and assignee
  127. selectItem('.select-milestone', '#milestone_id');
  128. selectItem('.select-assignee', '#assignee_id');
  129. }
  130. function initInstall() {
  131. if ($('.install').length == 0) {
  132. return;
  133. }
  134. // Database type change detection.
  135. $("#db_type").change(function () {
  136. var sqlite_default = 'data/gogs.db';
  137. var tidb_default = 'data/gogs_tidb';
  138. var db_type = $(this).val();
  139. if (db_type === "SQLite3" || db_type === "TiDB") {
  140. $('#sql_settings').hide();
  141. $('#pgsql_settings').hide();
  142. $('#sqlite_settings').show();
  143. if (db_type === "SQLite3" && $('#db_path').val() == tidb_default) {
  144. $('#db_path').val(sqlite_default);
  145. } else if (db_type === "TiDB" && $('#db_path').val() == sqlite_default) {
  146. $('#db_path').val(tidb_default);
  147. }
  148. return;
  149. }
  150. var mysql_default = '127.0.0.1:3306';
  151. var postgres_default = '127.0.0.1:5432';
  152. $('#sqlite_settings').hide();
  153. $('#sql_settings').show();
  154. if (db_type === "PostgreSQL") {
  155. $('#pgsql_settings').show();
  156. if ($('#db_host').val() == mysql_default) {
  157. $('#db_host').val(postgres_default);
  158. }
  159. } else {
  160. $('#pgsql_settings').hide();
  161. if ($('#db_host').val() == postgres_default) {
  162. $('#db_host').val(mysql_default);
  163. }
  164. }
  165. });
  166. $('#offline-mode input').change(function () {
  167. if ($(this).is(':checked')) {
  168. $('#disable-gravatar').checkbox('check');
  169. }
  170. });
  171. $('#disable-registration input').change(function () {
  172. if ($(this).is(':checked')) {
  173. $('#enable-captcha').checkbox('uncheck');
  174. }
  175. });
  176. $('#enable-captcha input').change(function () {
  177. if ($(this).is(':checked')) {
  178. $('#disable-registration').checkbox('uncheck');
  179. }
  180. });
  181. }
  182. function initRepository() {
  183. if ($('.repository').length == 0) {
  184. return;
  185. }
  186. function initFilterSearchDropdown(selector) {
  187. var $dropdown = $(selector);
  188. $dropdown.dropdown({
  189. fullTextSearch: true,
  190. onChange: function (text, value, $choice) {
  191. window.location.href = $choice.data('url');
  192. console.log($choice.data('url'))
  193. },
  194. message: {noResults: $dropdown.data('no-results')}
  195. });
  196. }
  197. // File list and commits
  198. if ($('.repository.file.list').length > 0 ||
  199. ('.repository.commits').length > 0) {
  200. initFilterSearchDropdown('.choose.reference .dropdown');
  201. $('.reference.column').click(function () {
  202. $('.choose.reference .scrolling.menu').css('display', 'none');
  203. $('.choose.reference .text').removeClass('black');
  204. $($(this).data('target')).css('display', 'block');
  205. $(this).find('.text').addClass('black');
  206. return false;
  207. });
  208. }
  209. // Wiki
  210. if ($('.repository.wiki.view').length > 0) {
  211. initFilterSearchDropdown('.choose.page .dropdown');
  212. }
  213. // Options
  214. if ($('.repository.settings.options').length > 0) {
  215. $('#repo_name').keyup(function () {
  216. var $prompt_span = $('#repo-name-change-prompt');
  217. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  218. $prompt_span.show();
  219. } else {
  220. $prompt_span.hide();
  221. }
  222. });
  223. // Enable or select internal/external wiki system and issue tracker.
  224. $('.enable-system').change(function () {
  225. if (this.checked) {
  226. $($(this).data('target')).removeClass('disabled');
  227. } else {
  228. $($(this).data('target')).addClass('disabled');
  229. }
  230. });
  231. $('.enable-system-radio').change(function () {
  232. if (this.value == 'false') {
  233. $($(this).data('target')).addClass('disabled');
  234. } else if (this.value == 'true') {
  235. $($(this).data('target')).removeClass('disabled');
  236. }
  237. });
  238. }
  239. // Labels
  240. if ($('.repository.labels').length > 0) {
  241. // Create label
  242. var $new_label_panel = $('.new-label.segment');
  243. $('.new-label.button').click(function () {
  244. $new_label_panel.show();
  245. });
  246. $('.new-label.segment .cancel').click(function () {
  247. $new_label_panel.hide();
  248. });
  249. $('.color-picker').each(function () {
  250. $(this).minicolors();
  251. });
  252. $('.precolors .color').click(function () {
  253. var color_hex = $(this).data('color-hex');
  254. $('.color-picker').val(color_hex);
  255. $('.minicolors-swatch-color').css("background-color", color_hex);
  256. });
  257. $('.edit-label-button').click(function () {
  258. $('#label-modal-id').val($(this).data('id'));
  259. $('.edit-label .new-label-input').val($(this).data('title'));
  260. $('.edit-label .color-picker').val($(this).data('color'));
  261. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  262. $('.edit-label.modal').modal({
  263. onApprove: function () {
  264. $('.edit-label.form').submit();
  265. }
  266. }).modal('show');
  267. return false;
  268. });
  269. }
  270. // Milestones
  271. if ($('.repository.milestones').length > 0) {
  272. }
  273. if ($('.repository.new.milestone').length > 0) {
  274. var $datepicker = $('.milestone.datepicker');
  275. $datepicker.datetimepicker({
  276. lang: $datepicker.data('lang'),
  277. inline: true,
  278. timepicker: false,
  279. startDate: $datepicker.data('start-date'),
  280. formatDate: 'Y-m-d',
  281. onSelectDate: function (ct) {
  282. $('#deadline').val(ct.dateFormat('Y-m-d'));
  283. }
  284. });
  285. $('#clear-date').click(function () {
  286. $('#deadline').val('');
  287. return false;
  288. });
  289. }
  290. // Issues
  291. if ($('.repository.view.issue').length > 0) {
  292. // Edit issue title
  293. var $issue_title = $('#issue-title');
  294. var $edit_input = $('#edit-title-input input');
  295. var editTitleToggle = function () {
  296. $issue_title.toggle();
  297. $('.not-in-edit').toggle();
  298. $('#edit-title-input').toggle();
  299. $('.in-edit').toggle();
  300. $edit_input.focus();
  301. return false;
  302. };
  303. $('#edit-title').click(editTitleToggle);
  304. $('#cancel-edit-title').click(editTitleToggle);
  305. $('#save-edit-title').click(editTitleToggle).click(function () {
  306. if ($edit_input.val().length == 0 ||
  307. $edit_input.val() == $issue_title.text()) {
  308. $edit_input.val($issue_title.text());
  309. return false;
  310. }
  311. $.post($(this).data('update-url'), {
  312. "_csrf": csrf,
  313. "title": $edit_input.val()
  314. },
  315. function (data) {
  316. $edit_input.val(data.title);
  317. $issue_title.text(data.title);
  318. });
  319. return false;
  320. });
  321. // Edit issue or comment content
  322. $('.edit-content').click(function () {
  323. var $segment = $(this).parent().parent().parent().next();
  324. var $edit_content_zone = $segment.find('.edit-content-zone');
  325. var $render_content = $segment.find('.render-content');
  326. var $raw_content = $segment.find('.raw-content');
  327. var $textarea;
  328. // Setup new form
  329. if ($edit_content_zone.html().length == 0) {
  330. $edit_content_zone.html($('#edit-content-form').html());
  331. $textarea = $segment.find('textarea');
  332. // Give new write/preview data-tab name to distinguish from others
  333. var $edit_content_form = $edit_content_zone.find('.ui.comment.form');
  334. var $tabular_menu = $edit_content_form.find('.tabular.menu');
  335. $tabular_menu.attr('data-write', $edit_content_zone.data('write'));
  336. $tabular_menu.attr('data-preview', $edit_content_zone.data('preview'));
  337. $tabular_menu.find('.write.item').attr('data-tab', $edit_content_zone.data('write'));
  338. $tabular_menu.find('.preview.item').attr('data-tab', $edit_content_zone.data('preview'));
  339. $edit_content_form.find('.write.segment').attr('data-tab', $edit_content_zone.data('write'));
  340. $edit_content_form.find('.preview.segment').attr('data-tab', $edit_content_zone.data('preview'));
  341. initCommentPreviewTab($edit_content_form);
  342. $edit_content_zone.find('.cancel.button').click(function () {
  343. $render_content.show();
  344. $edit_content_zone.hide();
  345. });
  346. $edit_content_zone.find('.save.button').click(function () {
  347. $render_content.show();
  348. $edit_content_zone.hide();
  349. $.post($edit_content_zone.data('update-url'), {
  350. "_csrf": csrf,
  351. "content": $textarea.val(),
  352. "context": $edit_content_zone.data('context')
  353. },
  354. function (data) {
  355. if (data.length == 0) {
  356. $render_content.html($('#no-content').html());
  357. } else {
  358. $render_content.html(data.content);
  359. emojify.run($render_content[0]);
  360. $('pre code', $render_content[0]).each(function (i, block) {
  361. hljs.highlightBlock(block);
  362. });
  363. }
  364. });
  365. });
  366. } else {
  367. $textarea = $segment.find('textarea');
  368. }
  369. // Show write/preview tab and copy raw content as needed
  370. $edit_content_zone.show();
  371. $render_content.hide();
  372. if ($textarea.val().length == 0) {
  373. $textarea.val($raw_content.text());
  374. }
  375. $textarea.focus();
  376. return false;
  377. });
  378. // Delete comment
  379. $('.delete-comment').click(function () {
  380. var $this = $(this);
  381. if (confirm($this.data('locale'))) {
  382. $.post($this.data('url'), {
  383. "_csrf": csrf
  384. }).success(function () {
  385. $('#' + $this.data('comment-id')).remove();
  386. });
  387. }
  388. return false;
  389. });
  390. // Change status
  391. var $status_btn = $('#status-button');
  392. $('#content').keyup(function () {
  393. if ($(this).val().length == 0) {
  394. $status_btn.text($status_btn.data('status'))
  395. } else {
  396. $status_btn.text($status_btn.data('status-and-comment'))
  397. }
  398. });
  399. $status_btn.click(function () {
  400. $('#status').val($status_btn.data('status-val'));
  401. $('#comment-form').submit();
  402. });
  403. }
  404. // Diff
  405. if ($('.repository.diff').length > 0) {
  406. var $counter = $('.diff-counter');
  407. if ($counter.length >= 1) {
  408. $counter.each(function (i, item) {
  409. var $item = $(item);
  410. var addLine = $item.find('span[data-line].add').data("line");
  411. var delLine = $item.find('span[data-line].del').data("line");
  412. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  413. $item.find(".bar .add").css("width", addPercent + "%");
  414. });
  415. }
  416. }
  417. // Quick start and repository home
  418. $('#repo-clone-ssh').click(function () {
  419. $('.clone-url').text($(this).data('link'));
  420. $('#repo-clone-url').val($(this).data('link'));
  421. $(this).addClass('blue');
  422. $('#repo-clone-https').removeClass('blue');
  423. localStorage.setItem('repo-clone-protocol', 'ssh');
  424. });
  425. $('#repo-clone-https').click(function () {
  426. $('.clone-url').text($(this).data('link'));
  427. $('#repo-clone-url').val($(this).data('link'));
  428. $(this).addClass('blue');
  429. $('#repo-clone-ssh').removeClass('blue');
  430. localStorage.setItem('repo-clone-protocol', 'https');
  431. });
  432. $('#repo-clone-url').click(function () {
  433. $(this).select();
  434. });
  435. // Pull request
  436. if ($('.repository.compare.pull').length > 0) {
  437. initFilterSearchDropdown('.choose.branch .dropdown');
  438. }
  439. }
  440. function initRepositoryCollaboration() {
  441. console.log('initRepositoryCollaboration');
  442. // Change collaborator access mode
  443. $('.access-mode.menu .item').click(function () {
  444. var $menu = $(this).parent();
  445. $.post($menu.data('url'), {
  446. "_csrf": csrf,
  447. "uid": $menu.data('uid'),
  448. "mode": $(this).data('value')
  449. })
  450. });
  451. }
  452. function initWiki() {
  453. if ($('.repository.wiki').length == 0) {
  454. return;
  455. }
  456. if ($('.repository.wiki.new').length > 0) {
  457. var $edit_area = $('#edit-area');
  458. var simplemde = new SimpleMDE({
  459. autoDownloadFontAwesome: false,
  460. element: $edit_area[0],
  461. forceSync: true,
  462. previewRender: function (plainText, preview) { // Async method
  463. setTimeout(function () {
  464. // FIXME: still send render request when return back to edit mode
  465. $.post($edit_area.data('url'), {
  466. "_csrf": csrf,
  467. "mode": "gfm",
  468. "context": $edit_area.data('context'),
  469. "text": plainText
  470. },
  471. function (data) {
  472. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  473. emojify.run($('.editor-preview')[0]);
  474. }
  475. );
  476. }, 0);
  477. return "Loading...";
  478. },
  479. renderingConfig: {
  480. singleLineBreaks: false
  481. },
  482. spellChecker: false,
  483. tabSize: 4,
  484. toolbar: ["bold", "italic", "strikethrough", "|",
  485. "heading", "heading-1", "heading-2", "heading-3", "|",
  486. "code", "quote", "|",
  487. "unordered-list", "ordered-list", "|",
  488. "link", "image", "horizontal-rule", "|",
  489. "preview", "fullscreen"]
  490. })
  491. }
  492. }
  493. function initOrganization() {
  494. if ($('.organization').length == 0) {
  495. return;
  496. }
  497. // Options
  498. if ($('.organization.settings.options').length > 0) {
  499. $('#org_name').keyup(function () {
  500. var $prompt_span = $('#org-name-change-prompt');
  501. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  502. $prompt_span.show();
  503. } else {
  504. $prompt_span.hide();
  505. }
  506. });
  507. }
  508. }
  509. function initUserSettings() {
  510. console.log('initUserSettings');
  511. // Options
  512. if ($('.user.settings.profile').length > 0) {
  513. $('#username').keyup(function () {
  514. var $prompt_span = $('#name-change-prompt');
  515. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  516. $prompt_span.show();
  517. } else {
  518. $prompt_span.hide();
  519. }
  520. });
  521. }
  522. }
  523. function initWebhook() {
  524. if ($('.new.webhook').length == 0) {
  525. return;
  526. }
  527. $('.events.checkbox input').change(function () {
  528. if ($(this).is(':checked')) {
  529. $('.events.fields').show();
  530. }
  531. });
  532. $('.non-events.checkbox input').change(function () {
  533. if ($(this).is(':checked')) {
  534. $('.events.fields').hide();
  535. }
  536. });
  537. // Test delivery
  538. $('#test-delivery').click(function () {
  539. var $this = $(this);
  540. $this.addClass('loading disabled');
  541. $.post($this.data('link'), {
  542. "_csrf": csrf
  543. }).done(
  544. setTimeout(function () {
  545. window.location.href = $this.data('redirect');
  546. }, 5000)
  547. )
  548. });
  549. }
  550. function initAdmin() {
  551. if ($('.admin').length == 0) {
  552. return;
  553. }
  554. // New user
  555. if ($('.admin.new.user').length > 0 ||
  556. $('.admin.edit.user').length > 0) {
  557. $('#login_type').change(function () {
  558. if ($(this).val().substring(0, 1) == '0') {
  559. $('#login_name').removeAttr('required');
  560. $('.non-local').hide();
  561. $('.local').show();
  562. $('#user_name').focus();
  563. if ($(this).data('password') == "required") {
  564. $('#password').attr('required', 'required');
  565. }
  566. } else {
  567. $('#login_name').attr('required', 'required');
  568. $('.non-local').show();
  569. $('.local').hide();
  570. $('#login_name').focus();
  571. $('#password').removeAttr('required');
  572. }
  573. });
  574. }
  575. function onSecurityProtocolChange() {
  576. if ($('#security_protocol').val() > 0) {
  577. $('.has-tls').show();
  578. } else {
  579. $('.has-tls').hide();
  580. }
  581. }
  582. // New authentication
  583. if ($('.admin.new.authentication').length > 0) {
  584. $('#auth_type').change(function () {
  585. $('.ldap').hide();
  586. $('.dldap').hide();
  587. $('.smtp').hide();
  588. $('.pam').hide();
  589. $('.has-tls').hide();
  590. var auth_type = $(this).val();
  591. switch (auth_type) {
  592. case '2': // LDAP
  593. $('.ldap').show();
  594. break;
  595. case '3': // SMTP
  596. $('.smtp').show();
  597. $('.has-tls').show();
  598. break;
  599. case '4': // PAM
  600. $('.pam').show();
  601. break;
  602. case '5': // LDAP
  603. $('.dldap').show();
  604. break;
  605. }
  606. if (auth_type == '2' || auth_type == '5') {
  607. onSecurityProtocolChange()
  608. }
  609. });
  610. $('#security_protocol').change(onSecurityProtocolChange)
  611. }
  612. // Edit authentication
  613. if ($('.admin.edit.authentication').length > 0) {
  614. var auth_type = $('#auth_type').val();
  615. if (auth_type == '2' || auth_type == '5') {
  616. $('#security_protocol').change(onSecurityProtocolChange);
  617. }
  618. }
  619. // Notice
  620. if ($('.admin.notice')) {
  621. var $detail_modal = $('#detail-modal');
  622. // Attach view detail modals
  623. $('.view-detail').click(function () {
  624. $detail_modal.find('.content p').text($(this).data('content'));
  625. $detail_modal.modal('show');
  626. return false;
  627. });
  628. // Select actions
  629. var $checkboxes = $('.select.table .ui.checkbox');
  630. $('.select.action').click(function () {
  631. switch ($(this).data('action')) {
  632. case 'select-all':
  633. $checkboxes.checkbox('check');
  634. break;
  635. case 'deselect-all':
  636. $checkboxes.checkbox('uncheck');
  637. break;
  638. case 'inverse':
  639. $checkboxes.checkbox('toggle');
  640. break;
  641. }
  642. });
  643. $('#delete-selection').click(function () {
  644. var $this = $(this);
  645. $this.addClass("loading disabled");
  646. var ids = [];
  647. $checkboxes.each(function () {
  648. if ($(this).checkbox('is checked')) {
  649. ids.push($(this).data('id'));
  650. }
  651. });
  652. $.post($this.data('link'), {
  653. "_csrf": csrf,
  654. "ids": ids
  655. }).done(function () {
  656. window.location.href = $this.data('redirect');
  657. });
  658. });
  659. }
  660. }
  661. function buttonsClickOnEnter() {
  662. $('.ui.button').keypress(function (e) {
  663. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  664. $(this).click();
  665. });
  666. }
  667. function hideWhenLostFocus(body, parent) {
  668. $(document).click(function (e) {
  669. var target = e.target;
  670. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  671. $(body).hide();
  672. }
  673. });
  674. }
  675. function searchUsers() {
  676. if (!$('#search-user-box .results').length) {
  677. return;
  678. }
  679. var $search_user_box = $('#search-user-box');
  680. var $result_list = $search_user_box.find('.results');
  681. $search_user_box.keyup(function () {
  682. var $this = $(this);
  683. var keyword = $this.find('input').val();
  684. if (keyword.length < 2) {
  685. $result_list.hide();
  686. return;
  687. }
  688. $.ajax({
  689. url: suburl + '/api/v1/users/search?q=' + keyword,
  690. dataType: "json",
  691. success: function (response) {
  692. var notEmpty = function (str) {
  693. return str && str.length > 0;
  694. };
  695. $result_list.html('');
  696. if (response.ok && response.data.length) {
  697. var html = '';
  698. $.each(response.data, function (i, item) {
  699. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  700. if (notEmpty(item.full_name)) {
  701. html += ' (' + item.full_name + ')';
  702. }
  703. html += '</div>';
  704. });
  705. $result_list.html(html);
  706. $this.find('.results .item').click(function () {
  707. $this.find('input').val($(this).find('.username').text());
  708. $result_list.hide();
  709. });
  710. $result_list.show();
  711. } else {
  712. $result_list.hide();
  713. }
  714. }
  715. });
  716. });
  717. $search_user_box.find('input').focus(function () {
  718. $search_user_box.keyup();
  719. });
  720. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  721. }
  722. // FIXME: merge common parts in two functions
  723. function searchRepositories() {
  724. if (!$('#search-repo-box .results').length) {
  725. return;
  726. }
  727. var $search_repo_box = $('#search-repo-box');
  728. var $result_list = $search_repo_box.find('.results');
  729. $search_repo_box.keyup(function () {
  730. var $this = $(this);
  731. var keyword = $this.find('input').val();
  732. if (keyword.length < 2) {
  733. $result_list.hide();
  734. return;
  735. }
  736. $.ajax({
  737. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $search_repo_box.data('uid'),
  738. dataType: "json",
  739. success: function (response) {
  740. var notEmpty = function (str) {
  741. return str && str.length > 0;
  742. };
  743. $result_list.html('');
  744. if (response.ok && response.data.length) {
  745. var html = '';
  746. $.each(response.data, function (i, item) {
  747. html += '<div class="item"><i class="icon octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  748. });
  749. $result_list.html(html);
  750. $this.find('.results .item').click(function () {
  751. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  752. $result_list.hide();
  753. });
  754. $result_list.show();
  755. } else {
  756. $result_list.hide();
  757. }
  758. }
  759. });
  760. });
  761. $search_repo_box.find('input').focus(function () {
  762. $search_repo_box.keyup();
  763. });
  764. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  765. }
  766. $(document).ready(function () {
  767. csrf = $('meta[name=_csrf]').attr("content");
  768. suburl = $('meta[name=_suburl]').attr("content");
  769. // Show exact time
  770. $('.time-since').each(function () {
  771. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  772. });
  773. // Semantic UI modules.
  774. $('.dropdown').dropdown();
  775. $('.jump.dropdown').dropdown({
  776. action: 'hide',
  777. onShow: function () {
  778. $('.poping.up').popup('hide');
  779. }
  780. });
  781. $('.slide.up.dropdown').dropdown({
  782. transition: 'slide up'
  783. });
  784. $('.upward.dropdown').dropdown({
  785. direction: 'upward'
  786. });
  787. $('.ui.accordion').accordion();
  788. $('.ui.checkbox').checkbox();
  789. $('.ui.progress').progress({
  790. showActivity: false
  791. });
  792. $('.poping.up').popup();
  793. $('.top.menu .poping.up').popup({
  794. onShow: function () {
  795. if ($('.top.menu .menu.transition').hasClass('visible')) {
  796. return false;
  797. }
  798. }
  799. });
  800. $('.tabular.menu .item').tab();
  801. $('.tabable.menu .item').tab();
  802. $('.toggle.button').click(function () {
  803. $($(this).data('target')).slideToggle(100);
  804. });
  805. // Highlight JS
  806. if (typeof hljs != 'undefined') {
  807. hljs.initHighlightingOnLoad();
  808. }
  809. // Dropzone
  810. if ($('#dropzone').length > 0) {
  811. // Disable auto discover for all elements:
  812. Dropzone.autoDiscover = false;
  813. var filenameDict = {};
  814. var $dropz = $('#dropzone');
  815. $dropz.dropzone({
  816. url: $dropz.data('upload-url'),
  817. headers: {"X-Csrf-Token": csrf},
  818. maxFiles: $dropz.data('max-file'),
  819. maxFilesize: $dropz.data('max-size'),
  820. acceptedFiles: ($dropz.data('accepts') === '*/*') ? null : $dropz.data('accepts'),
  821. addRemoveLinks: true,
  822. dictDefaultMessage: $dropz.data('default-message'),
  823. dictInvalidFileType: $dropz.data('invalid-input-type'),
  824. dictFileTooBig: $dropz.data('file-too-big'),
  825. dictRemoveFile: $dropz.data('remove-file'),
  826. init: function () {
  827. this.on("success", function (file, data) {
  828. filenameDict[file.name] = data.uuid;
  829. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  830. });
  831. this.on("removedfile", function (file) {
  832. if (file.name in filenameDict) {
  833. $('#' + filenameDict[file.name]).remove();
  834. }
  835. })
  836. }
  837. });
  838. }
  839. // Emojify
  840. emojify.setConfig({
  841. img_dir: suburl + '/img/emoji',
  842. ignore_emoticons: true
  843. });
  844. var hasEmoji = document.getElementsByClassName('has-emoji');
  845. for (var i = 0; i < hasEmoji.length; i++) {
  846. emojify.run(hasEmoji[i]);
  847. }
  848. // Clipboard JS
  849. var clipboard = new Clipboard('.clipboard');
  850. clipboard.on('success', function (e) {
  851. e.clearSelection();
  852. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  853. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  854. $('#' + e.trigger.getAttribute('id')).popup('show');
  855. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  856. });
  857. clipboard.on('error', function (e) {
  858. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  859. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  860. $('#' + e.trigger.getAttribute('id')).popup('show');
  861. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  862. });
  863. // Helpers.
  864. $('.delete-button').click(function () {
  865. var $this = $(this);
  866. $('.delete.modal').modal({
  867. closable: false,
  868. onApprove: function () {
  869. if ($this.data('type') == "form") {
  870. $($this.data('form')).submit();
  871. return;
  872. }
  873. $.post($this.data('url'), {
  874. "_csrf": csrf,
  875. "id": $this.data("id")
  876. }).done(function (data) {
  877. window.location.href = data.redirect;
  878. });
  879. }
  880. }).modal('show');
  881. return false;
  882. });
  883. $('.show-panel.button').click(function () {
  884. $($(this).data('panel')).show();
  885. });
  886. $('.show-modal.button').click(function () {
  887. $($(this).data('modal')).modal('show');
  888. });
  889. $('.delete-post.button').click(function () {
  890. var $this = $(this);
  891. $.post($this.data('request-url'), {
  892. "_csrf": csrf
  893. }).done(function () {
  894. window.location.href = $this.data('done-url');
  895. });
  896. });
  897. // Set anchor.
  898. $('.markdown').each(function () {
  899. var headers = {};
  900. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  901. var node = $(this);
  902. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  903. var name = val;
  904. if (headers[val] > 0) {
  905. name = val + '-' + headers[val];
  906. }
  907. if (headers[val] == undefined) {
  908. headers[val] = 1;
  909. } else {
  910. headers[val] += 1;
  911. }
  912. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  913. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  914. });
  915. });
  916. buttonsClickOnEnter();
  917. searchUsers();
  918. searchRepositories();
  919. initCommentForm();
  920. initInstall();
  921. initRepository();
  922. initWiki();
  923. initOrganization();
  924. initWebhook();
  925. initAdmin();
  926. var routes = {
  927. 'div.user.settings': initUserSettings,
  928. 'div.repository.settings.collaboration': initRepositoryCollaboration
  929. };
  930. var selector;
  931. for (selector in routes) {
  932. if ($(selector).length > 0) {
  933. routes[selector]();
  934. break;
  935. }
  936. }
  937. });
  938. $(window).load(function () {
  939. function changeHash(hash) {
  940. if (history.pushState) {
  941. history.pushState(null, null, hash);
  942. }
  943. else {
  944. location.hash = hash;
  945. }
  946. }
  947. function deSelect() {
  948. if (window.getSelection) {
  949. window.getSelection().removeAllRanges();
  950. } else {
  951. document.selection.empty();
  952. }
  953. }
  954. function selectRange($list, $select, $from) {
  955. $list.removeClass('active');
  956. if ($from) {
  957. var a = parseInt($select.attr('rel').substr(1));
  958. var b = parseInt($from.attr('rel').substr(1));
  959. var c;
  960. if (a != b) {
  961. if (a > b) {
  962. c = a;
  963. a = b;
  964. b = c;
  965. }
  966. var classes = [];
  967. for (i = a; i <= b; i++) {
  968. classes.push('.L' + i);
  969. }
  970. $list.filter(classes.join(',')).addClass('active');
  971. changeHash('#L' + a + '-' + 'L' + b);
  972. return
  973. }
  974. }
  975. $select.addClass('active');
  976. changeHash('#' + $select.attr('rel'));
  977. }
  978. // Code view.
  979. if ($('.code-view .linenums').length > 0) {
  980. var $block = $('.code-view .linenums');
  981. var lines = $block.html().split("\n");
  982. $block.html('');
  983. var $num_list = $('.code-view .lines-num');
  984. // Building blocks.
  985. var $toappendblock = [];
  986. var $toappendnum_list = [];
  987. for (var i = 0; i < lines.length; i++) {
  988. $toappendblock.push('<li class="L' + (i + 1) + '" rel="L' + (i + 1) + '">' + lines[i] + '</li>');
  989. $toappendnum_list.push('<span id="L' + (i + 1) + '">' + (i + 1) + '</span>');
  990. }
  991. $block.append($toappendblock.join(''));
  992. $num_list.append($toappendnum_list.join(''));
  993. $(document).on('click', '.lines-num span', function (e) {
  994. var $select = $(this);
  995. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  996. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  997. deSelect();
  998. });
  999. $(window).on('hashchange', function (e) {
  1000. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1001. var $list = $('.code-view ol.linenums > li');
  1002. var $first;
  1003. if (m) {
  1004. $first = $list.filter('.' + m[1]);
  1005. selectRange($list, $first, $list.filter('.' + m[2]));
  1006. $("html, body").scrollTop($first.offset().top - 200);
  1007. return;
  1008. }
  1009. m = window.location.hash.match(/^#(L\d+)$/);
  1010. if (m) {
  1011. $first = $list.filter('.' + m[1]);
  1012. selectRange($list, $first);
  1013. $("html, body").scrollTop($first.offset().top - 200);
  1014. }
  1015. }).trigger('hashchange');
  1016. }
  1017. // Repo clone url.
  1018. if ($('#repo-clone-url').length > 0) {
  1019. switch (localStorage.getItem('repo-clone-protocol')) {
  1020. case 'ssh':
  1021. if ($('#repo-clone-ssh').click().length === 0) {
  1022. $('#repo-clone-https').click();
  1023. }
  1024. ;
  1025. break;
  1026. default:
  1027. $('#repo-clone-https').click();
  1028. break;
  1029. }
  1030. }
  1031. });
  1032. $(function () {
  1033. if ($('.user.signin').length > 0) return;
  1034. $('form').areYouSure();
  1035. });