gogs.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. 'use strict';
  2. var csrf;
  3. function initCommentPreviewTab($form) {
  4. var $tab_menu = $form.find('.tabular.menu');
  5. $tab_menu.find('.item').tab();
  6. $tab_menu.find('.item[data-tab="' + $tab_menu.data('preview') + '"]').click(function () {
  7. var $this = $(this);
  8. $.post($this.data('url'), {
  9. "_csrf": csrf,
  10. "mode": "gfm",
  11. "context": $this.data('context'),
  12. "text": $form.find('.tab.segment[data-tab="' + $tab_menu.data('write') + '"] textarea').val()
  13. },
  14. function (data) {
  15. $form.find('.tab.segment[data-tab="' + $tab_menu.data('preview') + '"]').html(data);
  16. }
  17. );
  18. });
  19. }
  20. function initCommentForm() {
  21. if ($('.comment.form').length == 0) {
  22. return
  23. }
  24. initCommentPreviewTab($('.comment.form'));
  25. // Labels
  26. var $list = $('.ui.labels.list');
  27. var $no_select = $list.find('.no-select');
  28. var $label_menu = $('.select-label .menu');
  29. var has_label_update_action = $label_menu.data('action') == 'update';
  30. function updateIssueMeta(url, action, id) {
  31. $.post(url, {
  32. "_csrf": csrf,
  33. "action": action,
  34. "id": id
  35. });
  36. }
  37. $label_menu.find('.item:not(.no-select)').click(function () {
  38. if ($(this).hasClass('checked')) {
  39. $(this).removeClass('checked')
  40. $(this).find('.octicon').removeClass('octicon-check')
  41. if (has_label_update_action) {
  42. updateIssueMeta($label_menu.data('update-url'), "detach", $(this).data('id'));
  43. }
  44. } else {
  45. $(this).addClass('checked')
  46. $(this).find('.octicon').addClass('octicon-check')
  47. if (has_label_update_action) {
  48. updateIssueMeta($label_menu.data('update-url'), "attach", $(this).data('id'));
  49. }
  50. }
  51. var label_ids = "";
  52. $(this).parent().find('.item').each(function () {
  53. if ($(this).hasClass('checked')) {
  54. label_ids += $(this).data('id') + ",";
  55. $($(this).data('id-selector')).removeClass('hide');
  56. } else {
  57. $($(this).data('id-selector')).addClass('hide');
  58. }
  59. });
  60. if (label_ids.length == 0) {
  61. $no_select.removeClass('hide');
  62. } else {
  63. $no_select.addClass('hide');
  64. }
  65. $($(this).parent().data('id')).val(label_ids);
  66. return false;
  67. });
  68. $label_menu.find('.no-select.item').click(function () {
  69. if (has_label_update_action) {
  70. updateIssueMeta($label_menu.data('update-url'), "clear", '');
  71. }
  72. $(this).parent().find('.item').each(function () {
  73. $(this).removeClass('checked');
  74. $(this).find('.octicon').removeClass('octicon-check');
  75. });
  76. $list.find('.item').each(function () {
  77. $(this).addClass('hide');
  78. });
  79. $no_select.removeClass('hide');
  80. $($(this).parent().data('id')).val('');
  81. });
  82. function selectItem(select_id, input_id) {
  83. var $menu = $(select_id + ' .menu');
  84. var $list = $('.ui' + select_id + '.list')
  85. var has_update_action = $menu.data('action') == 'update';
  86. $menu.find('.item:not(.no-select)').click(function () {
  87. $(this).parent().find('.item').each(function () {
  88. $(this).removeClass('selected active')
  89. });
  90. $(this).addClass('selected active');
  91. if (has_update_action) {
  92. updateIssueMeta($menu.data('update-url'), '', $(this).data('id'));
  93. }
  94. switch (input_id) {
  95. case '#milestone_id':
  96. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  97. $(this).text() + '</a>');
  98. break;
  99. case '#assignee_id':
  100. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  101. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  102. $(this).text() + '</a>');
  103. }
  104. $('.ui' + select_id + '.list .no-select').addClass('hide');
  105. $(input_id).val($(this).data('id'));
  106. });
  107. $menu.find('.no-select.item').click(function () {
  108. $(this).parent().find('.item:not(.no-select)').each(function () {
  109. $(this).removeClass('selected active')
  110. });
  111. if (has_update_action) {
  112. updateIssueMeta($menu.data('update-url'), '', '');
  113. }
  114. $list.find('.selected').html('');
  115. $list.find('.no-select').removeClass('hide');
  116. $(input_id).val('');
  117. });
  118. }
  119. // Milestone and assignee
  120. selectItem('.select-milestone', '#milestone_id');
  121. selectItem('.select-assignee', '#assignee_id');
  122. }
  123. function initInstall() {
  124. if ($('.install').length == 0) {
  125. return;
  126. }
  127. // Database type change detection.
  128. $("#db_type").change(function () {
  129. var db_type = $('#db_type').val();
  130. if (db_type === "SQLite3") {
  131. $('#sql_settings').hide();
  132. $('#pgsql_settings').hide();
  133. $('#sqlite_settings').show();
  134. return;
  135. }
  136. var mysql_default = '127.0.0.1:3306';
  137. var postgres_default = '127.0.0.1:5432';
  138. $('#sqlite_settings').hide();
  139. $('#sql_settings').show();
  140. if (db_type === "PostgreSQL") {
  141. $('#pgsql_settings').show();
  142. if ($('#db_host').val() == mysql_default) {
  143. $('#db_host').val(postgres_default);
  144. }
  145. } else {
  146. $('#pgsql_settings').hide();
  147. if ($('#db_host').val() == postgres_default) {
  148. $('#db_host').val(mysql_default);
  149. }
  150. }
  151. });
  152. $('#offline-mode input').change(function () {
  153. if ($(this).is(':checked')) {
  154. $('#disable-gravatar').checkbox('check');
  155. }
  156. });
  157. };
  158. function initRepository() {
  159. if ($('.repository').length == 0) {
  160. return;
  161. }
  162. // Options
  163. if ($('.repository.settings.options').length > 0) {
  164. $('#repo_name').keyup(function () {
  165. var $prompt_span = $('#repo-name-change-prompt');
  166. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  167. $prompt_span.show();
  168. } else {
  169. $prompt_span.hide();
  170. }
  171. });
  172. }
  173. // Labels
  174. if ($('.repository.labels').length > 0) {
  175. // Create label
  176. var $new_label_panel = $('.new-label.segment');
  177. $('.new-label.button').click(function () {
  178. $new_label_panel.show();
  179. });
  180. $('.new-label.segment .cancel').click(function () {
  181. $new_label_panel.hide();
  182. });
  183. $('.color-picker').each(function () {
  184. $(this).minicolors();
  185. });
  186. $('.precolors .color').click(function () {
  187. var color_hex = $(this).data('color-hex')
  188. $('.color-picker').val(color_hex);
  189. $('.minicolors-swatch-color').css("background-color", color_hex);
  190. });
  191. $('.edit-label-button').click(function () {
  192. $('#label-modal-id').val($(this).data('id'));
  193. $('.edit-label .new-label-input').val($(this).data('title'));
  194. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  195. $('.edit-label.modal').modal({
  196. onApprove: function () {
  197. $('.edit-label.form').submit();
  198. }
  199. }).modal('show');
  200. return false;
  201. });
  202. }
  203. // Milestones
  204. if ($('.repository.milestones').length > 0) {
  205. }
  206. if ($('.repository.new.milestone').length > 0) {
  207. var $datepicker = $('.milestone.datepicker')
  208. $datepicker.datetimepicker({
  209. lang: $datepicker.data('lang'),
  210. inline: true,
  211. timepicker: false,
  212. startDate: $datepicker.data('start-date'),
  213. formatDate: 'Y-m-d',
  214. onSelectDate: function (ct) {
  215. $('#deadline').val(ct.dateFormat('Y-m-d'));
  216. }
  217. });
  218. $('#clear-date').click(function () {
  219. $('#deadline').val('');
  220. return false;
  221. });
  222. }
  223. // Issues
  224. if ($('.repository.view.issue').length > 0) {
  225. // Edit issue title
  226. var $issue_title = $('#issue-title');
  227. var $edit_input = $('#edit-title-input input');
  228. var editTitleToggle = function () {
  229. $issue_title.toggle();
  230. $('.not-in-edit').toggle();
  231. $('#edit-title-input').toggle();
  232. $('.in-edit').toggle();
  233. $edit_input.focus();
  234. return false;
  235. }
  236. $('#edit-title').click(editTitleToggle);
  237. $('#cancel-edit-title').click(editTitleToggle);
  238. $('#save-edit-title').click(editTitleToggle).
  239. click(function () {
  240. if ($edit_input.val().length == 0 ||
  241. $edit_input.val() == $issue_title.text()) {
  242. $edit_input.val($issue_title.text());
  243. return false;
  244. }
  245. $.post($(this).data('update-url'), {
  246. "_csrf": csrf,
  247. "title": $edit_input.val()
  248. },
  249. function (data) {
  250. $edit_input.val(data.title);
  251. $issue_title.text(data.title);
  252. });
  253. return false;
  254. });
  255. // Edit issue or comment content
  256. $('.edit-content').click(function () {
  257. var $segment = $(this).parent().parent().next();
  258. var $edit_content_zone = $segment.find('.edit-content-zone');
  259. var $render_content = $segment.find('.render-content');
  260. var $raw_content = $segment.find('.raw-content');
  261. var $textarea;
  262. // Setup new form
  263. if ($edit_content_zone.html().length == 0) {
  264. $edit_content_zone.html($('#edit-content-form').html());
  265. $textarea = $segment.find('textarea');
  266. // Give new write/preview data-tab name to distinguish from others
  267. var $edit_content_form = $edit_content_zone.find('.ui.comment.form');
  268. var $tabular_menu = $edit_content_form.find('.tabular.menu');
  269. $tabular_menu.attr('data-write', $edit_content_zone.data('write'));
  270. $tabular_menu.attr('data-preview', $edit_content_zone.data('preview'));
  271. $tabular_menu.find('.write.item').attr('data-tab', $edit_content_zone.data('write'));
  272. $tabular_menu.find('.preview.item').attr('data-tab', $edit_content_zone.data('preview'));
  273. $edit_content_form.find('.write.segment').attr('data-tab', $edit_content_zone.data('write'));
  274. $edit_content_form.find('.preview.segment').attr('data-tab', $edit_content_zone.data('preview'));
  275. initCommentPreviewTab($edit_content_form);
  276. $edit_content_zone.find('.cancel.button').click(function () {
  277. $render_content.show();
  278. $edit_content_zone.hide();
  279. });
  280. $edit_content_zone.find('.save.button').click(function () {
  281. $render_content.show();
  282. $edit_content_zone.hide();
  283. $.post($edit_content_zone.data('update-url'), {
  284. "_csrf": csrf,
  285. "content": $textarea.val(),
  286. "context": $edit_content_zone.data('context')
  287. },
  288. function (data) {
  289. if (data.length == 0) {
  290. $render_content.html($('#no-content').html());
  291. } else {
  292. $render_content.html(data.content);
  293. }
  294. });
  295. });
  296. } else {
  297. $textarea = $segment.find('textarea');
  298. }
  299. // Show write/preview tab and copy raw content as needed
  300. $edit_content_zone.show();
  301. $render_content.hide();
  302. if ($textarea.val().length == 0) {
  303. $textarea.val($raw_content.text());
  304. }
  305. $textarea.focus();
  306. return false;
  307. });
  308. // Change status
  309. var $status_btn = $('#status-button');
  310. $('#content').keyup(function () {
  311. if ($(this).val().length == 0) {
  312. $status_btn.text($status_btn.data('status'))
  313. } else {
  314. $status_btn.text($status_btn.data('status-and-comment'))
  315. }
  316. });
  317. $status_btn.click(function () {
  318. $('#status').val($status_btn.data('status-val'));
  319. $('#comment-form').submit();
  320. })
  321. }
  322. // Diff
  323. if ($('.repository.diff').length > 0) {
  324. var $counter = $('.diff-counter');
  325. if ($counter.length < 1) {
  326. return;
  327. }
  328. $counter.each(function (i, item) {
  329. var $item = $(item);
  330. var addLine = $item.find('span[data-line].add').data("line");
  331. var delLine = $item.find('span[data-line].del').data("line");
  332. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  333. $item.find(".bar .add").css("width", addPercent + "%");
  334. });
  335. }
  336. // Pull request
  337. if ($('.repository.compare.pull').length > 0) {
  338. var $branch_dropdown = $('.choose.branch .dropdown')
  339. $branch_dropdown.dropdown({
  340. fullTextSearch: true,
  341. onChange: function (text, value, $choice) {
  342. window.location.href = $choice.data('url');
  343. },
  344. message: {noResults: $branch_dropdown.data('no-results')}
  345. });
  346. }
  347. };
  348. function initWebhook() {
  349. if ($('.new.webhook').length == 0) {
  350. return;
  351. }
  352. $('.events.checkbox input').change(function () {
  353. if ($(this).is(':checked')) {
  354. $('.events.fields').show();
  355. }
  356. });
  357. $('.non-events.checkbox input').change(function () {
  358. if ($(this).is(':checked')) {
  359. $('.events.fields').hide();
  360. }
  361. });
  362. }
  363. $(document).ready(function () {
  364. csrf = $('meta[name=_csrf]').attr("content");
  365. // Show exact time
  366. $('.time-since').each(function () {
  367. $(this).addClass('poping up').
  368. attr('data-content', $(this).attr('title')).
  369. attr('data-variation', 'inverted tiny').
  370. attr('title', '');
  371. });
  372. // Semantic UI modules.
  373. $('.dropdown').dropdown();
  374. $('.jump.dropdown').dropdown({
  375. action: 'hide',
  376. onShow: function () {
  377. $('.poping.up').popup('hide');
  378. }
  379. });
  380. $('.slide.up.dropdown').dropdown({
  381. transition: 'slide up'
  382. });
  383. $('.ui.accordion').accordion();
  384. $('.ui.checkbox').checkbox();
  385. $('.ui.progress').progress({
  386. showActivity: false
  387. });
  388. $('.poping.up').popup();
  389. $('.top.menu .poping.up').popup({
  390. onShow: function () {
  391. if ($('.top.menu .menu.transition').hasClass('visible')) {
  392. return false;
  393. }
  394. }
  395. });
  396. $('.tabular.menu .item').tab();
  397. $('.toggle.button').click(function () {
  398. $($(this).data('target')).slideToggle(100);
  399. });
  400. // Highlight JS
  401. if (typeof hljs != 'undefined') {
  402. hljs.initHighlightingOnLoad();
  403. }
  404. // Dropzone
  405. if ($('#dropzone').length > 0) {
  406. // Disable auto discover for all elements:
  407. Dropzone.autoDiscover = false;
  408. var filenameDict = {};
  409. var $dropz = $('#dropzone');
  410. $dropz.dropzone({
  411. url: $dropz.data('upload-url'),
  412. headers: {"X-Csrf-Token": csrf},
  413. maxFiles: $dropz.data('max-file'),
  414. maxFilesize: $dropz.data('max-size'),
  415. acceptedFiles: $dropz.data('accepts'),
  416. addRemoveLinks: true,
  417. dictDefaultMessage: $dropz.data('default-message'),
  418. dictInvalidFileType: $dropz.data('invalid-input-type'),
  419. dictFileTooBig: $dropz.data('file-too-big'),
  420. dictRemoveFile: $dropz.data('remove-file'),
  421. init: function () {
  422. this.on("success", function (file, data) {
  423. filenameDict[file.name] = data.uuid;
  424. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  425. })
  426. this.on("removedfile", function (file) {
  427. if (file.name in filenameDict) {
  428. $('#' + filenameDict[file.name]).remove();
  429. }
  430. })
  431. }
  432. });
  433. }
  434. // Helpers.
  435. $('.delete-button').click(function () {
  436. var $this = $(this);
  437. $('.delete.modal').modal({
  438. closable: false,
  439. onApprove: function () {
  440. if ($this.data('type') == "form") {
  441. $($this.data('form')).submit();
  442. return;
  443. }
  444. $.post($this.data('url'), {
  445. "_csrf": csrf,
  446. "id": $this.data("id")
  447. }).done(function (data) {
  448. window.location.href = data.redirect;
  449. });
  450. }
  451. }).modal('show');
  452. return false;
  453. });
  454. $('.show-panel.button').click(function () {
  455. $($(this).data('panel')).show();
  456. });
  457. $('.show-modal.button').click(function () {
  458. $($(this).data('modal')).modal('show');
  459. });
  460. initCommentForm();
  461. initInstall();
  462. initRepository();
  463. initWebhook();
  464. });