app.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. var Gogits = {
  2. "PageIsSignup": false
  3. };
  4. (function ($) {
  5. // extend jQuery ajax, set csrf token value
  6. var ajax = $.ajax;
  7. $.extend({
  8. ajax: function (url, options) {
  9. if (typeof url === 'object') {
  10. options = url;
  11. url = undefined;
  12. }
  13. options = options || {};
  14. url = options.url;
  15. var csrftoken = $('meta[name=_csrf]').attr('content');
  16. var headers = options.headers || {};
  17. var domain = document.domain.replace(/\./ig, '\\.');
  18. if (!/^(http:|https:).*/.test(url) || eval('/^(http:|https:)\\/\\/(.+\\.)*' + domain + '.*/').test(url)) {
  19. headers = $.extend(headers, {'X-Csrf-Token': csrftoken});
  20. }
  21. options.headers = headers;
  22. var callback = options.success;
  23. options.success = function (data) {
  24. if (data.once) {
  25. // change all _once value if ajax data.once exist
  26. $('[name=_once]').val(data.once);
  27. }
  28. if (callback) {
  29. callback.apply(this, arguments);
  30. }
  31. };
  32. return ajax(url, options);
  33. },
  34. changeHash: function (hash) {
  35. if (history.pushState) {
  36. history.pushState(null, null, hash);
  37. }
  38. else {
  39. location.hash = hash;
  40. }
  41. },
  42. deSelect: function () {
  43. if (window.getSelection) {
  44. window.getSelection().removeAllRanges();
  45. } else {
  46. document.selection.empty();
  47. }
  48. }
  49. });
  50. $.fn.extend({
  51. toggleHide: function () {
  52. $(this).addClass("hidden");
  53. },
  54. toggleShow: function () {
  55. $(this).removeClass("hidden");
  56. },
  57. toggleAjax: function (successCallback) {
  58. var url = $(this).data("ajax");
  59. var method = $(this).data('ajax-method') || 'get';
  60. var ajaxName = $(this).data('ajax-name');
  61. var data = {};
  62. $('[data-ajax-rel=' + ajaxName + ']').each(function () {
  63. var field = $(this).data("ajax-field");
  64. var t = $(this).data("ajax-val");
  65. if (t == "val") {
  66. data[field] = $(this).val();
  67. return true;
  68. }
  69. if (t == "txt") {
  70. data[field] = $(this).text();
  71. return true;
  72. }
  73. if (t == "html") {
  74. data[field] = $(this).html();
  75. return true;
  76. }
  77. if (t == "data") {
  78. data[field] = $(this).data("ajax-data");
  79. return true;
  80. }
  81. return true;
  82. });
  83. $.ajax({
  84. url: url,
  85. method: method.toUpperCase(),
  86. data: data,
  87. success: function (d) {
  88. if (successCallback) {
  89. successCallback(d);
  90. }
  91. }
  92. })
  93. }
  94. })
  95. }(jQuery));
  96. (function ($) {
  97. Gogits.showTab = function (selector, index) {
  98. if (!index) {
  99. index = 0;
  100. }
  101. $(selector).tab("show");
  102. $(selector).find("li:eq(" + index + ") a").tab("show");
  103. };
  104. Gogits.validateForm = function (selector, options) {
  105. var $form = $(selector);
  106. options = options || {};
  107. options.showErrors = function (map, list) {
  108. var $error = $form.find('.form-error').addClass('hidden');
  109. $('.has-error').removeClass("has-error");
  110. $error.text(list[0].message).show().removeClass("hidden");
  111. $(list[0].element).parents(".form-group").addClass("has-error");
  112. };
  113. $form.validate(options);
  114. };
  115. // ----- init elements
  116. Gogits.initModals = function () {
  117. var modals = $("[data-toggle=modal]");
  118. if (modals.length < 1) {
  119. return;
  120. }
  121. $.each(modals, function (i, item) {
  122. var hide = $(item).data('modal');
  123. $(item).modal(hide ? hide : "hide");
  124. });
  125. };
  126. Gogits.initTooltips = function () {
  127. $("body").tooltip({
  128. selector: "[data-toggle=tooltip]"
  129. //container: "body"
  130. });
  131. };
  132. Gogits.initPopovers = function () {
  133. var hideAllPopovers = function () {
  134. $('[data-toggle=popover]').each(function () {
  135. $(this).popover('hide');
  136. });
  137. };
  138. $(document).on('click', function (e) {
  139. var $e = $(e.target);
  140. if ($e.data('toggle') == 'popover' || $e.parents("[data-toggle=popover], .popover").length > 0) {
  141. return;
  142. }
  143. hideAllPopovers();
  144. });
  145. $("body").popover({
  146. selector: "[data-toggle=popover]"
  147. });
  148. };
  149. Gogits.initTabs = function () {
  150. var $tabs = $('[data-init=tabs]');
  151. $tabs.tab("show");
  152. $tabs.find("li:eq(0) a").tab("show");
  153. };
  154. // fix dropdown inside click
  155. Gogits.initDropDown = function () {
  156. $('.dropdown-menu.no-propagation').on('click', function (e) {
  157. e.stopPropagation();
  158. });
  159. };
  160. // render markdown
  161. Gogits.renderMarkdown = function () {
  162. var $md = $('.markdown');
  163. var $pre = $md.find('pre > code').parent();
  164. $pre.addClass('prettyprint linenums');
  165. prettyPrint();
  166. // Set anchor.
  167. var headers = {};
  168. $md.find('h1, h2, h3, h4, h5, h6').each(function () {
  169. var node = $(this);
  170. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  171. var name = val;
  172. if (headers[val] > 0) {
  173. name = val + '-' + headers[val];
  174. }
  175. if (headers[val] == undefined) {
  176. headers[val] = 1;
  177. } else {
  178. headers[val] += 1;
  179. }
  180. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  181. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  182. });
  183. };
  184. // render code view
  185. Gogits.renderCodeView = function () {
  186. function selectRange($list, $select, $from) {
  187. $list.removeClass('active');
  188. if ($from) {
  189. var a = parseInt($select.attr('rel').substr(1));
  190. var b = parseInt($from.attr('rel').substr(1));
  191. var c;
  192. if (a != b) {
  193. if (a > b) {
  194. c = a;
  195. a = b;
  196. b = c;
  197. }
  198. var classes = [];
  199. for (i = a; i <= b; i++) {
  200. classes.push('.L' + i);
  201. }
  202. $list.filter(classes.join(',')).addClass('active');
  203. $.changeHash('#L' + a + '-' + 'L' + b);
  204. return
  205. }
  206. }
  207. $select.addClass('active');
  208. $.changeHash('#' + $select.attr('rel'));
  209. }
  210. $(document).on('click', '.lines-num span', function (e) {
  211. var $select = $(this);
  212. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  213. selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  214. $.deSelect();
  215. });
  216. $('.code-view .lines-code > pre').each(function () {
  217. var $pre = $(this);
  218. var $lineCode = $pre.parent();
  219. var $lineNums = $lineCode.siblings('.lines-num');
  220. if ($lineNums.length > 0) {
  221. var nums = $pre.find('ol.linenums > li').length;
  222. for (var i = 1; i <= nums; i++) {
  223. $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
  224. }
  225. }
  226. });
  227. $(window).on('hashchange', function (e) {
  228. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  229. var $list = $('.code-view ol.linenums > li');
  230. if (m) {
  231. var $first = $list.filter('.' + m[1]);
  232. selectRange($list, $first, $list.filter('.' + m[2]));
  233. $("html, body").scrollTop($first.offset().top - 200);
  234. return;
  235. }
  236. m = window.location.hash.match(/^#(L\d+)$/);
  237. if (m) {
  238. var $first = $list.filter('.' + m[1]);
  239. selectRange($list, $first);
  240. $("html, body").scrollTop($first.offset().top - 200);
  241. }
  242. }).trigger('hashchange');
  243. };
  244. // copy utils
  245. Gogits.bindCopy = function (selector) {
  246. if ($(selector).hasClass('js-copy-bind')) {
  247. return;
  248. }
  249. $(selector).zclip({
  250. path: "/js/ZeroClipboard.swf",
  251. copy: function () {
  252. var t = $(this).data("copy-val");
  253. var to = $($(this).data("copy-from"));
  254. var str = "";
  255. if (t == "txt") {
  256. str = to.text();
  257. }
  258. if (t == 'val') {
  259. str = to.val();
  260. }
  261. if (t == 'html') {
  262. str = to.html();
  263. }
  264. return str;
  265. },
  266. afterCopy: function () {
  267. var $this = $(this);
  268. $this.tooltip('hide')
  269. .attr('data-original-title', 'Copied OK');
  270. setTimeout(function () {
  271. $this.tooltip("show");
  272. }, 200);
  273. setTimeout(function () {
  274. $this.tooltip('hide')
  275. .attr('data-original-title', 'Copy to Clipboard');
  276. }, 3000);
  277. }
  278. }).addClass("js-copy-bind");
  279. }
  280. })(jQuery);
  281. // ajax utils
  282. (function ($) {
  283. Gogits.ajaxDelete = function (url, data, success) {
  284. data = data || {};
  285. data._method = "DELETE";
  286. $.ajax({
  287. url: url,
  288. data: data,
  289. method: "POST",
  290. dataType: "json",
  291. success: function (json) {
  292. if (success) {
  293. success(json);
  294. }
  295. }
  296. })
  297. }
  298. })(jQuery);
  299. function initCore() {
  300. Gogits.initTooltips();
  301. Gogits.initPopovers();
  302. Gogits.initTabs();
  303. Gogits.initModals();
  304. Gogits.initDropDown();
  305. Gogits.renderMarkdown();
  306. Gogits.renderCodeView();
  307. }
  308. function initRegister() {
  309. $.getScript("/js/jquery.validate.min.js", function () {
  310. Gogits.validateForm("#login-card", {
  311. rules: {
  312. "username": {
  313. required: true,
  314. maxlength: 30
  315. },
  316. "email": {
  317. required: true,
  318. email: true
  319. },
  320. "passwd": {
  321. required: true,
  322. minlength: 6,
  323. maxlength: 30
  324. },
  325. "re-passwd": {
  326. required: true,
  327. equalTo: "input[name=passwd]"
  328. }
  329. }
  330. });
  331. });
  332. }
  333. function initUserSetting() {
  334. // ssh confirmation
  335. $('#ssh-keys .delete').confirmation({
  336. singleton: true,
  337. onConfirm: function (e, $this) {
  338. Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
  339. if (json.ok) {
  340. window.location.reload();
  341. } else {
  342. alert(json.err);
  343. }
  344. });
  345. }
  346. });
  347. // profile form
  348. (function () {
  349. $('#user-setting-username').on("keyup", function () {
  350. var $this = $(this);
  351. if ($this.val() != $this.attr('title')) {
  352. $this.next('.help-block').toggleShow();
  353. } else {
  354. $this.next('.help-block').toggleHide();
  355. }
  356. });
  357. }())
  358. }
  359. function initRepository() {
  360. // clone group button script
  361. (function () {
  362. var $clone = $('.clone-group-btn');
  363. if ($clone.length) {
  364. var $url = $('.clone-group-url');
  365. $clone.find('button[data-link]').on("click", function (e) {
  366. var $this = $(this);
  367. if (!$this.hasClass('btn-primary')) {
  368. $clone.find('.input-group-btn .btn-primary').removeClass('btn-primary').addClass("btn-default");
  369. $(this).addClass('btn-primary').removeClass('btn-default');
  370. $url.val($this.data("link"));
  371. $clone.find('span.clone-url').text($this.data('link'));
  372. }
  373. }).eq(0).trigger("click");
  374. $("#repo-clone").on("shown.bs.dropdown", function () {
  375. Gogits.bindCopy("[data-init=copy]");
  376. });
  377. Gogits.bindCopy("[data-init=copy]:visible");
  378. }
  379. })();
  380. // watching script
  381. (function () {
  382. var $watch = $('#repo-watching'),
  383. watchLink = $watch.data("watch"),
  384. unwatchLink = $watch.data("unwatch");
  385. $watch.on('click', '.to-watch', function () {
  386. if ($watch.hasClass("watching")) {
  387. return false;
  388. }
  389. $.get(watchLink, function (json) {
  390. if (json.ok) {
  391. $watch.find('.text-primary').removeClass('text-primary');
  392. $watch.find('.to-watch h4').addClass('text-primary');
  393. $watch.find('.fa-eye-slash').removeClass('fa-eye-slash').addClass('fa-eye');
  394. $watch.removeClass("no-watching").addClass("watching");
  395. }
  396. });
  397. return false;
  398. }).on('click', '.to-unwatch', function () {
  399. if ($watch.hasClass("no-watching")) {
  400. return false;
  401. }
  402. $.get(unwatchLink, function (json) {
  403. if (json.ok) {
  404. $watch.find('.text-primary').removeClass('text-primary');
  405. $watch.find('.to-unwatch h4').addClass('text-primary');
  406. $watch.find('.fa-eye').removeClass('fa-eye').addClass('fa-eye-slash');
  407. $watch.removeClass("watching").addClass("no-watching");
  408. }
  409. });
  410. return false;
  411. });
  412. })();
  413. // repo diff counter
  414. (function () {
  415. var $counter = $('.diff-counter');
  416. if ($counter.length < 1) {
  417. return;
  418. }
  419. $counter.each(function (i, item) {
  420. var $item = $(item);
  421. var addLine = $item.find('span[data-line].add').data("line");
  422. var delLine = $item.find('span[data-line].del').data("line");
  423. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  424. $item.find(".bar .add").css("width", addPercent + "%");
  425. });
  426. }());
  427. // repo setting form
  428. (function () {
  429. $('#repo-setting-name').on("keyup", function () {
  430. var $this = $(this);
  431. if ($this.val() != $this.attr('title')) {
  432. $this.next('.help-block').toggleShow();
  433. } else {
  434. $this.next('.help-block').toggleHide();
  435. }
  436. });
  437. }())
  438. }
  439. function initInstall() {
  440. // database type change
  441. (function () {
  442. var mysql_default = '127.0.0.1:3306'
  443. var postgres_default = '127.0.0.1:5432'
  444. $('#install-database').on("change", function () {
  445. var val = $(this).val();
  446. if (val != "SQLite3") {
  447. $('.server-sql').show();
  448. $('.sqlite-setting').addClass("hide");
  449. if (val == "PostgreSQL") {
  450. $('.pgsql-setting').removeClass("hide");
  451. // Change the host value to the Postgres default, but only
  452. // if the user hasn't already changed it from the MySQL
  453. // default.
  454. if ($('#database-host').val() == mysql_default) {
  455. $('#database-host').val(postgres_default);
  456. }
  457. } else if (val == 'MySQL') {
  458. $('.pgsql-setting').addClass("hide");
  459. if ($('#database-host').val() == postgres_default) {
  460. $('#database-host').val(mysql_default);
  461. }
  462. } else {
  463. $('.pgsql-setting').addClass("hide");
  464. }
  465. } else {
  466. $('.server-sql').hide();
  467. $('.sqlite-setting').removeClass("hide");
  468. }
  469. });
  470. }());
  471. }
  472. function initIssue() {
  473. // close button
  474. (function () {
  475. var $closeBtn = $('#issue-close-btn');
  476. var $openBtn = $('#issue-open-btn');
  477. $('#issue-reply-content').on("keyup", function () {
  478. if ($(this).val().length) {
  479. $closeBtn.val($closeBtn.data("text"));
  480. $openBtn.val($openBtn.data("text"));
  481. } else {
  482. $closeBtn.val($closeBtn.data("origin"));
  483. $openBtn.val($openBtn.data("origin"));
  484. }
  485. });
  486. }());
  487. // issue edit mode
  488. (function () {
  489. $("#issue-edit-btn").on("click", function () {
  490. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleHide();
  491. $('#issue-edit-title,#issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleShow();
  492. });
  493. $('.issue-edit-cancel').on("click", function () {
  494. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleShow();
  495. $('#issue-edit-title,#issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleHide();
  496. })
  497. }());
  498. // issue ajax update
  499. (function () {
  500. $('.issue-edit-save').on("click", function () {
  501. $(this).toggleAjax(function (json) {
  502. if (json.ok) {
  503. $('.issue-head h1.title').text(json.title);
  504. $('.issue-main > .issue-content .content').html(json.content);
  505. $('.issue-edit-cancel').trigger("click");
  506. }
  507. });
  508. });
  509. }());
  510. // issue ajax preview
  511. (function () {
  512. $('[data-ajax-name=issue-preview]').on("click", function () {
  513. var $this = $(this);
  514. $this.toggleAjax(function (json) {
  515. if (json.ok) {
  516. $($this.data("preview")).html(json.content);
  517. }
  518. })
  519. });
  520. $('.issue-write a[data-toggle]').on("click", function () {
  521. $('.issue-preview-content').html("loading...");
  522. });
  523. }())
  524. }
  525. function initRelease() {
  526. // release new ajax preview
  527. (function () {
  528. $('[data-ajax-name=release-preview]').on("click", function () {
  529. var $this = $(this);
  530. $this.toggleAjax(function (json) {
  531. if (json.ok) {
  532. $($this.data("preview")).html(json.content);
  533. }
  534. })
  535. });
  536. $('.release-write a[data-toggle]').on("click", function () {
  537. $('.release-preview-content').html("loading...");
  538. });
  539. }());
  540. // release new target selection
  541. (function () {
  542. $('#release-new-target-branch-list').on('click', 'a', function () {
  543. $('#tag-target').val($(this).text());
  544. $('#release-new-target-name').text(" " + $(this).text());
  545. });
  546. }());
  547. }
  548. function initRepoSetting() {
  549. // repo member add
  550. $('#repo-collaborator').on('keyup', function () {
  551. var $this = $(this);
  552. if (!$this.val()) {
  553. $this.next().toggleHide();
  554. return;
  555. }
  556. $.ajax({
  557. url: '/api/v1/users/search?q=' + $this.val(),
  558. dataType: "json",
  559. success: function (json) {
  560. if (json.ok && json.data) {
  561. var html = '';
  562. $.each(json.data, function (i, item) {
  563. html += '<li><img src="' + item.avatar + '">' + item.username + '</li>';
  564. });
  565. $this.next().toggleShow();
  566. $this.next().find('ul').html(html);
  567. }else{
  568. $this.next().toggleHide();
  569. }
  570. }
  571. });
  572. }).on('focus', function () {
  573. if (!$(this).val()) {
  574. $(this).next().toggleHide();
  575. }
  576. }).next().on("click",'li',function(){
  577. $('#repo-collaborator').val($(this).text());
  578. });
  579. }
  580. (function ($) {
  581. $(function () {
  582. initCore();
  583. var body = $("#body");
  584. if (body.data("page") == "user-signup") {
  585. initRegister();
  586. }
  587. if (body.data("page") == "user") {
  588. initUserSetting();
  589. }
  590. if ($('.repo-nav').length) {
  591. initRepository();
  592. }
  593. if ($('#install-card').length) {
  594. initInstall();
  595. }
  596. if ($('#issue').length) {
  597. initIssue();
  598. }
  599. if ($('#release').length) {
  600. initRelease();
  601. }
  602. if ($('#repo-setting-container').length) {
  603. initRepoSetting();
  604. }
  605. });
  606. })(jQuery);