gogs.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. }
  20. );
  21. });
  22. buttonsClickOnEnter();
  23. }
  24. function initCommentForm() {
  25. if ($('.comment.form').length == 0) {
  26. return
  27. }
  28. initCommentPreviewTab($('.comment.form'));
  29. // Labels
  30. var $list = $('.ui.labels.list');
  31. var $no_select = $list.find('.no-select');
  32. var $label_menu = $('.select-label .menu');
  33. var has_label_update_action = $label_menu.data('action') == 'update';
  34. function updateIssueMeta(url, action, id) {
  35. $.post(url, {
  36. "_csrf": csrf,
  37. "action": action,
  38. "id": id
  39. });
  40. }
  41. $label_menu.find('.item:not(.no-select)').click(function () {
  42. if ($(this).hasClass('checked')) {
  43. $(this).removeClass('checked');
  44. $(this).find('.octicon').removeClass('octicon-check');
  45. if (has_label_update_action) {
  46. updateIssueMeta($label_menu.data('update-url'), "detach", $(this).data('id'));
  47. }
  48. } else {
  49. $(this).addClass('checked');
  50. $(this).find('.octicon').addClass('octicon-check');
  51. if (has_label_update_action) {
  52. updateIssueMeta($label_menu.data('update-url'), "attach", $(this).data('id'));
  53. }
  54. }
  55. var label_ids = "";
  56. $(this).parent().find('.item').each(function () {
  57. if ($(this).hasClass('checked')) {
  58. label_ids += $(this).data('id') + ",";
  59. $($(this).data('id-selector')).removeClass('hide');
  60. } else {
  61. $($(this).data('id-selector')).addClass('hide');
  62. }
  63. });
  64. if (label_ids.length == 0) {
  65. $no_select.removeClass('hide');
  66. } else {
  67. $no_select.addClass('hide');
  68. }
  69. $($(this).parent().data('id')).val(label_ids);
  70. return false;
  71. });
  72. $label_menu.find('.no-select.item').click(function () {
  73. if (has_label_update_action) {
  74. updateIssueMeta($label_menu.data('update-url'), "clear", '');
  75. }
  76. $(this).parent().find('.item').each(function () {
  77. $(this).removeClass('checked');
  78. $(this).find('.octicon').removeClass('octicon-check');
  79. });
  80. $list.find('.item').each(function () {
  81. $(this).addClass('hide');
  82. });
  83. $no_select.removeClass('hide');
  84. $($(this).parent().data('id')).val('');
  85. });
  86. function selectItem(select_id, input_id) {
  87. var $menu = $(select_id + ' .menu');
  88. var $list = $('.ui' + select_id + '.list');
  89. var has_update_action = $menu.data('action') == 'update';
  90. $menu.find('.item:not(.no-select)').click(function () {
  91. $(this).parent().find('.item').each(function () {
  92. $(this).removeClass('selected active')
  93. });
  94. $(this).addClass('selected active');
  95. if (has_update_action) {
  96. updateIssueMeta($menu.data('update-url'), '', $(this).data('id'));
  97. }
  98. switch (input_id) {
  99. case '#milestone_id':
  100. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  101. $(this).text() + '</a>');
  102. break;
  103. case '#assignee_id':
  104. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  105. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  106. $(this).text() + '</a>');
  107. }
  108. $('.ui' + select_id + '.list .no-select').addClass('hide');
  109. $(input_id).val($(this).data('id'));
  110. });
  111. $menu.find('.no-select.item').click(function () {
  112. $(this).parent().find('.item:not(.no-select)').each(function () {
  113. $(this).removeClass('selected active')
  114. });
  115. if (has_update_action) {
  116. updateIssueMeta($menu.data('update-url'), '', '');
  117. }
  118. $list.find('.selected').html('');
  119. $list.find('.no-select').removeClass('hide');
  120. $(input_id).val('');
  121. });
  122. }
  123. // Milestone and assignee
  124. selectItem('.select-milestone', '#milestone_id');
  125. selectItem('.select-assignee', '#assignee_id');
  126. }
  127. function initInstall() {
  128. if ($('.install').length == 0) {
  129. return;
  130. }
  131. // Database type change detection.
  132. $("#db_type").change(function () {
  133. var sqlite_default = 'data/gogs.db';
  134. var tidb_default = 'data/gogs_tidb';
  135. var db_type = $(this).val();
  136. if (db_type === "SQLite3" || db_type === "TiDB") {
  137. $('#sql_settings').hide();
  138. $('#pgsql_settings').hide();
  139. $('#sqlite_settings').show();
  140. if (db_type === "SQLite3" && $('#db_path').val() == tidb_default) {
  141. $('#db_path').val(sqlite_default);
  142. } else if (db_type === "TiDB" && $('#db_path').val() == sqlite_default) {
  143. $('#db_path').val(tidb_default);
  144. }
  145. return;
  146. }
  147. var mysql_default = '127.0.0.1:3306';
  148. var postgres_default = '127.0.0.1:5432';
  149. $('#sqlite_settings').hide();
  150. $('#sql_settings').show();
  151. if (db_type === "PostgreSQL") {
  152. $('#pgsql_settings').show();
  153. if ($('#db_host').val() == mysql_default) {
  154. $('#db_host').val(postgres_default);
  155. }
  156. } else {
  157. $('#pgsql_settings').hide();
  158. if ($('#db_host').val() == postgres_default) {
  159. $('#db_host').val(mysql_default);
  160. }
  161. }
  162. });
  163. $('#offline-mode input').change(function () {
  164. if ($(this).is(':checked')) {
  165. $('#disable-gravatar').checkbox('check');
  166. }
  167. });
  168. $('#disable-registration input').change(function () {
  169. if ($(this).is(':checked')) {
  170. $('#enable-captcha').checkbox('uncheck');
  171. }
  172. });
  173. $('#enable-captcha input').change(function () {
  174. if ($(this).is(':checked')) {
  175. $('#disable-registration').checkbox('uncheck');
  176. }
  177. });
  178. }
  179. function initRepository() {
  180. if ($('.repository').length == 0) {
  181. return;
  182. }
  183. // Options
  184. if ($('.repository.settings.options').length > 0) {
  185. $('#repo_name').keyup(function () {
  186. var $prompt_span = $('#repo-name-change-prompt');
  187. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  188. $prompt_span.show();
  189. } else {
  190. $prompt_span.hide();
  191. }
  192. });
  193. }
  194. // Labels
  195. if ($('.repository.labels').length > 0) {
  196. // Create label
  197. var $new_label_panel = $('.new-label.segment');
  198. $('.new-label.button').click(function () {
  199. $new_label_panel.show();
  200. });
  201. $('.new-label.segment .cancel').click(function () {
  202. $new_label_panel.hide();
  203. });
  204. $('.color-picker').each(function () {
  205. $(this).minicolors();
  206. });
  207. $('.precolors .color').click(function () {
  208. var color_hex = $(this).data('color-hex');
  209. $('.color-picker').val(color_hex);
  210. $('.minicolors-swatch-color').css("background-color", color_hex);
  211. });
  212. $('.edit-label-button').click(function () {
  213. $('#label-modal-id').val($(this).data('id'));
  214. $('.edit-label .new-label-input').val($(this).data('title'));
  215. $('.edit-label .color-picker').val($(this).data('color'));
  216. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  217. $('.edit-label.modal').modal({
  218. onApprove: function () {
  219. $('.edit-label.form').submit();
  220. }
  221. }).modal('show');
  222. return false;
  223. });
  224. }
  225. // Milestones
  226. if ($('.repository.milestones').length > 0) {
  227. }
  228. if ($('.repository.new.milestone').length > 0) {
  229. var $datepicker = $('.milestone.datepicker');
  230. $datepicker.datetimepicker({
  231. lang: $datepicker.data('lang'),
  232. inline: true,
  233. timepicker: false,
  234. startDate: $datepicker.data('start-date'),
  235. formatDate: 'Y-m-d',
  236. onSelectDate: function (ct) {
  237. $('#deadline').val(ct.dateFormat('Y-m-d'));
  238. }
  239. });
  240. $('#clear-date').click(function () {
  241. $('#deadline').val('');
  242. return false;
  243. });
  244. }
  245. // Issues
  246. if ($('.repository.view.issue').length > 0) {
  247. // Edit issue title
  248. var $issue_title = $('#issue-title');
  249. var $edit_input = $('#edit-title-input input');
  250. var editTitleToggle = function () {
  251. $issue_title.toggle();
  252. $('.not-in-edit').toggle();
  253. $('#edit-title-input').toggle();
  254. $('.in-edit').toggle();
  255. $edit_input.focus();
  256. return false;
  257. };
  258. $('#edit-title').click(editTitleToggle);
  259. $('#cancel-edit-title').click(editTitleToggle);
  260. $('#save-edit-title').click(editTitleToggle).
  261. click(function () {
  262. if ($edit_input.val().length == 0 ||
  263. $edit_input.val() == $issue_title.text()) {
  264. $edit_input.val($issue_title.text());
  265. return false;
  266. }
  267. $.post($(this).data('update-url'), {
  268. "_csrf": csrf,
  269. "title": $edit_input.val()
  270. },
  271. function (data) {
  272. $edit_input.val(data.title);
  273. $issue_title.text(data.title);
  274. });
  275. return false;
  276. });
  277. // Edit issue or comment content
  278. $('.edit-content').click(function () {
  279. var $segment = $(this).parent().parent().next();
  280. var $edit_content_zone = $segment.find('.edit-content-zone');
  281. var $render_content = $segment.find('.render-content');
  282. var $raw_content = $segment.find('.raw-content');
  283. var $textarea;
  284. // Setup new form
  285. if ($edit_content_zone.html().length == 0) {
  286. $edit_content_zone.html($('#edit-content-form').html());
  287. $textarea = $segment.find('textarea');
  288. // Give new write/preview data-tab name to distinguish from others
  289. var $edit_content_form = $edit_content_zone.find('.ui.comment.form');
  290. var $tabular_menu = $edit_content_form.find('.tabular.menu');
  291. $tabular_menu.attr('data-write', $edit_content_zone.data('write'));
  292. $tabular_menu.attr('data-preview', $edit_content_zone.data('preview'));
  293. $tabular_menu.find('.write.item').attr('data-tab', $edit_content_zone.data('write'));
  294. $tabular_menu.find('.preview.item').attr('data-tab', $edit_content_zone.data('preview'));
  295. $edit_content_form.find('.write.segment').attr('data-tab', $edit_content_zone.data('write'));
  296. $edit_content_form.find('.preview.segment').attr('data-tab', $edit_content_zone.data('preview'));
  297. initCommentPreviewTab($edit_content_form);
  298. $edit_content_zone.find('.cancel.button').click(function () {
  299. $render_content.show();
  300. $edit_content_zone.hide();
  301. });
  302. $edit_content_zone.find('.save.button').click(function () {
  303. $render_content.show();
  304. $edit_content_zone.hide();
  305. $.post($edit_content_zone.data('update-url'), {
  306. "_csrf": csrf,
  307. "content": $textarea.val(),
  308. "context": $edit_content_zone.data('context')
  309. },
  310. function (data) {
  311. if (data.length == 0) {
  312. $render_content.html($('#no-content').html());
  313. } else {
  314. $render_content.html(data.content);
  315. emojify.run($render_content[0]);
  316. }
  317. });
  318. });
  319. } else {
  320. $textarea = $segment.find('textarea');
  321. }
  322. // Show write/preview tab and copy raw content as needed
  323. $edit_content_zone.show();
  324. $render_content.hide();
  325. if ($textarea.val().length == 0) {
  326. $textarea.val($raw_content.text());
  327. }
  328. $textarea.focus();
  329. return false;
  330. });
  331. // Change status
  332. var $status_btn = $('#status-button');
  333. $('#content').keyup(function () {
  334. if ($(this).val().length == 0) {
  335. $status_btn.text($status_btn.data('status'))
  336. } else {
  337. $status_btn.text($status_btn.data('status-and-comment'))
  338. }
  339. });
  340. $status_btn.click(function () {
  341. $('#status').val($status_btn.data('status-val'));
  342. $('#comment-form').submit();
  343. })
  344. }
  345. // Diff
  346. if ($('.repository.diff').length > 0) {
  347. var $counter = $('.diff-counter');
  348. if ($counter.length >= 1) {
  349. $counter.each(function (i, item) {
  350. var $item = $(item);
  351. var addLine = $item.find('span[data-line].add').data("line");
  352. var delLine = $item.find('span[data-line].del').data("line");
  353. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  354. $item.find(".bar .add").css("width", addPercent + "%");
  355. });
  356. }
  357. }
  358. // Quick start
  359. if ($('.repository.quickstart').length > 0) {
  360. $('#repo-clone-ssh').click(function () {
  361. $('.clone-url').text($(this).data('link'));
  362. $('#repo-clone-url').val($(this).data('link'));
  363. $(this).addClass('blue');
  364. $('#repo-clone-https').removeClass('blue');
  365. });
  366. $('#repo-clone-https').click(function () {
  367. $('.clone-url').text($(this).data('link'));
  368. $('#repo-clone-url').val($(this).data('link'));
  369. $(this).addClass('blue');
  370. $('#repo-clone-ssh').removeClass('blue');
  371. });
  372. }
  373. // Pull request
  374. if ($('.repository.compare.pull').length > 0) {
  375. var $branch_dropdown = $('.choose.branch .dropdown');
  376. $branch_dropdown.dropdown({
  377. fullTextSearch: true,
  378. onChange: function (text, value, $choice) {
  379. window.location.href = $choice.data('url');
  380. console.log($choice.data('url'))
  381. },
  382. message: {noResults: $branch_dropdown.data('no-results')}
  383. });
  384. }
  385. }
  386. function initOrganization() {
  387. if ($('.organization').length == 0) {
  388. return;
  389. }
  390. // Options
  391. if ($('.organization.settings.options').length > 0) {
  392. $('#org_name').keyup(function () {
  393. var $prompt_span = $('#org-name-change-prompt');
  394. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  395. $prompt_span.show();
  396. } else {
  397. $prompt_span.hide();
  398. }
  399. });
  400. }
  401. }
  402. function initUser() {
  403. if ($('.user').length == 0) {
  404. return;
  405. }
  406. // Options
  407. if ($('.user.settings.profile').length > 0) {
  408. $('#username').keyup(function () {
  409. var $prompt_span = $('#name-change-prompt');
  410. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  411. $prompt_span.show();
  412. } else {
  413. $prompt_span.hide();
  414. }
  415. });
  416. }
  417. }
  418. function initWebhook() {
  419. if ($('.new.webhook').length == 0) {
  420. return;
  421. }
  422. $('.events.checkbox input').change(function () {
  423. if ($(this).is(':checked')) {
  424. $('.events.fields').show();
  425. }
  426. });
  427. $('.non-events.checkbox input').change(function () {
  428. if ($(this).is(':checked')) {
  429. $('.events.fields').hide();
  430. }
  431. });
  432. }
  433. function initAdmin() {
  434. if ($('.admin').length == 0) {
  435. return;
  436. }
  437. // New user
  438. if ($('.admin.new.user').length > 0 ||
  439. $('.admin.edit.user').length > 0) {
  440. $('#login_type').change(function () {
  441. if ($(this).val().substring(0, 1) == '0') {
  442. $('#login_name').removeAttr('required');
  443. $('.non-local').hide();
  444. $('.local').show();
  445. $('#user_name').focus();
  446. if ($(this).data('password') == "required") {
  447. $('#password').attr('required', 'required');
  448. }
  449. } else {
  450. $('#login_name').attr('required', 'required');
  451. $('.non-local').show();
  452. $('.local').hide();
  453. $('#login_name').focus();
  454. $('#password').removeAttr('required');
  455. }
  456. });
  457. }
  458. // New authentication
  459. if ($('.admin.new.authentication').length > 0) {
  460. $('#auth_type').change(function () {
  461. $('.ldap').hide();
  462. $('.dldap').hide();
  463. $('.smtp').hide();
  464. $('.pam').hide();
  465. var auth_type = $(this).val();
  466. switch (auth_type) {
  467. case '2': // LDAP
  468. $('.ldap').show();
  469. break;
  470. case '3': // SMTP
  471. $('.smtp').show();
  472. break;
  473. case '4': // PAM
  474. $('.pam').show();
  475. break;
  476. case '5': // LDAP
  477. $('.dldap').show();
  478. break;
  479. }
  480. });
  481. }
  482. }
  483. function buttonsClickOnEnter() {
  484. $('.ui.button').keypress(function(e){
  485. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  486. $(this).click();
  487. });
  488. }
  489. $(document).ready(function () {
  490. csrf = $('meta[name=_csrf]').attr("content");
  491. suburl = $('meta[name=_suburl]').attr("content");
  492. // Show exact time
  493. $('.time-since').each(function () {
  494. $(this).addClass('poping up').
  495. attr('data-content', $(this).attr('title')).
  496. attr('data-variation', 'inverted tiny').
  497. attr('title', '');
  498. });
  499. // Semantic UI modules.
  500. $('.dropdown').dropdown();
  501. $('.jump.dropdown').dropdown({
  502. action: 'hide',
  503. onShow: function () {
  504. $('.poping.up').popup('hide');
  505. }
  506. });
  507. $('.slide.up.dropdown').dropdown({
  508. transition: 'slide up'
  509. });
  510. $('.ui.accordion').accordion();
  511. $('.ui.checkbox').checkbox();
  512. $('.ui.progress').progress({
  513. showActivity: false
  514. });
  515. $('.poping.up').popup();
  516. $('.top.menu .poping.up').popup({
  517. onShow: function () {
  518. if ($('.top.menu .menu.transition').hasClass('visible')) {
  519. return false;
  520. }
  521. }
  522. });
  523. $('.tabular.menu .item').tab();
  524. $('.toggle.button').click(function () {
  525. $($(this).data('target')).slideToggle(100);
  526. });
  527. // Highlight JS
  528. if (typeof hljs != 'undefined') {
  529. hljs.initHighlightingOnLoad();
  530. }
  531. // Dropzone
  532. if ($('#dropzone').length > 0) {
  533. // Disable auto discover for all elements:
  534. Dropzone.autoDiscover = false;
  535. var filenameDict = {};
  536. var $dropz = $('#dropzone');
  537. $dropz.dropzone({
  538. url: $dropz.data('upload-url'),
  539. headers: {"X-Csrf-Token": csrf},
  540. maxFiles: $dropz.data('max-file'),
  541. maxFilesize: $dropz.data('max-size'),
  542. acceptedFiles: $dropz.data('accepts'),
  543. addRemoveLinks: true,
  544. dictDefaultMessage: $dropz.data('default-message'),
  545. dictInvalidFileType: $dropz.data('invalid-input-type'),
  546. dictFileTooBig: $dropz.data('file-too-big'),
  547. dictRemoveFile: $dropz.data('remove-file'),
  548. init: function () {
  549. this.on("success", function (file, data) {
  550. filenameDict[file.name] = data.uuid;
  551. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  552. });
  553. this.on("removedfile", function (file) {
  554. if (file.name in filenameDict) {
  555. $('#' + filenameDict[file.name]).remove();
  556. }
  557. })
  558. }
  559. });
  560. }
  561. // Emojify
  562. emojify.setConfig({
  563. img_dir: suburl + '/img/emoji'
  564. });
  565. $('.emojify').each(function () {
  566. emojify.run($(this)[0]);
  567. });
  568. // Clipboard JS
  569. var clipboard = new Clipboard('.clipboard');
  570. clipboard.on('success', function (e) {
  571. e.clearSelection();
  572. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  573. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  574. $('#' + e.trigger.getAttribute('id')).popup('show');
  575. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  576. });
  577. clipboard.on('error', function (e) {
  578. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  579. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  580. $('#' + e.trigger.getAttribute('id')).popup('show');
  581. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  582. });
  583. // Helpers.
  584. $('.delete-button').click(function () {
  585. var $this = $(this);
  586. $('.delete.modal').modal({
  587. closable: false,
  588. onApprove: function () {
  589. if ($this.data('type') == "form") {
  590. $($this.data('form')).submit();
  591. return;
  592. }
  593. $.post($this.data('url'), {
  594. "_csrf": csrf,
  595. "id": $this.data("id")
  596. }).done(function (data) {
  597. window.location.href = data.redirect;
  598. });
  599. }
  600. }).modal('show');
  601. return false;
  602. });
  603. $('.show-panel.button').click(function () {
  604. $($(this).data('panel')).show();
  605. });
  606. $('.show-modal.button').click(function () {
  607. $($(this).data('modal')).modal('show');
  608. });
  609. buttonsClickOnEnter();
  610. initCommentForm();
  611. initInstall();
  612. initRepository();
  613. initOrganization();
  614. initUser();
  615. initWebhook();
  616. initAdmin();
  617. });