gogs.js 17 KB

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