app.js 8.1 KB

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