gogs.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // @codekit-prepend "lib/jquery-1.11.1.min.js"
  2. // @codekit-prepend "lib/tabs.js"
  3. (function ($) {
  4. // extend jQuery ajax, set csrf token value
  5. var ajax = $.ajax;
  6. $.extend({
  7. ajax: function (url, options) {
  8. if (typeof url === 'object') {
  9. options = url;
  10. url = undefined;
  11. }
  12. options = options || {};
  13. url = options.url;
  14. var csrftoken = $('meta[name=_csrf]').attr('content');
  15. var headers = options.headers || {};
  16. var domain = document.domain.replace(/\./ig, '\\.');
  17. if (!/^(http:|https:).*/.test(url) || eval('/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/').test(url)) {
  18. headers = $.extend(headers, {'X-Csrf-Token': csrftoken});
  19. }
  20. options.headers = headers;
  21. var callback = options.success;
  22. options.success = function (data) {
  23. if (data.once) {
  24. // change all _once value if ajax data.once exist
  25. $('[name=_once]').val(data.once);
  26. }
  27. if (callback) {
  28. callback.apply(this, arguments);
  29. }
  30. };
  31. return ajax(url, options);
  32. },
  33. changeHash: function (hash) {
  34. if (history.pushState) {
  35. history.pushState(null, null, hash);
  36. }
  37. else {
  38. location.hash = hash;
  39. }
  40. },
  41. deSelect: function () {
  42. if (window.getSelection) {
  43. window.getSelection().removeAllRanges();
  44. } else {
  45. document.selection.empty();
  46. }
  47. }
  48. });
  49. }(jQuery));
  50. $(document).ready(function () {
  51. Tabs('#dashboard-sidebar-menu');
  52. homepage();
  53. settingsProfile();
  54. settingsSSHKeys();
  55. settingsDelete();
  56. renderMarkdown();
  57. renderCodeView();
  58. // Fix language drop-down menu height.
  59. var l = $('#footer-lang li').length;
  60. $('#footer-lang .drop-down').css({
  61. "top": (-31 * l) + "px",
  62. "height": (31 * l - 3) + "px"
  63. });
  64. });
  65. function homepage() {
  66. // Change method to GET if no username input.
  67. $('#promo-form').submit(function (e) {
  68. if ($('#username').val() === "") {
  69. e.preventDefault();
  70. window.location.href = '/user/login';
  71. return true
  72. }
  73. });
  74. // Redirect to register page.
  75. $('#register-button').click(function (e) {
  76. if ($('#username').val() === "") {
  77. e.preventDefault();
  78. window.location.href = '/user/sign_up';
  79. return true
  80. }
  81. $('#promo-form').attr('action', '/user/sign_up');
  82. });
  83. }
  84. function settingsProfile() {
  85. // Confirmation of change username in user profile page.
  86. $('#user-profile-form').submit(function (e) {
  87. if (($('#username').data('uname') != $('#username').val()) && !confirm('Username has been changed, do you want to continue?')) {
  88. e.preventDefault();
  89. return true;
  90. }
  91. });
  92. }
  93. function settingsSSHKeys() {
  94. // Show add SSH key panel.
  95. $('#ssh-add').click(function () {
  96. $('#user-ssh-add-form').removeClass("hide");
  97. });
  98. }
  99. function settingsDelete() {
  100. // Confirmation of delete account.
  101. $('#delete-account-button').click(function (e) {
  102. if (!confirm('This account is going to deleted, do you want to continue?')) {
  103. e.preventDefault();
  104. return true;
  105. }
  106. });
  107. }
  108. function renderMarkdown() {
  109. var $md = $('.markdown');
  110. var $pre = $md.find('pre > code').parent();
  111. $pre.addClass('prettyprint linenums');
  112. prettyPrint();
  113. // Set anchor.
  114. var headers = {};
  115. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  116. var node = $(this);
  117. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  118. var name = val;
  119. if (headers[val] > 0) {
  120. name = val + '-' + headers[val];
  121. }
  122. if (headers[val] == undefined) {
  123. headers[val] = 1;
  124. } else {
  125. headers[val] += 1;
  126. }
  127. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  128. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  129. });
  130. }
  131. function renderCodeView() {
  132. function selectRange($list, $select, $from) {
  133. $list.removeClass('active');
  134. if ($from) {
  135. var a = parseInt($select.attr('rel').substr(1));
  136. var b = parseInt($from.attr('rel').substr(1));
  137. var c;
  138. if (a != b) {
  139. if (a > b) {
  140. c = a;
  141. a = b;
  142. b = c;
  143. }
  144. var classes = [];
  145. for (i = a; i <= b; i++) {
  146. classes.push('.L' + i);
  147. }
  148. $list.filter(classes.join(',')).addClass('active');
  149. $.changeHash('#L' + a + '-' + 'L' + b);
  150. return
  151. }
  152. }
  153. $select.addClass('active');
  154. $.changeHash('#' + $select.attr('rel'));
  155. }
  156. $(document).on('click', '.lines-num span', function (e) {
  157. var $select = $(this);
  158. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  159. selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  160. $.deSelect();
  161. });
  162. $('.code-view .lines-code > pre').each(function () {
  163. var $pre = $(this);
  164. var $lineCode = $pre.parent();
  165. var $lineNums = $lineCode.siblings('.lines-num');
  166. if ($lineNums.length > 0) {
  167. var nums = $pre.find('ol.linenums > li').length;
  168. for (var i = 1; i <= nums; i++) {
  169. $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
  170. }
  171. }
  172. });
  173. $(window).on('hashchange', function (e) {
  174. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  175. var $list = $('.code-view ol.linenums > li');
  176. if (m) {
  177. var $first = $list.filter('.' + m[1]);
  178. selectRange($list, $first, $list.filter('.' + m[2]));
  179. $("html, body").scrollTop($first.offset().top - 200);
  180. return;
  181. }
  182. m = window.location.hash.match(/^#(L\d+)$/);
  183. if (m) {
  184. var $first = $list.filter('.' + m[1]);
  185. selectRange($list, $first);
  186. $("html, body").scrollTop($first.offset().top - 200);
  187. }
  188. }).trigger('hashchange');
  189. }