gogs.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. $(this).tipsy("hide").attr('original-title', $this.data('after-title'));
  252. setTimeout(function () {
  253. $(this).tipsy("show");
  254. }, 200);
  255. setTimeout(function () {
  256. $(this).tipsy('hide').attr('original-title', $this.data('original-title'));
  257. }, 3000);
  258. }
  259. }).addClass("js-copy-bind");
  260. }
  261. })(jQuery);
  262. function initCore() {
  263. Gogs.renderMarkdown();
  264. Gogs.renderCodeView();
  265. // Switch list.
  266. $('.js-tab-nav').click(function (e) {
  267. if (!$(this).hasClass('js-tab-nav-show')) {
  268. $(this).parent().find('.js-tab-nav-show').each(function () {
  269. $(this).removeClass('js-tab-nav-show');
  270. $($(this).data('tab-target')).hide();
  271. });
  272. $(this).addClass('js-tab-nav-show');
  273. $($(this).data('tab-target')).show();
  274. }
  275. });
  276. }
  277. function initUserSetting() {
  278. // Confirmation of change username in user profile page.
  279. $('#user-profile-form').submit(function (e) {
  280. var $username = $('#username');
  281. if (($username.data('uname') != $username.val()) && !confirm('Username has been changed, do you want to continue?')) {
  282. e.preventDefault();
  283. return true;
  284. }
  285. });
  286. // Show add SSH key panel.
  287. $('#ssh-add').click(function () {
  288. $('#user-ssh-add-form').removeClass("hide");
  289. });
  290. // Confirmation of delete account.
  291. $('#delete-account-button').click(function (e) {
  292. if (!confirm('This account is going to be deleted, do you want to continue?')) {
  293. e.preventDefault();
  294. return true;
  295. }
  296. });
  297. }
  298. function initRepoCreate() {
  299. // Owner switch menu click.
  300. $('#repo-create-owner-list').on('click', 'li', function () {
  301. if (!$(this).hasClass('checked')) {
  302. var uid = $(this).data('uid');
  303. $('#repo-owner-id').val(uid);
  304. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  305. $('#repo-owner-name').text($(this).text().trim());
  306. $(this).parent().find('.checked').removeClass('checked');
  307. $(this).addClass('checked');
  308. console.log("set repo owner to uid :", uid, $(this).text().trim());
  309. }
  310. });
  311. $('#auth-button').click(function (e) {
  312. $('#repo-migrate-auth').slideToggle('fast');
  313. e.preventDefault();
  314. })
  315. console.log('initRepoCreate');
  316. }
  317. function initRepo() {
  318. // Clone link switch button.
  319. $('#repo-clone-ssh').click(function () {
  320. $(this).removeClass('btn-gray').addClass('btn-blue');
  321. $('#repo-clone-https').removeClass('btn-blue').addClass('btn-gray');
  322. $('#repo-clone-url').val($(this).data('link'));
  323. $('.clone-url').text($(this).data('link'))
  324. });
  325. $('#repo-clone-https').click(function () {
  326. $(this).removeClass('btn-gray').addClass('btn-blue');
  327. $('#repo-clone-ssh').removeClass('btn-blue').addClass('btn-gray');
  328. $('#repo-clone-url').val($(this).data('link'));
  329. $('.clone-url').text($(this).data('link'))
  330. });
  331. // Copy URL.
  332. var $clone_btn = $('#repo-clone-copy');
  333. $clone_btn.hover(function () {
  334. Gogs.bindCopy($(this));
  335. })
  336. $clone_btn.tipsy({
  337. fade: true
  338. });
  339. }
  340. // when user changes hook type, hide/show proper divs
  341. function initHookTypeChange() {
  342. // web hook type change
  343. $('select#hook-type').on("change", function () {
  344. hookTypes = ['Gogs', 'Slack'];
  345. var curHook = $(this).val();
  346. hookTypes.forEach(function (hookType) {
  347. if (curHook === hookType) {
  348. $('div#' + hookType.toLowerCase()).toggleShow();
  349. }
  350. else {
  351. $('div#' + hookType.toLowerCase()).toggleHide();
  352. }
  353. });
  354. });
  355. }
  356. function initRepoSetting() {
  357. // Options.
  358. // Confirmation of changing repository name.
  359. $('#repo-setting-form').submit(function (e) {
  360. var $reponame = $('#repo_name');
  361. if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) {
  362. e.preventDefault();
  363. return true;
  364. }
  365. });
  366. initHookTypeChange();
  367. $('#transfer-button').click(function () {
  368. $('#transfer-form').show();
  369. });
  370. $('#delete-button').click(function () {
  371. $('#delete-form').show();
  372. });
  373. // Collaboration.
  374. $('#repo-collab-list hr:last-child').remove();
  375. var $ul = $('#repo-collaborator').next().next().find('ul');
  376. $('#repo-collaborator').on('keyup', function () {
  377. var $this = $(this);
  378. if (!$this.val()) {
  379. $ul.toggleHide();
  380. return;
  381. }
  382. Gogs.searchUsers($this.val(), $ul);
  383. }).on('focus', function () {
  384. if (!$(this).val()) {
  385. $ul.toggleHide();
  386. } else {
  387. $ul.toggleShow();
  388. }
  389. }).next().next().find('ul').on("click", 'li', function () {
  390. $('#repo-collaborator').val($(this).text());
  391. $ul.toggleHide();
  392. });
  393. }
  394. function initOrgSetting() {
  395. // Options.
  396. // Confirmation of changing organization name.
  397. $('#org-setting-form').submit(function (e) {
  398. var $orgname = $('#orgname');
  399. if (($orgname.data('orgname') != $orgname.val()) && !confirm('Organization name has been changed, do you want to continue?')) {
  400. e.preventDefault();
  401. return true;
  402. }
  403. });
  404. // Confirmation of delete organization.
  405. $('#delete-org-button').click(function (e) {
  406. if (!confirm('This organization is going to be deleted, do you want to continue?')) {
  407. e.preventDefault();
  408. return true;
  409. }
  410. });
  411. initHookTypeChange();
  412. }
  413. function initInvite() {
  414. // Invitation.
  415. var $ul = $('#org-member-invite-list');
  416. $('#org-member-invite').on('keyup', function () {
  417. var $this = $(this);
  418. if (!$this.val()) {
  419. $ul.toggleHide();
  420. return;
  421. }
  422. Gogs.searchUsers($this.val(), $ul);
  423. }).on('focus', function () {
  424. if (!$(this).val()) {
  425. $ul.toggleHide();
  426. } else {
  427. $ul.toggleShow();
  428. }
  429. }).next().next().find('ul').on("click", 'li', function () {
  430. $('#org-member-invite').val($(this).text());
  431. $ul.toggleHide();
  432. });
  433. }
  434. function initOrgTeamCreate() {
  435. // Delete team.
  436. $('#org-team-delete').click(function (e) {
  437. if (!confirm('This team is going to be deleted, do you want to continue?')) {
  438. e.preventDefault();
  439. return true;
  440. }
  441. var $form = $('#team-create-form');
  442. $form.attr('action', $form.data('delete-url'));
  443. });
  444. }
  445. function initTeamMembersList() {
  446. // Add team member.
  447. var $ul = $('#org-team-members-list');
  448. $('#org-team-members-add').on('keyup', function () {
  449. var $this = $(this);
  450. if (!$this.val()) {
  451. $ul.toggleHide();
  452. return;
  453. }
  454. Gogs.searchUsers($this.val(), $ul);
  455. }).on('focus', function () {
  456. if (!$(this).val()) {
  457. $ul.toggleHide();
  458. } else {
  459. $ul.toggleShow();
  460. }
  461. }).next().next().find('ul').on("click", 'li', function () {
  462. $('#org-team-members-add').val($(this).text());
  463. $ul.toggleHide();
  464. });
  465. }
  466. function initTeamRepositoriesList() {
  467. // Add team repository.
  468. var $ul = $('#org-team-repositories-list');
  469. $('#org-team-repositories-add').on('keyup', function () {
  470. var $this = $(this);
  471. if (!$this.val()) {
  472. $ul.toggleHide();
  473. return;
  474. }
  475. Gogs.searchRepos($this.val(), $ul, 'uid=' + $this.data('uid'));
  476. }).on('focus', function () {
  477. if (!$(this).val()) {
  478. $ul.toggleHide();
  479. } else {
  480. $ul.toggleShow();
  481. }
  482. }).next().next().find('ul').on("click", 'li', function () {
  483. $('#org-team-repositories-add').val($(this).text());
  484. $ul.toggleHide();
  485. });
  486. }
  487. function initAdmin() {
  488. // Create account.
  489. $('#login-type').on("change", function () {
  490. var v = $(this).val();
  491. if (v.indexOf("0-") + 1) {
  492. $('.auth-name').toggleHide();
  493. $(".pwd").find("input").attr("required", "required")
  494. .end().toggleShow();
  495. } else {
  496. $(".pwd").find("input").removeAttr("required")
  497. .end().toggleHide();
  498. $('.auth-name').toggleShow();
  499. }
  500. });
  501. // Delete account.
  502. $('#user-delete').click(function (e) {
  503. if (!confirm('This account is going to be deleted, do you want to continue?')) {
  504. e.preventDefault();
  505. return true;
  506. }
  507. var $form = $('#user-profile-form');
  508. $form.attr('action', $form.data('delete-url'));
  509. });
  510. // Create authorization.
  511. $('#auth-type').on("change", function () {
  512. var v = $(this).val();
  513. if (v == 2) {
  514. $('.ldap').toggleShow();
  515. $('.smtp').toggleHide();
  516. }
  517. if (v == 3) {
  518. $('.smtp').toggleShow();
  519. $('.ldap').toggleHide();
  520. }
  521. });
  522. // Delete authorization.
  523. $('#auth-delete').click(function (e) {
  524. if (!confirm('This authorization is going to be deleted, do you want to continue?')) {
  525. e.preventDefault();
  526. return true;
  527. }
  528. var $form = $('auth-setting-form');
  529. $form.attr('action', $form.data('delete-url'));
  530. });
  531. }
  532. function initInstall() {
  533. // Change database type.
  534. (function () {
  535. var mysql_default = '127.0.0.1:3306';
  536. var postgres_default = '127.0.0.1:5432';
  537. $('#install-database').on("change", function () {
  538. var val = $(this).val();
  539. if (val != "SQLite3") {
  540. $('.server-sql').show();
  541. $('.sqlite-setting').addClass("hide");
  542. if (val == "PostgreSQL") {
  543. $('.pgsql-setting').removeClass("hide");
  544. // Change the host value to the Postgres default, but only
  545. // if the user hasn't already changed it from the MySQL
  546. // default.
  547. if ($('#database-host').val() == mysql_default) {
  548. $('#database-host').val(postgres_default);
  549. }
  550. } else if (val == 'MySQL') {
  551. $('.pgsql-setting').addClass("hide");
  552. if ($('#database-host').val() == postgres_default) {
  553. $('#database-host').val(mysql_default);
  554. }
  555. } else {
  556. $('.pgsql-setting').addClass("hide");
  557. }
  558. } else {
  559. $('.server-sql').hide();
  560. $('.pgsql-setting').hide();
  561. $('.sqlite-setting').removeClass("hide");
  562. }
  563. });
  564. }());
  565. }
  566. $(document).ready(function () {
  567. Gogs.AppSubUrl = $('head').data('suburl');
  568. initCore();
  569. if ($('#user-profile-setting').length) {
  570. initUserSetting();
  571. }
  572. if ($('#repo-create-form').length || $('#repo-migrate-form').length) {
  573. initRepoCreate();
  574. }
  575. if ($('#repo-header').length) {
  576. initRepo();
  577. }
  578. if ($('#repo-setting').length) {
  579. initRepoSetting();
  580. }
  581. if ($('#org-setting').length) {
  582. initOrgSetting();
  583. }
  584. if ($('#invite-box').length) {
  585. initInvite();
  586. }
  587. if ($('#team-create-form').length) {
  588. initOrgTeamCreate();
  589. }
  590. if ($('#team-members-list').length) {
  591. initTeamMembersList();
  592. }
  593. if ($('#team-repositories-list').length) {
  594. initTeamRepositoriesList();
  595. }
  596. if ($('#admin-setting').length) {
  597. initAdmin();
  598. }
  599. if ($('#install-form').length) {
  600. initInstall();
  601. }
  602. Tabs('#dashboard-sidebar-menu');
  603. homepage();
  604. // Fix language drop-down menu height.
  605. var l = $('#footer-lang li').length;
  606. $('#footer-lang .drop-down').css({
  607. "top": (-31 * l) + "px",
  608. "height": (31 * l - 3) + "px"
  609. });
  610. });
  611. function homepage() {
  612. // Change method to GET if no username input.
  613. $('#promo-form').submit(function (e) {
  614. if ($('#username').val() === "") {
  615. e.preventDefault();
  616. window.location.href = Gogs.AppSubUrl + '/user/login';
  617. return true
  618. }
  619. });
  620. // Redirect to register page.
  621. $('#register-button').click(function (e) {
  622. if ($('#username').val() === "") {
  623. e.preventDefault();
  624. window.location.href = Gogs.AppSubUrl + '/user/sign_up';
  625. return true
  626. }
  627. $('#promo-form').attr('action', Gogs.AppSubUrl + '/user/sign_up');
  628. });
  629. }