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.

772 lines
24 KiB

  1. // @codekit-prepend "lib/jquery-1.11.1.min.js"
  2. // @codekit-prepend "lib/lib.js"
  3. // @codekit-prepend "utils/tabs.js"
  4. // @codekit-prepend "utils/preview.js"
  5. // @codekit-prepend "lib/jquery.tipsy.js"
  6. var Gogs = {};
  7. (function ($) {
  8. // Extend jQuery ajax, set CSRF token value.
  9. var ajax = $.ajax;
  10. $.extend({
  11. ajax: function (url, options) {
  12. if (typeof url === 'object') {
  13. options = url;
  14. url = undefined;
  15. }
  16. options = options || {};
  17. url = options.url;
  18. var csrftoken = $('meta[name=_csrf]').attr('content');
  19. var headers = options.headers || {};
  20. var domain = document.domain.replace(/\./ig, '\\.');
  21. if (!/^(http:|https:).*/.test(url) || eval('/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/').test(url)) {
  22. headers = $.extend(headers, {'X-Csrf-Token': csrftoken});
  23. }
  24. options.headers = headers;
  25. var callback = options.success;
  26. options.success = function (data) {
  27. if (data.once) {
  28. // change all _once value if ajax data.once exist
  29. $('[name=_once]').val(data.once);
  30. }
  31. if (callback) {
  32. callback.apply(this, arguments);
  33. }
  34. };
  35. return ajax(url, options);
  36. },
  37. changeHash: function (hash) {
  38. if (history.pushState) {
  39. history.pushState(null, null, hash);
  40. }
  41. else {
  42. location.hash = hash;
  43. }
  44. },
  45. deSelect: function () {
  46. if (window.getSelection) {
  47. window.getSelection().removeAllRanges();
  48. } else {
  49. document.selection.empty();
  50. }
  51. }
  52. });
  53. $.fn.extend({
  54. toggleHide: function () {
  55. $(this).addClass("hidden");
  56. },
  57. toggleShow: function () {
  58. $(this).removeClass("hidden");
  59. },
  60. toggleAjax: function (successCallback, errorCallback) {
  61. var url = $(this).data("ajax");
  62. var method = $(this).data('ajax-method') || 'get';
  63. var ajaxName = $(this).data('ajax-name');
  64. var data = {};
  65. if (ajaxName.endsWith("preview")) {
  66. data["mode"] = "gfm";
  67. data["context"] = $(this).data('ajax-context');
  68. }
  69. $('[data-ajax-rel=' + ajaxName + ']').each(function () {
  70. var field = $(this).data("ajax-field");
  71. var t = $(this).data("ajax-val");
  72. if (t == "val") {
  73. data[field] = $(this).val();
  74. return true;
  75. }
  76. if (t == "txt") {
  77. data[field] = $(this).text();
  78. return true;
  79. }
  80. if (t == "html") {
  81. data[field] = $(this).html();
  82. return true;
  83. }
  84. if (t == "data") {
  85. data[field] = $(this).data("ajax-data");
  86. return true;
  87. }
  88. return true;
  89. });
  90. console.log("toggleAjax:", method, url, data);
  91. $.ajax({
  92. url: url,
  93. method: method.toUpperCase(),
  94. data: data,
  95. error: errorCallback,
  96. success: function (d) {
  97. if (successCallback) {
  98. successCallback(d);
  99. }
  100. }
  101. })
  102. }
  103. });
  104. }(jQuery));
  105. (function ($) {
  106. // Render markdown.
  107. Gogs.renderMarkdown = function () {
  108. var $md = $('.markdown');
  109. var $pre = $md.find('pre > code').parent();
  110. $pre.addClass('prettyprint');
  111. prettyPrint();
  112. // Set anchor.
  113. var headers = {};
  114. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  115. var node = $(this);
  116. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  117. var name = val;
  118. if (headers[val] > 0) {
  119. name = val + '-' + headers[val];
  120. }
  121. if (headers[val] == undefined) {
  122. headers[val] = 1;
  123. } else {
  124. headers[val] += 1;
  125. }
  126. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  127. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  128. });
  129. };
  130. // Render code view.
  131. Gogs.renderCodeView = function () {
  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. var $first;
  177. if (m) {
  178. $first = $list.filter('.' + m[1]);
  179. selectRange($list, $first, $list.filter('.' + m[2]));
  180. $("html, body").scrollTop($first.offset().top - 200);
  181. return;
  182. }
  183. m = window.location.hash.match(/^#(L\d+)$/);
  184. if (m) {
  185. $first = $list.filter('.' + m[1]);
  186. selectRange($list, $first);
  187. $("html, body").scrollTop($first.offset().top - 200);
  188. }
  189. }).trigger('hashchange');
  190. };
  191. // Search users by keyword.
  192. Gogs.searchUsers = function (val, $target) {
  193. $.ajax({
  194. url: Gogs.AppSubUrl + '/api/v1/users/search?q=' + val,
  195. dataType: "json",
  196. success: function (json) {
  197. if (json.ok && json.data.length) {
  198. var html = '';
  199. $.each(json.data, function (i, item) {
  200. html += '<li><a><img src="' + item.avatar + '">' + item.username + '</a></li>';
  201. });
  202. $target.html(html);
  203. $target.toggleShow();
  204. } else {
  205. $target.toggleHide();
  206. }
  207. }
  208. });
  209. }
  210. // Search repositories by keyword.
  211. Gogs.searchRepos = function (val, $target, $param) {
  212. $.ajax({
  213. url: Gogs.AppSubUrl + '/api/v1/repos/search?q=' + val + '&' + $param,
  214. dataType: "json",
  215. success: function (json) {
  216. if (json.ok && json.data.length) {
  217. var html = '';
  218. $.each(json.data, function (i, item) {
  219. html += '<li><a><span class="octicon octicon-repo"></span> ' + item.repolink + '</a></li>';
  220. });
  221. $target.html(html);
  222. $target.toggleShow();
  223. } else {
  224. $target.toggleHide();
  225. }
  226. }
  227. });
  228. }
  229. // Copy util.
  230. Gogs.bindCopy = function (selector) {
  231. if ($(selector).hasClass('js-copy-bind')) {
  232. return;
  233. }
  234. $(selector).zclip({
  235. path: Gogs.AppSubUrl + "/js/ZeroClipboard.swf",
  236. copy: function () {
  237. var t = $(this).data("copy-val");
  238. var to = $($(this).data("copy-from"));
  239. var str = "";
  240. if (t == "txt") {
  241. str = to.text();
  242. }
  243. if (t == 'val') {
  244. str = to.val();
  245. }
  246. if (t == 'html') {
  247. str = to.html();
  248. }
  249. return str;
  250. },
  251. afterCopy: function () {
  252. var $this = $(this);
  253. $this.tipsy("hide").attr('original-title', $this.data('after-title'));
  254. setTimeout(function () {
  255. $this.tipsy("show");
  256. }, 200);
  257. setTimeout(function () {
  258. $this.tipsy('hide').attr('original-title', $this.data('original-title'));
  259. }, 2000);
  260. }
  261. }).addClass("js-copy-bind");
  262. }
  263. })(jQuery);
  264. function initCore() {
  265. Gogs.renderMarkdown();
  266. Gogs.renderCodeView();
  267. // Switch list.
  268. $('.js-tab-nav').click(function (e) {
  269. if (!$(this).hasClass('js-tab-nav-show')) {
  270. $(this).parent().find('.js-tab-nav-show').each(function () {
  271. $(this).removeClass('js-tab-nav-show');
  272. $($(this).data('tab-target')).hide();
  273. });
  274. $(this).addClass('js-tab-nav-show');
  275. $($(this).data('tab-target')).show();
  276. }
  277. e.preventDefault();
  278. });
  279. // Popup.
  280. $(document).on('click', '.popup-modal-dismiss', function (e) {
  281. e.preventDefault();
  282. $.magnificPopup.close();
  283. });
  284. }
  285. function initUserSetting() {
  286. // Confirmation of change username in user profile page.
  287. var $username = $('#username');
  288. var $profile_form = $('#user-profile-form');
  289. $('#change-username-btn').magnificPopup({
  290. modal: true,
  291. callbacks: {
  292. open: function () {
  293. if (($username.data('uname') == $username.val())) {
  294. $.magnificPopup.close();
  295. $profile_form.submit();
  296. }
  297. }
  298. }
  299. }).click(function () {
  300. if (($username.data('uname') != $username.val())) {
  301. e.preventDefault();
  302. return true;
  303. }
  304. });
  305. $('#change-username-submit').click(function () {
  306. $.magnificPopup.close();
  307. $profile_form.submit();
  308. });
  309. // Show add SSH key panel.
  310. $('#ssh-add').click(function () {
  311. $('#user-ssh-add-form').removeClass("hide");
  312. });
  313. // Confirmation of delete account.
  314. $('#delete-account-btn').magnificPopup({
  315. modal: true
  316. }).click(function (e) {
  317. e.preventDefault();
  318. return true;
  319. });
  320. $('#delete-account-submit').click(function () {
  321. $.magnificPopup.close();
  322. $('#delete-account-form').submit();
  323. });
  324. }
  325. function initRepoCreate() {
  326. // Owner switch menu click.
  327. $('#repo-create-owner-list').on('click', 'li', function () {
  328. if (!$(this).hasClass('checked')) {
  329. var uid = $(this).data('uid');
  330. $('#repo-owner-id').val(uid);
  331. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  332. $('#repo-owner-name').text($(this).text().trim());
  333. $(this).parent().find('.checked').removeClass('checked');
  334. $(this).addClass('checked');
  335. console.log("set repo owner to uid :", uid, $(this).text().trim());
  336. }
  337. });
  338. $('#auth-button').click(function (e) {
  339. $('#repo-migrate-auth').slideToggle('fast');
  340. e.preventDefault();
  341. })
  342. console.log('initRepoCreate');
  343. }
  344. function initRepo() {
  345. // Clone link switch button.
  346. $('#repo-clone-ssh').click(function () {
  347. $(this).removeClass('btn-gray').addClass('btn-blue');
  348. $('#repo-clone-https').removeClass('btn-blue').addClass('btn-gray');
  349. $('#repo-clone-url').val($(this).data('link'));
  350. $('.clone-url').text($(this).data('link'))
  351. });
  352. $('#repo-clone-https').click(function () {
  353. $(this).removeClass('btn-gray').addClass('btn-blue');
  354. $('#repo-clone-ssh').removeClass('btn-blue').addClass('btn-gray');
  355. $('#repo-clone-url').val($(this).data('link'));
  356. $('.clone-url').text($(this).data('link'))
  357. });
  358. // Copy URL.
  359. var $clone_btn = $('#repo-clone-copy');
  360. $clone_btn.hover(function () {
  361. Gogs.bindCopy($(this));
  362. })
  363. $clone_btn.tipsy({
  364. fade: true
  365. });
  366. }
  367. // when user changes hook type, hide/show proper divs
  368. function initHookTypeChange() {
  369. // web hook type change
  370. $('select#hook-type').on("change", function () {
  371. hookTypes = ['Gogs', 'Slack'];
  372. var curHook = $(this).val();
  373. hookTypes.forEach(function (hookType) {
  374. if (curHook === hookType) {
  375. $('div#' + hookType.toLowerCase()).toggleShow();
  376. }
  377. else {
  378. $('div#' + hookType.toLowerCase()).toggleHide();
  379. }
  380. });
  381. });
  382. }
  383. function initRepoSetting() {
  384. // Options.
  385. // Confirmation of changing repository name.
  386. var $reponame = $('#repo_name');
  387. var $setting_form = $('#repo-setting-form');
  388. $('#change-reponame-btn').magnificPopup({
  389. modal: true,
  390. callbacks: {
  391. open: function () {
  392. if (($reponame.data('repo-name') == $reponame.val())) {
  393. $.magnificPopup.close();
  394. $setting_form.submit();
  395. }
  396. }
  397. }
  398. }).click(function () {
  399. if (($reponame.data('repo-name') != $reponame.val())) {
  400. e.preventDefault();
  401. return true;
  402. }
  403. });
  404. $('#change-reponame-submit').click(function () {
  405. $.magnificPopup.close();
  406. $setting_form.submit();
  407. });
  408. initHookTypeChange();
  409. // Transfer repository.
  410. $('#transfer-repo-btn').magnificPopup({
  411. modal: true
  412. });
  413. $('#transfer-repo-submit').click(function () {
  414. $.magnificPopup.close();
  415. $('#transfer-repo-form').submit();
  416. });
  417. // Delete repository.
  418. $('#delete-repo-btn').magnificPopup({
  419. modal: true
  420. });
  421. $('#delete-repo-submit').click(function () {
  422. $.magnificPopup.close();
  423. $('#delete-repo-form').submit();
  424. });
  425. // Collaboration.
  426. $('#repo-collab-list hr:last-child').remove();
  427. var $ul = $('#repo-collaborator').next().next().find('ul');
  428. $('#repo-collaborator').on('keyup', function () {
  429. var $this = $(this);
  430. if (!$this.val()) {
  431. $ul.toggleHide();
  432. return;
  433. }
  434. Gogs.searchUsers($this.val(), $ul);
  435. }).on('focus', function () {
  436. if (!$(this).val()) {
  437. $ul.toggleHide();
  438. } else {
  439. $ul.toggleShow();
  440. }
  441. }).next().next().find('ul').on("click", 'li', function () {
  442. $('#repo-collaborator').val($(this).text());
  443. $ul.toggleHide();
  444. });
  445. }
  446. function initOrgSetting() {
  447. // Options.
  448. // Confirmation of changing organization name.
  449. var $orgname = $('#orgname');
  450. var $setting_form = $('#org-setting-form');
  451. $('#change-orgname-btn').magnificPopup({
  452. modal: true,
  453. callbacks: {
  454. open: function () {
  455. if (($orgname.data('orgname') == $orgname.val())) {
  456. $.magnificPopup.close();
  457. $setting_form.submit();
  458. }
  459. }
  460. }
  461. }).click(function () {
  462. if (($orgname.data('orgname') != $orgname.val())) {
  463. e.preventDefault();
  464. return true;
  465. }
  466. });
  467. $('#change-orgname-submit').click(function () {
  468. $.magnificPopup.close();
  469. $setting_form.submit();
  470. });
  471. // Confirmation of delete organization.
  472. $('#delete-org-btn').magnificPopup({
  473. modal: true
  474. }).click(function (e) {
  475. e.preventDefault();
  476. return true;
  477. });
  478. $('#delete-org-submit').click(function () {
  479. $.magnificPopup.close();
  480. $('#delete-org-form').submit();
  481. });
  482. initHookTypeChange();
  483. }
  484. function initInvite() {
  485. // Invitation.
  486. var $ul = $('#org-member-invite-list');
  487. $('#org-member-invite').on('keyup', function () {
  488. var $this = $(this);
  489. if (!$this.val()) {
  490. $ul.toggleHide();
  491. return;
  492. }
  493. Gogs.searchUsers($this.val(), $ul);
  494. }).on('focus', function () {
  495. if (!$(this).val()) {
  496. $ul.toggleHide();
  497. } else {
  498. $ul.toggleShow();
  499. }
  500. }).next().next().find('ul').on("click", 'li', function () {
  501. $('#org-member-invite').val($(this).text());
  502. $ul.toggleHide();
  503. });
  504. }
  505. function initOrgTeamCreate() {
  506. // Delete team.
  507. $('#org-team-delete').magnificPopup({
  508. modal: true
  509. }).click(function (e) {
  510. e.preventDefault();
  511. return true;
  512. });
  513. $('#delete-team-submit').click(function () {
  514. $.magnificPopup.close();
  515. var $form = $('#team-create-form');
  516. $form.attr('action', $form.data('delete-url'));
  517. });
  518. }
  519. function initTeamMembersList() {
  520. // Add team member.
  521. var $ul = $('#org-team-members-list');
  522. $('#org-team-members-add').on('keyup', function () {
  523. var $this = $(this);
  524. if (!$this.val()) {
  525. $ul.toggleHide();
  526. return;
  527. }
  528. Gogs.searchUsers($this.val(), $ul);
  529. }).on('focus', function () {
  530. if (!$(this).val()) {
  531. $ul.toggleHide();
  532. } else {
  533. $ul.toggleShow();
  534. }
  535. }).next().next().find('ul').on("click", 'li', function () {
  536. $('#org-team-members-add').val($(this).text());
  537. $ul.toggleHide();
  538. });
  539. }
  540. function initTeamRepositoriesList() {
  541. // Add team repository.
  542. var $ul = $('#org-team-repositories-list');
  543. $('#org-team-repositories-add').on('keyup', function () {
  544. var $this = $(this);
  545. if (!$this.val()) {
  546. $ul.toggleHide();
  547. return;
  548. }
  549. Gogs.searchRepos($this.val(), $ul, 'uid=' + $this.data('uid'));
  550. }).on('focus', function () {
  551. if (!$(this).val()) {
  552. $ul.toggleHide();
  553. } else {
  554. $ul.toggleShow();
  555. }
  556. }).next().next().find('ul').on("click", 'li', function () {
  557. $('#org-team-repositories-add').val($(this).text());
  558. $ul.toggleHide();
  559. });
  560. }
  561. function initAdmin() {
  562. // Create account.
  563. $('#login-type').on("change", function () {
  564. var v = $(this).val();
  565. if (v.indexOf("0-") + 1) {
  566. $('.auth-name').toggleHide();
  567. $(".pwd").find("input").attr("required", "required")
  568. .end().toggleShow();
  569. } else {
  570. $(".pwd").find("input").removeAttr("required")
  571. .end().toggleHide();
  572. $('.auth-name').toggleShow();
  573. }
  574. });
  575. // Delete account.
  576. $('#delete-account-btn').magnificPopup({
  577. modal: true
  578. }).click(function (e) {
  579. e.preventDefault();
  580. return true;
  581. });
  582. $('#delete-account-submit').click(function () {
  583. $.magnificPopup.close();
  584. var $form = $('#user-profile-form');
  585. $form.attr('action', $form.data('delete-url'));
  586. });
  587. // Create authorization.
  588. $('#auth-type').on("change", function () {
  589. var v = $(this).val();
  590. if (v == 2) {
  591. $('.ldap').toggleShow();
  592. $('.smtp').toggleHide();
  593. }
  594. if (v == 3) {
  595. $('.smtp').toggleShow();
  596. $('.ldap').toggleHide();
  597. }
  598. });
  599. // Delete authorization.
  600. $('#delete-auth-btn').magnificPopup({
  601. modal: true
  602. }).click(function (e) {
  603. e.preventDefault();
  604. return true;
  605. });
  606. $('#delete-auth-submit').click(function () {
  607. $.magnificPopup.close();
  608. var $form = $('#auth-setting-form');
  609. $form.attr('action', $form.data('delete-url'));
  610. });
  611. }
  612. function initInstall() {
  613. // Change database type.
  614. (function () {
  615. var mysql_default = '127.0.0.1:3306';
  616. var postgres_default = '127.0.0.1:5432';
  617. $('#install-database').on("change", function () {
  618. var val = $(this).val();
  619. if (val != "SQLite3") {
  620. $('.server-sql').show();
  621. $('.sqlite-setting').addClass("hide");
  622. if (val == "PostgreSQL") {
  623. $('.pgsql-setting').removeClass("hide");
  624. // Change the host value to the Postgres default, but only
  625. // if the user hasn't already changed it from the MySQL
  626. // default.
  627. if ($('#database-host').val() == mysql_default) {
  628. $('#database-host').val(postgres_default);
  629. }
  630. } else if (val == 'MySQL') {
  631. $('.pgsql-setting').addClass("hide");
  632. if ($('#database-host').val() == postgres_default) {
  633. $('#database-host').val(mysql_default);
  634. }
  635. } else {
  636. $('.pgsql-setting').addClass("hide");
  637. }
  638. } else {
  639. $('.server-sql').hide();
  640. $('.pgsql-setting').hide();
  641. $('.sqlite-setting').removeClass("hide");
  642. }
  643. });
  644. }());
  645. }
  646. function initProfile() {
  647. // Avatar.
  648. $('#profile-avatar').tipsy({
  649. fade: true
  650. });
  651. }
  652. $(document).ready(function () {
  653. Gogs.AppSubUrl = $('head').data('suburl') || '';
  654. initCore();
  655. if ($('#user-profile-setting').length) {
  656. initUserSetting();
  657. }
  658. if ($('#repo-create-form').length || $('#repo-migrate-form').length) {
  659. initRepoCreate();
  660. }
  661. if ($('#repo-header').length) {
  662. initRepo();
  663. }
  664. if ($('#repo-setting').length) {
  665. initRepoSetting();
  666. }
  667. if ($('#org-setting').length) {
  668. initOrgSetting();
  669. }
  670. if ($('#invite-box').length) {
  671. initInvite();
  672. }
  673. if ($('#team-create-form').length) {
  674. initOrgTeamCreate();
  675. }
  676. if ($('#team-members-list').length) {
  677. initTeamMembersList();
  678. }
  679. if ($('#team-repositories-list').length) {
  680. initTeamRepositoriesList();
  681. }
  682. if ($('#admin-setting').length) {
  683. initAdmin();
  684. }
  685. if ($('#install-form').length) {
  686. initInstall();
  687. }
  688. if ($('#user-profile-page').length) {
  689. initProfile();
  690. }
  691. $('#dashboard-sidebar-menu').tabs();
  692. $('#pull-issue-preview').markdown_preview(".issue-add-comment");
  693. homepage();
  694. // Fix language drop-down menu height.
  695. var l = $('#footer-lang li').length;
  696. $('#footer-lang .drop-down').css({
  697. "top": (-31 * l) + "px",
  698. "height": (31 * l - 3) + "px"
  699. });
  700. });
  701. function homepage() {
  702. // Change method to GET if no username input.
  703. $('#promo-form').submit(function (e) {
  704. if ($('#username').val() === "") {
  705. e.preventDefault();
  706. window.location.href = Gogs.AppSubUrl + '/user/login';
  707. return true
  708. }
  709. });
  710. // Redirect to register page.
  711. $('#register-button').click(function (e) {
  712. if ($('#username').val() === "") {
  713. e.preventDefault();
  714. window.location.href = Gogs.AppSubUrl + '/user/sign_up';
  715. return true
  716. }
  717. $('#promo-form').attr('action', Gogs.AppSubUrl + '/user/sign_up');
  718. });
  719. }