gogs.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. // Collapse.
  285. $('.collapse').hide();
  286. }
  287. function initUserSetting() {
  288. // Confirmation of change username in user profile page.
  289. var $username = $('#username');
  290. var $profile_form = $('#user-profile-form');
  291. $('#change-username-btn').magnificPopup({
  292. modal: true,
  293. callbacks: {
  294. open: function () {
  295. if (($username.data('uname') == $username.val())) {
  296. $.magnificPopup.close();
  297. $profile_form.submit();
  298. }
  299. }
  300. }
  301. }).click(function () {
  302. if (($username.data('uname') != $username.val())) {
  303. e.preventDefault();
  304. return true;
  305. }
  306. });
  307. $('#change-username-submit').click(function () {
  308. $.magnificPopup.close();
  309. $profile_form.submit();
  310. });
  311. // Show add SSH key panel.
  312. $('#ssh-add').click(function () {
  313. $('#user-ssh-add-form').removeClass("hide");
  314. });
  315. // Confirmation of delete account.
  316. $('#delete-account-btn').magnificPopup({
  317. modal: true
  318. }).click(function (e) {
  319. e.preventDefault();
  320. return true;
  321. });
  322. $('#delete-account-submit').click(function () {
  323. $.magnificPopup.close();
  324. $('#delete-account-form').submit();
  325. });
  326. }
  327. function initRepoCreate() {
  328. // Owner switch menu click.
  329. $('#repo-create-owner-list').on('click', 'li', function () {
  330. if (!$(this).hasClass('checked')) {
  331. var uid = $(this).data('uid');
  332. $('#repo-owner-id').val(uid);
  333. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  334. $('#repo-owner-name').text($(this).text().trim());
  335. $(this).parent().find('.checked').removeClass('checked');
  336. $(this).addClass('checked');
  337. console.log("set repo owner to uid :", uid, $(this).text().trim());
  338. }
  339. });
  340. $('#auth-button').click(function (e) {
  341. $('#repo-migrate-auth').slideToggle('fast');
  342. e.preventDefault();
  343. })
  344. console.log('initRepoCreate');
  345. }
  346. function initRepo() {
  347. // Clone link switch button.
  348. $('#repo-clone-ssh').click(function () {
  349. $(this).removeClass('btn-gray').addClass('btn-blue');
  350. $('#repo-clone-https').removeClass('btn-blue').addClass('btn-gray');
  351. $('#repo-clone-url').val($(this).data('link'));
  352. $('.clone-url').text($(this).data('link'))
  353. });
  354. $('#repo-clone-https').click(function () {
  355. $(this).removeClass('btn-gray').addClass('btn-blue');
  356. $('#repo-clone-ssh').removeClass('btn-blue').addClass('btn-gray');
  357. $('#repo-clone-url').val($(this).data('link'));
  358. $('.clone-url').text($(this).data('link'))
  359. });
  360. // Copy URL.
  361. var $clone_btn = $('#repo-clone-copy');
  362. $clone_btn.hover(function () {
  363. Gogs.bindCopy($(this));
  364. })
  365. $clone_btn.tipsy({
  366. fade: true
  367. });
  368. }
  369. // when user changes hook type, hide/show proper divs
  370. function initHookTypeChange() {
  371. // web hook type change
  372. $('select#hook-type').on("change", function () {
  373. hookTypes = ['Gogs', 'Slack'];
  374. var curHook = $(this).val();
  375. hookTypes.forEach(function (hookType) {
  376. if (curHook === hookType) {
  377. $('div#' + hookType.toLowerCase()).toggleShow();
  378. }
  379. else {
  380. $('div#' + hookType.toLowerCase()).toggleHide();
  381. }
  382. });
  383. });
  384. }
  385. function initRepoSetting() {
  386. // Options.
  387. // Confirmation of changing repository name.
  388. var $reponame = $('#repo_name');
  389. var $setting_form = $('#repo-setting-form');
  390. $('#change-reponame-btn').magnificPopup({
  391. modal: true,
  392. callbacks: {
  393. open: function () {
  394. if (($reponame.data('repo-name') == $reponame.val())) {
  395. $.magnificPopup.close();
  396. $setting_form.submit();
  397. }
  398. }
  399. }
  400. }).click(function () {
  401. if (($reponame.data('repo-name') != $reponame.val())) {
  402. e.preventDefault();
  403. return true;
  404. }
  405. });
  406. $('#change-reponame-submit').click(function () {
  407. $.magnificPopup.close();
  408. $setting_form.submit();
  409. });
  410. initHookTypeChange();
  411. // Transfer repository.
  412. $('#transfer-repo-btn').magnificPopup({
  413. modal: true
  414. });
  415. $('#transfer-repo-submit').click(function () {
  416. $.magnificPopup.close();
  417. $('#transfer-repo-form').submit();
  418. });
  419. // Delete repository.
  420. $('#delete-repo-btn').magnificPopup({
  421. modal: true
  422. });
  423. $('#delete-repo-submit').click(function () {
  424. $.magnificPopup.close();
  425. $('#delete-repo-form').submit();
  426. });
  427. // Collaboration.
  428. $('#repo-collab-list hr:last-child').remove();
  429. var $ul = $('#repo-collaborator').next().next().find('ul');
  430. $('#repo-collaborator').on('keyup', function () {
  431. var $this = $(this);
  432. if (!$this.val()) {
  433. $ul.toggleHide();
  434. return;
  435. }
  436. Gogs.searchUsers($this.val(), $ul);
  437. }).on('focus', function () {
  438. if (!$(this).val()) {
  439. $ul.toggleHide();
  440. } else {
  441. $ul.toggleShow();
  442. }
  443. }).next().next().find('ul').on("click", 'li', function () {
  444. $('#repo-collaborator').val($(this).text());
  445. $ul.toggleHide();
  446. });
  447. }
  448. function initOrgSetting() {
  449. // Options.
  450. // Confirmation of changing organization name.
  451. var $orgname = $('#orgname');
  452. var $setting_form = $('#org-setting-form');
  453. $('#change-orgname-btn').magnificPopup({
  454. modal: true,
  455. callbacks: {
  456. open: function () {
  457. if (($orgname.data('orgname') == $orgname.val())) {
  458. $.magnificPopup.close();
  459. $setting_form.submit();
  460. }
  461. }
  462. }
  463. }).click(function () {
  464. if (($orgname.data('orgname') != $orgname.val())) {
  465. e.preventDefault();
  466. return true;
  467. }
  468. });
  469. $('#change-orgname-submit').click(function () {
  470. $.magnificPopup.close();
  471. $setting_form.submit();
  472. });
  473. // Confirmation of delete organization.
  474. $('#delete-org-btn').magnificPopup({
  475. modal: true
  476. }).click(function (e) {
  477. e.preventDefault();
  478. return true;
  479. });
  480. $('#delete-org-submit').click(function () {
  481. $.magnificPopup.close();
  482. $('#delete-org-form').submit();
  483. });
  484. initHookTypeChange();
  485. }
  486. function initInvite() {
  487. // Invitation.
  488. var $ul = $('#org-member-invite-list');
  489. $('#org-member-invite').on('keyup', function () {
  490. var $this = $(this);
  491. if (!$this.val()) {
  492. $ul.toggleHide();
  493. return;
  494. }
  495. Gogs.searchUsers($this.val(), $ul);
  496. }).on('focus', function () {
  497. if (!$(this).val()) {
  498. $ul.toggleHide();
  499. } else {
  500. $ul.toggleShow();
  501. }
  502. }).next().next().find('ul').on("click", 'li', function () {
  503. $('#org-member-invite').val($(this).text());
  504. $ul.toggleHide();
  505. });
  506. }
  507. function initOrgTeamCreate() {
  508. // Delete team.
  509. $('#org-team-delete').magnificPopup({
  510. modal: true
  511. }).click(function (e) {
  512. e.preventDefault();
  513. return true;
  514. });
  515. $('#delete-team-submit').click(function () {
  516. $.magnificPopup.close();
  517. var $form = $('#team-create-form');
  518. $form.attr('action', $form.data('delete-url'));
  519. });
  520. }
  521. function initTeamMembersList() {
  522. // Add team member.
  523. var $ul = $('#org-team-members-list');
  524. $('#org-team-members-add').on('keyup', function () {
  525. var $this = $(this);
  526. if (!$this.val()) {
  527. $ul.toggleHide();
  528. return;
  529. }
  530. Gogs.searchUsers($this.val(), $ul);
  531. }).on('focus', function () {
  532. if (!$(this).val()) {
  533. $ul.toggleHide();
  534. } else {
  535. $ul.toggleShow();
  536. }
  537. }).next().next().find('ul').on("click", 'li', function () {
  538. $('#org-team-members-add').val($(this).text());
  539. $ul.toggleHide();
  540. });
  541. }
  542. function initTeamRepositoriesList() {
  543. // Add team repository.
  544. var $ul = $('#org-team-repositories-list');
  545. $('#org-team-repositories-add').on('keyup', function () {
  546. var $this = $(this);
  547. if (!$this.val()) {
  548. $ul.toggleHide();
  549. return;
  550. }
  551. Gogs.searchRepos($this.val(), $ul, 'uid=' + $this.data('uid'));
  552. }).on('focus', function () {
  553. if (!$(this).val()) {
  554. $ul.toggleHide();
  555. } else {
  556. $ul.toggleShow();
  557. }
  558. }).next().next().find('ul').on("click", 'li', function () {
  559. $('#org-team-repositories-add').val($(this).text());
  560. $ul.toggleHide();
  561. });
  562. }
  563. function initAdmin() {
  564. // Create account.
  565. $('#login-type').on("change", function () {
  566. var v = $(this).val();
  567. if (v.indexOf("0-") + 1) {
  568. $('.auth-name').toggleHide();
  569. $(".pwd").find("input").attr("required", "required")
  570. .end().toggleShow();
  571. } else {
  572. $(".pwd").find("input").removeAttr("required")
  573. .end().toggleHide();
  574. $('.auth-name').toggleShow();
  575. }
  576. });
  577. // Delete account.
  578. $('#delete-account-btn').magnificPopup({
  579. modal: true
  580. }).click(function (e) {
  581. e.preventDefault();
  582. return true;
  583. });
  584. $('#delete-account-submit').click(function () {
  585. $.magnificPopup.close();
  586. var $form = $('#user-profile-form');
  587. $form.attr('action', $form.data('delete-url'));
  588. });
  589. // Create authorization.
  590. $('#auth-type').on("change", function () {
  591. var v = $(this).val();
  592. if (v == 2) {
  593. $('.ldap').toggleShow();
  594. $('.smtp').toggleHide();
  595. }
  596. if (v == 3) {
  597. $('.smtp').toggleShow();
  598. $('.ldap').toggleHide();
  599. }
  600. });
  601. // Delete authorization.
  602. $('#delete-auth-btn').magnificPopup({
  603. modal: true
  604. }).click(function (e) {
  605. e.preventDefault();
  606. return true;
  607. });
  608. $('#delete-auth-submit').click(function () {
  609. $.magnificPopup.close();
  610. var $form = $('#auth-setting-form');
  611. $form.attr('action', $form.data('delete-url'));
  612. });
  613. }
  614. function initInstall() {
  615. // Change database type.
  616. (function () {
  617. var mysql_default = '127.0.0.1:3306';
  618. var postgres_default = '127.0.0.1:5432';
  619. $('#install-database').on("change", function () {
  620. var val = $(this).val();
  621. if (val != "SQLite3") {
  622. $('.server-sql').show();
  623. $('.sqlite-setting').addClass("hide");
  624. if (val == "PostgreSQL") {
  625. $('.pgsql-setting').removeClass("hide");
  626. // Change the host value to the Postgres default, but only
  627. // if the user hasn't already changed it from the MySQL
  628. // default.
  629. if ($('#database-host').val() == mysql_default) {
  630. $('#database-host').val(postgres_default);
  631. }
  632. } else if (val == 'MySQL') {
  633. $('.pgsql-setting').addClass("hide");
  634. if ($('#database-host').val() == postgres_default) {
  635. $('#database-host').val(mysql_default);
  636. }
  637. } else {
  638. $('.pgsql-setting').addClass("hide");
  639. }
  640. } else {
  641. $('.server-sql').hide();
  642. $('.pgsql-setting').hide();
  643. $('.sqlite-setting').removeClass("hide");
  644. }
  645. });
  646. }());
  647. }
  648. function initProfile() {
  649. // Avatar.
  650. $('#profile-avatar').tipsy({
  651. fade: true
  652. });
  653. }
  654. function initTimeSwitch() {
  655. // Time switch.
  656. $(".time-since[title]").on("click", function () {
  657. var $this = $(this);
  658. var title = $this.attr("title");
  659. var text = $this.text();
  660. $this.text(title);
  661. $this.attr("title", text);
  662. });
  663. }
  664. function initDiff() {
  665. $('.diff-detail-box>a').click(function () {
  666. $($(this).data('target')).slideToggle(100);
  667. })
  668. var $counter = $('.diff-counter');
  669. if ($counter.length < 1) {
  670. return;
  671. }
  672. $counter.each(function (i, item) {
  673. var $item = $(item);
  674. var addLine = $item.find('span[data-line].add').data("line");
  675. var delLine = $item.find('span[data-line].del').data("line");
  676. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  677. $item.find(".bar .add").css("width", addPercent + "%");
  678. });
  679. }
  680. $(document).ready(function () {
  681. Gogs.AppSubUrl = $('head').data('suburl') || '';
  682. initCore();
  683. if ($('#user-profile-setting').length) {
  684. initUserSetting();
  685. }
  686. if ($('#repo-create-form').length || $('#repo-migrate-form').length) {
  687. initRepoCreate();
  688. }
  689. if ($('#repo-header').length) {
  690. initRepo();
  691. }
  692. if ($('#repo-setting').length) {
  693. initRepoSetting();
  694. }
  695. if ($('#org-setting').length) {
  696. initOrgSetting();
  697. }
  698. if ($('#invite-box').length) {
  699. initInvite();
  700. }
  701. if ($('#team-create-form').length) {
  702. initOrgTeamCreate();
  703. }
  704. if ($('#team-members-list').length) {
  705. initTeamMembersList();
  706. }
  707. if ($('#team-repositories-list').length) {
  708. initTeamRepositoriesList();
  709. }
  710. if ($('#admin-setting').length) {
  711. initAdmin();
  712. }
  713. if ($('#install-form').length) {
  714. initInstall();
  715. }
  716. if ($('#user-profile-page').length) {
  717. initProfile();
  718. }
  719. if ($('#diff-page').length) {
  720. initTimeSwitch();
  721. initDiff();
  722. }
  723. $('#dashboard-sidebar-menu').tabs();
  724. $('#pull-issue-preview').markdown_preview(".issue-add-comment");
  725. homepage();
  726. // Fix language drop-down menu height.
  727. var l = $('#footer-lang li').length;
  728. $('#footer-lang .drop-down').css({
  729. "top": (-31 * l) + "px",
  730. "height": (31 * l - 3) + "px"
  731. });
  732. });
  733. function homepage() {
  734. // Change method to GET if no username input.
  735. $('#promo-form').submit(function (e) {
  736. if ($('#username').val() === "") {
  737. e.preventDefault();
  738. window.location.href = Gogs.AppSubUrl + '/user/login';
  739. return true
  740. }
  741. });
  742. // Redirect to register page.
  743. $('#register-button').click(function (e) {
  744. if ($('#username').val() === "") {
  745. e.preventDefault();
  746. window.location.href = Gogs.AppSubUrl + '/user/sign_up';
  747. return true
  748. }
  749. $('#promo-form').attr('action', Gogs.AppSubUrl + '/user/sign_up');
  750. });
  751. }