app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. changeHash: function (hash) {
  35. if (history.pushState) {
  36. history.pushState(null, null, hash);
  37. }
  38. else {
  39. location.hash = hash;
  40. }
  41. },
  42. deSelect: function () {
  43. if (window.getSelection) {
  44. window.getSelection().removeAllRanges();
  45. } else {
  46. document.selection.empty();
  47. }
  48. }
  49. });
  50. }(jQuery));
  51. (function ($) {
  52. Gogits.showTab = function (selector, index) {
  53. if (!index) {
  54. index = 0;
  55. }
  56. $(selector).tab("show");
  57. $(selector).find("li:eq(" + index + ") a").tab("show");
  58. };
  59. Gogits.validateForm = function (selector, options) {
  60. var $form = $(selector);
  61. options = options || {};
  62. options.showErrors = function (map, list) {
  63. var $error = $form.find('.form-error').addClass('hidden');
  64. $('.has-error').removeClass("has-error");
  65. $error.text(list[0].message).show().removeClass("hidden");
  66. $(list[0].element).parents(".form-group").addClass("has-error");
  67. };
  68. $form.validate(options);
  69. };
  70. // ----- init elements
  71. Gogits.initModals = function () {
  72. var modals = $("[data-toggle=modal]");
  73. if (modals.length < 1) {
  74. return;
  75. }
  76. $.each(modals, function (i, item) {
  77. var hide = $(item).data('modal');
  78. $(item).modal(hide ? hide : "hide");
  79. });
  80. };
  81. Gogits.initTooltips = function () {
  82. $("body").tooltip({
  83. selector: "[data-toggle=tooltip]"
  84. //container: "body"
  85. });
  86. };
  87. Gogits.initPopovers = function () {
  88. var hideAllPopovers = function () {
  89. $('[data-toggle=popover]').each(function () {
  90. $(this).popover('hide');
  91. });
  92. };
  93. $(document).on('click', function (e) {
  94. var $e = $(e.target);
  95. if ($e.data('toggle') == 'popover' || $e.parents("[data-toggle=popover], .popover").length > 0) {
  96. return;
  97. }
  98. hideAllPopovers();
  99. });
  100. $("body").popover({
  101. selector: "[data-toggle=popover]"
  102. });
  103. };
  104. Gogits.initTabs = function () {
  105. var $tabs = $('[data-init=tabs]');
  106. $tabs.find("li:eq(0) a").tab("show");
  107. };
  108. // fix dropdown inside click
  109. Gogits.initDropDown = function () {
  110. $('.dropdown-menu.no-propagation').on('click', function (e) {
  111. e.stopPropagation();
  112. });
  113. };
  114. // render markdown
  115. Gogits.renderMarkdown = function () {
  116. var $md = $('.markdown');
  117. var $pre = $md.find('pre > code').parent();
  118. $pre.addClass('prettyprint linenums');
  119. prettyPrint();
  120. // Set anchor.
  121. var headers = {};
  122. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  123. var node = $(this);
  124. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  125. var name = val;
  126. if (headers[val] > 0) {
  127. name = val + '-' + headers[val];
  128. }
  129. if (headers[val] == undefined) {
  130. headers[val] = 1;
  131. } else {
  132. headers[val] += 1;
  133. }
  134. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  135. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  136. });
  137. };
  138. Gogits.renderCodeView = function () {
  139. function selectRange($list, $select, $from) {
  140. $list.removeClass('active');
  141. if ($from) {
  142. var a = parseInt($select.attr('rel').substr(1));
  143. var b = parseInt($from.attr('rel').substr(1));
  144. var c;
  145. if (a != b) {
  146. if (a > b) {
  147. c = a;
  148. a = b;
  149. b = c;
  150. }
  151. var classes = [];
  152. for (i = a; i <= b; i++) {
  153. classes.push('.L' + i);
  154. }
  155. $list.filter(classes.join(',')).addClass('active');
  156. $.changeHash('#L' + a + '-' + 'L' + b);
  157. return
  158. }
  159. }
  160. $select.addClass('active');
  161. $.changeHash('#' + $select.attr('rel'));
  162. }
  163. $(document).on('click', '.lines-num span', function (e) {
  164. var $select = $(this);
  165. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  166. selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  167. $.deSelect();
  168. });
  169. $('.code-view .lines-code > pre').each(function () {
  170. var $pre = $(this);
  171. var $lineCode = $pre.parent();
  172. var $lineNums = $lineCode.siblings('.lines-num');
  173. if ($lineNums.length > 0) {
  174. var nums = $pre.find('ol.linenums > li').length;
  175. for (var i = 1; i <= nums; i++) {
  176. $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
  177. }
  178. }
  179. });
  180. $(window).on('hashchange',function (e) {
  181. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  182. var $list = $('.code-view ol.linenums > li');
  183. if (m) {
  184. var $first = $list.filter('.' + m[1]);
  185. selectRange($list, $first, $list.filter('.' + m[2]));
  186. $("html, body").scrollTop($first.offset().top - 200);
  187. return;
  188. }
  189. m = window.location.hash.match(/^#(L\d+)$/);
  190. if (m) {
  191. var $first = $list.filter('.' + m[1]);
  192. selectRange($list, $first);
  193. $("html, body").scrollTop($first.offset().top - 200);
  194. }
  195. }).trigger('hashchange');
  196. };
  197. })(jQuery);
  198. // ajax utils
  199. (function ($) {
  200. Gogits.ajaxDelete = function (url, data, success) {
  201. data = data || {};
  202. data._method = "DELETE";
  203. $.ajax({
  204. url: url,
  205. data: data,
  206. method: "POST",
  207. dataType: "json",
  208. success: function (json) {
  209. if (success) {
  210. success(json);
  211. }
  212. }
  213. })
  214. }
  215. })(jQuery);
  216. function initCore() {
  217. Gogits.initTooltips();
  218. Gogits.initPopovers();
  219. Gogits.initTabs();
  220. Gogits.initModals();
  221. Gogits.initDropDown();
  222. Gogits.renderMarkdown();
  223. Gogits.renderCodeView();
  224. }
  225. function initRegister() {
  226. $.getScript("/js/jquery.validate.min.js", function () {
  227. Gogits.validateForm("#login-card", {
  228. rules: {
  229. "username": {
  230. required: true,
  231. maxlength: 30
  232. },
  233. "email": {
  234. required: true,
  235. email: true
  236. },
  237. "passwd": {
  238. required: true,
  239. minlength: 6,
  240. maxlength: 30
  241. },
  242. "re-passwd": {
  243. required: true,
  244. equalTo: "input[name=passwd]"
  245. }
  246. }
  247. });
  248. });
  249. }
  250. function initUserSetting() {
  251. $('#ssh-keys .delete').confirmation({
  252. singleton: true,
  253. onConfirm: function (e, $this) {
  254. Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
  255. if (json.ok) {
  256. window.location.reload();
  257. } else {
  258. alert(json.err);
  259. }
  260. });
  261. }
  262. });
  263. }
  264. function initRepository() {
  265. // clone group button script
  266. (function () {
  267. var $clone = $('.clone-group-btn');
  268. if ($clone.length) {
  269. var $url = $('.clone-group-url');
  270. $clone.find('button[data-link]').on("click",function (e) {
  271. var $this = $(this);
  272. if (!$this.hasClass('btn-primary')) {
  273. $clone.find('.input-group-btn .btn-primary').removeClass('btn-primary').addClass("btn-default");
  274. $(this).addClass('btn-primary').removeClass('btn-default');
  275. $url.val($this.data("link"));
  276. $clone.find('span.clone-url').text($this.data('link'));
  277. }
  278. }).eq(0).trigger("click");
  279. // todo copy to clipboard
  280. }
  281. })();
  282. // watching script
  283. (function () {
  284. var $watch = $('#repo-watching'),
  285. watchLink = $watch.data("watch"),
  286. unwatchLink = $watch.data("unwatch");
  287. $watch.on('click', '.to-watch',function () {
  288. if ($watch.hasClass("watching")) {
  289. return false;
  290. }
  291. $.get(watchLink, function (json) {
  292. if (json.ok) {
  293. $watch.find('.text-primary').removeClass('text-primary');
  294. $watch.find('.to-watch h4').addClass('text-primary');
  295. $watch.find('.fa-eye-slash').removeClass('fa-eye-slash').addClass('fa-eye');
  296. $watch.removeClass("no-watching").addClass("watching");
  297. }
  298. });
  299. return false;
  300. }).on('click', '.to-unwatch', function () {
  301. if ($watch.hasClass("no-watching")) {
  302. return false;
  303. }
  304. $.get(unwatchLink, function (json) {
  305. if (json.ok) {
  306. $watch.find('.text-primary').removeClass('text-primary');
  307. $watch.find('.to-unwatch h4').addClass('text-primary');
  308. $watch.find('.fa-eye').removeClass('fa-eye').addClass('fa-eye-slash');
  309. $watch.removeClass("watching").addClass("no-watching");
  310. }
  311. });
  312. return false;
  313. });
  314. })();
  315. // repo diff counter
  316. (function () {
  317. var $counter = $('.diff-counter');
  318. if ($counter.length < 1) {
  319. return;
  320. }
  321. $counter.each(function (i, item) {
  322. var $item = $(item);
  323. var addLine = $item.find('span[data-line].add').data("line");
  324. var delLine = $item.find('span[data-line].del').data("line");
  325. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  326. $item.find(".bar .add").css("width", addPercent + "%");
  327. });
  328. }());
  329. }
  330. (function ($) {
  331. $(function () {
  332. initCore();
  333. var body = $("#body");
  334. if (body.data("page") == "user-signup") {
  335. initRegister();
  336. }
  337. if (body.data("page") == "user") {
  338. initUserSetting();
  339. }
  340. if ($('.repo-nav').length) {
  341. initRepository();
  342. }
  343. });
  344. })(jQuery);