gogs.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. // @codekit-prepend "lib/jquery-1.11.1.min.js"
  2. // @codekit-prepend "lib/lib.js"
  3. // @codekit-prepend "lib/tabs.js"
  4. // @codekit-prepend "lib/jquery.tipsy.js"
  5. var Gogs = {};
  6. (function ($) {
  7. // Extend jQuery ajax, set CSRF token value.
  8. var ajax = $.ajax;
  9. $.extend({
  10. ajax: function (url, options) {
  11. if (typeof url === 'object') {
  12. options = url;
  13. url = undefined;
  14. }
  15. options = options || {};
  16. url = options.url;
  17. var csrftoken = $('meta[name=_csrf]').attr('content');
  18. var headers = options.headers || {};
  19. var domain = document.domain.replace(/\./ig, '\\.');
  20. if (!/^(http:|https:).*/.test(url) || eval('/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/').test(url)) {
  21. headers = $.extend(headers, {'X-Csrf-Token': csrftoken});
  22. }
  23. options.headers = headers;
  24. var callback = options.success;
  25. options.success = function (data) {
  26. if (data.once) {
  27. // change all _once value if ajax data.once exist
  28. $('[name=_once]').val(data.once);
  29. }
  30. if (callback) {
  31. callback.apply(this, arguments);
  32. }
  33. };
  34. return ajax(url, options);
  35. },
  36. changeHash: function (hash) {
  37. if (history.pushState) {
  38. history.pushState(null, null, hash);
  39. }
  40. else {
  41. location.hash = hash;
  42. }
  43. },
  44. deSelect: function () {
  45. if (window.getSelection) {
  46. window.getSelection().removeAllRanges();
  47. } else {
  48. document.selection.empty();
  49. }
  50. }
  51. });
  52. $.fn.extend({
  53. toggleHide: function () {
  54. $(this).addClass("hidden");
  55. },
  56. toggleShow: function () {
  57. $(this).removeClass("hidden");
  58. },
  59. toggleAjax: function (successCallback, errorCallback) {
  60. var url = $(this).data("ajax");
  61. var method = $(this).data('ajax-method') || 'get';
  62. var ajaxName = $(this).data('ajax-name');
  63. var data = {};
  64. if (ajaxName.endsWith("preview")) {
  65. data["mode"] = "gfm";
  66. data["context"] = $(this).data('ajax-context');
  67. }
  68. $('[data-ajax-rel=' + ajaxName + ']').each(function () {
  69. var field = $(this).data("ajax-field");
  70. var t = $(this).data("ajax-val");
  71. if (t == "val") {
  72. data[field] = $(this).val();
  73. return true;
  74. }
  75. if (t == "txt") {
  76. data[field] = $(this).text();
  77. return true;
  78. }
  79. if (t == "html") {
  80. data[field] = $(this).html();
  81. return true;
  82. }
  83. if (t == "data") {
  84. data[field] = $(this).data("ajax-data");
  85. return true;
  86. }
  87. return true;
  88. });
  89. console.log("toggleAjax:", method, url, data);
  90. $.ajax({
  91. url: url,
  92. method: method.toUpperCase(),
  93. data: data,
  94. error: errorCallback,
  95. success: function (d) {
  96. if (successCallback) {
  97. successCallback(d);
  98. }
  99. }
  100. })
  101. }
  102. });
  103. }(jQuery));
  104. (function ($) {
  105. // Render markdown.
  106. Gogs.renderMarkdown = function () {
  107. var $md = $('.markdown');
  108. var $pre = $md.find('pre > code').parent();
  109. $pre.addClass('prettyprint');
  110. prettyPrint();
  111. // Set anchor.
  112. var headers = {};
  113. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  114. var node = $(this);
  115. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  116. var name = val;
  117. if (headers[val] > 0) {
  118. name = val + '-' + headers[val];
  119. }
  120. if (headers[val] == undefined) {
  121. headers[val] = 1;
  122. } else {
  123. headers[val] += 1;
  124. }
  125. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  126. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  127. });
  128. };
  129. // Render code view.
  130. Gogs.renderCodeView = function () {
  131. function selectRange($list, $select, $from) {
  132. $list.removeClass('active');
  133. if ($from) {
  134. var a = parseInt($select.attr('rel').substr(1));
  135. var b = parseInt($from.attr('rel').substr(1));
  136. var c;
  137. if (a != b) {
  138. if (a > b) {
  139. c = a;
  140. a = b;
  141. b = c;
  142. }
  143. var classes = [];
  144. for (i = a; i <= b; i++) {
  145. classes.push('.L' + i);
  146. }
  147. $list.filter(classes.join(',')).addClass('active');
  148. $.changeHash('#L' + a + '-' + 'L' + b);
  149. return
  150. }
  151. }
  152. $select.addClass('active');
  153. $.changeHash('#' + $select.attr('rel'));
  154. }
  155. $(document).on('click', '.lines-num span', function (e) {
  156. var $select = $(this);
  157. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  158. selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  159. $.deSelect();
  160. });
  161. $('.code-view .lines-code > pre').each(function () {
  162. var $pre = $(this);
  163. var $lineCode = $pre.parent();
  164. var $lineNums = $lineCode.siblings('.lines-num');
  165. if ($lineNums.length > 0) {
  166. var nums = $pre.find('ol.linenums > li').length;
  167. for (var i = 1; i <= nums; i++) {
  168. $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
  169. }
  170. }
  171. });
  172. $(window).on('hashchange', function (e) {
  173. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  174. var $list = $('.code-view ol.linenums > li');
  175. var $first;
  176. if (m) {
  177. $first = $list.filter('.' + m[1]);
  178. selectRange($list, $first, $list.filter('.' + m[2]));
  179. $("html, body").scrollTop($first.offset().top - 200);
  180. return;
  181. }
  182. m = window.location.hash.match(/^#(L\d+)$/);
  183. if (m) {
  184. $first = $list.filter('.' + m[1]);
  185. selectRange($list, $first);
  186. $("html, body").scrollTop($first.offset().top - 200);
  187. }
  188. }).trigger('hashchange');
  189. };
  190. // Search users by keyword.
  191. Gogs.searchUsers = function (val, $target) {
  192. $.ajax({
  193. url: Gogs.AppSubUrl + '/api/v1/users/search?q=' + val,
  194. dataType: "json",
  195. success: function (json) {
  196. if (json.ok && json.data.length) {
  197. var html = '';
  198. $.each(json.data, function (i, item) {
  199. html += '<li><a><img src="' + item.avatar + '">' + item.username + '</a></li>';
  200. });
  201. $target.html(html);
  202. $target.toggleShow();
  203. } else {
  204. $target.toggleHide();
  205. }
  206. }
  207. });
  208. }
  209. // Search repositories by keyword.
  210. Gogs.searchRepos = function (val, $target, $param) {
  211. $.ajax({
  212. url: Gogs.AppSubUrl + '/api/v1/repos/search?q=' + val + '&' + $param,
  213. dataType: "json",
  214. success: function (json) {
  215. if (json.ok && json.data.length) {
  216. var html = '';
  217. $.each(json.data, function (i, item) {
  218. html += '<li><a><span class="octicon octicon-repo"></span> ' + item.repolink + '</a></li>';
  219. });
  220. $target.html(html);
  221. $target.toggleShow();
  222. } else {
  223. $target.toggleHide();
  224. }
  225. }
  226. });
  227. }
  228. // Copy util.
  229. Gogs.bindCopy = function (selector) {
  230. if ($(selector).hasClass('js-copy-bind')) {
  231. return;
  232. }
  233. $(selector).zclip({
  234. path: Gogs.AppSubUrl + "/js/ZeroClipboard.swf",
  235. copy: function () {
  236. var t = $(this).data("copy-val");
  237. var to = $($(this).data("copy-from"));
  238. var str = "";
  239. if (t == "txt") {
  240. str = to.text();
  241. }
  242. if (t == 'val') {
  243. str = to.val();
  244. }
  245. if (t == 'html') {
  246. str = to.html();
  247. }
  248. return str;
  249. },
  250. afterCopy: function () {
  251. var $this = $(this);
  252. $this.tipsy("hide").attr('original-title', $this.data('after-title'));
  253. setTimeout(function () {
  254. $this.tipsy("show");
  255. }, 200);
  256. setTimeout(function () {
  257. $this.tipsy('hide').attr('original-title', $this.data('original-title'));
  258. }, 2000);
  259. }
  260. }).addClass("js-copy-bind");
  261. }
  262. })(jQuery);
  263. function initCore() {
  264. Gogs.renderMarkdown();
  265. Gogs.renderCodeView();
  266. // Switch list.
  267. $('.js-tab-nav').click(function (e) {
  268. if (!$(this).hasClass('js-tab-nav-show')) {
  269. $(this).parent().find('.js-tab-nav-show').each(function () {
  270. $(this).removeClass('js-tab-nav-show');
  271. $($(this).data('tab-target')).hide();
  272. });
  273. $(this).addClass('js-tab-nav-show');
  274. $($(this).data('tab-target')).show();
  275. }
  276. });
  277. }
  278. function initUserSetting() {
  279. // Confirmation of change username in user profile page.
  280. $('#user-profile-form').submit(function (e) {
  281. var $username = $('#username');
  282. if (($username.data('uname') != $username.val()) && !confirm('Username has been changed, do you want to continue?')) {
  283. e.preventDefault();
  284. return true;
  285. }
  286. });
  287. // Show add SSH key panel.
  288. $('#ssh-add').click(function () {
  289. $('#user-ssh-add-form').removeClass("hide");
  290. });
  291. // Confirmation of delete account.
  292. $('#delete-account-button').click(function (e) {
  293. if (!confirm('This account is going to be deleted, do you want to continue?')) {
  294. e.preventDefault();
  295. return true;
  296. }
  297. });
  298. }
  299. function initRepoCreate() {
  300. // Owner switch menu click.
  301. $('#repo-create-owner-list').on('click', 'li', function () {
  302. if (!$(this).hasClass('checked')) {
  303. var uid = $(this).data('uid');
  304. $('#repo-owner-id').val(uid);
  305. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  306. $('#repo-owner-name').text($(this).text().trim());
  307. $(this).parent().find('.checked').removeClass('checked');
  308. $(this).addClass('checked');
  309. console.log("set repo owner to uid :", uid, $(this).text().trim());
  310. }
  311. });
  312. $('#auth-button').click(function (e) {
  313. $('#repo-migrate-auth').slideToggle('fast');
  314. e.preventDefault();
  315. })
  316. console.log('initRepoCreate');
  317. }
  318. function initRepo() {
  319. // Clone link switch button.
  320. $('#repo-clone-ssh').click(function () {
  321. $(this).removeClass('btn-gray').addClass('btn-blue');
  322. $('#repo-clone-https').removeClass('btn-blue').addClass('btn-gray');
  323. $('#repo-clone-url').val($(this).data('link'));
  324. $('.clone-url').text($(this).data('link'))
  325. });
  326. $('#repo-clone-https').click(function () {
  327. $(this).removeClass('btn-gray').addClass('btn-blue');
  328. $('#repo-clone-ssh').removeClass('btn-blue').addClass('btn-gray');
  329. $('#repo-clone-url').val($(this).data('link'));
  330. $('.clone-url').text($(this).data('link'))
  331. });
  332. // Copy URL.
  333. var $clone_btn = $('#repo-clone-copy');
  334. $clone_btn.hover(function () {
  335. Gogs.bindCopy($(this));
  336. })
  337. $clone_btn.tipsy({
  338. fade: true
  339. });
  340. }
  341. // when user changes hook type, hide/show proper divs
  342. function initHookTypeChange() {
  343. // web hook type change
  344. $('select#hook-type').on("change", function () {
  345. hookTypes = ['Gogs', 'Slack'];
  346. var curHook = $(this).val();
  347. hookTypes.forEach(function (hookType) {
  348. if (curHook === hookType) {
  349. $('div#' + hookType.toLowerCase()).toggleShow();
  350. }
  351. else {
  352. $('div#' + hookType.toLowerCase()).toggleHide();
  353. }
  354. });
  355. });
  356. }
  357. function initRepoSetting() {
  358. // Options.
  359. // Confirmation of changing repository name.
  360. $('#repo-setting-form').submit(function (e) {
  361. var $reponame = $('#repo_name');
  362. if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) {
  363. e.preventDefault();
  364. return true;
  365. }
  366. });
  367. initHookTypeChange();
  368. $('#transfer-button').click(function () {
  369. $('#transfer-form').show();
  370. });
  371. $('#delete-button').click(function () {
  372. $('#delete-form').show();
  373. });
  374. // Collaboration.
  375. $('#repo-collab-list hr:last-child').remove();
  376. var $ul = $('#repo-collaborator').next().next().find('ul');
  377. $('#repo-collaborator').on('keyup', function () {
  378. var $this = $(this);
  379. if (!$this.val()) {
  380. $ul.toggleHide();
  381. return;
  382. }
  383. Gogs.searchUsers($this.val(), $ul);
  384. }).on('focus', function () {
  385. if (!$(this).val()) {
  386. $ul.toggleHide();
  387. } else {
  388. $ul.toggleShow();
  389. }
  390. }).next().next().find('ul').on("click", 'li', function () {
  391. $('#repo-collaborator').val($(this).text());
  392. $ul.toggleHide();
  393. });
  394. }
  395. function initOrgSetting() {
  396. // Options.
  397. // Confirmation of changing organization name.
  398. $('#org-setting-form').submit(function (e) {
  399. var $orgname = $('#orgname');
  400. if (($orgname.data('orgname') != $orgname.val()) && !confirm('Organization name has been changed, do you want to continue?')) {
  401. e.preventDefault();
  402. return true;
  403. }
  404. });
  405. // Confirmation of delete organization.
  406. $('#delete-org-button').click(function (e) {
  407. if (!confirm('This organization is going to be deleted, do you want to continue?')) {
  408. e.preventDefault();
  409. return true;
  410. }
  411. });
  412. initHookTypeChange();
  413. }
  414. function initInvite() {
  415. // Invitation.
  416. var $ul = $('#org-member-invite-list');
  417. $('#org-member-invite').on('keyup', function () {
  418. var $this = $(this);
  419. if (!$this.val()) {
  420. $ul.toggleHide();
  421. return;
  422. }
  423. Gogs.searchUsers($this.val(), $ul);
  424. }).on('focus', function () {
  425. if (!$(this).val()) {
  426. $ul.toggleHide();
  427. } else {
  428. $ul.toggleShow();
  429. }
  430. }).next().next().find('ul').on("click", 'li', function () {
  431. $('#org-member-invite').val($(this).text());
  432. $ul.toggleHide();
  433. });
  434. }
  435. function initOrgTeamCreate() {
  436. // Delete team.
  437. $('#org-team-delete').click(function (e) {
  438. if (!confirm('This team is going to be deleted, do you want to continue?')) {
  439. e.preventDefault();
  440. return true;
  441. }
  442. var $form = $('#team-create-form');
  443. $form.attr('action', $form.data('delete-url'));
  444. });
  445. }
  446. function initTeamMembersList() {
  447. // Add team member.
  448. var $ul = $('#org-team-members-list');
  449. $('#org-team-members-add').on('keyup', function () {
  450. var $this = $(this);
  451. if (!$this.val()) {
  452. $ul.toggleHide();
  453. return;
  454. }
  455. Gogs.searchUsers($this.val(), $ul);
  456. }).on('focus', function () {
  457. if (!$(this).val()) {
  458. $ul.toggleHide();
  459. } else {
  460. $ul.toggleShow();
  461. }
  462. }).next().next().find('ul').on("click", 'li', function () {
  463. $('#org-team-members-add').val($(this).text());
  464. $ul.toggleHide();
  465. });
  466. }
  467. function initTeamRepositoriesList() {
  468. // Add team repository.
  469. var $ul = $('#org-team-repositories-list');
  470. $('#org-team-repositories-add').on('keyup', function () {
  471. var $this = $(this);
  472. if (!$this.val()) {
  473. $ul.toggleHide();
  474. return;
  475. }
  476. Gogs.searchRepos($this.val(), $ul, 'uid=' + $this.data('uid'));
  477. }).on('focus', function () {
  478. if (!$(this).val()) {
  479. $ul.toggleHide();
  480. } else {
  481. $ul.toggleShow();
  482. }
  483. }).next().next().find('ul').on("click", 'li', function () {
  484. $('#org-team-repositories-add').val($(this).text());
  485. $ul.toggleHide();
  486. });
  487. }
  488. function initAdmin() {
  489. // Create account.
  490. $('#login-type').on("change", function () {
  491. var v = $(this).val();
  492. if (v.indexOf("0-") + 1) {
  493. $('.auth-name').toggleHide();
  494. $(".pwd").find("input").attr("required", "required")
  495. .end().toggleShow();
  496. } else {
  497. $(".pwd").find("input").removeAttr("required")
  498. .end().toggleHide();
  499. $('.auth-name').toggleShow();
  500. }
  501. });
  502. // Delete account.
  503. $('#user-delete').click(function (e) {
  504. if (!confirm('This account is going to be deleted, do you want to continue?')) {
  505. e.preventDefault();
  506. return true;
  507. }
  508. var $form = $('#user-profile-form');
  509. $form.attr('action', $form.data('delete-url'));
  510. });
  511. // Create authorization.
  512. $('#auth-type').on("change", function () {
  513. var v = $(this).val();
  514. if (v == 2) {
  515. $('.ldap').toggleShow();
  516. $('.smtp').toggleHide();
  517. }
  518. if (v == 3) {
  519. $('.smtp').toggleShow();
  520. $('.ldap').toggleHide();
  521. }
  522. });
  523. // Delete authorization.
  524. $('#auth-delete').click(function (e) {
  525. if (!confirm('This authorization is going to be deleted, do you want to continue?')) {
  526. e.preventDefault();
  527. return true;
  528. }
  529. var $form = $('auth-setting-form');
  530. $form.attr('action', $form.data('delete-url'));
  531. });
  532. }
  533. function initInstall() {
  534. // Change database type.
  535. (function () {
  536. var mysql_default = '127.0.0.1:3306';
  537. var postgres_default = '127.0.0.1:5432';
  538. $('#install-database').on("change", function () {
  539. var val = $(this).val();
  540. if (val != "SQLite3") {
  541. $('.server-sql').show();
  542. $('.sqlite-setting').addClass("hide");
  543. if (val == "PostgreSQL") {
  544. $('.pgsql-setting').removeClass("hide");
  545. // Change the host value to the Postgres default, but only
  546. // if the user hasn't already changed it from the MySQL
  547. // default.
  548. if ($('#database-host').val() == mysql_default) {
  549. $('#database-host').val(postgres_default);
  550. }
  551. } else if (val == 'MySQL') {
  552. $('.pgsql-setting').addClass("hide");
  553. if ($('#database-host').val() == postgres_default) {
  554. $('#database-host').val(mysql_default);
  555. }
  556. } else {
  557. $('.pgsql-setting').addClass("hide");
  558. }
  559. } else {
  560. $('.server-sql').hide();
  561. $('.pgsql-setting').hide();
  562. $('.sqlite-setting').removeClass("hide");
  563. }
  564. });
  565. }());
  566. }
  567. $(document).ready(function () {
  568. Gogs.AppSubUrl = $('head').data('suburl');
  569. initCore();
  570. if ($('#user-profile-setting').length) {
  571. initUserSetting();
  572. }
  573. if ($('#repo-create-form').length || $('#repo-migrate-form').length) {
  574. initRepoCreate();
  575. }
  576. if ($('#repo-header').length) {
  577. initRepo();
  578. }
  579. if ($('#repo-setting').length) {
  580. initRepoSetting();
  581. }
  582. if ($('#org-setting').length) {
  583. initOrgSetting();
  584. }
  585. if ($('#invite-box').length) {
  586. initInvite();
  587. }
  588. if ($('#team-create-form').length) {
  589. initOrgTeamCreate();
  590. }
  591. if ($('#team-members-list').length) {
  592. initTeamMembersList();
  593. }
  594. if ($('#team-repositories-list').length) {
  595. initTeamRepositoriesList();
  596. }
  597. if ($('#admin-setting').length) {
  598. initAdmin();
  599. }
  600. if ($('#install-form').length) {
  601. initInstall();
  602. }
  603. Tabs('#dashboard-sidebar-menu');
  604. homepage();
  605. // Fix language drop-down menu height.
  606. var l = $('#footer-lang li').length;
  607. $('#footer-lang .drop-down').css({
  608. "top": (-31 * l) + "px",
  609. "height": (31 * l - 3) + "px"
  610. });
  611. });
  612. function homepage() {
  613. // Change method to GET if no username input.
  614. $('#promo-form').submit(function (e) {
  615. if ($('#username').val() === "") {
  616. e.preventDefault();
  617. window.location.href = Gogs.AppSubUrl + '/user/login';
  618. return true
  619. }
  620. });
  621. // Redirect to register page.
  622. $('#register-button').click(function (e) {
  623. if ($('#username').val() === "") {
  624. e.preventDefault();
  625. window.location.href = Gogs.AppSubUrl + '/user/sign_up';
  626. return true
  627. }
  628. $('#promo-form').attr('action', Gogs.AppSubUrl + '/user/sign_up');
  629. });
  630. }