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

1094 lines
36 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var Gogits = {};
  2. (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 ((/\.(png|jpg|jpeg|gif)$/i).test($this.text()) == 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 $attachedList = $("#attached-list");
  530. var $addButton = $("#attachments-button");
  531. var files = [];
  532. var fileInput = document.getElementById("attachments-input");
  533. if (fileInput === null) {
  534. return;
  535. }
  536. $attachedList.on("click", "span.attachment-remove", function(event) {
  537. var $parent = $(this).parent();
  538. files.splice($parent.data("index"), 1);
  539. $parent.remove();
  540. });
  541. var clickedButton = undefined;
  542. $("button,input[type=\"submit\"]", fileInput.form).on("click", function() {
  543. clickedButton = this;
  544. var $button = $(this);
  545. $button.removeClass("btn-success");
  546. $button.addClass("btn-warning");
  547. $button.text("Submiting...");
  548. });
  549. fileInput.form.addEventListener("submit", function(event) {
  550. event.stopImmediatePropagation();
  551. event.preventDefault();
  552. //var data = new FormData(this);
  553. // Internet Explorer ... -_-
  554. var data = new FormData();
  555. $.each($("[name]", this), function(i, e) {
  556. if (e.name == "attachments" || e.type == "submit") {
  557. return;
  558. }
  559. data.append(e.name, $(e).val());
  560. });
  561. data.append(clickedButton.name, $(clickedButton).val());
  562. files.forEach(function(file) {
  563. data.append("attachments", file);
  564. });
  565. var xhr = new XMLHttpRequest();
  566. xhr.addEventListener("error", function() {
  567. debugger;
  568. });
  569. xhr.addEventListener("load", function() {
  570. var response = xhr.response;
  571. if (typeof response == "string") {
  572. try {
  573. response = JSON.parse(response);
  574. } catch (err) {
  575. response = { ok: false, error: "Could not parse JSON" };
  576. }
  577. }
  578. if (response.ok === false) {
  579. $("#submit-error").text(response.error);
  580. $("#submit-error").show();
  581. var $button = $(clickedButton);
  582. $button.removeClass("btn-warning");
  583. $button.addClass("btn-danger");
  584. $button.text("An error encoured!")
  585. return;
  586. }
  587. window.location.href = response.data;
  588. });
  589. xhr.open("POST", this.action, true);
  590. xhr.send(data);
  591. return false;
  592. });
  593. fileInput.addEventListener("change", function(event) {
  594. for (var index = 0; index < fileInput.files.length; index++) {
  595. var file = fileInput.files[index];
  596. if (files.indexOf(file) > -1) {
  597. continue;
  598. }
  599. var $span = $("<span></span>");
  600. $span.addClass("label");
  601. $span.addClass("label-default");
  602. $span.data("index", files.length);
  603. $span.append(file.name);
  604. $span.append(" <span class=\"attachment-remove fa fa-times-circle\"></span>");
  605. $attachedList.append($span);
  606. files.push(file);
  607. }
  608. this.value = "";
  609. });
  610. $addButton.on("click", function() {
  611. fileInput.click();
  612. return false;
  613. });
  614. }());
  615. // issue edit mode
  616. (function () {
  617. $("#issue-edit-btn").on("click", function () {
  618. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleHide();
  619. $('#issue-edit-title,.issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleShow();
  620. });
  621. $('.issue-edit-cancel').on("click", function () {
  622. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleShow();
  623. $('#issue-edit-title,.issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleHide();
  624. })
  625. }());
  626. // issue ajax update
  627. (function () {
  628. var $cnt = $('#issue-edit-content');
  629. $('.issue-edit-save').on("click", function () {
  630. $cnt.attr('data-ajax-rel', 'issue-edit-save');
  631. $(this).toggleAjax(function (json) {
  632. if (json.ok) {
  633. $('.issue-head h1.title').text(json.title);
  634. $('.issue-main > .issue-content .content').html(json.content);
  635. $('.issue-edit-cancel').trigger("click");
  636. }
  637. });
  638. setTimeout(function () {
  639. $cnt.attr('data-ajax-rel', 'issue-edit-preview');
  640. }, 200)
  641. });
  642. }());
  643. // issue ajax preview
  644. (function () {
  645. $('[data-ajax-name=issue-preview],[data-ajax-name=issue-edit-preview]').on("click", function () {
  646. var $this = $(this);
  647. $this.toggleAjax(function (resp) {
  648. $($this.data("preview")).html(resp);
  649. }, function () {
  650. $($this.data("preview")).html("no content");
  651. })
  652. });
  653. $('.issue-write a[data-toggle]').on("click", function () {
  654. var selector = $(this).parent().next(".issue-preview").find('a').data('preview');
  655. $(selector).html("loading...");
  656. });
  657. }());
  658. // assignee
  659. var is_issue_bar = $('.issue-bar').length > 0;
  660. var $a = $('.assignee');
  661. if ($a.data("assigned") > 0) {
  662. $('.clear-assignee').toggleShow();
  663. }
  664. $('.assignee', '#issue').on('click', 'li', function () {
  665. var uid = $(this).data("uid");
  666. if (is_issue_bar) {
  667. var assignee = $a.data("assigned");
  668. if (uid != assignee) {
  669. var text = $(this).text();
  670. var img = $("img", this).attr("src");
  671. $.post($a.data("ajax"), {
  672. issue: $('#issue').data("id"),
  673. assigneeid: uid
  674. }, function (json) {
  675. if (json.ok) {
  676. //window.location.reload();
  677. $a.data("assigned", uid);
  678. if (uid > 0) {
  679. $('.clear-assignee').toggleShow();
  680. $(".assignee > p").html('<img src="' + img + '"><strong>' + text + '</strong>');
  681. } else {
  682. $('.clear-assignee').toggleHide();
  683. $(".assignee > p").text("No one assigned");
  684. }
  685. }
  686. })
  687. }
  688. return;
  689. }
  690. $('#assignee').val(uid);
  691. if (uid > 0) {
  692. $('.clear-assignee').toggleShow();
  693. $('#assigned').text($(this).find("strong").text())
  694. } else {
  695. $('.clear-assignee').toggleHide();
  696. $('#assigned').text($('#assigned').data("no-assigned"));
  697. }
  698. });
  699. // milestone
  700. $('#issue .dropdown-menu a[data-toggle="tab"]').on("click", function (e) {
  701. e.stopPropagation();
  702. $(this).tab('show');
  703. return false;
  704. });
  705. var $m = $('.milestone');
  706. if ($m.data("milestone") > 0) {
  707. $('.clear-milestone').toggleShow();
  708. }
  709. $('.milestone', '#issue').on('click', 'li.milestone-item', function () {
  710. var id = $(this).data("id");
  711. if (is_issue_bar) {
  712. var m = $m.data("milestone");
  713. if (id != m) {
  714. var text = $(this).text();
  715. $.post($m.data("ajax"), {
  716. issue: $('#issue').data("id"),
  717. milestone: id
  718. }, function (json) {
  719. if (json.ok) {
  720. //window.location.reload();
  721. $m.data("milestone", id);
  722. if (id > 0) {
  723. $('.clear-milestone').toggleShow();
  724. $(".milestone > .name").html('<a href="' + location.pathname + '?milestone=' + id + '"><strong>' + text + '</strong></a>');
  725. } else {
  726. $('.clear-milestone').toggleHide();
  727. $(".milestone > .name").text("No milestone");
  728. }
  729. }
  730. });
  731. }
  732. return;
  733. }
  734. $('#milestone-id').val(id);
  735. if (id > 0) {
  736. $('.clear-milestone').toggleShow();
  737. $('#milestone').text($(this).find("strong").text())
  738. } else {
  739. $('.clear-milestone').toggleHide();
  740. $('#milestone').text($('#milestone').data("no-milestone"));
  741. }
  742. });
  743. // labels
  744. var removeLabels = [];
  745. $('#label-manage-btn').on("click", function () {
  746. var $list = $('#label-list');
  747. if ($list.hasClass("managing")) {
  748. var ids = [];
  749. $list.find('li').each(function (i, item) {
  750. var id = $(item).data("id");
  751. if (id > 0) {
  752. ids.push(id);
  753. }
  754. });
  755. $.post($list.data("ajax"), {"ids": ids.join(","), "remove": removeLabels.join(",")}, function (json) {
  756. if (json.ok) {
  757. window.location.reload();
  758. }
  759. })
  760. } else {
  761. $list.addClass("managing");
  762. $list.find(".count").hide();
  763. $list.find(".del").show();
  764. $(this).text("Save Labels");
  765. $list.on('click', 'li.label-item', function () {
  766. var $this = $(this);
  767. $this.after($('.label-change-li').detach().show());
  768. $('#label-name-change-ipt').val($this.find('.name').text());
  769. var color = $this.find('.color').data("color");
  770. $('.label-change-color-picker').colorpicker("setValue", color);
  771. $('#label-color-change-ipt,#label-color-change-ipt2').val(color);
  772. $('#label-change-id-ipt').val($this.data("id"));
  773. return false;
  774. });
  775. }
  776. });
  777. var colorRegex = new RegExp("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
  778. $('#label-color-ipt2').on('keyup', function () {
  779. var val = $(this).val();
  780. if (val.length > 7) {
  781. $(this).val(val.substr(0, 7));
  782. }
  783. if (colorRegex.test(val)) {
  784. $('.label-color-picker').colorpicker("setValue", val);
  785. }
  786. return true;
  787. });
  788. $('#label-color-change-ipt2').on('keyup', function () {
  789. var val = $(this).val();
  790. console.log(val);
  791. if (val.length > 7) {
  792. $(this).val(val.substr(0, 7));
  793. }
  794. if (colorRegex.test(val)) {
  795. $('.label-change-color-picker').colorpicker("setValue", val);
  796. }
  797. return true;
  798. });
  799. $("#label-list").on('click', '.del', function () {
  800. var $p = $(this).parent();
  801. removeLabels.push($p.data('id'));
  802. $p.remove();
  803. return false;
  804. });
  805. $('.label-selected').each(function (i, item) {
  806. var $item = $(item);
  807. var color = $item.find('.color').data('color');
  808. $item.css('background-color', color);
  809. });
  810. $('.issue-bar .labels .dropdown-menu').on('click', 'li', function (e) {
  811. var $labels = $('.issue-bar .labels');
  812. var url = $labels.data("ajax");
  813. var id = $(this).data('id');
  814. var check = $(this).hasClass("checked");
  815. var item = this;
  816. $.post(url, {id: id, action: check ? 'detach' : "attach", issue: $('#issue').data('id')}, function (json) {
  817. if (json.ok) {
  818. if (check) {
  819. $("span.check.pull-left", item).remove();
  820. $(item).removeClass("checked");
  821. $(item).addClass("no-checked");
  822. $("#label-" + id, $labels).remove();
  823. } else {
  824. $(item).prepend('<span class="check pull-left"><i class="fa fa-check"></i></span>');
  825. $(item).removeClass("no-checked");
  826. $(item).addClass("checked");
  827. var $l = $("<p></p>");
  828. var c = $("span.color", item).css("background-color");
  829. $l.attr("id", "label-" + id);
  830. $l.attr("class", "label-item label-white");
  831. $l.css("background-color", c);
  832. $l.append("<strong>" + $(item).text() + "</strong>");
  833. $labels.append($l);
  834. }
  835. }
  836. });
  837. e.stopPropagation();
  838. return false;
  839. })
  840. }
  841. function initRelease() {
  842. // release new ajax preview
  843. (function () {
  844. $('[data-ajax-name=release-preview]').on("click", function () {
  845. var $this = $(this);
  846. $this.toggleAjax(function (resp) {
  847. $($this.data("preview")).html(resp);
  848. }, function () {
  849. $($this.data("preview")).html("no content");
  850. })
  851. });
  852. $('.release-write a[data-toggle]').on("click", function () {
  853. $('.release-preview-content').html("loading...");
  854. });
  855. }());
  856. // release new target selection
  857. (function () {
  858. $('#release-new-target-branch-list').on('click', 'a', function () {
  859. $('#tag-target').val($(this).text());
  860. $('#release-new-target-name').text(" " + $(this).text());
  861. });
  862. }());
  863. }
  864. function initRepoSetting() {
  865. // repo member add
  866. $('#repo-collaborator').on('keyup', function () {
  867. var $this = $(this);
  868. if (!$this.val()) {
  869. $this.next().toggleHide();
  870. return;
  871. }
  872. Gogits.getUsers($this.val(), $this.next());
  873. /*$.ajax({
  874. url: '/api/v1/users/search?q=' + $this.val(),
  875. dataType: "json",
  876. success: function (json) {
  877. if (json.ok && json.data.length) {
  878. var html = '';
  879. $.each(json.data, function (i, item) {
  880. html += '<li><img src="' + item.avatar + '">' + item.username + '</li>';
  881. });
  882. $this.next().toggleShow();
  883. $this.next().find('ul').html(html);
  884. } else {
  885. $this.next().toggleHide();
  886. }
  887. }
  888. });*/
  889. }).on('focus', function () {
  890. if (!$(this).val()) {
  891. $(this).next().toggleHide();
  892. }
  893. }).next().on("click", 'li', function () {
  894. $('#repo-collaborator').val($(this).text());
  895. });
  896. }
  897. function initRepoCreating() {
  898. // owner switch menu click
  899. (function () {
  900. $('#repo-owner-switch .dropdown-menu').on("click", "li", function () {
  901. var uid = $(this).data('uid');
  902. // set to input
  903. $('#repo-owner-id').val(uid);
  904. // set checked class
  905. if (!$(this).hasClass("checked")) {
  906. $(this).parent().find(".checked").removeClass("checked");
  907. $(this).addClass("checked");
  908. }
  909. // set button group to show clicked owner
  910. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  911. $('#repo-owner-name').text($(this).text().trim());
  912. console.log("set repo owner to uid :", uid, $(this).text().trim());
  913. });
  914. }());
  915. console.log("init repo-creating scripts");
  916. }
  917. function initOrganization() {
  918. (function(){
  919. $('#org-team-add-user').on('keyup', function () {
  920. var $this = $(this);
  921. if (!$this.val()) {
  922. $this.next().toggleHide();
  923. return;
  924. }
  925. Gogits.getUsers($this.val(), $this.next());
  926. }).on('focus', function () {
  927. if (!$(this).val()) {
  928. $(this).next().toggleHide();
  929. }
  930. }).next().on("click", 'li', function () {
  931. $('#org-team-add-user').val($(this).text());
  932. $('#org-team-add-user-form').submit();
  933. }).toggleHide();
  934. console.log("init script : add user to team");
  935. }());
  936. (function(){
  937. $('#org-team-add-repo').next().toggleHide();
  938. console.log("init script : add repository to team");
  939. }());
  940. console.log("init script : organization done");
  941. }
  942. (function ($) {
  943. $(function () {
  944. initCore();
  945. var body = $("#body");
  946. if (body.data("page") == "user") {
  947. initUserSetting();
  948. }
  949. if ($('.repo-nav').length) {
  950. initRepository();
  951. }
  952. if ($('#install-card').length) {
  953. initInstall();
  954. }
  955. if ($('#issue').length) {
  956. initIssue();
  957. }
  958. if ($('#release').length) {
  959. initRelease();
  960. }
  961. if ($('#repo-setting-container').length) {
  962. initRepoSetting();
  963. }
  964. if ($('#repo-create').length) {
  965. initRepoCreating();
  966. }
  967. if ($('#body-nav').hasClass("org-nav")) {
  968. initOrganization();
  969. }
  970. });
  971. })(jQuery);
  972. String.prototype.endsWith = function (suffix) {
  973. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  974. };