gogs.js 23 KB

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