gogs.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. $(selector).zclip({
  317. path: Gogs.AppSubUrl + "/js/ZeroClipboard.swf",
  318. copy: function () {
  319. var t = $(this).data("copy-val");
  320. var to = $($(this).data("copy-from"));
  321. var str = "";
  322. if (t == "txt") {
  323. str = to.text();
  324. }
  325. if (t == 'val') {
  326. str = to.val();
  327. }
  328. if (t == 'html') {
  329. str = to.html();
  330. }
  331. return str;
  332. },
  333. afterCopy: function () {
  334. var $this = $(this);
  335. $this.tipsy("hide").attr('original-title', $this.data('after-title'));
  336. setTimeout(function () {
  337. $this.tipsy("show");
  338. }, 200);
  339. setTimeout(function () {
  340. $this.tipsy('hide').attr('original-title', $this.data('original-title'));
  341. }, 2000);
  342. }
  343. }).addClass("js-copy-bind");
  344. }
  345. })(jQuery);
  346. function initCore() {
  347. Gogs.renderMarkdown();
  348. if ($('.code-diff').length == 0) {
  349. Gogs.renderCodeView();
  350. } else {
  351. Gogs.renderDiffView();
  352. }
  353. // Switch list.
  354. $('.js-tab-nav').click(function (e) {
  355. if (!$(this).hasClass('js-tab-nav-show')) {
  356. $(this).parent().find('.js-tab-nav-show').each(function () {
  357. $(this).removeClass('js-tab-nav-show');
  358. $($(this).data('tab-target')).hide();
  359. });
  360. $(this).addClass('js-tab-nav-show');
  361. $($(this).data('tab-target')).show();
  362. }
  363. e.preventDefault();
  364. });
  365. // Popup.
  366. $(document).on('click', '.popup-modal-dismiss', function (e) {
  367. e.preventDefault();
  368. $.magnificPopup.close();
  369. });
  370. // Plugins.
  371. $('.collapse').hide();
  372. $('.tipsy-tooltip').tipsy({
  373. fade: true
  374. });
  375. }
  376. function initUserSetting() {
  377. // Confirmation of change username in user profile page.
  378. var $username = $('#username');
  379. var $profile_form = $('#user-profile-form');
  380. $('#change-username-btn').magnificPopup({
  381. modal: true,
  382. callbacks: {
  383. open: function () {
  384. if (($username.data('uname') == $username.val())) {
  385. $.magnificPopup.close();
  386. $profile_form.submit();
  387. }
  388. }
  389. }
  390. }).click(function () {
  391. if (($username.data('uname') != $username.val())) {
  392. e.preventDefault();
  393. return true;
  394. }
  395. });
  396. $('#change-username-submit').click(function () {
  397. $.magnificPopup.close();
  398. $profile_form.submit();
  399. });
  400. // Show panels.
  401. $('.show-form-btn').click(function () {
  402. $($(this).data('target-form')).removeClass("hide");
  403. });
  404. // Confirmation of delete account.
  405. $('#delete-account-btn').magnificPopup({
  406. modal: true
  407. }).click(function (e) {
  408. e.preventDefault();
  409. return true;
  410. });
  411. $('#delete-account-submit').click(function () {
  412. $.magnificPopup.close();
  413. $('#delete-account-form').submit();
  414. });
  415. }
  416. function initRepoCreate() {
  417. // Owner switch menu click.
  418. $('#repo-create-owner-list').on('click', 'li', function () {
  419. if (!$(this).hasClass('checked')) {
  420. var uid = $(this).data('uid');
  421. $('#repo-owner-id').val(uid);
  422. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  423. $('#repo-owner-name').text($(this).text().trim());
  424. $(this).parent().find('.checked').removeClass('checked');
  425. $(this).addClass('checked');
  426. console.log("set repo owner to uid :", uid, $(this).text().trim());
  427. }
  428. });
  429. $('#auth-button').click(function (e) {
  430. $('#repo-migrate-auth').slideToggle('fast');
  431. e.preventDefault();
  432. })
  433. console.log('initRepoCreate');
  434. }
  435. function initRepo() {
  436. // Clone link switch button.
  437. $('#repo-clone-ssh').click(function () {
  438. $(this).removeClass('btn-gray').addClass('btn-blue');
  439. $('#repo-clone-https').removeClass('btn-blue').addClass('btn-gray');
  440. $('#repo-clone-url').val($(this).data('link'));
  441. $('.clone-url').text($(this).data('link'))
  442. });
  443. $('#repo-clone-https').click(function () {
  444. $(this).removeClass('btn-gray').addClass('btn-blue');
  445. $('#repo-clone-ssh').removeClass('btn-blue').addClass('btn-gray');
  446. $('#repo-clone-url').val($(this).data('link'));
  447. $('.clone-url').text($(this).data('link'))
  448. });
  449. // Copy URL.
  450. var $clone_btn = $('#repo-clone-copy');
  451. $clone_btn.hover(function () {
  452. Gogs.bindCopy($(this));
  453. })
  454. $clone_btn.tipsy({
  455. fade: true
  456. });
  457. // Markdown preview.
  458. $('.markdown-preview').click(function() {
  459. var $this = $(this);
  460. $this.toggleAjax(function (resp) {
  461. $($this.data("preview")).html(resp);
  462. }, function () {
  463. $($this.data("preview")).html("no content");
  464. })
  465. });
  466. }
  467. // when user changes hook type, hide/show proper divs
  468. function initHookTypeChange() {
  469. // web hook type change
  470. $('select#hook-type').on("change", function () {
  471. hookTypes = ['Gogs', 'Slack'];
  472. var curHook = $(this).val();
  473. hookTypes.forEach(function (hookType) {
  474. if (curHook === hookType) {
  475. $('div#' + hookType.toLowerCase()).toggleShow();
  476. }
  477. else {
  478. $('div#' + hookType.toLowerCase()).toggleHide();
  479. }
  480. });
  481. });
  482. }
  483. function initRepoRelease() {
  484. $('#release-new-target-branch-list li').click(function() {
  485. if (!$(this).hasClass('checked')) {
  486. $('#repo-branch-current').text($(this).text());
  487. $('#tag-target').val($(this).text());
  488. $(this).parent().find('.checked').removeClass('checked');
  489. $(this).addClass('checked');
  490. }
  491. })
  492. }
  493. function initRepoSetting() {
  494. // Options.
  495. // Confirmation of changing repository name.
  496. var $reponame = $('#repo_name');
  497. var $setting_form = $('#repo-setting-form');
  498. $('#change-reponame-btn').magnificPopup({
  499. modal: true,
  500. callbacks: {
  501. open: function () {
  502. if (($reponame.data('repo-name') == $reponame.val())) {
  503. $.magnificPopup.close();
  504. $setting_form.submit();
  505. }
  506. }
  507. }
  508. }).click(function () {
  509. if (($reponame.data('repo-name') != $reponame.val())) {
  510. e.preventDefault();
  511. return true;
  512. }
  513. });
  514. $('#change-reponame-submit').click(function () {
  515. $.magnificPopup.close();
  516. $setting_form.submit();
  517. });
  518. initHookTypeChange();
  519. // Transfer repository.
  520. $('#transfer-repo-btn').magnificPopup({
  521. modal: true
  522. });
  523. $('#transfer-repo-submit').click(function () {
  524. $.magnificPopup.close();
  525. $('#transfer-repo-form').submit();
  526. });
  527. // Delete repository.
  528. $('#delete-repo-btn').magnificPopup({
  529. modal: true
  530. });
  531. $('#delete-repo-submit').click(function () {
  532. $.magnificPopup.close();
  533. $('#delete-repo-form').submit();
  534. });
  535. // Collaboration.
  536. $('#repo-collab-list hr:last-child').remove();
  537. var $ul = $('#repo-collaborator').next().next().find('ul');
  538. $('#repo-collaborator').on('keyup', function () {
  539. var $this = $(this);
  540. if (!$this.val()) {
  541. $ul.toggleHide();
  542. return;
  543. }
  544. Gogs.searchUsers($this.val(), $ul);
  545. }).on('focus', function () {
  546. if (!$(this).val()) {
  547. $ul.toggleHide();
  548. } else {
  549. $ul.toggleShow();
  550. }
  551. }).next().next().find('ul').on("click", 'li', function () {
  552. $('#repo-collaborator').val($(this).find('.username').text());
  553. $ul.toggleHide();
  554. });
  555. }
  556. function initOrgSetting() {
  557. // Options.
  558. // Confirmation of changing organization name.
  559. var $orgname = $('#orgname');
  560. var $setting_form = $('#org-setting-form');
  561. $('#change-orgname-btn').magnificPopup({
  562. modal: true,
  563. callbacks: {
  564. open: function () {
  565. if (($orgname.data('orgname') == $orgname.val())) {
  566. $.magnificPopup.close();
  567. $setting_form.submit();
  568. }
  569. }
  570. }
  571. }).click(function () {
  572. if (($orgname.data('orgname') != $orgname.val())) {
  573. e.preventDefault();
  574. return true;
  575. }
  576. });
  577. $('#change-orgname-submit').click(function () {
  578. $.magnificPopup.close();
  579. $setting_form.submit();
  580. });
  581. // Confirmation of delete organization.
  582. $('#delete-org-btn').magnificPopup({
  583. modal: true
  584. }).click(function (e) {
  585. e.preventDefault();
  586. return true;
  587. });
  588. $('#delete-org-submit').click(function () {
  589. $.magnificPopup.close();
  590. $('#delete-org-form').submit();
  591. });
  592. initHookTypeChange();
  593. }
  594. function initInvite() {
  595. // Invitation.
  596. var $ul = $('#org-member-invite-list');
  597. $('#org-member-invite').on('keyup', function () {
  598. var $this = $(this);
  599. if (!$this.val()) {
  600. $ul.toggleHide();
  601. return;
  602. }
  603. Gogs.searchUsers($this.val(), $ul);
  604. }).on('focus', function () {
  605. if (!$(this).val()) {
  606. $ul.toggleHide();
  607. } else {
  608. $ul.toggleShow();
  609. }
  610. }).next().next().find('ul').on("click", 'li', function () {
  611. $('#org-member-invite').val($(this).find('.username').text());
  612. $ul.toggleHide();
  613. });
  614. }
  615. function initOrgTeamCreate() {
  616. // Delete team.
  617. $('#org-team-delete').magnificPopup({
  618. modal: true
  619. }).click(function (e) {
  620. e.preventDefault();
  621. return true;
  622. });
  623. $('#delete-team-submit').click(function () {
  624. $.magnificPopup.close();
  625. var $form = $('#team-create-form');
  626. $form.attr('action', $form.data('delete-url'));
  627. });
  628. }
  629. function initTeamMembersList() {
  630. // Add team member.
  631. var $ul = $('#org-team-members-list');
  632. $('#org-team-members-add').on('keyup', function () {
  633. var $this = $(this);
  634. if (!$this.val()) {
  635. $ul.toggleHide();
  636. return;
  637. }
  638. Gogs.searchUsers($this.val(), $ul);
  639. }).on('focus', function () {
  640. if (!$(this).val()) {
  641. $ul.toggleHide();
  642. } else {
  643. $ul.toggleShow();
  644. }
  645. }).next().next().find('ul').on("click", 'li', function () {
  646. $('#org-team-members-add').val($(this).find('.username').text());
  647. $ul.toggleHide();
  648. });
  649. }
  650. function initTeamRepositoriesList() {
  651. // Add team repository.
  652. var $ul = $('#org-team-repositories-list');
  653. $('#org-team-repositories-add').on('keyup', function () {
  654. var $this = $(this);
  655. if (!$this.val()) {
  656. $ul.toggleHide();
  657. return;
  658. }
  659. Gogs.searchRepos($this.val(), $ul, 'uid=' + $this.data('uid'));
  660. }).on('focus', function () {
  661. if (!$(this).val()) {
  662. $ul.toggleHide();
  663. } else {
  664. $ul.toggleShow();
  665. }
  666. }).next().next().find('ul').on("click", 'li', function () {
  667. $('#org-team-repositories-add').val($(this).text());
  668. $ul.toggleHide();
  669. });
  670. }
  671. function initAdmin() {
  672. // Create account.
  673. $('#login-type').on("change", function () {
  674. var v = $(this).val();
  675. if (v.indexOf("0-") + 1) {
  676. $('.auth-name').toggleHide();
  677. $(".pwd").find("input").attr("required", "required")
  678. .end().toggleShow();
  679. } else {
  680. $(".pwd").find("input").removeAttr("required")
  681. .end().toggleHide();
  682. $('.auth-name').toggleShow();
  683. }
  684. });
  685. // Delete account.
  686. $('#delete-account-btn').magnificPopup({
  687. modal: true
  688. }).click(function (e) {
  689. e.preventDefault();
  690. return true;
  691. });
  692. $('#delete-account-submit').click(function () {
  693. $.magnificPopup.close();
  694. var $form = $('#user-profile-form');
  695. $form.attr('action', $form.data('delete-url'));
  696. });
  697. // Create authorization.
  698. $('#auth-type').on("change", function () {
  699. var v = $(this).val();
  700. if (v == 2) {
  701. $('.ldap').toggleShow();
  702. $('.smtp').toggleHide();
  703. }
  704. if (v == 3) {
  705. $('.smtp').toggleShow();
  706. $('.ldap').toggleHide();
  707. }
  708. });
  709. // Delete authorization.
  710. $('#delete-auth-btn').magnificPopup({
  711. modal: true
  712. }).click(function (e) {
  713. e.preventDefault();
  714. return true;
  715. });
  716. $('#delete-auth-submit').click(function () {
  717. $.magnificPopup.close();
  718. var $form = $('#auth-setting-form');
  719. $form.attr('action', $form.data('delete-url'));
  720. });
  721. }
  722. function initInstall() {
  723. // Change database type.
  724. (function () {
  725. var mysql_default = '127.0.0.1:3306';
  726. var postgres_default = '127.0.0.1:5432';
  727. $('#install-database').on("change", function () {
  728. var val = $(this).val();
  729. if (val != "SQLite3") {
  730. $('.server-sql').show();
  731. $('.sqlite-setting').addClass("hide");
  732. if (val == "PostgreSQL") {
  733. $('.pgsql-setting').removeClass("hide");
  734. // Change the host value to the Postgres default, but only
  735. // if the user hasn't already changed it from the MySQL
  736. // default.
  737. if ($('#database-host').val() == mysql_default) {
  738. $('#database-host').val(postgres_default);
  739. }
  740. } else if (val == 'MySQL') {
  741. $('.pgsql-setting').addClass("hide");
  742. if ($('#database-host').val() == postgres_default) {
  743. $('#database-host').val(mysql_default);
  744. }
  745. } else {
  746. $('.pgsql-setting').addClass("hide");
  747. }
  748. } else {
  749. $('.server-sql').hide();
  750. $('.pgsql-setting').hide();
  751. $('.sqlite-setting').removeClass("hide");
  752. }
  753. });
  754. }());
  755. }
  756. function initProfile() {
  757. // Avatar.
  758. $('#profile-avatar').tipsy({
  759. fade: true
  760. });
  761. }
  762. function initTimeSwitch() {
  763. // Time switch.
  764. $(".time-since[title]").on("click", function () {
  765. var $this = $(this);
  766. var title = $this.attr("title");
  767. var text = $this.text();
  768. $this.text(title);
  769. $this.attr("title", text);
  770. });
  771. }
  772. function initDiff() {
  773. $('.diff-detail-box>a').click(function () {
  774. $($(this).data('target')).slideToggle(100);
  775. })
  776. var $counter = $('.diff-counter');
  777. if ($counter.length < 1) {
  778. return;
  779. }
  780. $counter.each(function (i, item) {
  781. var $item = $(item);
  782. var addLine = $item.find('span[data-line].add').data("line");
  783. var delLine = $item.find('span[data-line].del').data("line");
  784. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  785. $item.find(".bar .add").css("width", addPercent + "%");
  786. });
  787. }
  788. $(document).ready(function () {
  789. Gogs.AppSubUrl = $('head').data('suburl') || '';
  790. initCore();
  791. if ($('#user-profile-setting').length) {
  792. initUserSetting();
  793. }
  794. if ($('#repo-create-form').length || $('#repo-migrate-form').length) {
  795. initRepoCreate();
  796. }
  797. if ($('#repo-header').length) {
  798. initTimeSwitch();
  799. initRepo();
  800. }
  801. if ($('#release').length) {
  802. initRepoRelease();
  803. }
  804. if ($('#repo-setting').length) {
  805. initRepoSetting();
  806. }
  807. if ($('#org-setting').length) {
  808. initOrgSetting();
  809. }
  810. if ($('#invite-box').length) {
  811. initInvite();
  812. }
  813. if ($('#team-create-form').length) {
  814. initOrgTeamCreate();
  815. }
  816. if ($('#team-members-list').length) {
  817. initTeamMembersList();
  818. }
  819. if ($('#team-repositories-list').length) {
  820. initTeamRepositoriesList();
  821. }
  822. if ($('#admin-setting').length) {
  823. initAdmin();
  824. }
  825. if ($('#install-form').length) {
  826. initInstall();
  827. }
  828. if ($('#user-profile-page').length) {
  829. initProfile();
  830. }
  831. if ($('#diff-page').length) {
  832. initTimeSwitch();
  833. initDiff();
  834. }
  835. $('#dashboard-sidebar-menu').tabs();
  836. $('#pull-issue-preview').markdown_preview(".issue-add-comment");
  837. homepage();
  838. // Fix language drop-down menu height.
  839. var l = $('#footer-lang li').length;
  840. $('#footer-lang .drop-down').css({
  841. "top": (-31 * l) + "px",
  842. "height": (31 * l - 3) + "px"
  843. });
  844. });
  845. function homepage() {
  846. // Change method to GET if no username input.
  847. $('#promo-form').submit(function (e) {
  848. if ($('#username').val() === "") {
  849. e.preventDefault();
  850. window.location.href = Gogs.AppSubUrl + '/user/login';
  851. return true
  852. }
  853. });
  854. // Redirect to register page.
  855. $('#register-button').click(function (e) {
  856. if ($('#username').val() === "") {
  857. e.preventDefault();
  858. window.location.href = Gogs.AppSubUrl + '/user/sign_up';
  859. return true
  860. }
  861. $('#promo-form').attr('action', Gogs.AppSubUrl + '/user/sign_up');
  862. });
  863. }
  864. String.prototype.endsWith = function (suffix) {
  865. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  866. };