gogs.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. return;
  349. }
  350. $counter.each(function (i, item) {
  351. var $item = $(item);
  352. var addLine = $item.find('span[data-line].add').data("line");
  353. var delLine = $item.find('span[data-line].del').data("line");
  354. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  355. $item.find(".bar .add").css("width", addPercent + "%");
  356. });
  357. }
  358. // Pull request
  359. if ($('.repository.compare.pull').length > 0) {
  360. var $branch_dropdown = $('.choose.branch .dropdown');
  361. $branch_dropdown.dropdown({
  362. fullTextSearch: true,
  363. onChange: function (text, value, $choice) {
  364. window.location.href = $choice.data('url');
  365. },
  366. message: {noResults: $branch_dropdown.data('no-results')}
  367. });
  368. }
  369. }
  370. function initOrganization() {
  371. if ($('.organization').length == 0) {
  372. return;
  373. }
  374. // Options
  375. if ($('.organization.settings.options').length > 0) {
  376. $('#org_name').keyup(function () {
  377. var $prompt_span = $('#org-name-change-prompt');
  378. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  379. $prompt_span.show();
  380. } else {
  381. $prompt_span.hide();
  382. }
  383. });
  384. }
  385. }
  386. function initUser() {
  387. if ($('.user').length == 0) {
  388. return;
  389. }
  390. // Options
  391. if ($('.user.settings.profile').length > 0) {
  392. $('#username').keyup(function () {
  393. var $prompt_span = $('#name-change-prompt');
  394. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  395. $prompt_span.show();
  396. } else {
  397. $prompt_span.hide();
  398. }
  399. });
  400. }
  401. }
  402. function initWebhook() {
  403. if ($('.new.webhook').length == 0) {
  404. return;
  405. }
  406. $('.events.checkbox input').change(function () {
  407. if ($(this).is(':checked')) {
  408. $('.events.fields').show();
  409. }
  410. });
  411. $('.non-events.checkbox input').change(function () {
  412. if ($(this).is(':checked')) {
  413. $('.events.fields').hide();
  414. }
  415. });
  416. }
  417. function initAdmin() {
  418. if ($('.admin').length == 0) {
  419. return;
  420. }
  421. // New user
  422. if ($('.admin.new.user').length > 0 ||
  423. $('.admin.edit.user').length > 0) {
  424. $('#login_type').change(function () {
  425. if ($(this).val().substring(0, 1) == '0') {
  426. $('#login_name').removeAttr('required');
  427. $('.non-local').hide();
  428. $('.local').show();
  429. $('#user_name').focus();
  430. if($(this).data('password')=="required"){
  431. $('#password').attr('required', 'required');
  432. }
  433. } else {
  434. $('#login_name').attr('required', 'required');
  435. $('.non-local').show();
  436. $('.local').hide();
  437. $('#login_name').focus();
  438. $('#password').removeAttr('required');
  439. }
  440. });
  441. }
  442. // New authentication
  443. if ($('.admin.new.authentication').length > 0) {
  444. $('#auth_type').change(function () {
  445. $('.ldap').hide();
  446. $('.dldap').hide();
  447. $('.smtp').hide();
  448. $('.pam').hide();
  449. var auth_type = $(this).val();
  450. switch (auth_type) {
  451. case '2': // LDAP
  452. $('.ldap').show();
  453. break;
  454. case '3': // SMTP
  455. $('.smtp').show();
  456. break;
  457. case '4': // PAM
  458. $('.pam').show();
  459. break;
  460. case '5': // LDAP
  461. $('.dldap').show();
  462. break;
  463. }
  464. });
  465. }
  466. }
  467. $(document).ready(function () {
  468. csrf = $('meta[name=_csrf]').attr("content");
  469. suburl = $('meta[name=_suburl]').attr("content");
  470. // Show exact time
  471. $('.time-since').each(function () {
  472. $(this).addClass('poping up').
  473. attr('data-content', $(this).attr('title')).
  474. attr('data-variation', 'inverted tiny').
  475. attr('title', '');
  476. });
  477. // Semantic UI modules.
  478. $('.dropdown').dropdown();
  479. $('.jump.dropdown').dropdown({
  480. action: 'hide',
  481. onShow: function () {
  482. $('.poping.up').popup('hide');
  483. }
  484. });
  485. $('.slide.up.dropdown').dropdown({
  486. transition: 'slide up'
  487. });
  488. $('.ui.accordion').accordion();
  489. $('.ui.checkbox').checkbox();
  490. $('.ui.progress').progress({
  491. showActivity: false
  492. });
  493. $('.poping.up').popup();
  494. $('.top.menu .poping.up').popup({
  495. onShow: function () {
  496. if ($('.top.menu .menu.transition').hasClass('visible')) {
  497. return false;
  498. }
  499. }
  500. });
  501. $('.tabular.menu .item').tab();
  502. $('.toggle.button').click(function () {
  503. $($(this).data('target')).slideToggle(100);
  504. });
  505. // Highlight JS
  506. if (typeof hljs != 'undefined') {
  507. hljs.initHighlightingOnLoad();
  508. }
  509. // Dropzone
  510. if ($('#dropzone').length > 0) {
  511. // Disable auto discover for all elements:
  512. Dropzone.autoDiscover = false;
  513. var filenameDict = {};
  514. var $dropz = $('#dropzone');
  515. $dropz.dropzone({
  516. url: $dropz.data('upload-url'),
  517. headers: {"X-Csrf-Token": csrf},
  518. maxFiles: $dropz.data('max-file'),
  519. maxFilesize: $dropz.data('max-size'),
  520. acceptedFiles: $dropz.data('accepts'),
  521. addRemoveLinks: true,
  522. dictDefaultMessage: $dropz.data('default-message'),
  523. dictInvalidFileType: $dropz.data('invalid-input-type'),
  524. dictFileTooBig: $dropz.data('file-too-big'),
  525. dictRemoveFile: $dropz.data('remove-file'),
  526. init: function () {
  527. this.on("success", function (file, data) {
  528. filenameDict[file.name] = data.uuid;
  529. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  530. });
  531. this.on("removedfile", function (file) {
  532. if (file.name in filenameDict) {
  533. $('#' + filenameDict[file.name]).remove();
  534. }
  535. })
  536. }
  537. });
  538. }
  539. // Emojify
  540. emojify.setConfig({
  541. img_dir: suburl + '/img/emoji'
  542. });
  543. $('.emojify').each(function () {
  544. emojify.run($(this)[0]);
  545. });
  546. // Helpers.
  547. $('.delete-button').click(function () {
  548. var $this = $(this);
  549. $('.delete.modal').modal({
  550. closable: false,
  551. onApprove: function () {
  552. if ($this.data('type') == "form") {
  553. $($this.data('form')).submit();
  554. return;
  555. }
  556. $.post($this.data('url'), {
  557. "_csrf": csrf,
  558. "id": $this.data("id")
  559. }).done(function (data) {
  560. window.location.href = data.redirect;
  561. });
  562. }
  563. }).modal('show');
  564. return false;
  565. });
  566. $('.show-panel.button').click(function () {
  567. $($(this).data('panel')).show();
  568. });
  569. $('.show-modal.button').click(function () {
  570. $($(this).data('modal')).modal('show');
  571. });
  572. initCommentForm();
  573. initInstall();
  574. initRepository();
  575. initOrganization();
  576. initUser();
  577. initWebhook();
  578. initAdmin();
  579. });