app.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. console.log("toggleAjax:", method, url, data);
  86. $.ajax({
  87. url: url,
  88. method: method.toUpperCase(),
  89. data: data,
  90. error: errorCallback,
  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 initUserSetting() {
  313. // ssh confirmation
  314. $('#ssh-keys .delete').confirmation({
  315. singleton: true,
  316. onConfirm: function (e, $this) {
  317. Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
  318. if (json.ok) {
  319. window.location.reload();
  320. } else {
  321. alert(json.err);
  322. }
  323. });
  324. }
  325. });
  326. // profile form
  327. (function () {
  328. $('#user-setting-username').on("keyup", function () {
  329. var $this = $(this);
  330. if ($this.val() != $this.attr('title')) {
  331. $this.next('.help-block').toggleShow();
  332. } else {
  333. $this.next('.help-block').toggleHide();
  334. }
  335. });
  336. }())
  337. }
  338. function initRepository() {
  339. // clone group button script
  340. (function () {
  341. var $clone = $('.clone-group-btn');
  342. if ($clone.length) {
  343. var $url = $('.clone-group-url');
  344. $clone.find('button[data-link]').on("click", function (e) {
  345. var $this = $(this);
  346. if (!$this.hasClass('btn-primary')) {
  347. $clone.find('.input-group-btn .btn-primary').removeClass('btn-primary').addClass("btn-default");
  348. $(this).addClass('btn-primary').removeClass('btn-default');
  349. $url.val($this.data("link"));
  350. $clone.find('span.clone-url').text($this.data('link'));
  351. }
  352. }).eq(0).trigger("click");
  353. $("#repo-clone").on("shown.bs.dropdown", function () {
  354. Gogits.bindCopy("[data-init=copy]");
  355. });
  356. Gogits.bindCopy("[data-init=copy]:visible");
  357. }
  358. })();
  359. // watching script
  360. (function () {
  361. var $watch = $('#repo-watching'),
  362. watchLink = $watch.data("watch"),
  363. unwatchLink = $watch.data("unwatch");
  364. $watch.on('click', '.to-watch', function () {
  365. if ($watch.hasClass("watching")) {
  366. return false;
  367. }
  368. $.get(watchLink, function (json) {
  369. if (json.ok) {
  370. $watch.find('.text-primary').removeClass('text-primary');
  371. $watch.find('.to-watch h4').addClass('text-primary');
  372. $watch.find('.fa-eye-slash').removeClass('fa-eye-slash').addClass('fa-eye');
  373. $watch.removeClass("no-watching").addClass("watching");
  374. }
  375. });
  376. return false;
  377. }).on('click', '.to-unwatch', function () {
  378. if ($watch.hasClass("no-watching")) {
  379. return false;
  380. }
  381. $.get(unwatchLink, function (json) {
  382. if (json.ok) {
  383. $watch.find('.text-primary').removeClass('text-primary');
  384. $watch.find('.to-unwatch h4').addClass('text-primary');
  385. $watch.find('.fa-eye').removeClass('fa-eye').addClass('fa-eye-slash');
  386. $watch.removeClass("watching").addClass("no-watching");
  387. }
  388. });
  389. return false;
  390. });
  391. })();
  392. // repo diff counter
  393. (function () {
  394. var $counter = $('.diff-counter');
  395. if ($counter.length < 1) {
  396. return;
  397. }
  398. $counter.each(function (i, item) {
  399. var $item = $(item);
  400. var addLine = $item.find('span[data-line].add').data("line");
  401. var delLine = $item.find('span[data-line].del').data("line");
  402. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  403. $item.find(".bar .add").css("width", addPercent + "%");
  404. });
  405. }());
  406. // repo setting form
  407. (function () {
  408. $('#repo-setting-name').on("keyup", function () {
  409. var $this = $(this);
  410. if ($this.val() != $this.attr('title')) {
  411. $this.next('.help-block').toggleShow();
  412. } else {
  413. $this.next('.help-block').toggleHide();
  414. }
  415. });
  416. }())
  417. }
  418. function initInstall() {
  419. // database type change
  420. (function () {
  421. var mysql_default = '127.0.0.1:3306'
  422. var postgres_default = '127.0.0.1:5432'
  423. $('#install-database').on("change", function () {
  424. var val = $(this).val();
  425. if (val != "SQLite3") {
  426. $('.server-sql').show();
  427. $('.sqlite-setting').addClass("hide");
  428. if (val == "PostgreSQL") {
  429. $('.pgsql-setting').removeClass("hide");
  430. // Change the host value to the Postgres default, but only
  431. // if the user hasn't already changed it from the MySQL
  432. // default.
  433. if ($('#database-host').val() == mysql_default) {
  434. $('#database-host').val(postgres_default);
  435. }
  436. } else if (val == 'MySQL') {
  437. $('.pgsql-setting').addClass("hide");
  438. if ($('#database-host').val() == postgres_default) {
  439. $('#database-host').val(mysql_default);
  440. }
  441. } else {
  442. $('.pgsql-setting').addClass("hide");
  443. }
  444. } else {
  445. $('.server-sql').hide();
  446. $('.sqlite-setting').removeClass("hide");
  447. }
  448. });
  449. }());
  450. }
  451. function initIssue() {
  452. // close button
  453. (function () {
  454. var $closeBtn = $('#issue-close-btn');
  455. var $openBtn = $('#issue-open-btn');
  456. $('#issue-reply-content').on("keyup", function () {
  457. if ($(this).val().length) {
  458. $closeBtn.val($closeBtn.data("text"));
  459. $openBtn.val($openBtn.data("text"));
  460. } else {
  461. $closeBtn.val($closeBtn.data("origin"));
  462. $openBtn.val($openBtn.data("origin"));
  463. }
  464. });
  465. }());
  466. // issue edit mode
  467. (function () {
  468. $("#issue-edit-btn").on("click", function () {
  469. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleHide();
  470. $('#issue-edit-title,.issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleShow();
  471. });
  472. $('.issue-edit-cancel').on("click", function () {
  473. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleShow();
  474. $('#issue-edit-title,.issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleHide();
  475. })
  476. }());
  477. // issue ajax update
  478. (function () {
  479. var $cnt = $('#issue-edit-content');
  480. $('.issue-edit-save').on("click", function () {
  481. $cnt.attr('data-ajax-rel', 'issue-edit-save');
  482. $(this).toggleAjax(function (json) {
  483. if (json.ok) {
  484. $('.issue-head h1.title').text(json.title);
  485. $('.issue-main > .issue-content .content').html(json.content);
  486. $('.issue-edit-cancel').trigger("click");
  487. }
  488. });
  489. setTimeout(function () {
  490. $cnt.attr('data-ajax-rel', 'issue-edit-preview');
  491. }, 200)
  492. });
  493. }());
  494. // issue ajax preview
  495. (function () {
  496. $('[data-ajax-name=issue-preview],[data-ajax-name=issue-edit-preview]').on("click", function () {
  497. var $this = $(this);
  498. $this.toggleAjax(function (resp) {
  499. $($this.data("preview")).html(resp);
  500. }, function () {
  501. $($this.data("preview")).html("no content");
  502. })
  503. });
  504. $('.issue-write a[data-toggle]').on("click", function () {
  505. var selector = $(this).parent().next(".issue-preview").find('a').data('preview');
  506. $(selector).html("loading...");
  507. });
  508. }());
  509. // assignee
  510. var is_issue_bar = $('.issue-bar').length > 0;
  511. var $a = $('.assignee');
  512. if ($a.data("assigned") > 0) {
  513. $('.clear-assignee').toggleShow();
  514. }
  515. $('.assignee', '#issue').on('click', 'li', function () {
  516. var uid = $(this).data("uid");
  517. if (is_issue_bar) {
  518. var assignee = $a.data("assigned");
  519. if (uid != assignee) {
  520. $.post($a.data("ajax"), {
  521. issue: $('#issue').data("id"),
  522. assigneeid: uid
  523. }, function (json) {
  524. if (json.ok) {
  525. window.location.reload();
  526. }
  527. })
  528. }
  529. return;
  530. }
  531. $('#assignee').val(uid);
  532. if (uid > 0) {
  533. $('.clear-assignee').toggleShow();
  534. $('#assigned').text($(this).find("strong").text())
  535. } else {
  536. $('.clear-assignee').toggleHide();
  537. $('#assigned').text($('#assigned').data("no-assigned"));
  538. }
  539. });
  540. // milestone
  541. $('#issue .dropdown-menu a[data-toggle="tab"]').on("click", function (e) {
  542. e.stopPropagation();
  543. $(this).tab('show');
  544. return false;
  545. });
  546. var $m = $('.milestone');
  547. if ($m.data("milestone") > 0) {
  548. $('.clear-milestone').toggleShow();
  549. }
  550. $('.milestone', '#issue').on('click', 'li.milestone-item', function () {
  551. var id = $(this).data("id");
  552. if (is_issue_bar) {
  553. var m = $m.data("milestone");
  554. if (id != m) {
  555. $.post($m.data("ajax"), {
  556. issue: $('#issue').data("id"),
  557. milestone: id
  558. }, function (json) {
  559. if (json.ok) {
  560. window.location.reload();
  561. if (id > 0) {
  562. $('.clear-milestone').toggleShow();
  563. } else {
  564. $('.clear-milestone').toggleHide();
  565. }
  566. }
  567. })
  568. }
  569. return;
  570. }
  571. $('#milestone-id').val(id);
  572. if (id > 0) {
  573. $('.clear-milestone').toggleShow();
  574. $('#milestone').text($(this).find("strong").text())
  575. } else {
  576. $('.clear-milestone').toggleHide();
  577. $('#milestone').text($('#milestone').data("no-milestone"));
  578. }
  579. });
  580. // labels
  581. var removeLabels = [];
  582. $('#label-manage-btn').on("click", function () {
  583. var $list = $('#label-list');
  584. if ($list.hasClass("managing")) {
  585. var ids = [];
  586. $list.find('li').each(function (i, item) {
  587. var id = $(item).data("id");
  588. if (id > 0) {
  589. ids.push(id);
  590. }
  591. });
  592. $.post($list.data("ajax"), {"ids": ids.join(","), "remove": removeLabels.join(",")}, function (json) {
  593. if (json.ok) {
  594. window.location.reload();
  595. }
  596. })
  597. } else {
  598. $list.addClass("managing");
  599. $list.find(".count").hide();
  600. $list.find(".del").show();
  601. $(this).text("Save Labels");
  602. $list.on('click', 'li.label-item', function () {
  603. var $this = $(this);
  604. $this.after($('.label-change-li').detach().show());
  605. $('#label-name-change-ipt').val($this.find('.name').text());
  606. var color = $this.find('.color').data("color");
  607. $('.label-change-color-picker').colorpicker("setValue", color);
  608. $('#label-color-change-ipt,#label-color-change-ipt2').val(color);
  609. $('#label-change-id-ipt').val($this.data("id"));
  610. return false;
  611. });
  612. }
  613. });
  614. var colorRegex = new RegExp("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
  615. $('#label-color-ipt2').on('keyup', function () {
  616. var val = $(this).val();
  617. if (val.length > 7) {
  618. $(this).val(val.substr(0, 7));
  619. }
  620. if (colorRegex.test(val)) {
  621. $('.label-color-picker').colorpicker("setValue", val);
  622. }
  623. return true;
  624. });
  625. $('#label-color-change-ipt2').on('keyup', function () {
  626. var val = $(this).val();
  627. console.log(val);
  628. if (val.length > 7) {
  629. $(this).val(val.substr(0, 7));
  630. }
  631. if (colorRegex.test(val)) {
  632. $('.label-change-color-picker').colorpicker("setValue", val);
  633. }
  634. return true;
  635. });
  636. $("#label-list").on('click', '.del', function () {
  637. var $p = $(this).parent();
  638. removeLabels.push($p.data('id'));
  639. $p.remove();
  640. return false;
  641. });
  642. $('.label-selected').each(function (i, item) {
  643. var $item = $(item);
  644. var color = $item.find('.color').data('color');
  645. $item.css('background-color', color);
  646. });
  647. $('.issue-bar .labels .dropdown-menu').on('click', 'li', function (e) {
  648. var url = $('.issue-bar .labels').data("ajax");
  649. var id = $(this).data('id');
  650. var check = $(this).hasClass("checked");
  651. $.post(url, {id: id, action: check ? 'detach' : "attach", issue: $('#issue').data('id')}, function (json) {
  652. if (json.ok) {
  653. window.location.reload();
  654. }
  655. });
  656. e.stopPropagation();
  657. return false;
  658. })
  659. }
  660. function initRelease() {
  661. // release new ajax preview
  662. (function () {
  663. $('[data-ajax-name=release-preview]').on("click", function () {
  664. var $this = $(this);
  665. $this.toggleAjax(function (resp) {
  666. $($this.data("preview")).html(resp);
  667. }, function () {
  668. $($this.data("preview")).html("no content");
  669. })
  670. });
  671. $('.release-write a[data-toggle]').on("click", function () {
  672. $('.release-preview-content').html("loading...");
  673. });
  674. }());
  675. // release new target selection
  676. (function () {
  677. $('#release-new-target-branch-list').on('click', 'a', function () {
  678. $('#tag-target').val($(this).text());
  679. $('#release-new-target-name').text(" " + $(this).text());
  680. });
  681. }());
  682. }
  683. function initRepoSetting() {
  684. // repo member add
  685. $('#repo-collaborator').on('keyup', function () {
  686. var $this = $(this);
  687. if (!$this.val()) {
  688. $this.next().toggleHide();
  689. return;
  690. }
  691. $.ajax({
  692. url: '/api/v1/users/search?q=' + $this.val(),
  693. dataType: "json",
  694. success: function (json) {
  695. if (json.ok && json.data.length) {
  696. var html = '';
  697. $.each(json.data, function (i, item) {
  698. html += '<li><img src="' + item.avatar + '">' + item.username + '</li>';
  699. });
  700. $this.next().toggleShow();
  701. $this.next().find('ul').html(html);
  702. } else {
  703. $this.next().toggleHide();
  704. }
  705. }
  706. });
  707. }).on('focus', function () {
  708. if (!$(this).val()) {
  709. $(this).next().toggleHide();
  710. }
  711. }).next().on("click", 'li', function () {
  712. $('#repo-collaborator').val($(this).text());
  713. });
  714. }
  715. function initRepoCreating() {
  716. // owner switch menu click
  717. (function () {
  718. $('#repo-owner-switch .dropdown-menu').on("click", "li", function () {
  719. var uid = $(this).data('uid');
  720. // set to input
  721. $('#repo-owner-id').val(uid);
  722. // set checked class
  723. if (!$(this).hasClass("checked")) {
  724. $(this).parent().find(".checked").removeClass("checked");
  725. $(this).addClass("checked");
  726. }
  727. console.log("set repo owner to uid :",uid);
  728. });
  729. }());
  730. console.log("init repo-creating scripts");
  731. }
  732. (function ($) {
  733. $(function () {
  734. initCore();
  735. var body = $("#body");
  736. if (body.data("page") == "user") {
  737. initUserSetting();
  738. }
  739. if ($('.repo-nav').length) {
  740. initRepository();
  741. }
  742. if ($('#install-card').length) {
  743. initInstall();
  744. }
  745. if ($('#issue').length) {
  746. initIssue();
  747. }
  748. if ($('#release').length) {
  749. initRelease();
  750. }
  751. if ($('#repo-setting-container').length) {
  752. initRepoSetting();
  753. }
  754. if ($('#repo-create').length) {
  755. initRepoCreating();
  756. }
  757. });
  758. })(jQuery);
  759. String.prototype.endsWith = function (suffix) {
  760. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  761. };