app.js 23 KB

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