gogs.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. var $milestone_menu = $('.select-milestone .menu');
  62. var $milestone_list = $('.ui.select-milestone.list')
  63. // Milestones
  64. $milestone_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. $milestone_list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  70. $(this).text() + '</a>');
  71. $('.ui.select-milestone.list .no-select').addClass('hide');
  72. $('#milestone_id').val($(this).data('id'));
  73. });
  74. $milestone_menu.find('.no-select.item').click(function () {
  75. $(this).parent().find('.item:not(.no-select)').each(function () {
  76. $(this).removeClass('selected active')
  77. });
  78. $milestone_list.find('.selected').html('');
  79. $milestone_list.find('.no-select').removeClass('hide');
  80. $('#milestone_id').val('');
  81. });
  82. }
  83. function initInstall() {
  84. if ($('.install').length == 0) {
  85. return;
  86. }
  87. // Database type change detection.
  88. $("#db_type").change(function () {
  89. var db_type = $('#db_type').val();
  90. if (db_type === "SQLite3") {
  91. $('#sql_settings').hide();
  92. $('#pgsql_settings').hide();
  93. $('#sqlite_settings').show();
  94. return;
  95. }
  96. var mysql_default = '127.0.0.1:3306';
  97. var postgres_default = '127.0.0.1:5432';
  98. $('#sqlite_settings').hide();
  99. $('#sql_settings').show();
  100. if (db_type === "PostgreSQL") {
  101. $('#pgsql_settings').show();
  102. if ($('#db_host').val() == mysql_default) {
  103. $('#db_host').val(postgres_default);
  104. }
  105. } else {
  106. $('#pgsql_settings').hide();
  107. if ($('#db_host').val() == postgres_default) {
  108. $('#db_host').val(mysql_default);
  109. }
  110. }
  111. });
  112. };
  113. function initRepository() {
  114. if ($('.repository').length == 0) {
  115. return;
  116. }
  117. // Labels
  118. if ($('.repository.labels').length > 0) {
  119. // Create label
  120. var $new_label_panel = $('.new-label.segment');
  121. $('.new-label.button').click(function () {
  122. $new_label_panel.show();
  123. });
  124. $('.new-label.segment .cancel').click(function () {
  125. $new_label_panel.hide();
  126. });
  127. $('.color-picker').each(function () {
  128. $(this).minicolors();
  129. });
  130. $('.precolors .color').click(function () {
  131. var color_hex = $(this).data('color-hex')
  132. $('.color-picker').val(color_hex);
  133. $('.minicolors-swatch-color').css("background-color", color_hex);
  134. });
  135. $('.edit-label-button').click(function () {
  136. $('#label-modal-id').val($(this).data('id'));
  137. $('.edit-label .new-label-input').val($(this).data('title'));
  138. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  139. $('.edit-label.modal').modal({
  140. onApprove: function () {
  141. $('.edit-label.form').submit();
  142. }
  143. }).modal('show');
  144. return false;
  145. });
  146. }
  147. // Milestones
  148. if ($('.repository.milestones').length > 0) {
  149. }
  150. if ($('.repository.new.milestone').length > 0) {
  151. var $datepicker = $('.milestone.datepicker')
  152. $datepicker.datetimepicker({
  153. lang: $datepicker.data('lang'),
  154. inline: true,
  155. timepicker: false,
  156. startDate: $datepicker.data('start-date'),
  157. formatDate: 'Y-m-d',
  158. onSelectDate: function (ct) {
  159. $('#deadline').val(ct.dateFormat('Y-m-d'));
  160. }
  161. });
  162. $('#clear-date').click(function () {
  163. $('#deadline').val('');
  164. return false;
  165. });
  166. }
  167. // Settings
  168. if ($('.repository.settings').length > 0) {
  169. $('#add-deploy-key').click(function () {
  170. $('#add-deploy-key-panel').show();
  171. });
  172. }
  173. // Pull request
  174. if ($('.repository.compare.pull').length > 0) {
  175. var $branch_dropdown = $('.choose.branch .dropdown')
  176. $branch_dropdown.dropdown({
  177. fullTextSearch: true,
  178. onChange: function (text, value, $choice) {
  179. window.location.href = $choice.data('url');
  180. },
  181. message: {noResults: $branch_dropdown.data('no-results')}
  182. });
  183. }
  184. };
  185. $(document).ready(function () {
  186. csrf = $('meta[name=_csrf]').attr("content");
  187. // Semantic UI modules.
  188. $('.dropdown').dropdown();
  189. $('.jump.dropdown').dropdown({
  190. action: 'hide'
  191. });
  192. $('.slide.up.dropdown').dropdown({
  193. transition: 'slide up'
  194. });
  195. $('.ui.accordion').accordion();
  196. $('.ui.checkbox').checkbox();
  197. $('.ui.progress').progress({
  198. showActivity: false
  199. });
  200. $('.poping.up').popup();
  201. // Helpers.
  202. $('.delete-button').click(function () {
  203. var $this = $(this);
  204. $('.delete.modal').modal({
  205. closable: false,
  206. onApprove: function () {
  207. $.post($this.data('url'), {
  208. "_csrf": csrf,
  209. "id": $this.data("id")
  210. }).done(function (data) {
  211. window.location.href = data.redirect;
  212. });
  213. }
  214. }).modal('show');
  215. return false;
  216. });
  217. initCommentForm();
  218. initInstall();
  219. initRepository();
  220. });