You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gogs.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. // Issues
  205. if ($('.repository.view.issue').length > 0) {
  206. var $status_btn = $('#status-button');
  207. $('#content').keyup(function () {
  208. if ($(this).val().length == 0) {
  209. $status_btn.text($status_btn.data('status'))
  210. } else {
  211. $status_btn.text($status_btn.data('status-and-comment'))
  212. }
  213. });
  214. $status_btn.click(function () {
  215. $('#status').val($status_btn.data('status-val'));
  216. $('#comment-form').submit();
  217. })
  218. }
  219. // Pull request
  220. if ($('.repository.compare.pull').length > 0) {
  221. var $branch_dropdown = $('.choose.branch .dropdown')
  222. $branch_dropdown.dropdown({
  223. fullTextSearch: true,
  224. onChange: function (text, value, $choice) {
  225. window.location.href = $choice.data('url');
  226. },
  227. message: {noResults: $branch_dropdown.data('no-results')}
  228. });
  229. }
  230. };
  231. $(document).ready(function () {
  232. csrf = $('meta[name=_csrf]').attr("content");
  233. // Show exact time
  234. $('.time-since').each(function () {
  235. $(this).addClass('poping up').
  236. attr('data-content', $(this).attr('title')).
  237. attr('data-variation', 'inverted tiny').
  238. attr('title', '');
  239. });
  240. // Semantic UI modules.
  241. $('.dropdown').dropdown();
  242. $('.jump.dropdown').dropdown({
  243. action: 'hide',
  244. onShow: function () {
  245. $('.poping.up').popup('hide');
  246. }
  247. });
  248. $('.slide.up.dropdown').dropdown({
  249. transition: 'slide up'
  250. });
  251. $('.ui.accordion').accordion();
  252. $('.ui.checkbox').checkbox();
  253. $('.ui.progress').progress({
  254. showActivity: false
  255. });
  256. $('.poping.up').popup();
  257. $('.top.menu .poping.up').popup({
  258. onShow: function () {
  259. if ($('.top.menu .menu.transition').hasClass('visible')) {
  260. return false;
  261. }
  262. }
  263. });
  264. // Dropzone
  265. if ($('#dropzone').length > 0) {
  266. // Disable auto discover for all elements:
  267. Dropzone.autoDiscover = false;
  268. var filenameDict = {};
  269. var $dropz = $('#dropzone');
  270. $dropz.dropzone({
  271. url: $dropz.data('upload-url'),
  272. headers: {"X-Csrf-Token": csrf},
  273. maxFiles: $dropz.data('max-file'),
  274. maxFilesize: $dropz.data('max-size'),
  275. acceptedFiles: $dropz.data('accepts'),
  276. addRemoveLinks: true,
  277. dictDefaultMessage: $dropz.data('default-message'),
  278. dictInvalidFileType: $dropz.data('invalid-input-type'),
  279. dictFileTooBig: $dropz.data('file-too-big'),
  280. dictRemoveFile: $dropz.data('remove-file'),
  281. init: function () {
  282. this.on("success", function (file, data) {
  283. filenameDict[file.name] = data.uuid;
  284. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  285. })
  286. this.on("removedfile", function (file) {
  287. if (file.name in filenameDict) {
  288. $('#' + filenameDict[file.name]).remove();
  289. }
  290. })
  291. }
  292. });
  293. }
  294. // Helpers.
  295. $('.delete-button').click(function () {
  296. var $this = $(this);
  297. $('.delete.modal').modal({
  298. closable: false,
  299. onApprove: function () {
  300. if ($this.data('type') == "form") {
  301. $($this.data('form')).submit();
  302. return;
  303. }
  304. $.post($this.data('url'), {
  305. "_csrf": csrf,
  306. "id": $this.data("id")
  307. }).done(function (data) {
  308. window.location.href = data.redirect;
  309. });
  310. }
  311. }).modal('show');
  312. return false;
  313. });
  314. $('.show-panel.button').click(function () {
  315. $($(this).data('panel')).show();
  316. });
  317. initCommentForm();
  318. initInstall();
  319. initRepository();
  320. });