gogs.js 26 KB

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