gogs.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. $('.select-label .menu .item:not(.no-select)').click(function () {
  26. if ($(this).hasClass('checked')) {
  27. $(this).removeClass('checked')
  28. $(this).find('.octicon').removeClass('octicon-check')
  29. } else {
  30. $(this).addClass('checked')
  31. $(this).find('.octicon').addClass('octicon-check')
  32. }
  33. var label_ids = "";
  34. $(this).parent().find('.item').each(function () {
  35. if ($(this).hasClass('checked')) {
  36. label_ids += $(this).data('id') + ",";
  37. $($(this).data('id-selector')).removeClass('hide');
  38. } else {
  39. $($(this).data('id-selector')).addClass('hide');
  40. }
  41. });
  42. if (label_ids.length == 0) {
  43. $no_select.removeClass('hide');
  44. } else {
  45. $no_select.addClass('hide');
  46. }
  47. $($(this).parent().data('id')).val(label_ids);
  48. return false;
  49. });
  50. $('.select-label .menu .no-select.item').click(function () {
  51. $(this).parent().find('.item').each(function () {
  52. $(this).removeClass('checked');
  53. $(this).find('.octicon').removeClass('octicon-check');
  54. });
  55. $list.find('.item').each(function () {
  56. $(this).addClass('hide');
  57. });
  58. $no_select.removeClass('hide');
  59. $($(this).parent().data('id')).val('');
  60. });
  61. function selectItem(select_id, input_id) {
  62. var $menu = $(select_id + ' .menu');
  63. var $list = $('.ui' + select_id + '.list')
  64. $menu.find('.item:not(.no-select)').click(function () {
  65. $(this).parent().find('.item').each(function () {
  66. $(this).removeClass('selected active')
  67. });
  68. $(this).addClass('selected active');
  69. switch (input_id) {
  70. case '#milestone_id':
  71. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  72. $(this).text() + '</a>');
  73. break;
  74. case '#assignee_id':
  75. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  76. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  77. $(this).text() + '</a>');
  78. }
  79. $('.ui' + select_id + '.list .no-select').addClass('hide');
  80. $(input_id).val($(this).data('id'));
  81. });
  82. $menu.find('.no-select.item').click(function () {
  83. $(this).parent().find('.item:not(.no-select)').each(function () {
  84. $(this).removeClass('selected active')
  85. });
  86. $list.find('.selected').html('');
  87. $list.find('.no-select').removeClass('hide');
  88. $(input_id).val('');
  89. });
  90. }
  91. // Milestone and assignee
  92. selectItem('.select-milestone', '#milestone_id');
  93. selectItem('.select-assignee', '#assignee_id');
  94. }
  95. function initInstall() {
  96. if ($('.install').length == 0) {
  97. return;
  98. }
  99. // Database type change detection.
  100. $("#db_type").change(function () {
  101. var db_type = $('#db_type').val();
  102. if (db_type === "SQLite3") {
  103. $('#sql_settings').hide();
  104. $('#pgsql_settings').hide();
  105. $('#sqlite_settings').show();
  106. return;
  107. }
  108. var mysql_default = '127.0.0.1:3306';
  109. var postgres_default = '127.0.0.1:5432';
  110. $('#sqlite_settings').hide();
  111. $('#sql_settings').show();
  112. if (db_type === "PostgreSQL") {
  113. $('#pgsql_settings').show();
  114. if ($('#db_host').val() == mysql_default) {
  115. $('#db_host').val(postgres_default);
  116. }
  117. } else {
  118. $('#pgsql_settings').hide();
  119. if ($('#db_host').val() == postgres_default) {
  120. $('#db_host').val(mysql_default);
  121. }
  122. }
  123. });
  124. };
  125. function initRepository() {
  126. if ($('.repository').length == 0) {
  127. return;
  128. }
  129. // Labels
  130. if ($('.repository.labels').length > 0) {
  131. // Create label
  132. var $new_label_panel = $('.new-label.segment');
  133. $('.new-label.button').click(function () {
  134. $new_label_panel.show();
  135. });
  136. $('.new-label.segment .cancel').click(function () {
  137. $new_label_panel.hide();
  138. });
  139. $('.color-picker').each(function () {
  140. $(this).minicolors();
  141. });
  142. $('.precolors .color').click(function () {
  143. var color_hex = $(this).data('color-hex')
  144. $('.color-picker').val(color_hex);
  145. $('.minicolors-swatch-color').css("background-color", color_hex);
  146. });
  147. $('.edit-label-button').click(function () {
  148. $('#label-modal-id').val($(this).data('id'));
  149. $('.edit-label .new-label-input').val($(this).data('title'));
  150. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  151. $('.edit-label.modal').modal({
  152. onApprove: function () {
  153. $('.edit-label.form').submit();
  154. }
  155. }).modal('show');
  156. return false;
  157. });
  158. }
  159. // Milestones
  160. if ($('.repository.milestones').length > 0) {
  161. }
  162. if ($('.repository.new.milestone').length > 0) {
  163. var $datepicker = $('.milestone.datepicker')
  164. $datepicker.datetimepicker({
  165. lang: $datepicker.data('lang'),
  166. inline: true,
  167. timepicker: false,
  168. startDate: $datepicker.data('start-date'),
  169. formatDate: 'Y-m-d',
  170. onSelectDate: function (ct) {
  171. $('#deadline').val(ct.dateFormat('Y-m-d'));
  172. }
  173. });
  174. $('#clear-date').click(function () {
  175. $('#deadline').val('');
  176. return false;
  177. });
  178. }
  179. // Settings
  180. if ($('.repository.settings').length > 0) {
  181. $('#add-deploy-key').click(function () {
  182. $('#add-deploy-key-panel').show();
  183. });
  184. }
  185. // Pull request
  186. if ($('.repository.compare.pull').length > 0) {
  187. var $branch_dropdown = $('.choose.branch .dropdown')
  188. $branch_dropdown.dropdown({
  189. fullTextSearch: true,
  190. onChange: function (text, value, $choice) {
  191. window.location.href = $choice.data('url');
  192. },
  193. message: {noResults: $branch_dropdown.data('no-results')}
  194. });
  195. }
  196. };
  197. $(document).ready(function () {
  198. csrf = $('meta[name=_csrf]').attr("content");
  199. // Semantic UI modules.
  200. $('.dropdown').dropdown();
  201. $('.jump.dropdown').dropdown({
  202. action: 'hide',
  203. onShow: function () {
  204. $('.poping.up').popup('hide');
  205. }
  206. });
  207. $('.slide.up.dropdown').dropdown({
  208. transition: 'slide up'
  209. });
  210. $('.ui.accordion').accordion();
  211. $('.ui.checkbox').checkbox();
  212. $('.ui.progress').progress({
  213. showActivity: false
  214. });
  215. $('.poping.up').popup();
  216. $('.top.menu .poping.up').popup({
  217. onShow: function () {
  218. if ($('.top.menu .menu.transition').hasClass('visible')) {
  219. return false;
  220. }
  221. }
  222. });
  223. // Helpers.
  224. $('.delete-button').click(function () {
  225. var $this = $(this);
  226. $('.delete.modal').modal({
  227. closable: false,
  228. onApprove: function () {
  229. $.post($this.data('url'), {
  230. "_csrf": csrf,
  231. "id": $this.data("id")
  232. }).done(function (data) {
  233. window.location.href = data.redirect;
  234. });
  235. }
  236. }).modal('show');
  237. return false;
  238. });
  239. initCommentForm();
  240. initInstall();
  241. initRepository();
  242. });