app.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. // api working
  285. Gogits.getUsers = function (val, $target) {
  286. $.ajax({
  287. url: '/api/v1/users/search?q=' + val,
  288. dataType: "json",
  289. success: function (json) {
  290. if (json.ok && json.data.length) {
  291. var html = '';
  292. $.each(json.data, function (i, item) {
  293. html += '<li><img src="' + item.avatar + '">' + item.username + '</li>';
  294. });
  295. $target.toggleShow();
  296. $target.find('ul').html(html);
  297. } else {
  298. $target.toggleHide();
  299. }
  300. }
  301. });
  302. }
  303. })(jQuery);
  304. // ajax utils
  305. (function ($) {
  306. Gogits.ajaxDelete = function (url, data, success) {
  307. data = data || {};
  308. data._method = "DELETE";
  309. $.ajax({
  310. url: url,
  311. data: data,
  312. method: "POST",
  313. dataType: "json",
  314. success: function (json) {
  315. if (success) {
  316. success(json);
  317. }
  318. }
  319. })
  320. }
  321. })(jQuery);
  322. function initCore() {
  323. Gogits.initTooltips();
  324. Gogits.initPopovers();
  325. Gogits.initTabs();
  326. Gogits.initModals();
  327. Gogits.initDropDown();
  328. Gogits.renderMarkdown();
  329. Gogits.renderCodeView();
  330. }
  331. function initUserSetting() {
  332. // ssh confirmation
  333. $('#ssh-keys .delete').confirmation({
  334. singleton: true,
  335. onConfirm: function (e, $this) {
  336. Gogits.ajaxDelete("", {"id": $this.data("del")}, function (json) {
  337. if (json.ok) {
  338. window.location.reload();
  339. } else {
  340. alert(json.err);
  341. }
  342. });
  343. }
  344. });
  345. // profile form
  346. (function () {
  347. $('#user-setting-username').on("keyup", function () {
  348. var $this = $(this);
  349. if ($this.val() != $this.attr('title')) {
  350. $this.next('.help-block').toggleShow();
  351. } else {
  352. $this.next('.help-block').toggleHide();
  353. }
  354. });
  355. }())
  356. }
  357. function initRepository() {
  358. // clone group button script
  359. (function () {
  360. var $clone = $('.clone-group-btn');
  361. if ($clone.length) {
  362. var $url = $('.clone-group-url');
  363. $clone.find('button[data-link]').on("click", function (e) {
  364. var $this = $(this);
  365. if (!$this.hasClass('btn-primary')) {
  366. $clone.find('.input-group-btn .btn-primary').removeClass('btn-primary').addClass("btn-default");
  367. $(this).addClass('btn-primary').removeClass('btn-default');
  368. $url.val($this.data("link"));
  369. $clone.find('span.clone-url').text($this.data('link'));
  370. }
  371. }).eq(0).trigger("click");
  372. $("#repo-clone").on("shown.bs.dropdown", function () {
  373. Gogits.bindCopy("[data-init=copy]");
  374. });
  375. Gogits.bindCopy("[data-init=copy]:visible");
  376. }
  377. })();
  378. // watching script
  379. (function () {
  380. var $watch = $('#repo-watching'),
  381. watchLink = $watch.attr("data-watch"),
  382. // Use $.attr() to work around jQuery not finding $.data("unwatch") in Firefox,
  383. // which has a method "unwatch" on `Object` that gets returned instead.
  384. unwatchLink = $watch.attr("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. var mysql_default = '127.0.0.1:3306'
  443. var postgres_default = '127.0.0.1:5432'
  444. $('#install-database').on("change", function () {
  445. var val = $(this).val();
  446. if (val != "SQLite3") {
  447. $('.server-sql').show();
  448. $('.sqlite-setting').addClass("hide");
  449. if (val == "PostgreSQL") {
  450. $('.pgsql-setting').removeClass("hide");
  451. // Change the host value to the Postgres default, but only
  452. // if the user hasn't already changed it from the MySQL
  453. // default.
  454. if ($('#database-host').val() == mysql_default) {
  455. $('#database-host').val(postgres_default);
  456. }
  457. } else if (val == 'MySQL') {
  458. $('.pgsql-setting').addClass("hide");
  459. if ($('#database-host').val() == postgres_default) {
  460. $('#database-host').val(mysql_default);
  461. }
  462. } else {
  463. $('.pgsql-setting').addClass("hide");
  464. }
  465. } else {
  466. $('.server-sql').hide();
  467. $('.sqlite-setting').removeClass("hide");
  468. }
  469. });
  470. }());
  471. }
  472. function initIssue() {
  473. // close button
  474. (function () {
  475. var $closeBtn = $('#issue-close-btn');
  476. var $openBtn = $('#issue-open-btn');
  477. $('#issue-reply-content').on("keyup", function () {
  478. if ($(this).val().length) {
  479. $closeBtn.val($closeBtn.data("text"));
  480. $openBtn.val($openBtn.data("text"));
  481. } else {
  482. $closeBtn.val($closeBtn.data("origin"));
  483. $openBtn.val($openBtn.data("origin"));
  484. }
  485. });
  486. }());
  487. // Preview for images.
  488. (function() {
  489. var $hoverElement = $("<div></div>");
  490. var $hoverImage = $("<img />");
  491. $hoverElement.addClass("attachment-preview");
  492. $hoverElement.hide();
  493. $hoverImage.addClass("attachment-preview-img");
  494. $hoverElement.append($hoverImage);
  495. $(document.body).append($hoverElement);
  496. var over = function() {
  497. var $this = $(this);
  498. if ($this.text().match(/\.(png|jpg|jpeg|gif)$/) == false) {
  499. return;
  500. }
  501. if ($hoverImage.attr("src") != $this.attr("href")) {
  502. $hoverImage.attr("src", $this.attr("href"));
  503. $hoverImage.load(function() {
  504. var height = this.height;
  505. var width = this.width;
  506. if (height > 300) {
  507. var factor = 300 / height;
  508. height = factor * height;
  509. width = factor * width;
  510. }
  511. $hoverImage.css({"height": height, "width": width});
  512. var offset = $this.offset();
  513. var left = offset.left, top = offset.top + $this.height() + 5;
  514. $hoverElement.css({"top": top + "px", "left": left + "px"});
  515. $hoverElement.css({"height": height + 16, "width": width + 16});
  516. $hoverElement.show();
  517. });
  518. } else {
  519. $hoverElement.show();
  520. }
  521. };
  522. var out = function() {
  523. $hoverElement.hide();
  524. };
  525. $(".issue-main .attachments .attachment").hover(over, out);
  526. }());
  527. // Upload.
  528. (function() {
  529. var $attached = $("#attached");
  530. var $attachments = $("input[name=attachments]");
  531. var $addButton = $("#attachments-button");
  532. var commentId = $addButton.attr("data-comment-id"); // "0" == for issue, "" == for comment
  533. var accepted = $addButton.attr("data-accept");
  534. $addButton.on("click", function() {
  535. // TODO: (nuss-justin): open dialog, upload file, add id to list, add file to $attached list
  536. return false;
  537. });
  538. }());
  539. // issue edit mode
  540. (function () {
  541. $("#issue-edit-btn").on("click", function () {
  542. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleHide();
  543. $('#issue-edit-title,.issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleShow();
  544. });
  545. $('.issue-edit-cancel').on("click", function () {
  546. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleShow();
  547. $('#issue-edit-title,.issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleHide();
  548. })
  549. }());
  550. // issue ajax update
  551. (function () {
  552. var $cnt = $('#issue-edit-content');
  553. $('.issue-edit-save').on("click", function () {
  554. $cnt.attr('data-ajax-rel', 'issue-edit-save');
  555. $(this).toggleAjax(function (json) {
  556. if (json.ok) {
  557. $('.issue-head h1.title').text(json.title);
  558. $('.issue-main > .issue-content .content').html(json.content);
  559. $('.issue-edit-cancel').trigger("click");
  560. }
  561. });
  562. setTimeout(function () {
  563. $cnt.attr('data-ajax-rel', 'issue-edit-preview');
  564. }, 200)
  565. });
  566. }());
  567. // issue ajax preview
  568. (function () {
  569. $('[data-ajax-name=issue-preview],[data-ajax-name=issue-edit-preview]').on("click", function () {
  570. var $this = $(this);
  571. $this.toggleAjax(function (resp) {
  572. $($this.data("preview")).html(resp);
  573. }, function () {
  574. $($this.data("preview")).html("no content");
  575. })
  576. });
  577. $('.issue-write a[data-toggle]').on("click", function () {
  578. var selector = $(this).parent().next(".issue-preview").find('a').data('preview');
  579. $(selector).html("loading...");
  580. });
  581. }());
  582. // assignee
  583. var is_issue_bar = $('.issue-bar').length > 0;
  584. var $a = $('.assignee');
  585. if ($a.data("assigned") > 0) {
  586. $('.clear-assignee').toggleShow();
  587. }
  588. $('.assignee', '#issue').on('click', 'li', function () {
  589. var uid = $(this).data("uid");
  590. if (is_issue_bar) {
  591. var assignee = $a.data("assigned");
  592. if (uid != assignee) {
  593. var text = $(this).text();
  594. var img = $("img", this).attr("src");
  595. $.post($a.data("ajax"), {
  596. issue: $('#issue').data("id"),
  597. assigneeid: uid
  598. }, function (json) {
  599. if (json.ok) {
  600. //window.location.reload();
  601. $a.data("assigned", uid);
  602. if (uid > 0) {
  603. $('.clear-assignee').toggleShow();
  604. $(".assignee > p").html('<img src="' + img + '"><strong>' + text + '</strong>');
  605. } else {
  606. $('.clear-assignee').toggleHide();
  607. $(".assignee > p").text("No one assigned");
  608. }
  609. }
  610. })
  611. }
  612. return;
  613. }
  614. $('#assignee').val(uid);
  615. if (uid > 0) {
  616. $('.clear-assignee').toggleShow();
  617. $('#assigned').text($(this).find("strong").text())
  618. } else {
  619. $('.clear-assignee').toggleHide();
  620. $('#assigned').text($('#assigned').data("no-assigned"));
  621. }
  622. });
  623. // milestone
  624. $('#issue .dropdown-menu a[data-toggle="tab"]').on("click", function (e) {
  625. e.stopPropagation();
  626. $(this).tab('show');
  627. return false;
  628. });
  629. var $m = $('.milestone');
  630. if ($m.data("milestone") > 0) {
  631. $('.clear-milestone').toggleShow();
  632. }
  633. $('.milestone', '#issue').on('click', 'li.milestone-item', function () {
  634. var id = $(this).data("id");
  635. if (is_issue_bar) {
  636. var m = $m.data("milestone");
  637. if (id != m) {
  638. var text = $(this).text();
  639. $.post($m.data("ajax"), {
  640. issue: $('#issue').data("id"),
  641. milestone: id
  642. }, function (json) {
  643. if (json.ok) {
  644. //window.location.reload();
  645. $m.data("milestone", id);
  646. if (id > 0) {
  647. $('.clear-milestone').toggleShow();
  648. $(".milestone > .name").html('<a href="' + location.pathname + '?milestone=' + id + '"><strong>' + text + '</strong></a>');
  649. } else {
  650. $('.clear-milestone').toggleHide();
  651. $(".milestone > .name").text("No milestone");
  652. }
  653. }
  654. });
  655. }
  656. return;
  657. }
  658. $('#milestone-id').val(id);
  659. if (id > 0) {
  660. $('.clear-milestone').toggleShow();
  661. $('#milestone').text($(this).find("strong").text())
  662. } else {
  663. $('.clear-milestone').toggleHide();
  664. $('#milestone').text($('#milestone').data("no-milestone"));
  665. }
  666. });
  667. // labels
  668. var removeLabels = [];
  669. $('#label-manage-btn').on("click", function () {
  670. var $list = $('#label-list');
  671. if ($list.hasClass("managing")) {
  672. var ids = [];
  673. $list.find('li').each(function (i, item) {
  674. var id = $(item).data("id");
  675. if (id > 0) {
  676. ids.push(id);
  677. }
  678. });
  679. $.post($list.data("ajax"), {"ids": ids.join(","), "remove": removeLabels.join(",")}, function (json) {
  680. if (json.ok) {
  681. window.location.reload();
  682. }
  683. })
  684. } else {
  685. $list.addClass("managing");
  686. $list.find(".count").hide();
  687. $list.find(".del").show();
  688. $(this).text("Save Labels");
  689. $list.on('click', 'li.label-item', function () {
  690. var $this = $(this);
  691. $this.after($('.label-change-li').detach().show());
  692. $('#label-name-change-ipt').val($this.find('.name').text());
  693. var color = $this.find('.color').data("color");
  694. $('.label-change-color-picker').colorpicker("setValue", color);
  695. $('#label-color-change-ipt,#label-color-change-ipt2').val(color);
  696. $('#label-change-id-ipt').val($this.data("id"));
  697. return false;
  698. });
  699. }
  700. });
  701. var colorRegex = new RegExp("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
  702. $('#label-color-ipt2').on('keyup', function () {
  703. var val = $(this).val();
  704. if (val.length > 7) {
  705. $(this).val(val.substr(0, 7));
  706. }
  707. if (colorRegex.test(val)) {
  708. $('.label-color-picker').colorpicker("setValue", val);
  709. }
  710. return true;
  711. });
  712. $('#label-color-change-ipt2').on('keyup', function () {
  713. var val = $(this).val();
  714. console.log(val);
  715. if (val.length > 7) {
  716. $(this).val(val.substr(0, 7));
  717. }
  718. if (colorRegex.test(val)) {
  719. $('.label-change-color-picker').colorpicker("setValue", val);
  720. }
  721. return true;
  722. });
  723. $("#label-list").on('click', '.del', function () {
  724. var $p = $(this).parent();
  725. removeLabels.push($p.data('id'));
  726. $p.remove();
  727. return false;
  728. });
  729. $('.label-selected').each(function (i, item) {
  730. var $item = $(item);
  731. var color = $item.find('.color').data('color');
  732. $item.css('background-color', color);
  733. });
  734. $('.issue-bar .labels .dropdown-menu').on('click', 'li', function (e) {
  735. var $labels = $('.issue-bar .labels');
  736. var url = $labels.data("ajax");
  737. var id = $(this).data('id');
  738. var check = $(this).hasClass("checked");
  739. var item = this;
  740. $.post(url, {id: id, action: check ? 'detach' : "attach", issue: $('#issue').data('id')}, function (json) {
  741. if (json.ok) {
  742. if (check) {
  743. $("span.check.pull-left", item).remove();
  744. $(item).removeClass("checked");
  745. $(item).addClass("no-checked");
  746. $("#label-" + id, $labels).remove();
  747. } else {
  748. $(item).prepend('<span class="check pull-left"><i class="fa fa-check"></i></span>');
  749. $(item).removeClass("no-checked");
  750. $(item).addClass("checked");
  751. var $l = $("<p></p>");
  752. var c = $("span.color", item).css("background-color");
  753. $l.attr("id", "label-" + id);
  754. $l.attr("class", "label-item label-white");
  755. $l.css("background-color", c);
  756. $l.append("<strong>" + $(item).text() + "</strong>");
  757. $labels.append($l);
  758. }
  759. }
  760. });
  761. e.stopPropagation();
  762. return false;
  763. })
  764. }
  765. function initRelease() {
  766. // release new ajax preview
  767. (function () {
  768. $('[data-ajax-name=release-preview]').on("click", function () {
  769. var $this = $(this);
  770. $this.toggleAjax(function (resp) {
  771. $($this.data("preview")).html(resp);
  772. }, function () {
  773. $($this.data("preview")).html("no content");
  774. })
  775. });
  776. $('.release-write a[data-toggle]').on("click", function () {
  777. $('.release-preview-content').html("loading...");
  778. });
  779. }());
  780. // release new target selection
  781. (function () {
  782. $('#release-new-target-branch-list').on('click', 'a', function () {
  783. $('#tag-target').val($(this).text());
  784. $('#release-new-target-name').text(" " + $(this).text());
  785. });
  786. }());
  787. }
  788. function initRepoSetting() {
  789. // repo member add
  790. $('#repo-collaborator').on('keyup', function () {
  791. var $this = $(this);
  792. if (!$this.val()) {
  793. $this.next().toggleHide();
  794. return;
  795. }
  796. Gogits.getUsers($this.val(), $this.next());
  797. /*$.ajax({
  798. url: '/api/v1/users/search?q=' + $this.val(),
  799. dataType: "json",
  800. success: function (json) {
  801. if (json.ok && json.data.length) {
  802. var html = '';
  803. $.each(json.data, function (i, item) {
  804. html += '<li><img src="' + item.avatar + '">' + item.username + '</li>';
  805. });
  806. $this.next().toggleShow();
  807. $this.next().find('ul').html(html);
  808. } else {
  809. $this.next().toggleHide();
  810. }
  811. }
  812. });*/
  813. }).on('focus', function () {
  814. if (!$(this).val()) {
  815. $(this).next().toggleHide();
  816. }
  817. }).next().on("click", 'li', function () {
  818. $('#repo-collaborator').val($(this).text());
  819. });
  820. }
  821. function initRepoCreating() {
  822. // owner switch menu click
  823. (function () {
  824. $('#repo-owner-switch .dropdown-menu').on("click", "li", function () {
  825. var uid = $(this).data('uid');
  826. // set to input
  827. $('#repo-owner-id').val(uid);
  828. // set checked class
  829. if (!$(this).hasClass("checked")) {
  830. $(this).parent().find(".checked").removeClass("checked");
  831. $(this).addClass("checked");
  832. }
  833. // set button group to show clicked owner
  834. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  835. $('#repo-owner-name').text($(this).text().trim());
  836. console.log("set repo owner to uid :", uid, $(this).text().trim());
  837. });
  838. }());
  839. console.log("init repo-creating scripts");
  840. }
  841. function initOrganization() {
  842. (function(){
  843. $('#org-team-add-user').on('keyup', function () {
  844. var $this = $(this);
  845. if (!$this.val()) {
  846. $this.next().toggleHide();
  847. return;
  848. }
  849. Gogits.getUsers($this.val(), $this.next());
  850. }).on('focus', function () {
  851. if (!$(this).val()) {
  852. $(this).next().toggleHide();
  853. }
  854. }).next().on("click", 'li', function () {
  855. $('#org-team-add-user').val($(this).text());
  856. $('#org-team-add-user-form').submit();
  857. }).toggleHide();
  858. console.log("init script : add user to team");
  859. }());
  860. (function(){
  861. $('#org-team-add-repo').next().toggleHide();
  862. console.log("init script : add repository to team");
  863. }());
  864. console.log("init script : organization done");
  865. }
  866. (function ($) {
  867. $(function () {
  868. initCore();
  869. var body = $("#body");
  870. if (body.data("page") == "user") {
  871. initUserSetting();
  872. }
  873. if ($('.repo-nav').length) {
  874. initRepository();
  875. }
  876. if ($('#install-card').length) {
  877. initInstall();
  878. }
  879. if ($('#issue').length) {
  880. initIssue();
  881. }
  882. if ($('#release').length) {
  883. initRelease();
  884. }
  885. if ($('#repo-setting-container').length) {
  886. initRepoSetting();
  887. }
  888. if ($('#repo-create').length) {
  889. initRepoCreating();
  890. }
  891. if ($('#body-nav').hasClass("org-nav")) {
  892. initOrganization();
  893. }
  894. });
  895. })(jQuery);
  896. String.prototype.endsWith = function (suffix) {
  897. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  898. };