You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
19 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. $('#install-database').on("change", function () {
  443. var val = $(this).val();
  444. if (val != "sqlite") {
  445. $('.server-sql').show();
  446. $('.sqlite-setting').addClass("hide");
  447. if (val == "pgsql") {
  448. $('.pgsql-setting').removeClass("hide");
  449. } else {
  450. $('.pgsql-setting').addClass("hide");
  451. }
  452. } else {
  453. $('.server-sql').hide();
  454. $('.sqlite-setting').removeClass("hide");
  455. }
  456. });
  457. }());
  458. }
  459. function initIssue() {
  460. // close button
  461. (function () {
  462. var $closeBtn = $('#issue-close-btn');
  463. var $openBtn = $('#issue-open-btn');
  464. $('#issue-reply-content').on("keyup", function () {
  465. if ($(this).val().length) {
  466. $closeBtn.val($closeBtn.data("text"));
  467. $openBtn.val($openBtn.data("text"));
  468. } else {
  469. $closeBtn.val($closeBtn.data("origin"));
  470. $openBtn.val($openBtn.data("origin"));
  471. }
  472. });
  473. }());
  474. // issue edit mode
  475. (function () {
  476. $("#issue-edit-btn").on("click", function () {
  477. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleHide();
  478. $('#issue-edit-title,#issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleShow();
  479. });
  480. $('.issue-edit-cancel').on("click", function () {
  481. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleShow();
  482. $('#issue-edit-title,#issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleHide();
  483. })
  484. }());
  485. // issue ajax update
  486. (function () {
  487. $('.issue-edit-save').on("click", function () {
  488. $(this).toggleAjax(function (json) {
  489. if (json.ok) {
  490. $('.issue-head h1.title').text(json.title);
  491. $('.issue-main > .issue-content .content').html(json.content);
  492. $('.issue-edit-cancel').trigger("click");
  493. }
  494. });
  495. });
  496. }());
  497. // issue ajax preview
  498. (function () {
  499. $('[data-ajax-name=issue-preview]').on("click", function () {
  500. var $this = $(this);
  501. $this.toggleAjax(function (json) {
  502. if (json.ok) {
  503. $($this.data("preview")).html(json.content);
  504. }
  505. })
  506. });
  507. $('.issue-write a[data-toggle]').on("click", function () {
  508. $('.issue-preview-content').html("loading...");
  509. });
  510. }())
  511. }
  512. function initRelease() {
  513. // release new ajax preview
  514. (function () {
  515. $('[data-ajax-name=release-preview]').on("click", function () {
  516. var $this = $(this);
  517. $this.toggleAjax(function (json) {
  518. if (json.ok) {
  519. $($this.data("preview")).html(json.content);
  520. }
  521. })
  522. });
  523. $('.release-write a[data-toggle]').on("click", function () {
  524. $('.release-preview-content').html("loading...");
  525. });
  526. }())
  527. }
  528. (function ($) {
  529. $(function () {
  530. initCore();
  531. var body = $("#body");
  532. if (body.data("page") == "user-signup") {
  533. initRegister();
  534. }
  535. if (body.data("page") == "user") {
  536. initUserSetting();
  537. }
  538. if ($('.repo-nav').length) {
  539. initRepository();
  540. }
  541. if ($('#install-card').length) {
  542. initInstall();
  543. }
  544. if ($('#issue').length) {
  545. initIssue();
  546. }
  547. if ($('#release').length) {
  548. initRelease();
  549. }
  550. });
  551. })(jQuery);