gogs.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. }
  168. function initRepository() {
  169. if ($('.repository').length == 0) {
  170. return;
  171. }
  172. // Options
  173. if ($('.repository.settings.options').length > 0) {
  174. $('#repo_name').keyup(function () {
  175. var $prompt_span = $('#repo-name-change-prompt');
  176. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  177. $prompt_span.show();
  178. } else {
  179. $prompt_span.hide();
  180. }
  181. });
  182. }
  183. // Labels
  184. if ($('.repository.labels').length > 0) {
  185. // Create label
  186. var $new_label_panel = $('.new-label.segment');
  187. $('.new-label.button').click(function () {
  188. $new_label_panel.show();
  189. });
  190. $('.new-label.segment .cancel').click(function () {
  191. $new_label_panel.hide();
  192. });
  193. $('.color-picker').each(function () {
  194. $(this).minicolors();
  195. });
  196. $('.precolors .color').click(function () {
  197. var color_hex = $(this).data('color-hex');
  198. $('.color-picker').val(color_hex);
  199. $('.minicolors-swatch-color').css("background-color", color_hex);
  200. });
  201. $('.edit-label-button').click(function () {
  202. $('#label-modal-id').val($(this).data('id'));
  203. $('.edit-label .new-label-input').val($(this).data('title'));
  204. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  205. $('.edit-label.modal').modal({
  206. onApprove: function () {
  207. $('.edit-label.form').submit();
  208. }
  209. }).modal('show');
  210. return false;
  211. });
  212. }
  213. // Milestones
  214. if ($('.repository.milestones').length > 0) {
  215. }
  216. if ($('.repository.new.milestone').length > 0) {
  217. var $datepicker = $('.milestone.datepicker');
  218. $datepicker.datetimepicker({
  219. lang: $datepicker.data('lang'),
  220. inline: true,
  221. timepicker: false,
  222. startDate: $datepicker.data('start-date'),
  223. formatDate: 'Y-m-d',
  224. onSelectDate: function (ct) {
  225. $('#deadline').val(ct.dateFormat('Y-m-d'));
  226. }
  227. });
  228. $('#clear-date').click(function () {
  229. $('#deadline').val('');
  230. return false;
  231. });
  232. }
  233. // Issues
  234. if ($('.repository.view.issue').length > 0) {
  235. // Edit issue title
  236. var $issue_title = $('#issue-title');
  237. var $edit_input = $('#edit-title-input input');
  238. var editTitleToggle = function () {
  239. $issue_title.toggle();
  240. $('.not-in-edit').toggle();
  241. $('#edit-title-input').toggle();
  242. $('.in-edit').toggle();
  243. $edit_input.focus();
  244. return false;
  245. };
  246. $('#edit-title').click(editTitleToggle);
  247. $('#cancel-edit-title').click(editTitleToggle);
  248. $('#save-edit-title').click(editTitleToggle).
  249. click(function () {
  250. if ($edit_input.val().length == 0 ||
  251. $edit_input.val() == $issue_title.text()) {
  252. $edit_input.val($issue_title.text());
  253. return false;
  254. }
  255. $.post($(this).data('update-url'), {
  256. "_csrf": csrf,
  257. "title": $edit_input.val()
  258. },
  259. function (data) {
  260. $edit_input.val(data.title);
  261. $issue_title.text(data.title);
  262. });
  263. return false;
  264. });
  265. // Edit issue or comment content
  266. $('.edit-content').click(function () {
  267. var $segment = $(this).parent().parent().next();
  268. var $edit_content_zone = $segment.find('.edit-content-zone');
  269. var $render_content = $segment.find('.render-content');
  270. var $raw_content = $segment.find('.raw-content');
  271. var $textarea;
  272. // Setup new form
  273. if ($edit_content_zone.html().length == 0) {
  274. $edit_content_zone.html($('#edit-content-form').html());
  275. $textarea = $segment.find('textarea');
  276. // Give new write/preview data-tab name to distinguish from others
  277. var $edit_content_form = $edit_content_zone.find('.ui.comment.form');
  278. var $tabular_menu = $edit_content_form.find('.tabular.menu');
  279. $tabular_menu.attr('data-write', $edit_content_zone.data('write'));
  280. $tabular_menu.attr('data-preview', $edit_content_zone.data('preview'));
  281. $tabular_menu.find('.write.item').attr('data-tab', $edit_content_zone.data('write'));
  282. $tabular_menu.find('.preview.item').attr('data-tab', $edit_content_zone.data('preview'));
  283. $edit_content_form.find('.write.segment').attr('data-tab', $edit_content_zone.data('write'));
  284. $edit_content_form.find('.preview.segment').attr('data-tab', $edit_content_zone.data('preview'));
  285. initCommentPreviewTab($edit_content_form);
  286. $edit_content_zone.find('.cancel.button').click(function () {
  287. $render_content.show();
  288. $edit_content_zone.hide();
  289. });
  290. $edit_content_zone.find('.save.button').click(function () {
  291. $render_content.show();
  292. $edit_content_zone.hide();
  293. $.post($edit_content_zone.data('update-url'), {
  294. "_csrf": csrf,
  295. "content": $textarea.val(),
  296. "context": $edit_content_zone.data('context')
  297. },
  298. function (data) {
  299. if (data.length == 0) {
  300. $render_content.html($('#no-content').html());
  301. } else {
  302. $render_content.html(data.content);
  303. emojify.run($render_content[0]);
  304. }
  305. });
  306. });
  307. } else {
  308. $textarea = $segment.find('textarea');
  309. }
  310. // Show write/preview tab and copy raw content as needed
  311. $edit_content_zone.show();
  312. $render_content.hide();
  313. if ($textarea.val().length == 0) {
  314. $textarea.val($raw_content.text());
  315. }
  316. $textarea.focus();
  317. return false;
  318. });
  319. // Change status
  320. var $status_btn = $('#status-button');
  321. $('#content').keyup(function () {
  322. if ($(this).val().length == 0) {
  323. $status_btn.text($status_btn.data('status'))
  324. } else {
  325. $status_btn.text($status_btn.data('status-and-comment'))
  326. }
  327. });
  328. $status_btn.click(function () {
  329. $('#status').val($status_btn.data('status-val'));
  330. $('#comment-form').submit();
  331. })
  332. }
  333. // Diff
  334. if ($('.repository.diff').length > 0) {
  335. var $counter = $('.diff-counter');
  336. if ($counter.length < 1) {
  337. return;
  338. }
  339. $counter.each(function (i, item) {
  340. var $item = $(item);
  341. var addLine = $item.find('span[data-line].add').data("line");
  342. var delLine = $item.find('span[data-line].del').data("line");
  343. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  344. $item.find(".bar .add").css("width", addPercent + "%");
  345. });
  346. }
  347. // Pull request
  348. if ($('.repository.compare.pull').length > 0) {
  349. var $branch_dropdown = $('.choose.branch .dropdown');
  350. $branch_dropdown.dropdown({
  351. fullTextSearch: true,
  352. onChange: function (text, value, $choice) {
  353. window.location.href = $choice.data('url');
  354. },
  355. message: {noResults: $branch_dropdown.data('no-results')}
  356. });
  357. }
  358. }
  359. function initOrganization() {
  360. if ($('.organization').length == 0) {
  361. return;
  362. }
  363. // Options
  364. if ($('.organization.settings.options').length > 0) {
  365. $('#org_name').keyup(function () {
  366. var $prompt_span = $('#org-name-change-prompt');
  367. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  368. $prompt_span.show();
  369. } else {
  370. $prompt_span.hide();
  371. }
  372. });
  373. }
  374. }
  375. function initUser() {
  376. if ($('.user').length == 0) {
  377. return;
  378. }
  379. // Options
  380. if ($('.user.settings.profile').length > 0) {
  381. $('#username').keyup(function () {
  382. var $prompt_span = $('#name-change-prompt');
  383. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  384. $prompt_span.show();
  385. } else {
  386. $prompt_span.hide();
  387. }
  388. });
  389. }
  390. }
  391. function initWebhook() {
  392. if ($('.new.webhook').length == 0) {
  393. return;
  394. }
  395. $('.events.checkbox input').change(function () {
  396. if ($(this).is(':checked')) {
  397. $('.events.fields').show();
  398. }
  399. });
  400. $('.non-events.checkbox input').change(function () {
  401. if ($(this).is(':checked')) {
  402. $('.events.fields').hide();
  403. }
  404. });
  405. }
  406. function initAdmin() {
  407. if ($('.admin').length == 0) {
  408. return;
  409. }
  410. // New user
  411. if ($('.admin.new.user').length > 0) {
  412. $('#login_type').change(function () {
  413. if ($(this).val().substring(0, 1) == '0') {
  414. $('#login_name').removeAttr('required');
  415. $('#password').attr('required', 'required');
  416. $('.non-local').hide();
  417. $('.local').show();
  418. $('#user_name').focus();
  419. } else {
  420. $('#login_name').attr('required', 'required');
  421. $('#password').removeAttr('required');
  422. $('.non-local').show();
  423. $('.local').hide();
  424. $('#login_name').focus();
  425. }
  426. });
  427. }
  428. // New authentication
  429. if ($('.admin.new.authentication').length > 0) {
  430. $('#auth_type').change(function () {
  431. $('.ldap').hide();
  432. $('.dldap').hide();
  433. $('.smtp').hide();
  434. $('.pam').hide();
  435. var auth_type = $(this).val();
  436. switch (auth_type) {
  437. case '2': // LDAP
  438. $('.ldap').show();
  439. break;
  440. case '3': // SMTP
  441. $('.smtp').show();
  442. break;
  443. case '4': // PAM
  444. $('.pam').show();
  445. break;
  446. case '5': // LDAP
  447. $('.dldap').show();
  448. break;
  449. }
  450. });
  451. }
  452. }
  453. $(document).ready(function () {
  454. csrf = $('meta[name=_csrf]').attr("content");
  455. suburl = $('meta[name=_suburl]').attr("content");
  456. // Show exact time
  457. $('.time-since').each(function () {
  458. $(this).addClass('poping up').
  459. attr('data-content', $(this).attr('title')).
  460. attr('data-variation', 'inverted tiny').
  461. attr('title', '');
  462. });
  463. // Semantic UI modules.
  464. $('.dropdown').dropdown();
  465. $('.jump.dropdown').dropdown({
  466. action: 'hide',
  467. onShow: function () {
  468. $('.poping.up').popup('hide');
  469. }
  470. });
  471. $('.slide.up.dropdown').dropdown({
  472. transition: 'slide up'
  473. });
  474. $('.ui.accordion').accordion();
  475. $('.ui.checkbox').checkbox();
  476. $('.ui.progress').progress({
  477. showActivity: false
  478. });
  479. $('.poping.up').popup();
  480. $('.top.menu .poping.up').popup({
  481. onShow: function () {
  482. if ($('.top.menu .menu.transition').hasClass('visible')) {
  483. return false;
  484. }
  485. }
  486. });
  487. $('.tabular.menu .item').tab();
  488. $('.toggle.button').click(function () {
  489. $($(this).data('target')).slideToggle(100);
  490. });
  491. // Highlight JS
  492. if (typeof hljs != 'undefined') {
  493. hljs.initHighlightingOnLoad();
  494. }
  495. // Dropzone
  496. if ($('#dropzone').length > 0) {
  497. // Disable auto discover for all elements:
  498. Dropzone.autoDiscover = false;
  499. var filenameDict = {};
  500. var $dropz = $('#dropzone');
  501. $dropz.dropzone({
  502. url: $dropz.data('upload-url'),
  503. headers: {"X-Csrf-Token": csrf},
  504. maxFiles: $dropz.data('max-file'),
  505. maxFilesize: $dropz.data('max-size'),
  506. acceptedFiles: $dropz.data('accepts'),
  507. addRemoveLinks: true,
  508. dictDefaultMessage: $dropz.data('default-message'),
  509. dictInvalidFileType: $dropz.data('invalid-input-type'),
  510. dictFileTooBig: $dropz.data('file-too-big'),
  511. dictRemoveFile: $dropz.data('remove-file'),
  512. init: function () {
  513. this.on("success", function (file, data) {
  514. filenameDict[file.name] = data.uuid;
  515. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  516. });
  517. this.on("removedfile", function (file) {
  518. if (file.name in filenameDict) {
  519. $('#' + filenameDict[file.name]).remove();
  520. }
  521. })
  522. }
  523. });
  524. }
  525. // Emojify
  526. emojify.setConfig({
  527. img_dir: suburl + '/img/emoji'
  528. });
  529. $('.emojify').each(function () {
  530. emojify.run($(this)[0]);
  531. });
  532. // Helpers.
  533. $('.delete-button').click(function () {
  534. var $this = $(this);
  535. $('.delete.modal').modal({
  536. closable: false,
  537. onApprove: function () {
  538. if ($this.data('type') == "form") {
  539. $($this.data('form')).submit();
  540. return;
  541. }
  542. $.post($this.data('url'), {
  543. "_csrf": csrf,
  544. "id": $this.data("id")
  545. }).done(function (data) {
  546. window.location.href = data.redirect;
  547. });
  548. }
  549. }).modal('show');
  550. return false;
  551. });
  552. $('.show-panel.button').click(function () {
  553. $($(this).data('panel')).show();
  554. });
  555. $('.show-modal.button').click(function () {
  556. $($(this).data('modal')).modal('show');
  557. });
  558. initCommentForm();
  559. initInstall();
  560. initRepository();
  561. initOrganization();
  562. initUser();
  563. initWebhook();
  564. initAdmin();
  565. });