gogs.js 8.5 KB

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