gogs.js 31 KB

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