app.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. // issue edit mode
  488. (function () {
  489. $("#issue-edit-btn").on("click", function () {
  490. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleHide();
  491. $('#issue-edit-title,.issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleShow();
  492. });
  493. $('.issue-edit-cancel').on("click", function () {
  494. $('#issue h1.title,#issue .issue-main > .issue-content .content,#issue-edit-btn').toggleShow();
  495. $('#issue-edit-title,.issue-edit-content,.issue-edit-cancel,.issue-edit-save').toggleHide();
  496. })
  497. }());
  498. // issue ajax update
  499. (function () {
  500. var $cnt = $('#issue-edit-content');
  501. $('.issue-edit-save').on("click", function () {
  502. $cnt.attr('data-ajax-rel', 'issue-edit-save');
  503. $(this).toggleAjax(function (json) {
  504. if (json.ok) {
  505. $('.issue-head h1.title').text(json.title);
  506. $('.issue-main > .issue-content .content').html(json.content);
  507. $('.issue-edit-cancel').trigger("click");
  508. }
  509. });
  510. setTimeout(function () {
  511. $cnt.attr('data-ajax-rel', 'issue-edit-preview');
  512. }, 200)
  513. });
  514. }());
  515. // issue ajax preview
  516. (function () {
  517. $('[data-ajax-name=issue-preview],[data-ajax-name=issue-edit-preview]').on("click", function () {
  518. var $this = $(this);
  519. $this.toggleAjax(function (resp) {
  520. $($this.data("preview")).html(resp);
  521. }, function () {
  522. $($this.data("preview")).html("no content");
  523. })
  524. });
  525. $('.issue-write a[data-toggle]').on("click", function () {
  526. var selector = $(this).parent().next(".issue-preview").find('a').data('preview');
  527. $(selector).html("loading...");
  528. });
  529. }());
  530. // assignee
  531. var is_issue_bar = $('.issue-bar').length > 0;
  532. var $a = $('.assignee');
  533. if ($a.data("assigned") > 0) {
  534. $('.clear-assignee').toggleShow();
  535. }
  536. $('.assignee', '#issue').on('click', 'li', function () {
  537. var uid = $(this).data("uid");
  538. if (is_issue_bar) {
  539. var assignee = $a.data("assigned");
  540. if (uid != assignee) {
  541. var text = $(this).text();
  542. var img = $("img", this).attr("src");
  543. $.post($a.data("ajax"), {
  544. issue: $('#issue').data("id"),
  545. assigneeid: uid
  546. }, function (json) {
  547. if (json.ok) {
  548. //window.location.reload();
  549. $a.data("assigned", uid);
  550. if (uid > 0) {
  551. $('.clear-assignee').toggleShow();
  552. $(".assignee > p").html('<img src="' + img + '"><strong>' + text + '</strong>');
  553. } else {
  554. $('.clear-assignee').toggleHide();
  555. $(".assignee > p").text("No one assigned");
  556. }
  557. }
  558. })
  559. }
  560. return;
  561. }
  562. $('#assignee').val(uid);
  563. if (uid > 0) {
  564. $('.clear-assignee').toggleShow();
  565. $('#assigned').text($(this).find("strong").text())
  566. } else {
  567. $('.clear-assignee').toggleHide();
  568. $('#assigned').text($('#assigned').data("no-assigned"));
  569. }
  570. });
  571. // milestone
  572. $('#issue .dropdown-menu a[data-toggle="tab"]').on("click", function (e) {
  573. e.stopPropagation();
  574. $(this).tab('show');
  575. return false;
  576. });
  577. var $m = $('.milestone');
  578. if ($m.data("milestone") > 0) {
  579. $('.clear-milestone').toggleShow();
  580. }
  581. $('.milestone', '#issue').on('click', 'li.milestone-item', function () {
  582. var id = $(this).data("id");
  583. if (is_issue_bar) {
  584. var m = $m.data("milestone");
  585. if (id != m) {
  586. var text = $(this).text();
  587. $.post($m.data("ajax"), {
  588. issue: $('#issue').data("id"),
  589. milestone: id
  590. }, function (json) {
  591. if (json.ok) {
  592. //window.location.reload();
  593. $m.data("milestone", id);
  594. if (id > 0) {
  595. $('.clear-milestone').toggleShow();
  596. $(".milestone > .name").html('<a href="' + location.pathname + '?milestone=' + id + '"><strong>' + text + '</strong></a>');
  597. } else {
  598. $('.clear-milestone').toggleHide();
  599. $(".milestone > .name").text("No milestone");
  600. }
  601. }
  602. });
  603. }
  604. return;
  605. }
  606. $('#milestone-id').val(id);
  607. if (id > 0) {
  608. $('.clear-milestone').toggleShow();
  609. $('#milestone').text($(this).find("strong").text())
  610. } else {
  611. $('.clear-milestone').toggleHide();
  612. $('#milestone').text($('#milestone').data("no-milestone"));
  613. }
  614. });
  615. // labels
  616. var removeLabels = [];
  617. $('#label-manage-btn').on("click", function () {
  618. var $list = $('#label-list');
  619. if ($list.hasClass("managing")) {
  620. var ids = [];
  621. $list.find('li').each(function (i, item) {
  622. var id = $(item).data("id");
  623. if (id > 0) {
  624. ids.push(id);
  625. }
  626. });
  627. $.post($list.data("ajax"), {"ids": ids.join(","), "remove": removeLabels.join(",")}, function (json) {
  628. if (json.ok) {
  629. window.location.reload();
  630. }
  631. })
  632. } else {
  633. $list.addClass("managing");
  634. $list.find(".count").hide();
  635. $list.find(".del").show();
  636. $(this).text("Save Labels");
  637. $list.on('click', 'li.label-item', function () {
  638. var $this = $(this);
  639. $this.after($('.label-change-li').detach().show());
  640. $('#label-name-change-ipt').val($this.find('.name').text());
  641. var color = $this.find('.color').data("color");
  642. $('.label-change-color-picker').colorpicker("setValue", color);
  643. $('#label-color-change-ipt,#label-color-change-ipt2').val(color);
  644. $('#label-change-id-ipt').val($this.data("id"));
  645. return false;
  646. });
  647. }
  648. });
  649. var colorRegex = new RegExp("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
  650. $('#label-color-ipt2').on('keyup', function () {
  651. var val = $(this).val();
  652. if (val.length > 7) {
  653. $(this).val(val.substr(0, 7));
  654. }
  655. if (colorRegex.test(val)) {
  656. $('.label-color-picker').colorpicker("setValue", val);
  657. }
  658. return true;
  659. });
  660. $('#label-color-change-ipt2').on('keyup', function () {
  661. var val = $(this).val();
  662. console.log(val);
  663. if (val.length > 7) {
  664. $(this).val(val.substr(0, 7));
  665. }
  666. if (colorRegex.test(val)) {
  667. $('.label-change-color-picker').colorpicker("setValue", val);
  668. }
  669. return true;
  670. });
  671. $("#label-list").on('click', '.del', function () {
  672. var $p = $(this).parent();
  673. removeLabels.push($p.data('id'));
  674. $p.remove();
  675. return false;
  676. });
  677. $('.label-selected').each(function (i, item) {
  678. var $item = $(item);
  679. var color = $item.find('.color').data('color');
  680. $item.css('background-color', color);
  681. });
  682. $('.issue-bar .labels .dropdown-menu').on('click', 'li', function (e) {
  683. var $labels = $('.issue-bar .labels');
  684. var url = $labels.data("ajax");
  685. var id = $(this).data('id');
  686. var check = $(this).hasClass("checked");
  687. var item = this;
  688. $.post(url, {id: id, action: check ? 'detach' : "attach", issue: $('#issue').data('id')}, function (json) {
  689. if (json.ok) {
  690. if (check) {
  691. $("span.check.pull-left", item).remove();
  692. $(item).removeClass("checked");
  693. $(item).addClass("no-checked");
  694. $("#label-" + id, $labels).remove();
  695. } else {
  696. $(item).prepend('<span class="check pull-left"><i class="fa fa-check"></i></span>');
  697. $(item).removeClass("no-checked");
  698. $(item).addClass("checked");
  699. var $l = $("<p></p>");
  700. var c = $("span.color", item).css("background-color");
  701. $l.attr("id", "label-" + id);
  702. $l.attr("class", "label-item label-white");
  703. $l.css("background-color", c);
  704. $l.append("<strong>" + $(item).text() + "</strong>");
  705. $labels.append($l);
  706. }
  707. }
  708. });
  709. e.stopPropagation();
  710. return false;
  711. })
  712. }
  713. function initRelease() {
  714. // release new ajax preview
  715. (function () {
  716. $('[data-ajax-name=release-preview]').on("click", function () {
  717. var $this = $(this);
  718. $this.toggleAjax(function (resp) {
  719. $($this.data("preview")).html(resp);
  720. }, function () {
  721. $($this.data("preview")).html("no content");
  722. })
  723. });
  724. $('.release-write a[data-toggle]').on("click", function () {
  725. $('.release-preview-content').html("loading...");
  726. });
  727. }());
  728. // release new target selection
  729. (function () {
  730. $('#release-new-target-branch-list').on('click', 'a', function () {
  731. $('#tag-target').val($(this).text());
  732. $('#release-new-target-name').text(" " + $(this).text());
  733. });
  734. }());
  735. }
  736. function initRepoSetting() {
  737. // repo member add
  738. $('#repo-collaborator').on('keyup', function () {
  739. var $this = $(this);
  740. if (!$this.val()) {
  741. $this.next().toggleHide();
  742. return;
  743. }
  744. Gogits.getUsers($this.val(), $this.next());
  745. /*$.ajax({
  746. url: '/api/v1/users/search?q=' + $this.val(),
  747. dataType: "json",
  748. success: function (json) {
  749. if (json.ok && json.data.length) {
  750. var html = '';
  751. $.each(json.data, function (i, item) {
  752. html += '<li><img src="' + item.avatar + '">' + item.username + '</li>';
  753. });
  754. $this.next().toggleShow();
  755. $this.next().find('ul').html(html);
  756. } else {
  757. $this.next().toggleHide();
  758. }
  759. }
  760. });*/
  761. }).on('focus', function () {
  762. if (!$(this).val()) {
  763. $(this).next().toggleHide();
  764. }
  765. }).next().on("click", 'li', function () {
  766. $('#repo-collaborator').val($(this).text());
  767. });
  768. }
  769. function initRepoCreating() {
  770. // owner switch menu click
  771. (function () {
  772. $('#repo-owner-switch .dropdown-menu').on("click", "li", function () {
  773. var uid = $(this).data('uid');
  774. // set to input
  775. $('#repo-owner-id').val(uid);
  776. // set checked class
  777. if (!$(this).hasClass("checked")) {
  778. $(this).parent().find(".checked").removeClass("checked");
  779. $(this).addClass("checked");
  780. }
  781. // set button group to show clicked owner
  782. $('#repo-owner-avatar').attr("src", $(this).find('img').attr("src"));
  783. $('#repo-owner-name').text($(this).text().trim());
  784. console.log("set repo owner to uid :", uid, $(this).text().trim());
  785. });
  786. }());
  787. console.log("init repo-creating scripts");
  788. }
  789. function initOrganization() {
  790. (function(){
  791. $('#org-team-add-user').on('keyup', function () {
  792. var $this = $(this);
  793. if (!$this.val()) {
  794. $this.next().toggleHide();
  795. return;
  796. }
  797. Gogits.getUsers($this.val(), $this.next());
  798. }).on('focus', function () {
  799. if (!$(this).val()) {
  800. $(this).next().toggleHide();
  801. }
  802. }).next().on("click", 'li', function () {
  803. $('#org-team-add-user').val($(this).text());
  804. $('#org-team-add-user-form').submit();
  805. }).toggleHide();
  806. console.log("init script : add user to team");
  807. }());
  808. (function(){
  809. $('#org-team-add-repo').next().toggleHide();
  810. console.log("init script : add repository to team");
  811. }());
  812. console.log("init script : organization done");
  813. }
  814. (function ($) {
  815. $(function () {
  816. initCore();
  817. var body = $("#body");
  818. if (body.data("page") == "user") {
  819. initUserSetting();
  820. }
  821. if ($('.repo-nav').length) {
  822. initRepository();
  823. }
  824. if ($('#install-card').length) {
  825. initInstall();
  826. }
  827. if ($('#issue').length) {
  828. initIssue();
  829. }
  830. if ($('#release').length) {
  831. initRelease();
  832. }
  833. if ($('#repo-setting-container').length) {
  834. initRepoSetting();
  835. }
  836. if ($('#repo-create').length) {
  837. initRepoCreating();
  838. }
  839. if ($('#body-nav').hasClass("org-nav")) {
  840. initOrganization();
  841. }
  842. });
  843. })(jQuery);
  844. String.prototype.endsWith = function (suffix) {
  845. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  846. };