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.

156 lines
4.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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");
  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 ($) {
  133. $(function () {
  134. initCore();
  135. var body = $("#gogs-body");
  136. if (body.data("page") == "user-signup") {
  137. initRegister();
  138. }
  139. if (body.data("page") == "user") {
  140. initUserSetting();
  141. }
  142. });
  143. })(jQuery);