app.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. var Gogits = {
  2. "PageIsSignup": false
  3. };
  4. (function ($) {
  5. Gogits.showTab = function (selector, index) {
  6. if (!index) {
  7. index = 0;
  8. }
  9. $(selector).tab("show");
  10. $(selector).find("li:eq(" + index + ") a").tab("show");
  11. };
  12. Gogits.validateForm = function (selector, options) {
  13. var $form = $(selector);
  14. options = options || {};
  15. options.showErrors = function (map, list) {
  16. var $error = $form.find('.form-error').addClass('hidden');
  17. $('.has-error').removeClass("has-error");
  18. $error.text(list[0].message).show().removeClass("hidden");
  19. $(list[0].element).parents(".form-group").addClass("has-error");
  20. };
  21. $form.validate(options);
  22. };
  23. // ----- init elements
  24. Gogits.initModals = function () {
  25. var modals = $("[data-toggle=modal]");
  26. if (modals.length < 1) {
  27. return;
  28. }
  29. $.each(modals, function (i, item) {
  30. var hide = $(item).data('modal');
  31. $(item).modal(hide ? hide : "hide");
  32. });
  33. };
  34. Gogits.initTooltips = function () {
  35. $("body").tooltip({
  36. selector: "[data-toggle=tooltip]"
  37. //container: "body"
  38. });
  39. };
  40. Gogits.initPopovers = function () {
  41. var hideAllPopovers = function () {
  42. $('[data-toggle=popover]').each(function () {
  43. $(this).popover('hide');
  44. });
  45. };
  46. $(document).on('click', function (e) {
  47. var $e = $(e.target);
  48. if ($e.data('toggle') == 'popover' || $e.parents("[data-toggle=popover], .popover").length > 0) {
  49. return;
  50. }
  51. hideAllPopovers();
  52. });
  53. $("body").popover({
  54. selector: "[data-toggle=popover]"
  55. });
  56. };
  57. Gogits.initTabs = function () {
  58. var $tabs = $('[data-init=tabs]');
  59. $tabs.find("li:eq(0) a").tab("show");
  60. };
  61. // render markdown
  62. Gogits.renderMarkdown = function () {
  63. var $pre = $('.markdown').find('pre > code').parent();
  64. $pre.addClass("prettyprint linenums");
  65. prettyPrint();
  66. }
  67. })(jQuery);
  68. // ajax utils
  69. (function ($) {
  70. Gogits.ajaxDelete = function (url, data, success) {
  71. data = data || {};
  72. data._method = "DELETE";
  73. $.ajax({
  74. url: url,
  75. data: data,
  76. method: "POST",
  77. dataType: "json",
  78. success: function (json) {
  79. if (success) {
  80. success(json);
  81. }
  82. }
  83. })
  84. }
  85. })(jQuery);
  86. function initCore() {
  87. Gogits.initTooltips();
  88. Gogits.initPopovers();
  89. Gogits.initTabs();
  90. Gogits.initModals();
  91. Gogits.renderMarkdown();
  92. }
  93. function initRegister() {
  94. $.getScript("/js/jquery.validate.min.js", function () {
  95. Gogits.validateForm("#gogs-login-card", {
  96. rules: {
  97. "username": {
  98. required: true,
  99. maxlength: 30
  100. },
  101. "email": {
  102. required: true,
  103. email: true
  104. },
  105. "passwd": {
  106. required: true,
  107. minlength: 6,
  108. maxlength: 30
  109. },
  110. "re-passwd": {
  111. required: true,
  112. equalTo: "input[name=passwd]"
  113. }
  114. }
  115. });
  116. });
  117. }
  118. function initUserSetting() {
  119. $('#gogs-ssh-keys .delete').confirmation({
  120. singleton: true,
  121. onConfirm: function (e, $this) {
  122. Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
  123. if (json.ok) {
  124. window.location.reload();
  125. } else {
  126. alert(json.err);
  127. }
  128. });
  129. }
  130. });
  131. }
  132. function initRepository() {
  133. var $guide = $('.guide-box');
  134. if ($guide.length) {
  135. var $url = $('#guide-clone-url');
  136. $guide.find('button[data-url]').on("click",function () {
  137. var $this = $(this);
  138. if (!$this.hasClass('btn-primary')) {
  139. $guide.find('.btn-primary').removeClass('btn-primary').addClass("btn-default");
  140. $(this).addClass('btn-primary').removeClass('btn-default');
  141. $url.val($this.data("url"));
  142. $guide.find('span.clone-url').text($this.data('url'));
  143. }
  144. }).eq(0).trigger("click");
  145. // todo copy to clipboard
  146. }
  147. }
  148. (function ($) {
  149. $(function () {
  150. initCore();
  151. var body = $("#gogs-body");
  152. if (body.data("page") == "user-signup") {
  153. initRegister();
  154. }
  155. if (body.data("page") == "user") {
  156. initUserSetting();
  157. }
  158. if ($('.gogs-repo-nav').length) {
  159. initRepository();
  160. }
  161. });
  162. })(jQuery);