gogs.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. $.fn.extend({
  52. toggleHide: function () {
  53. $(this).addClass("hidden");
  54. },
  55. toggleShow: function () {
  56. $(this).removeClass("hidden");
  57. },
  58. toggleAjax: function (successCallback, errorCallback) {
  59. var url = $(this).data("ajax");
  60. var method = $(this).data('ajax-method') || 'get';
  61. var ajaxName = $(this).data('ajax-name');
  62. var data = {};
  63. if (ajaxName.endsWith("preview")) {
  64. data["mode"] = "gfm";
  65. data["context"] = $(this).data('ajax-context');
  66. }
  67. $('[data-ajax-rel=' + ajaxName + ']').each(function () {
  68. var field = $(this).data("ajax-field");
  69. var t = $(this).data("ajax-val");
  70. if (t == "val") {
  71. data[field] = $(this).val();
  72. return true;
  73. }
  74. if (t == "txt") {
  75. data[field] = $(this).text();
  76. return true;
  77. }
  78. if (t == "html") {
  79. data[field] = $(this).html();
  80. return true;
  81. }
  82. if (t == "data") {
  83. data[field] = $(this).data("ajax-data");
  84. return true;
  85. }
  86. return true;
  87. });
  88. console.log("toggleAjax:", method, url, data);
  89. $.ajax({
  90. url: url,
  91. method: method.toUpperCase(),
  92. data: data,
  93. error: errorCallback,
  94. success: function (d) {
  95. if (successCallback) {
  96. successCallback(d);
  97. }
  98. }
  99. })
  100. }
  101. });
  102. }(jQuery));
  103. (function ($) {
  104. // Render markdown.
  105. Gogs.renderMarkdown = function () {
  106. var $md = $('.markdown');
  107. var $pre = $md.find('pre > code').parent();
  108. $pre.addClass('prettyprint');
  109. prettyPrint();
  110. // Set anchor.
  111. var headers = {};
  112. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  113. var node = $(this);
  114. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  115. var name = val;
  116. if (headers[val] > 0) {
  117. name = val + '-' + headers[val];
  118. }
  119. if (headers[val] == undefined) {
  120. headers[val] = 1;
  121. } else {
  122. headers[val] += 1;
  123. }
  124. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  125. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  126. });
  127. };
  128. // Render code view.
  129. Gogs.renderCodeView = function () {
  130. function selectRange($list, $select, $from) {
  131. $list.removeClass('active');
  132. if ($from) {
  133. var a = parseInt($select.attr('rel').substr(1));
  134. var b = parseInt($from.attr('rel').substr(1));
  135. var c;
  136. if (a != b) {
  137. if (a > b) {
  138. c = a;
  139. a = b;
  140. b = c;
  141. }
  142. var classes = [];
  143. for (i = a; i <= b; i++) {
  144. classes.push('.L' + i);
  145. }
  146. $list.filter(classes.join(',')).addClass('active');
  147. $.changeHash('#L' + a + '-' + 'L' + b);
  148. return
  149. }
  150. }
  151. $select.addClass('active');
  152. $.changeHash('#' + $select.attr('rel'));
  153. }
  154. $(document).on('click', '.lines-num span', function (e) {
  155. var $select = $(this);
  156. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  157. selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  158. $.deSelect();
  159. });
  160. $('.code-view .lines-code > pre').each(function () {
  161. var $pre = $(this);
  162. var $lineCode = $pre.parent();
  163. var $lineNums = $lineCode.siblings('.lines-num');
  164. if ($lineNums.length > 0) {
  165. var nums = $pre.find('ol.linenums > li').length;
  166. for (var i = 1; i <= nums; i++) {
  167. $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
  168. }
  169. }
  170. });
  171. $(window).on('hashchange', function (e) {
  172. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  173. var $list = $('.code-view ol.linenums > li');
  174. var $first;
  175. if (m) {
  176. $first = $list.filter('.' + m[1]);
  177. selectRange($list, $first, $list.filter('.' + m[2]));
  178. $("html, body").scrollTop($first.offset().top - 200);
  179. return;
  180. }
  181. m = window.location.hash.match(/^#(L\d+)$/);
  182. if (m) {
  183. $first = $list.filter('.' + m[1]);
  184. selectRange($list, $first);
  185. $("html, body").scrollTop($first.offset().top - 200);
  186. }
  187. }).trigger('hashchange');
  188. };
  189. // Search users by keyword.
  190. Gogs.searchUsers = function (val, $target) {
  191. $.ajax({
  192. url: '/api/v1/users/search?q=' + val,
  193. dataType: "json",
  194. success: function (json) {
  195. if (json.ok && json.data.length) {
  196. var html = '';
  197. $.each(json.data, function (i, item) {
  198. html += '<li><a><img src="' + item.avatar + '">' + item.username + '</a></li>';
  199. });
  200. $target.html(html);
  201. $target.toggleShow();
  202. } else {
  203. $target.toggleHide();
  204. }
  205. }
  206. });
  207. }
  208. })(jQuery);
  209. function initCore() {
  210. Gogs.renderMarkdown();
  211. Gogs.renderCodeView();
  212. }
  213. function initUserSetting() {
  214. // Confirmation of change username in user profile page.
  215. $('#user-profile-form').submit(function (e) {
  216. var $username = $('#username');
  217. if (($username.data('uname') != $username.val()) && !confirm('Username has been changed, do you want to continue?')) {
  218. e.preventDefault();
  219. return true;
  220. }
  221. });
  222. // Show add SSH key panel.
  223. $('#ssh-add').click(function () {
  224. $('#user-ssh-add-form').removeClass("hide");
  225. });
  226. // Confirmation of delete account.
  227. $('#delete-account-button').click(function (e) {
  228. if (!confirm('This account is going to be deleted, do you want to continue?')) {
  229. e.preventDefault();
  230. return true;
  231. }
  232. });
  233. }
  234. function initRepoCreate() {
  235. // Owner switch menu click.
  236. $('#repo-create-owner-list').on('click', 'li', function () {
  237. if (!$(this).hasClass('checked')) {
  238. var uid = $(this).data('uid');
  239. $('#repo-owner-id').val(uid);
  240. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  241. $('#repo-owner-name').text($(this).text().trim());
  242. $(this).parent().find('.checked').removeClass('checked');
  243. $(this).addClass('checked');
  244. console.log("set repo owner to uid :", uid, $(this).text().trim());
  245. }
  246. });
  247. $('#auth-button').click(function (e) {
  248. $('#repo-migrate-auth').slideToggle('fast');
  249. e.preventDefault();
  250. })
  251. console.log('initRepoCreate');
  252. }
  253. function initRepoSetting() {
  254. // Options.
  255. // Confirmation of changing repository name.
  256. $('#repo-setting-form').submit(function (e) {
  257. var $reponame = $('#repo_name');
  258. if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) {
  259. e.preventDefault();
  260. return true;
  261. }
  262. });
  263. $('#transfer-button').click(function () {
  264. $('#transfer-form').show();
  265. });
  266. $('#delete-button').click(function () {
  267. $('#delete-form').show();
  268. });
  269. // Collaboration.
  270. $('#repo-collab-list hr:last-child').remove();
  271. var $ul = $('#repo-collaborator').next().next().find('ul');
  272. $('#repo-collaborator').on('keyup', function () {
  273. var $this = $(this);
  274. if (!$this.val()) {
  275. $ul.toggleHide();
  276. return;
  277. }
  278. Gogs.searchUsers($this.val(), $ul);
  279. }).on('focus', function () {
  280. if (!$(this).val()) {
  281. $ul.toggleHide();
  282. } else {
  283. $ul.toggleShow();
  284. }
  285. }).next().next().find('ul').on("click", 'li', function () {
  286. $('#repo-collaborator').val($(this).text());
  287. $ul.toggleHide();
  288. });
  289. }
  290. function initOrgSetting() {
  291. // Options.
  292. // Confirmation of changing organization name.
  293. $('#org-setting-form').submit(function (e) {
  294. var $orgname = $('#orgname');
  295. if (($orgname.data('orgname') != $orgname.val()) && !confirm('Organization name has been changed, do you want to continue?')) {
  296. e.preventDefault();
  297. return true;
  298. }
  299. });
  300. // Confirmation of delete organization.
  301. $('#delete-org-button').click(function (e) {
  302. if (!confirm('This organization is going to be deleted, do you want to continue?')) {
  303. e.preventDefault();
  304. return true;
  305. }
  306. });
  307. }
  308. function initInvite() {
  309. // Invitation.
  310. var $ul = $('#org-member-invite-list');
  311. $('#org-member-invite').on('keyup', function () {
  312. var $this = $(this);
  313. if (!$this.val()) {
  314. $ul.toggleHide();
  315. return;
  316. }
  317. Gogs.searchUsers($this.val(), $ul);
  318. }).on('focus', function () {
  319. if (!$(this).val()) {
  320. $ul.toggleHide();
  321. } else {
  322. $ul.toggleShow();
  323. }
  324. }).next().next().find('ul').on("click", 'li', function () {
  325. $('#org-member-invite').val($(this).text());
  326. $ul.toggleHide();
  327. });
  328. }
  329. $(document).ready(function () {
  330. initCore();
  331. if ($('#user-profile-setting').length) {
  332. initUserSetting();
  333. }
  334. if ($('#repo-create-form').length || $('#repo-migrate-form').length) {
  335. initRepoCreate();
  336. }
  337. if ($('#repo-setting').length) {
  338. initRepoSetting();
  339. }
  340. if ($('#org-setting').length) {
  341. initOrgSetting();
  342. }
  343. if ($('#invite-box').length) {
  344. initInvite();
  345. }
  346. Tabs('#dashboard-sidebar-menu');
  347. homepage();
  348. // Fix language drop-down menu height.
  349. var l = $('#footer-lang li').length;
  350. $('#footer-lang .drop-down').css({
  351. "top": (-31 * l) + "px",
  352. "height": (31 * l - 3) + "px"
  353. });
  354. });
  355. function homepage() {
  356. // Change method to GET if no username input.
  357. $('#promo-form').submit(function (e) {
  358. if ($('#username').val() === "") {
  359. e.preventDefault();
  360. window.location.href = '/user/login';
  361. return true
  362. }
  363. });
  364. // Redirect to register page.
  365. $('#register-button').click(function (e) {
  366. if ($('#username').val() === "") {
  367. e.preventDefault();
  368. window.location.href = '/user/sign_up';
  369. return true
  370. }
  371. $('#promo-form').attr('action', '/user/sign_up');
  372. });
  373. }