You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.js 9.4 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. var Gogits = {
  2. "PageIsSignup": false
  3. };
  4. (function($){
  5. // extend jQuery ajax, set csrf token value
  6. var ajax = $.ajax;
  7. $.extend({
  8. ajax: function(url, options) {
  9. if (typeof url === 'object') {
  10. options = url;
  11. url = undefined;
  12. }
  13. options = options || {};
  14. url = options.url;
  15. var csrftoken = $('meta[name=_csrf]').attr('content');
  16. var headers = options.headers || {};
  17. var domain = document.domain.replace(/\./ig, '\\.');
  18. if (!/^(http:|https:).*/.test(url) || eval('/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/').test(url)) {
  19. headers = $.extend(headers, {'X-Csrf-Token':csrftoken});
  20. }
  21. options.headers = headers;
  22. var callback = options.success;
  23. options.success = function(data){
  24. if(data.once){
  25. // change all _once value if ajax data.once exist
  26. $('[name=_once]').val(data.once);
  27. }
  28. if(callback){
  29. callback.apply(this, arguments);
  30. }
  31. };
  32. return ajax(url, options);
  33. }
  34. });
  35. }(jQuery));
  36. (function ($) {
  37. Gogits.showTab = function (selector, index) {
  38. if (!index) {
  39. index = 0;
  40. }
  41. $(selector).tab("show");
  42. $(selector).find("li:eq(" + index + ") a").tab("show");
  43. };
  44. Gogits.validateForm = function (selector, options) {
  45. var $form = $(selector);
  46. options = options || {};
  47. options.showErrors = function (map, list) {
  48. var $error = $form.find('.form-error').addClass('hidden');
  49. $('.has-error').removeClass("has-error");
  50. $error.text(list[0].message).show().removeClass("hidden");
  51. $(list[0].element).parents(".form-group").addClass("has-error");
  52. };
  53. $form.validate(options);
  54. };
  55. // ----- init elements
  56. Gogits.initModals = function () {
  57. var modals = $("[data-toggle=modal]");
  58. if (modals.length < 1) {
  59. return;
  60. }
  61. $.each(modals, function (i, item) {
  62. var hide = $(item).data('modal');
  63. $(item).modal(hide ? hide : "hide");
  64. });
  65. };
  66. Gogits.initTooltips = function () {
  67. $("body").tooltip({
  68. selector: "[data-toggle=tooltip]"
  69. //container: "body"
  70. });
  71. };
  72. Gogits.initPopovers = function () {
  73. var hideAllPopovers = function () {
  74. $('[data-toggle=popover]').each(function () {
  75. $(this).popover('hide');
  76. });
  77. };
  78. $(document).on('click', function (e) {
  79. var $e = $(e.target);
  80. if ($e.data('toggle') == 'popover' || $e.parents("[data-toggle=popover], .popover").length > 0) {
  81. return;
  82. }
  83. hideAllPopovers();
  84. });
  85. $("body").popover({
  86. selector: "[data-toggle=popover]"
  87. });
  88. };
  89. Gogits.initTabs = function () {
  90. var $tabs = $('[data-init=tabs]');
  91. $tabs.find("li:eq(0) a").tab("show");
  92. };
  93. // fix dropdown inside click
  94. Gogits.initDropDown = function(){
  95. $('.dropdown-menu').on('click','a,button,input,select',function(e){
  96. e.stopPropagation();
  97. });
  98. };
  99. // render markdown
  100. Gogits.renderMarkdown = function () {
  101. var $md = $('.markdown');
  102. var $pre = $md.find('pre > code').parent();
  103. $pre.addClass('prettyprint linenums');
  104. prettyPrint();
  105. var $lineNums = $pre.parent().siblings('.lines-num');
  106. if ($lineNums.length > 0) {
  107. var nums = $pre.find('ol.linenums > li').length;
  108. for (var i = 1; i <= nums; i++) {
  109. $lineNums.append('<span id="L' + i + '" rel=".L' + i + '">' + i + '</span>');
  110. }
  111. var last;
  112. $(document).on('click', '.lines-num span', function () {
  113. var $e = $(this);
  114. if (last) {
  115. last.removeClass('active');
  116. }
  117. last = $e.parent().siblings('.lines-code').find('ol.linenums > ' + $e.attr('rel'));
  118. last.addClass('active');
  119. window.location.href = '#' + $e.attr('id');
  120. });
  121. }
  122. // Set anchor.
  123. var headers = {};
  124. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  125. var node = $(this);
  126. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  127. var name = val;
  128. if (headers[val] > 0) {
  129. name = val + '-' + headers[val];
  130. }
  131. if (headers[val] == undefined) {
  132. headers[val] = 1;
  133. } else {
  134. headers[val] += 1;
  135. }
  136. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  137. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  138. });
  139. }
  140. })(jQuery);
  141. // ajax utils
  142. (function ($) {
  143. Gogits.ajaxDelete = function (url, data, success) {
  144. data = data || {};
  145. data._method = "DELETE";
  146. $.ajax({
  147. url: url,
  148. data: data,
  149. method: "POST",
  150. dataType: "json",
  151. success: function (json) {
  152. if (success) {
  153. success(json);
  154. }
  155. }
  156. })
  157. }
  158. })(jQuery);
  159. function initCore() {
  160. Gogits.initTooltips();
  161. Gogits.initPopovers();
  162. Gogits.initTabs();
  163. Gogits.initModals();
  164. Gogits.initDropDown();
  165. Gogits.renderMarkdown();
  166. }
  167. function initRegister() {
  168. $.getScript("/js/jquery.validate.min.js", function () {
  169. Gogits.validateForm("#gogs-login-card", {
  170. rules: {
  171. "username": {
  172. required: true,
  173. maxlength: 30
  174. },
  175. "email": {
  176. required: true,
  177. email: true
  178. },
  179. "passwd": {
  180. required: true,
  181. minlength: 6,
  182. maxlength: 30
  183. },
  184. "re-passwd": {
  185. required: true,
  186. equalTo: "input[name=passwd]"
  187. }
  188. }
  189. });
  190. });
  191. }
  192. function initUserSetting() {
  193. $('#gogs-ssh-keys .delete').confirmation({
  194. singleton: true,
  195. onConfirm: function (e, $this) {
  196. Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
  197. if (json.ok) {
  198. window.location.reload();
  199. } else {
  200. alert(json.err);
  201. }
  202. });
  203. }
  204. });
  205. }
  206. function initRepository() {
  207. // clone group button script
  208. (function () {
  209. var $clone = $('.clone-group-btn');
  210. if ($clone.length) {
  211. var $url = $('.clone-group-url');
  212. $clone.find('button[data-link]').on("click",function (e) {
  213. var $this = $(this);
  214. if (!$this.hasClass('btn-primary')) {
  215. $clone.find('.btn-primary').removeClass('btn-primary').addClass("btn-default");
  216. $(this).addClass('btn-primary').removeClass('btn-default');
  217. $url.val($this.data("link"));
  218. $clone.find('span.clone-url').text($this.data('link'));
  219. }
  220. }).eq(0).trigger("click");
  221. // todo copy to clipboard
  222. }
  223. })();
  224. // watching script
  225. (function () {
  226. var $watch = $('#gogs-repo-watching'),
  227. watchLink = $watch.data("watch"),
  228. unwatchLink = $watch.data("unwatch");
  229. $watch.on('click', '.to-watch',function () {
  230. if ($watch.hasClass("watching")) {
  231. return false;
  232. }
  233. $.get(watchLink, function (json) {
  234. if (json.ok) {
  235. $watch.find('.text-primary').removeClass('text-primary');
  236. $watch.find('.to-watch h4').addClass('text-primary');
  237. $watch.find('.fa-eye-slash').removeClass('fa-eye-slash').addClass('fa-eye');
  238. $watch.removeClass("no-watching").addClass("watching");
  239. }
  240. });
  241. return false;
  242. }).on('click', '.to-unwatch', function () {
  243. if ($watch.hasClass("no-watching")) {
  244. return false;
  245. }
  246. $.get(unwatchLink, function (json) {
  247. if (json.ok) {
  248. $watch.find('.text-primary').removeClass('text-primary');
  249. $watch.find('.to-unwatch h4').addClass('text-primary');
  250. $watch.find('.fa-eye').removeClass('fa-eye').addClass('fa-eye-slash');
  251. $watch.removeClass("watching").addClass("no-watching");
  252. }
  253. });
  254. return false;
  255. });
  256. })();
  257. }
  258. (function ($) {
  259. $(function () {
  260. initCore();
  261. var body = $("#gogs-body");
  262. if (body.data("page") == "user-signup") {
  263. initRegister();
  264. }
  265. if (body.data("page") == "user") {
  266. initUserSetting();
  267. }
  268. if ($('.gogs-repo-nav').length) {
  269. initRepository();
  270. }
  271. });
  272. })(jQuery);