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

app.js 17 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. Gogits.renderCodeView = function () {
  185. function selectRange($list, $select, $from) {
  186. $list.removeClass('active');
  187. if ($from) {
  188. var a = parseInt($select.attr('rel').substr(1));
  189. var b = parseInt($from.attr('rel').substr(1));
  190. var c;
  191. if (a != b) {
  192. if (a > b) {
  193. c = a;
  194. a = b;
  195. b = c;
  196. }
  197. var classes = [];
  198. for (i = a; i <= b; i++) {
  199. classes.push('.L' + i);
  200. }
  201. $list.filter(classes.join(',')).addClass('active');
  202. $.changeHash('#L' + a + '-' + 'L' + b);
  203. return
  204. }
  205. }
  206. $select.addClass('active');
  207. $.changeHash('#' + $select.attr('rel'));
  208. }
  209. $(document).on('click', '.lines-num span', function (e) {
  210. var $select = $(this);
  211. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  212. selectRange($list, $list.filter('[rel=' + $select.attr('rel') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  213. $.deSelect();
  214. });
  215. $('.code-view .lines-code > pre').each(function () {
  216. var $pre = $(this);
  217. var $lineCode = $pre.parent();
  218. var $lineNums = $lineCode.siblings('.lines-num');
  219. if ($lineNums.length > 0) {
  220. var nums = $pre.find('ol.linenums > li').length;
  221. for (var i = 1; i <= nums; i++) {
  222. $lineNums.append('<span id="L' + i + '" rel="L' + i + '">' + i + '</span>');
  223. }
  224. }
  225. });
  226. $(window).on('hashchange',function (e) {
  227. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  228. var $list = $('.code-view ol.linenums > li');
  229. if (m) {
  230. var $first = $list.filter('.' + m[1]);
  231. selectRange($list, $first, $list.filter('.' + m[2]));
  232. $("html, body").scrollTop($first.offset().top - 200);
  233. return;
  234. }
  235. m = window.location.hash.match(/^#(L\d+)$/);
  236. if (m) {
  237. var $first = $list.filter('.' + m[1]);
  238. selectRange($list, $first);
  239. $("html, body").scrollTop($first.offset().top - 200);
  240. }
  241. }).trigger('hashchange');
  242. };
  243. })(jQuery);
  244. // ajax utils
  245. (function ($) {
  246. Gogits.ajaxDelete = function (url, data, success) {
  247. data = data || {};
  248. data._method = "DELETE";
  249. $.ajax({
  250. url: url,
  251. data: data,
  252. method: "POST",
  253. dataType: "json",
  254. success: function (json) {
  255. if (success) {
  256. success(json);
  257. }
  258. }
  259. })
  260. }
  261. })(jQuery);
  262. function initCore() {
  263. Gogits.initTooltips();
  264. Gogits.initPopovers();
  265. Gogits.initTabs();
  266. Gogits.initModals();
  267. Gogits.initDropDown();
  268. Gogits.renderMarkdown();
  269. Gogits.renderCodeView();
  270. }
  271. function initRegister() {
  272. $.getScript("/js/jquery.validate.min.js", function () {
  273. Gogits.validateForm("#login-card", {
  274. rules: {
  275. "username": {
  276. required: true,
  277. maxlength: 30
  278. },
  279. "email": {
  280. required: true,
  281. email: true
  282. },
  283. "passwd": {
  284. required: true,
  285. minlength: 6,
  286. maxlength: 30
  287. },
  288. "re-passwd": {
  289. required: true,
  290. equalTo: "input[name=passwd]"
  291. }
  292. }
  293. });
  294. });
  295. }
  296. function initUserSetting() {
  297. $('#ssh-keys .delete').confirmation({
  298. singleton: true,
  299. onConfirm: function (e, $this) {
  300. Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
  301. if (json.ok) {
  302. window.location.reload();
  303. } else {
  304. alert(json.err);
  305. }
  306. });
  307. }
  308. });
  309. }
  310. function initRepository() {
  311. // clone group button script
  312. (function () {
  313. var $clone = $('.clone-group-btn');
  314. if ($clone.length) {
  315. var $url = $('.clone-group-url');
  316. $clone.find('button[data-link]').on("click",function (e) {
  317. var $this = $(this);
  318. if (!$this.hasClass('btn-primary')) {
  319. $clone.find('.input-group-btn .btn-primary').removeClass('btn-primary').addClass("btn-default");
  320. $(this).addClass('btn-primary').removeClass('btn-default');
  321. $url.val($this.data("link"));
  322. $clone.find('span.clone-url').text($this.data('link'));
  323. }
  324. }).eq(0).trigger("click");
  325. // todo copy to clipboard
  326. }
  327. })();
  328. // watching script
  329. (function () {
  330. var $watch = $('#repo-watching'),
  331. watchLink = $watch.data("watch"),
  332. unwatchLink = $watch.data("unwatch");
  333. $watch.on('click', '.to-watch',function () {
  334. if ($watch.hasClass("watching")) {
  335. return false;
  336. }
  337. $.get(watchLink, function (json) {
  338. if (json.ok) {
  339. $watch.find('.text-primary').removeClass('text-primary');
  340. $watch.find('.to-watch h4').addClass('text-primary');
  341. $watch.find('.fa-eye-slash').removeClass('fa-eye-slash').addClass('fa-eye');
  342. $watch.removeClass("no-watching").addClass("watching");
  343. }
  344. });
  345. return false;
  346. }).on('click', '.to-unwatch', function () {
  347. if ($watch.hasClass("no-watching")) {
  348. return false;
  349. }
  350. $.get(unwatchLink, function (json) {
  351. if (json.ok) {
  352. $watch.find('.text-primary').removeClass('text-primary');
  353. $watch.find('.to-unwatch h4').addClass('text-primary');
  354. $watch.find('.fa-eye').removeClass('fa-eye').addClass('fa-eye-slash');
  355. $watch.removeClass("watching").addClass("no-watching");
  356. }
  357. });
  358. return false;
  359. });
  360. })();
  361. // repo diff counter
  362. (function () {
  363. var $counter = $('.diff-counter');
  364. if ($counter.length < 1) {
  365. return;
  366. }
  367. $counter.each(function (i, item) {
  368. var $item = $(item);
  369. var addLine = $item.find('span[data-line].add').data("line");
  370. var delLine = $item.find('span[data-line].del').data("line");
  371. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  372. $item.find(".bar .add").css("width", addPercent + "%");
  373. });
  374. }());
  375. }
  376. function initInstall() {
  377. // database type change
  378. (function () {
  379. $('#install-database').on("change", function () {
  380. var val = $(this).val();
  381. if (val != "sqlite") {
  382. $('.server-sql').show();
  383. $('.sqlite-setting').addClass("hide");
  384. if (val == "pgsql") {
  385. $('.pgsql-setting').removeClass("hide");
  386. } else {
  387. $('.pgsql-setting').addClass("hide");
  388. }
  389. } else {
  390. $('.server-sql').hide();
  391. $('.sqlite-setting').removeClass("hide");
  392. }
  393. });
  394. }());
  395. }
  396. function initIssue() {
  397. // close button
  398. (function () {
  399. var $closeBtn = $('#issue-close-btn');
  400. var $openBtn = $('#issue-open-btn');
  401. $('#issue-reply-content').on("keyup", function () {
  402. if ($(this).val().length) {
  403. $closeBtn.val($closeBtn.data("text"));
  404. $openBtn.val($openBtn.data("text"));
  405. } else {
  406. $closeBtn.val($closeBtn.data("origin"));
  407. $openBtn.val($openBtn.data("origin"));
  408. }
  409. });
  410. }());
  411. // issue edit mode
  412. (function () {
  413. $("#issue-edit-btn").on("click", function () {
  414. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleHide();
  415. $('#issue-edit-title,#issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleShow();
  416. });
  417. $('.issue-edit-cancel').on("click", function () {
  418. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleShow();
  419. $('#issue-edit-title,#issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleHide();
  420. })
  421. }());
  422. // issue ajax update
  423. (function () {
  424. $('.issue-edit-save').on("click", function () {
  425. $(this).toggleAjax(function (json) {
  426. if (json.ok) {
  427. $('.issue-head h1.title').text(json.title);
  428. $('.issue-main > .issue-content .content').html(json.content);
  429. $('.issue-edit-cancel').trigger("click");
  430. }
  431. });
  432. });
  433. }());
  434. // issue ajax preview
  435. (function () {
  436. $('[data-ajax-name=issue-preview]').on("click", function () {
  437. var $this = $(this);
  438. $this.toggleAjax(function (json) {
  439. if (json.ok) {
  440. $($this.data("preview")).html(json.content);
  441. }
  442. })
  443. });
  444. $('.issue-write a[data-toggle]').on("click", function () {
  445. $('.issue-preview-content').html("loading...");
  446. });
  447. }())
  448. }
  449. (function ($) {
  450. $(function () {
  451. initCore();
  452. var body = $("#body");
  453. if (body.data("page") == "user-signup") {
  454. initRegister();
  455. }
  456. if (body.data("page") == "user") {
  457. initUserSetting();
  458. }
  459. if ($('.repo-nav').length) {
  460. initRepository();
  461. }
  462. if ($('#install-card').length) {
  463. initInstall();
  464. }
  465. if ($('#issue').length) {
  466. initIssue();
  467. }
  468. });
  469. })(jQuery);