gogs.js 12 KB

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