gogs.js 18 KB

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