gogs.js 26 KB

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