gogs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. 'use strict';
  2. var csrf;
  3. function initCommentForm() {
  4. if ($('.comment.form').length == 0) {
  5. return
  6. }
  7. var $form = $('.comment.form');
  8. $form.find('.tabular.menu .item').tab();
  9. $form.find('.tabular.menu .item[data-tab="preview"]').click(function () {
  10. var $this = $(this);
  11. $.post($this.data('url'), {
  12. "_csrf": csrf,
  13. "mode": "gfm",
  14. "context": $this.data('context'),
  15. "text": $form.find('.tab.segment[data-tab="write"] textarea').val()
  16. },
  17. function (data) {
  18. $form.find('.tab.segment[data-tab="preview"]').html(data);
  19. }
  20. );
  21. });
  22. // Labels
  23. var $list = $('.ui.labels.list');
  24. var $no_select = $list.find('.no-select');
  25. var $label_menu = $('.select-label .menu');
  26. var has_label_update_action = $label_menu.data('action') == 'update';
  27. function updateIssueMeta(url, action, id) {
  28. $.post(url, {
  29. "_csrf": csrf,
  30. "action": action,
  31. "id": id
  32. });
  33. }
  34. $label_menu.find('.item:not(.no-select)').click(function () {
  35. if ($(this).hasClass('checked')) {
  36. $(this).removeClass('checked')
  37. $(this).find('.octicon').removeClass('octicon-check')
  38. if (has_label_update_action) {
  39. updateIssueMeta($label_menu.data('update-url'), "detach", $(this).data('id'));
  40. }
  41. } else {
  42. $(this).addClass('checked')
  43. $(this).find('.octicon').addClass('octicon-check')
  44. if (has_label_update_action) {
  45. updateIssueMeta($label_menu.data('update-url'), "attach", $(this).data('id'));
  46. }
  47. }
  48. var label_ids = "";
  49. $(this).parent().find('.item').each(function () {
  50. if ($(this).hasClass('checked')) {
  51. label_ids += $(this).data('id') + ",";
  52. $($(this).data('id-selector')).removeClass('hide');
  53. } else {
  54. $($(this).data('id-selector')).addClass('hide');
  55. }
  56. });
  57. if (label_ids.length == 0) {
  58. $no_select.removeClass('hide');
  59. } else {
  60. $no_select.addClass('hide');
  61. }
  62. $($(this).parent().data('id')).val(label_ids);
  63. return false;
  64. });
  65. $label_menu.find('.no-select.item').click(function () {
  66. if (has_label_update_action) {
  67. updateIssueMeta($label_menu.data('update-url'), "clear", '');
  68. }
  69. $(this).parent().find('.item').each(function () {
  70. $(this).removeClass('checked');
  71. $(this).find('.octicon').removeClass('octicon-check');
  72. });
  73. $list.find('.item').each(function () {
  74. $(this).addClass('hide');
  75. });
  76. $no_select.removeClass('hide');
  77. $($(this).parent().data('id')).val('');
  78. });
  79. function selectItem(select_id, input_id) {
  80. var $menu = $(select_id + ' .menu');
  81. var $list = $('.ui' + select_id + '.list')
  82. var has_update_action = $menu.data('action') == 'update';
  83. $menu.find('.item:not(.no-select)').click(function () {
  84. $(this).parent().find('.item').each(function () {
  85. $(this).removeClass('selected active')
  86. });
  87. $(this).addClass('selected active');
  88. if (has_update_action) {
  89. updateIssueMeta($menu.data('update-url'), '', $(this).data('id'));
  90. }
  91. switch (input_id) {
  92. case '#milestone_id':
  93. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  94. $(this).text() + '</a>');
  95. break;
  96. case '#assignee_id':
  97. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  98. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  99. $(this).text() + '</a>');
  100. }
  101. $('.ui' + select_id + '.list .no-select').addClass('hide');
  102. $(input_id).val($(this).data('id'));
  103. });
  104. $menu.find('.no-select.item').click(function () {
  105. $(this).parent().find('.item:not(.no-select)').each(function () {
  106. $(this).removeClass('selected active')
  107. });
  108. if (has_update_action) {
  109. updateIssueMeta($menu.data('update-url'), '', '');
  110. }
  111. $list.find('.selected').html('');
  112. $list.find('.no-select').removeClass('hide');
  113. $(input_id).val('');
  114. });
  115. }
  116. // Milestone and assignee
  117. selectItem('.select-milestone', '#milestone_id');
  118. selectItem('.select-assignee', '#assignee_id');
  119. }
  120. function initInstall() {
  121. if ($('.install').length == 0) {
  122. return;
  123. }
  124. // Database type change detection.
  125. $("#db_type").change(function () {
  126. var db_type = $('#db_type').val();
  127. if (db_type === "SQLite3") {
  128. $('#sql_settings').hide();
  129. $('#pgsql_settings').hide();
  130. $('#sqlite_settings').show();
  131. return;
  132. }
  133. var mysql_default = '127.0.0.1:3306';
  134. var postgres_default = '127.0.0.1:5432';
  135. $('#sqlite_settings').hide();
  136. $('#sql_settings').show();
  137. if (db_type === "PostgreSQL") {
  138. $('#pgsql_settings').show();
  139. if ($('#db_host').val() == mysql_default) {
  140. $('#db_host').val(postgres_default);
  141. }
  142. } else {
  143. $('#pgsql_settings').hide();
  144. if ($('#db_host').val() == postgres_default) {
  145. $('#db_host').val(mysql_default);
  146. }
  147. }
  148. });
  149. };
  150. function initRepository() {
  151. if ($('.repository').length == 0) {
  152. return;
  153. }
  154. // Labels
  155. if ($('.repository.labels').length > 0) {
  156. // Create label
  157. var $new_label_panel = $('.new-label.segment');
  158. $('.new-label.button').click(function () {
  159. $new_label_panel.show();
  160. });
  161. $('.new-label.segment .cancel').click(function () {
  162. $new_label_panel.hide();
  163. });
  164. $('.color-picker').each(function () {
  165. $(this).minicolors();
  166. });
  167. $('.precolors .color').click(function () {
  168. var color_hex = $(this).data('color-hex')
  169. $('.color-picker').val(color_hex);
  170. $('.minicolors-swatch-color').css("background-color", color_hex);
  171. });
  172. $('.edit-label-button').click(function () {
  173. $('#label-modal-id').val($(this).data('id'));
  174. $('.edit-label .new-label-input').val($(this).data('title'));
  175. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  176. $('.edit-label.modal').modal({
  177. onApprove: function () {
  178. $('.edit-label.form').submit();
  179. }
  180. }).modal('show');
  181. return false;
  182. });
  183. }
  184. // Milestones
  185. if ($('.repository.milestones').length > 0) {
  186. }
  187. if ($('.repository.new.milestone').length > 0) {
  188. var $datepicker = $('.milestone.datepicker')
  189. $datepicker.datetimepicker({
  190. lang: $datepicker.data('lang'),
  191. inline: true,
  192. timepicker: false,
  193. startDate: $datepicker.data('start-date'),
  194. formatDate: 'Y-m-d',
  195. onSelectDate: function (ct) {
  196. $('#deadline').val(ct.dateFormat('Y-m-d'));
  197. }
  198. });
  199. $('#clear-date').click(function () {
  200. $('#deadline').val('');
  201. return false;
  202. });
  203. }
  204. // Settings
  205. if ($('.repository.settings').length > 0) {
  206. $('#add-deploy-key').click(function () {
  207. $('#add-deploy-key-panel').show();
  208. });
  209. }
  210. // Issues
  211. if ($('.repository.view.issue').length > 0) {
  212. var $status_btn = $('#status-button');
  213. $('#content').keyup(function () {
  214. if ($(this).val().length == 0) {
  215. $status_btn.text($status_btn.data('status'))
  216. } else {
  217. $status_btn.text($status_btn.data('status-and-comment'))
  218. }
  219. });
  220. $status_btn.click(function () {
  221. $('#status').val($status_btn.data('status-val'));
  222. $('#comment-form').submit();
  223. })
  224. }
  225. // Pull request
  226. if ($('.repository.compare.pull').length > 0) {
  227. var $branch_dropdown = $('.choose.branch .dropdown')
  228. $branch_dropdown.dropdown({
  229. fullTextSearch: true,
  230. onChange: function (text, value, $choice) {
  231. window.location.href = $choice.data('url');
  232. },
  233. message: {noResults: $branch_dropdown.data('no-results')}
  234. });
  235. }
  236. };
  237. $(document).ready(function () {
  238. csrf = $('meta[name=_csrf]').attr("content");
  239. // Show exact time
  240. $('.time-since').each(function () {
  241. $(this).addClass('poping up').
  242. attr('data-content', $(this).attr('title')).
  243. attr('data-variation', 'inverted tiny').
  244. attr('title', '');
  245. });
  246. // Semantic UI modules.
  247. $('.dropdown').dropdown();
  248. $('.jump.dropdown').dropdown({
  249. action: 'hide',
  250. onShow: function () {
  251. $('.poping.up').popup('hide');
  252. }
  253. });
  254. $('.slide.up.dropdown').dropdown({
  255. transition: 'slide up'
  256. });
  257. $('.ui.accordion').accordion();
  258. $('.ui.checkbox').checkbox();
  259. $('.ui.progress').progress({
  260. showActivity: false
  261. });
  262. $('.poping.up').popup();
  263. $('.top.menu .poping.up').popup({
  264. onShow: function () {
  265. if ($('.top.menu .menu.transition').hasClass('visible')) {
  266. return false;
  267. }
  268. }
  269. });
  270. // Dropzone
  271. if ($('#dropzone').length > 0) {
  272. // Disable auto discover for all elements:
  273. Dropzone.autoDiscover = false;
  274. var filenameDict = {};
  275. var $dropz = $('#dropzone');
  276. $dropz.dropzone({
  277. url: $dropz.data('upload-url'),
  278. headers: {"X-Csrf-Token": csrf},
  279. maxFiles: $dropz.data('max-file'),
  280. maxFilesize: $dropz.data('max-size'),
  281. acceptedFiles: $dropz.data('accepts'),
  282. addRemoveLinks: true,
  283. dictDefaultMessage: $dropz.data('default-message'),
  284. dictInvalidFileType: $dropz.data('invalid-input-type'),
  285. dictFileTooBig: $dropz.data('file-too-big'),
  286. dictRemoveFile: $dropz.data('remove-file'),
  287. init: function () {
  288. this.on("success", function (file, data) {
  289. filenameDict[file.name] = data.uuid;
  290. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  291. })
  292. this.on("removedfile", function (file) {
  293. if (file.name in filenameDict) {
  294. $('#' + filenameDict[file.name]).remove();
  295. }
  296. })
  297. }
  298. });
  299. }
  300. // Helpers.
  301. $('.delete-button').click(function () {
  302. var $this = $(this);
  303. $('.delete.modal').modal({
  304. closable: false,
  305. onApprove: function () {
  306. $.post($this.data('url'), {
  307. "_csrf": csrf,
  308. "id": $this.data("id")
  309. }).done(function (data) {
  310. window.location.href = data.redirect;
  311. });
  312. }
  313. }).modal('show');
  314. return false;
  315. });
  316. initCommentForm();
  317. initInstall();
  318. initRepository();
  319. });