gogs.js 19 KB

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