gogs.js 17 KB

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