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

613 lines
20 KiB

  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. // Search repositories by keyword.
  209. Gogs.searchRepos = function (val, $target, $param) {
  210. $.ajax({
  211. url: '/api/v1/repos/search?q=' + val + '&' + $param,
  212. dataType: "json",
  213. success: function (json) {
  214. if (json.ok && json.data.length) {
  215. var html = '';
  216. $.each(json.data, function (i, item) {
  217. html += '<li><a><span class="octicon octicon-repo"></span> ' + item.repolink + '</a></li>';
  218. });
  219. $target.html(html);
  220. $target.toggleShow();
  221. } else {
  222. $target.toggleHide();
  223. }
  224. }
  225. });
  226. }
  227. // Copy util.
  228. Gogs.bindCopy = function (selector) {
  229. if ($(selector).hasClass('js-copy-bind')) {
  230. return;
  231. }
  232. $(selector).zclip({
  233. path: "/js/ZeroClipboard.swf",
  234. copy: function () {
  235. var t = $(this).data("copy-val");
  236. var to = $($(this).data("copy-from"));
  237. var str = "";
  238. if (t == "txt") {
  239. str = to.text();
  240. }
  241. if (t == 'val') {
  242. str = to.val();
  243. }
  244. if (t == 'html') {
  245. str = to.html();
  246. }
  247. return str;
  248. },
  249. afterCopy: function () {
  250. alert("Clone URL has copied!");
  251. // var $this = $(this);
  252. // $this.tooltip('hide')
  253. // .attr('data-original-title', 'Copied OK');
  254. // setTimeout(function () {
  255. // $this.tooltip("show");
  256. // }, 200);
  257. // setTimeout(function () {
  258. // $this.tooltip('hide')
  259. // .attr('data-original-title', 'Copy to Clipboard');
  260. // }, 3000);
  261. }
  262. }).addClass("js-copy-bind");
  263. }
  264. })(jQuery);
  265. function initCore() {
  266. Gogs.renderMarkdown();
  267. Gogs.renderCodeView();
  268. }
  269. function initUserSetting() {
  270. // Confirmation of change username in user profile page.
  271. $('#user-profile-form').submit(function (e) {
  272. var $username = $('#username');
  273. if (($username.data('uname') != $username.val()) && !confirm('Username has been changed, do you want to continue?')) {
  274. e.preventDefault();
  275. return true;
  276. }
  277. });
  278. // Show add SSH key panel.
  279. $('#ssh-add').click(function () {
  280. $('#user-ssh-add-form').removeClass("hide");
  281. });
  282. // Confirmation of delete account.
  283. $('#delete-account-button').click(function (e) {
  284. if (!confirm('This account is going to be deleted, do you want to continue?')) {
  285. e.preventDefault();
  286. return true;
  287. }
  288. });
  289. }
  290. function initRepoCreate() {
  291. // Owner switch menu click.
  292. $('#repo-create-owner-list').on('click', 'li', function () {
  293. if (!$(this).hasClass('checked')) {
  294. var uid = $(this).data('uid');
  295. $('#repo-owner-id').val(uid);
  296. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  297. $('#repo-owner-name').text($(this).text().trim());
  298. $(this).parent().find('.checked').removeClass('checked');
  299. $(this).addClass('checked');
  300. console.log("set repo owner to uid :", uid, $(this).text().trim());
  301. }
  302. });
  303. $('#auth-button').click(function (e) {
  304. $('#repo-migrate-auth').slideToggle('fast');
  305. e.preventDefault();
  306. })
  307. console.log('initRepoCreate');
  308. }
  309. function initRepo() {
  310. // Clone link switch button.
  311. $('#repo-clone-ssh').click(function () {
  312. $(this).removeClass('btn-gray').addClass('btn-blue');
  313. $('#repo-clone-https').removeClass('btn-blue').addClass('btn-gray');
  314. $('#repo-clone-url').val($(this).data('link'));
  315. $('.clone-url').text($(this).data('link'))
  316. });
  317. $('#repo-clone-https').click(function () {
  318. $(this).removeClass('btn-gray').addClass('btn-blue');
  319. $('#repo-clone-ssh').removeClass('btn-blue').addClass('btn-gray');
  320. $('#repo-clone-url').val($(this).data('link'));
  321. $('.clone-url').text($(this).data('link'))
  322. });
  323. // Copy URL.
  324. $('#repo-clone-copy').hover(function () {
  325. Gogs.bindCopy($(this));
  326. })
  327. }
  328. function initRepoSetting() {
  329. // Options.
  330. // Confirmation of changing repository name.
  331. $('#repo-setting-form').submit(function (e) {
  332. var $reponame = $('#repo_name');
  333. if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) {
  334. e.preventDefault();
  335. return true;
  336. }
  337. });
  338. // web hook type change
  339. $('select#hook-type').on("change", function () {
  340. hookTypes = ['Gogs','Slack'];
  341. var curHook = $(this).val();
  342. hookTypes.forEach(function(hookType) {
  343. if (curHook === hookType) {
  344. $('div#'+hookType.toLowerCase()).toggleShow();
  345. }
  346. else {
  347. $('div#'+hookType.toLowerCase()).toggleHide();
  348. }
  349. });
  350. });
  351. $('#transfer-button').click(function () {
  352. $('#transfer-form').show();
  353. });
  354. $('#delete-button').click(function () {
  355. $('#delete-form').show();
  356. });
  357. // Collaboration.
  358. $('#repo-collab-list hr:last-child').remove();
  359. var $ul = $('#repo-collaborator').next().next().find('ul');
  360. $('#repo-collaborator').on('keyup', function () {
  361. var $this = $(this);
  362. if (!$this.val()) {
  363. $ul.toggleHide();
  364. return;
  365. }
  366. Gogs.searchUsers($this.val(), $ul);
  367. }).on('focus', function () {
  368. if (!$(this).val()) {
  369. $ul.toggleHide();
  370. } else {
  371. $ul.toggleShow();
  372. }
  373. }).next().next().find('ul').on("click", 'li', function () {
  374. $('#repo-collaborator').val($(this).text());
  375. $ul.toggleHide();
  376. });
  377. }
  378. function initOrgSetting() {
  379. // Options.
  380. // Confirmation of changing organization name.
  381. $('#org-setting-form').submit(function (e) {
  382. var $orgname = $('#orgname');
  383. if (($orgname.data('orgname') != $orgname.val()) && !confirm('Organization name has been changed, do you want to continue?')) {
  384. e.preventDefault();
  385. return true;
  386. }
  387. });
  388. // Confirmation of delete organization.
  389. $('#delete-org-button').click(function (e) {
  390. if (!confirm('This organization is going to be deleted, do you want to continue?')) {
  391. e.preventDefault();
  392. return true;
  393. }
  394. });
  395. }
  396. function initInvite() {
  397. // Invitation.
  398. var $ul = $('#org-member-invite-list');
  399. $('#org-member-invite').on('keyup', function () {
  400. var $this = $(this);
  401. if (!$this.val()) {
  402. $ul.toggleHide();
  403. return;
  404. }
  405. Gogs.searchUsers($this.val(), $ul);
  406. }).on('focus', function () {
  407. if (!$(this).val()) {
  408. $ul.toggleHide();
  409. } else {
  410. $ul.toggleShow();
  411. }
  412. }).next().next().find('ul').on("click", 'li', function () {
  413. $('#org-member-invite').val($(this).text());
  414. $ul.toggleHide();
  415. });
  416. }
  417. function initOrgTeamCreate() {
  418. // Delete team.
  419. $('#org-team-delete').click(function (e) {
  420. if (!confirm('This team is going to be deleted, do you want to continue?')) {
  421. e.preventDefault();
  422. return true;
  423. }
  424. var $form = $('#team-create-form');
  425. $form.attr('action', $form.data('delete-url'));
  426. });
  427. }
  428. function initTeamMembersList() {
  429. // Add team member.
  430. var $ul = $('#org-team-members-list');
  431. $('#org-team-members-add').on('keyup', function () {
  432. var $this = $(this);
  433. if (!$this.val()) {
  434. $ul.toggleHide();
  435. return;
  436. }
  437. Gogs.searchUsers($this.val(), $ul);
  438. }).on('focus', function () {
  439. if (!$(this).val()) {
  440. $ul.toggleHide();
  441. } else {
  442. $ul.toggleShow();
  443. }
  444. }).next().next().find('ul').on("click", 'li', function () {
  445. $('#org-team-members-add').val($(this).text());
  446. $ul.toggleHide();
  447. });
  448. }
  449. function initTeamRepositoriesList() {
  450. // Add team repository.
  451. var $ul = $('#org-team-repositories-list');
  452. $('#org-team-repositories-add').on('keyup', function () {
  453. var $this = $(this);
  454. if (!$this.val()) {
  455. $ul.toggleHide();
  456. return;
  457. }
  458. Gogs.searchRepos($this.val(), $ul, 'uid=' + $this.data('uid'));
  459. }).on('focus', function () {
  460. if (!$(this).val()) {
  461. $ul.toggleHide();
  462. } else {
  463. $ul.toggleShow();
  464. }
  465. }).next().next().find('ul').on("click", 'li', function () {
  466. $('#org-team-repositories-add').val($(this).text());
  467. $ul.toggleHide();
  468. });
  469. }
  470. function initAdmin() {
  471. // Create account.
  472. $('#login-type').on("change", function () {
  473. var v = $(this).val();
  474. if (v.indexOf("0-") + 1) {
  475. $('.auth-name').toggleHide();
  476. $(".pwd").find("input").attr("required", "required")
  477. .end().toggleShow();
  478. } else {
  479. $(".pwd").find("input").removeAttr("required")
  480. .end().toggleHide();
  481. $('.auth-name').toggleShow();
  482. }
  483. });
  484. // Delete account.
  485. $('#user-delete').click(function (e) {
  486. if (!confirm('This account is going to be deleted, do you want to continue?')) {
  487. e.preventDefault();
  488. return true;
  489. }
  490. var $form = $('user-profile-form');
  491. $form.attr('action', $form.data('delete-url'));
  492. });
  493. // Create authorization.
  494. $('#auth-type').on("change", function () {
  495. var v = $(this).val();
  496. if (v == 2) {
  497. $('.ldap').toggleShow();
  498. $('.smtp').toggleHide();
  499. }
  500. if (v == 3) {
  501. $('.smtp').toggleShow();
  502. $('.ldap').toggleHide();
  503. }
  504. });
  505. // Delete authorization.
  506. $('#auth-delete').click(function (e) {
  507. if (!confirm('This authorization is going to be deleted, do you want to continue?')) {
  508. e.preventDefault();
  509. return true;
  510. }
  511. var $form = $('auth-setting-form');
  512. $form.attr('action', $form.data('delete-url'));
  513. });
  514. }
  515. $(document).ready(function () {
  516. initCore();
  517. if ($('#user-profile-setting').length) {
  518. initUserSetting();
  519. }
  520. if ($('#repo-create-form').length || $('#repo-migrate-form').length) {
  521. initRepoCreate();
  522. }
  523. if ($('#repo-header').length) {
  524. initRepo();
  525. }
  526. if ($('#repo-setting').length) {
  527. initRepoSetting();
  528. }
  529. if ($('#org-setting').length) {
  530. initOrgSetting();
  531. }
  532. if ($('#invite-box').length) {
  533. initInvite();
  534. }
  535. if ($('#team-create-form').length) {
  536. initOrgTeamCreate();
  537. }
  538. if ($('#team-members-list').length) {
  539. initTeamMembersList();
  540. }
  541. if ($('#team-repositories-list').length) {
  542. initTeamRepositoriesList();
  543. }
  544. if ($('#admin-setting').length) {
  545. initAdmin();
  546. }
  547. Tabs('#dashboard-sidebar-menu');
  548. homepage();
  549. // Fix language drop-down menu height.
  550. var l = $('#footer-lang li').length;
  551. $('#footer-lang .drop-down').css({
  552. "top": (-31 * l) + "px",
  553. "height": (31 * l - 3) + "px"
  554. });
  555. });
  556. function homepage() {
  557. // Change method to GET if no username input.
  558. $('#promo-form').submit(function (e) {
  559. if ($('#username').val() === "") {
  560. e.preventDefault();
  561. window.location.href = '/user/login';
  562. return true
  563. }
  564. });
  565. // Redirect to register page.
  566. $('#register-button').click(function (e) {
  567. if ($('#username').val() === "") {
  568. e.preventDefault();
  569. window.location.href = '/user/sign_up';
  570. return true
  571. }
  572. $('#promo-form').attr('action', '/user/sign_up');
  573. });
  574. }