app.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 $md = $('.markdown');
  64. var $pre = $md.find('pre > code').parent();
  65. $pre.addClass('prettyprint');
  66. prettyPrint();
  67. var $lineNums = $pre.parent().siblings('.lines-num');
  68. if ($lineNums.length > 0) {
  69. var nums = $pre.find('ol.linenums > li').length;
  70. for (var i = 1; i <= nums; i++) {
  71. $lineNums.append('<span id="L' + i + '" rel=".L' + i + '">' + i + '</span>');
  72. }
  73. var last;
  74. $(document).on('click', '.lines-num span', function () {
  75. var $e = $(this);
  76. if (last) {
  77. last.removeClass('active');
  78. }
  79. last = $e.parent().siblings('.lines-code').find('ol.linenums > ' + $e.attr('rel'));
  80. last.addClass('active');
  81. window.location.href = '#' + $e.attr('id');
  82. });
  83. }
  84. // Set anchor.
  85. var headers = {};
  86. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  87. var node = $(this);
  88. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  89. var name = val;
  90. if (headers[val] > 0) {
  91. name = val + '-' + headers[val];
  92. }
  93. if (headers[val] == undefined) {
  94. headers[val] = 1;
  95. } else {
  96. headers[val] += 1;
  97. }
  98. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  99. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  100. });
  101. }
  102. })(jQuery);
  103. // ajax utils
  104. (function ($) {
  105. Gogits.ajaxDelete = function (url, data, success) {
  106. data = data || {};
  107. data._method = "DELETE";
  108. $.ajax({
  109. url: url,
  110. data: data,
  111. method: "POST",
  112. dataType: "json",
  113. success: function (json) {
  114. if (success) {
  115. success(json);
  116. }
  117. }
  118. })
  119. }
  120. })(jQuery);
  121. function initCore() {
  122. Gogits.initTooltips();
  123. Gogits.initPopovers();
  124. Gogits.initTabs();
  125. Gogits.initModals();
  126. Gogits.renderMarkdown();
  127. }
  128. function initRegister() {
  129. $.getScript("/js/jquery.validate.min.js", function () {
  130. Gogits.validateForm("#gogs-login-card", {
  131. rules: {
  132. "username": {
  133. required: true,
  134. maxlength: 30
  135. },
  136. "email": {
  137. required: true,
  138. email: true
  139. },
  140. "passwd": {
  141. required: true,
  142. minlength: 6,
  143. maxlength: 30
  144. },
  145. "re-passwd": {
  146. required: true,
  147. equalTo: "input[name=passwd]"
  148. }
  149. }
  150. });
  151. });
  152. }
  153. function initUserSetting() {
  154. $('#gogs-ssh-keys .delete').confirmation({
  155. singleton: true,
  156. onConfirm: function (e, $this) {
  157. Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
  158. if (json.ok) {
  159. window.location.reload();
  160. } else {
  161. alert(json.err);
  162. }
  163. });
  164. }
  165. });
  166. }
  167. function initRepository() {
  168. (function () {
  169. var $guide = $('.guide-box');
  170. if ($guide.length) {
  171. var $url = $('#guide-clone-url');
  172. $guide.find('button[data-link]').on("click",function () {
  173. var $this = $(this);
  174. if (!$this.hasClass('btn-primary')) {
  175. $guide.find('.btn-primary').removeClass('btn-primary').addClass("btn-default");
  176. $(this).addClass('btn-primary').removeClass('btn-default');
  177. $url.val($this.data("link"));
  178. $guide.find('span.clone-url').text($this.data('link'));
  179. }
  180. }).eq(0).trigger("click");
  181. // todo copy to clipboard
  182. }
  183. })();
  184. }
  185. (function ($) {
  186. $(function () {
  187. initCore();
  188. var body = $("#gogs-body");
  189. if (body.data("page") == "user-signup") {
  190. initRegister();
  191. }
  192. if (body.data("page") == "user") {
  193. initUserSetting();
  194. }
  195. if ($('.gogs-repo-nav').length) {
  196. initRepository();
  197. }
  198. });
  199. })(jQuery);