app.js 21 KB

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