app.js 35 KB

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