gogs.js 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798
  1. "use strict";
  2. var csrf;
  3. var suburl;
  4. function initCommentPreviewTab($form) {
  5. var $tabMenu = $form.find(".tabular.menu");
  6. $tabMenu.find(".item").tab();
  7. $tabMenu
  8. .find('.item[data-tab="' + $tabMenu.data("preview") + '"]')
  9. .click(function() {
  10. var $this = $(this);
  11. $.post(
  12. $this.data("url"),
  13. {
  14. _csrf: csrf,
  15. mode: "gfm",
  16. context: $this.data("context"),
  17. text: $form
  18. .find(
  19. '.tab.segment[data-tab="' + $tabMenu.data("write") + '"] textarea'
  20. )
  21. .val()
  22. },
  23. function(data) {
  24. var $previewPanel = $form.find(
  25. '.tab.segment[data-tab="' + $tabMenu.data("preview") + '"]'
  26. );
  27. $previewPanel.html(data);
  28. emojify.run($previewPanel[0]);
  29. $("pre code", $previewPanel[0]).each(function(i, block) {
  30. hljs.highlightBlock(block);
  31. });
  32. }
  33. );
  34. });
  35. buttonsClickOnEnter();
  36. }
  37. var previewFileModes;
  38. function initEditPreviewTab($form) {
  39. var $tabMenu = $form.find(".tabular.menu");
  40. $tabMenu.find(".item").tab();
  41. var $previewTab = $tabMenu.find(
  42. '.item[data-tab="' + $tabMenu.data("preview") + '"]'
  43. );
  44. if ($previewTab.length) {
  45. previewFileModes = $previewTab.data("preview-file-modes").split(",");
  46. $previewTab.click(function() {
  47. var $this = $(this);
  48. $.post(
  49. $this.data("url"),
  50. {
  51. _csrf: csrf,
  52. context: $this.data("context"),
  53. text: $form
  54. .find(
  55. '.tab.segment[data-tab="' + $tabMenu.data("write") + '"] textarea'
  56. )
  57. .val()
  58. },
  59. function(data) {
  60. var $previewPanel = $form.find(
  61. '.tab.segment[data-tab="' + $tabMenu.data("preview") + '"]'
  62. );
  63. $previewPanel.html(data);
  64. emojify.run($previewPanel[0]);
  65. $("pre code", $previewPanel[0]).each(function(i, block) {
  66. hljs.highlightBlock(block);
  67. });
  68. }
  69. );
  70. });
  71. }
  72. }
  73. function initEditDiffTab($form) {
  74. var $tabMenu = $form.find(".tabular.menu");
  75. $tabMenu.find(".item").tab();
  76. $tabMenu
  77. .find('.item[data-tab="' + $tabMenu.data("diff") + '"]')
  78. .click(function() {
  79. var $this = $(this);
  80. $.post(
  81. $this.data("url"),
  82. {
  83. _csrf: csrf,
  84. content: $form
  85. .find(
  86. '.tab.segment[data-tab="' + $tabMenu.data("write") + '"] textarea'
  87. )
  88. .val()
  89. },
  90. function(data) {
  91. var $diffPreviewPanel = $form.find(
  92. '.tab.segment[data-tab="' + $tabMenu.data("diff") + '"]'
  93. );
  94. $diffPreviewPanel.html(data);
  95. emojify.run($diffPreviewPanel[0]);
  96. }
  97. );
  98. });
  99. }
  100. function initEditForm() {
  101. if ($(".edit.form").length == 0) {
  102. return;
  103. }
  104. initEditPreviewTab($(".edit.form"));
  105. initEditDiffTab($(".edit.form"));
  106. }
  107. function initCommentForm() {
  108. if ($(".comment.form").length == 0) {
  109. return;
  110. }
  111. initCommentPreviewTab($(".comment.form"));
  112. // Labels
  113. var $list = $(".ui.labels.list");
  114. var $noSelect = $list.find(".no-select");
  115. var $labelMenu = $(".select-label .menu");
  116. var hasLabelUpdateAction = $labelMenu.data("action") == "update";
  117. function updateIssueMeta(url, action, id) {
  118. $.post(url, {
  119. _csrf: csrf,
  120. action: action,
  121. id: id
  122. });
  123. }
  124. // Add   to each unselected label to keep UI looks good.
  125. // This should be added directly to HTML but somehow just get empty <span> on this page.
  126. $labelMenu
  127. .find(".item:not(.no-select) .octicon:not(.octicon-check)")
  128. .each(function() {
  129. $(this).html("&nbsp;");
  130. });
  131. $labelMenu.find(".item:not(.no-select)").click(function() {
  132. if ($(this).hasClass("checked")) {
  133. $(this).removeClass("checked");
  134. $(this)
  135. .find(".octicon")
  136. .removeClass("octicon-check")
  137. .html("&nbsp;");
  138. if (hasLabelUpdateAction) {
  139. updateIssueMeta(
  140. $labelMenu.data("update-url"),
  141. "detach",
  142. $(this).data("id")
  143. );
  144. }
  145. } else {
  146. $(this).addClass("checked");
  147. $(this)
  148. .find(".octicon")
  149. .addClass("octicon-check")
  150. .html("");
  151. if (hasLabelUpdateAction) {
  152. updateIssueMeta(
  153. $labelMenu.data("update-url"),
  154. "attach",
  155. $(this).data("id")
  156. );
  157. }
  158. }
  159. var labelIds = "";
  160. $(this)
  161. .parent()
  162. .find(".item")
  163. .each(function() {
  164. if ($(this).hasClass("checked")) {
  165. labelIds += $(this).data("id") + ",";
  166. $($(this).data("id-selector")).removeClass("hide");
  167. } else {
  168. $($(this).data("id-selector")).addClass("hide");
  169. }
  170. });
  171. if (labelIds.length == 0) {
  172. $noSelect.removeClass("hide");
  173. } else {
  174. $noSelect.addClass("hide");
  175. }
  176. $(
  177. $(this)
  178. .parent()
  179. .data("id")
  180. ).val(labelIds);
  181. return false;
  182. });
  183. $labelMenu.find(".no-select.item").click(function() {
  184. if (hasLabelUpdateAction) {
  185. updateIssueMeta($labelMenu.data("update-url"), "clear", "");
  186. }
  187. $(this)
  188. .parent()
  189. .find(".item")
  190. .each(function() {
  191. $(this).removeClass("checked");
  192. $(this)
  193. .find(".octicon")
  194. .removeClass("octicon-check")
  195. .html("&nbsp;");
  196. });
  197. $list.find(".item").each(function() {
  198. $(this).addClass("hide");
  199. });
  200. $noSelect.removeClass("hide");
  201. $(
  202. $(this)
  203. .parent()
  204. .data("id")
  205. ).val("");
  206. });
  207. function selectItem(select_id, input_id) {
  208. var $menu = $(select_id + " .menu");
  209. var $list = $(".ui" + select_id + ".list");
  210. var hasUpdateAction = $menu.data("action") == "update";
  211. $menu.find(".item:not(.no-select)").click(function() {
  212. $(this)
  213. .parent()
  214. .find(".item")
  215. .each(function() {
  216. $(this).removeClass("selected active");
  217. });
  218. $(this).addClass("selected active");
  219. if (hasUpdateAction) {
  220. updateIssueMeta($menu.data("update-url"), "", $(this).data("id"));
  221. }
  222. switch (input_id) {
  223. case "#milestone_id":
  224. $list
  225. .find(".selected")
  226. .html(
  227. '<a class="item" href=' +
  228. $(this).data("href") +
  229. ">" +
  230. $(this).text() +
  231. "</a>"
  232. );
  233. break;
  234. case "#assignee_id":
  235. $list
  236. .find(".selected")
  237. .html(
  238. '<a class="item" href=' +
  239. $(this).data("href") +
  240. ">" +
  241. '<img class="ui avatar image" src=' +
  242. $(this).data("avatar") +
  243. ">" +
  244. $(this).text() +
  245. "</a>"
  246. );
  247. }
  248. $(".ui" + select_id + ".list .no-select").addClass("hide");
  249. $(input_id).val($(this).data("id"));
  250. });
  251. $menu.find(".no-select.item").click(function() {
  252. $(this)
  253. .parent()
  254. .find(".item:not(.no-select)")
  255. .each(function() {
  256. $(this).removeClass("selected active");
  257. });
  258. if (hasUpdateAction) {
  259. updateIssueMeta($menu.data("update-url"), "", "");
  260. }
  261. $list.find(".selected").html("");
  262. $list.find(".no-select").removeClass("hide");
  263. $(input_id).val("");
  264. });
  265. }
  266. // Milestone and assignee
  267. selectItem(".select-milestone", "#milestone_id");
  268. selectItem(".select-assignee", "#assignee_id");
  269. }
  270. function initRepository() {
  271. if ($(".repository").length == 0) {
  272. return;
  273. }
  274. function initFilterSearchDropdown(selector) {
  275. var $dropdown = $(selector);
  276. $dropdown.dropdown({
  277. fullTextSearch: true,
  278. onChange: function(text, value, $choice) {
  279. window.location.href = $choice.data("url");
  280. console.log($choice.data("url"));
  281. },
  282. message: { noResults: $dropdown.data("no-results") }
  283. });
  284. }
  285. // File list and commits
  286. if (
  287. $(".repository.file.list").length > 0 ||
  288. ".repository.commits".length > 0
  289. ) {
  290. initFilterSearchDropdown(".choose.reference .dropdown");
  291. $(".reference.column").click(function() {
  292. $(".choose.reference .scrolling.menu").css("display", "none");
  293. $(".choose.reference .text").removeClass("black");
  294. $($(this).data("target")).css("display", "block");
  295. $(this)
  296. .find(".text")
  297. .addClass("black");
  298. return false;
  299. });
  300. }
  301. // Wiki
  302. if ($(".repository.wiki.view").length > 0) {
  303. initFilterSearchDropdown(".choose.page .dropdown");
  304. }
  305. // Options
  306. if ($(".repository.settings.options").length > 0) {
  307. $("#repo_name").keyup(function() {
  308. var $prompt = $("#repo-name-change-prompt");
  309. if (
  310. $(this)
  311. .val()
  312. .toString()
  313. .toLowerCase() !=
  314. $(this)
  315. .data("repo-name")
  316. .toString()
  317. .toLowerCase()
  318. ) {
  319. $prompt.show();
  320. } else {
  321. $prompt.hide();
  322. }
  323. });
  324. }
  325. // Branches
  326. if ($(".repository.settings.branches").length > 0) {
  327. initFilterSearchDropdown(".protected-branches .dropdown");
  328. $(".enable-protection, .enable-whitelist").change(function() {
  329. if (this.checked) {
  330. $($(this).data("target")).removeClass("disabled");
  331. } else {
  332. $($(this).data("target")).addClass("disabled");
  333. }
  334. });
  335. }
  336. // Labels
  337. if ($(".repository.labels").length > 0) {
  338. // Create label
  339. var $newLabelPanel = $(".new-label.segment");
  340. $(".new-label.button").click(function() {
  341. $newLabelPanel.show();
  342. });
  343. $(".new-label.segment .cancel").click(function() {
  344. $newLabelPanel.hide();
  345. });
  346. $(".color-picker").each(function() {
  347. $(this).minicolors();
  348. });
  349. $(".precolors .color").click(function() {
  350. var color_hex = $(this).data("color-hex");
  351. $(".color-picker").val(color_hex);
  352. $(".minicolors-swatch-color").css("background-color", color_hex);
  353. });
  354. $(".edit-label-button").click(function() {
  355. $("#label-modal-id").val($(this).data("id"));
  356. $(".edit-label .new-label-input").val($(this).data("title"));
  357. $(".edit-label .color-picker").val($(this).data("color"));
  358. $(".minicolors-swatch-color").css(
  359. "background-color",
  360. $(this).data("color")
  361. );
  362. $(".edit-label.modal")
  363. .modal({
  364. onApprove: function() {
  365. $(".edit-label.form").submit();
  366. }
  367. })
  368. .modal("show");
  369. return false;
  370. });
  371. }
  372. // Milestones
  373. if ($(".repository.milestones").length > 0) {
  374. }
  375. if ($(".repository.new.milestone").length > 0) {
  376. var $datepicker = $(".milestone.datepicker");
  377. $datepicker.datetimepicker({
  378. lang: $datepicker.data("lang"),
  379. inline: true,
  380. timepicker: false,
  381. startDate: $datepicker.data("start-date"),
  382. formatDate: "Y-m-d",
  383. onSelectDate: function(ct) {
  384. $("#deadline").val(ct.dateFormat("Y-m-d"));
  385. }
  386. });
  387. $("#clear-date").click(function() {
  388. $("#deadline").val("");
  389. return false;
  390. });
  391. }
  392. // Issues
  393. if ($(".repository.view.issue").length > 0) {
  394. // Edit issue title
  395. var $issueTitle = $("#issue-title");
  396. var $editInput = $("#edit-title-input").find("input");
  397. var editTitleToggle = function() {
  398. $issueTitle.toggle();
  399. $(".not-in-edit").toggle();
  400. $("#edit-title-input").toggle();
  401. $(".in-edit").toggle();
  402. $editInput.focus();
  403. return false;
  404. };
  405. $("#edit-title").click(editTitleToggle);
  406. $("#cancel-edit-title").click(editTitleToggle);
  407. $("#save-edit-title")
  408. .click(editTitleToggle)
  409. .click(function() {
  410. if (
  411. $editInput.val().length == 0 ||
  412. $editInput.val() == $issueTitle.text()
  413. ) {
  414. $editInput.val($issueTitle.text());
  415. return false;
  416. }
  417. $.post(
  418. $(this).data("update-url"),
  419. {
  420. _csrf: csrf,
  421. title: $editInput.val()
  422. },
  423. function(data) {
  424. $editInput.val(data.title);
  425. $issueTitle.text(data.title);
  426. }
  427. );
  428. return false;
  429. });
  430. // Edit issue or comment content
  431. $(".edit-content").click(function() {
  432. var $segment = $(this)
  433. .parent()
  434. .parent()
  435. .parent()
  436. .next();
  437. var $editContentZone = $segment.find(".edit-content-zone");
  438. var $renderContent = $segment.find(".render-content");
  439. var $rawContent = $segment.find(".raw-content");
  440. var $textarea;
  441. // Setup new form
  442. if ($editContentZone.html().length == 0) {
  443. $editContentZone.html($("#edit-content-form").html());
  444. $textarea = $segment.find("textarea");
  445. // Give new write/preview data-tab name to distinguish from others
  446. var $editContentForm = $editContentZone.find(".ui.comment.form");
  447. var $tabMenu = $editContentForm.find(".tabular.menu");
  448. $tabMenu.attr("data-write", $editContentZone.data("write"));
  449. $tabMenu.attr("data-preview", $editContentZone.data("preview"));
  450. $tabMenu
  451. .find(".write.item")
  452. .attr("data-tab", $editContentZone.data("write"));
  453. $tabMenu
  454. .find(".preview.item")
  455. .attr("data-tab", $editContentZone.data("preview"));
  456. $editContentForm
  457. .find(".write.segment")
  458. .attr("data-tab", $editContentZone.data("write"));
  459. $editContentForm
  460. .find(".preview.segment")
  461. .attr("data-tab", $editContentZone.data("preview"));
  462. initCommentPreviewTab($editContentForm);
  463. $editContentZone.find(".cancel.button").click(function() {
  464. $renderContent.show();
  465. $editContentZone.hide();
  466. });
  467. $editContentZone.find(".save.button").click(function() {
  468. $renderContent.show();
  469. $editContentZone.hide();
  470. $.post(
  471. $editContentZone.data("update-url"),
  472. {
  473. _csrf: csrf,
  474. content: $textarea.val(),
  475. context: $editContentZone.data("context")
  476. },
  477. function(data) {
  478. if (data.length == 0) {
  479. $renderContent.html($("#no-content").html());
  480. } else {
  481. $renderContent.html(data.content);
  482. emojify.run($renderContent[0]);
  483. $("pre code", $renderContent[0]).each(function(i, block) {
  484. hljs.highlightBlock(block);
  485. });
  486. }
  487. }
  488. );
  489. });
  490. } else {
  491. $textarea = $segment.find("textarea");
  492. }
  493. // Show write/preview tab and copy raw content as needed
  494. $editContentZone.show();
  495. $renderContent.hide();
  496. if ($textarea.val().length == 0) {
  497. $textarea.val($rawContent.text());
  498. }
  499. $textarea.focus();
  500. return false;
  501. });
  502. // Delete comment
  503. $(".delete-comment").click(function() {
  504. var $this = $(this);
  505. if (confirm($this.data("locale"))) {
  506. $.post($this.data("url"), {
  507. _csrf: csrf
  508. }).done(function() {
  509. $("#" + $this.data("comment-id")).remove();
  510. });
  511. }
  512. return false;
  513. });
  514. // Change status
  515. var $statusButton = $("#status-button");
  516. $("#comment-form .edit_area").keyup(function() {
  517. if ($(this).val().length == 0) {
  518. $statusButton.text($statusButton.data("status"));
  519. } else {
  520. $statusButton.text($statusButton.data("status-and-comment"));
  521. }
  522. });
  523. $statusButton.click(function() {
  524. $("#status").val($statusButton.data("status-val"));
  525. $("#comment-form").submit();
  526. });
  527. }
  528. // Diff
  529. if ($(".repository.diff").length > 0) {
  530. var $counter = $(".diff-counter");
  531. if ($counter.length >= 1) {
  532. $counter.each(function(i, item) {
  533. var $item = $(item);
  534. var addLine = $item.find("span[data-line].add").data("line");
  535. var delLine = $item.find("span[data-line].del").data("line");
  536. var addPercent =
  537. (parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine))) *
  538. 100;
  539. $item.find(".bar .add").css("width", addPercent + "%");
  540. });
  541. }
  542. $(".diff-file-box .lines-num").click(function() {
  543. if ($(this).attr("id")) {
  544. window.location.href = "#" + $(this).attr("id");
  545. }
  546. });
  547. $(window)
  548. .on("hashchange", function(e) {
  549. $(".diff-file-box .lines-code.active").removeClass("active");
  550. var m = window.location.hash.match(/^#diff-.+$/);
  551. if (m) {
  552. $(m[0])
  553. .siblings(".lines-code")
  554. .addClass("active");
  555. }
  556. })
  557. .trigger("hashchange");
  558. }
  559. // Quick start and repository home
  560. $("#repo-clone-ssh").click(function() {
  561. $(".clone-url").text($(this).data("link"));
  562. $("#repo-clone-url").val($(this).data("link"));
  563. $(this).addClass("blue");
  564. $("#repo-clone-https").removeClass("blue");
  565. localStorage.setItem("repo-clone-protocol", "ssh");
  566. });
  567. $("#repo-clone-https").click(function() {
  568. $(".clone-url").text($(this).data("link"));
  569. $("#repo-clone-url").val($(this).data("link"));
  570. $(this).addClass("blue");
  571. $("#repo-clone-ssh").removeClass("blue");
  572. localStorage.setItem("repo-clone-protocol", "https");
  573. });
  574. $("#repo-clone-url").click(function() {
  575. $(this).select();
  576. });
  577. // Pull request
  578. if ($(".repository.compare.pull").length > 0) {
  579. initFilterSearchDropdown(".choose.branch .dropdown");
  580. }
  581. if ($(".repository.view.pull").length > 0) {
  582. $(".comment.merge.box input[name=merge_style]").change(function() {
  583. if ($(this).val() === "create_merge_commit") {
  584. $(".commit.description.field").show();
  585. } else {
  586. $(".commit.description.field").hide();
  587. }
  588. });
  589. }
  590. }
  591. function initWikiForm() {
  592. var $editArea = $(".repository.wiki textarea#edit_area");
  593. if ($editArea.length > 0) {
  594. new SimpleMDE({
  595. autoDownloadFontAwesome: false,
  596. element: $editArea[0],
  597. forceSync: true,
  598. previewRender: function(plainText, preview) {
  599. // Async method
  600. setTimeout(function() {
  601. // FIXME: still send render request when return back to edit mode
  602. $.post(
  603. $editArea.data("url"),
  604. {
  605. _csrf: csrf,
  606. mode: "gfm",
  607. context: $editArea.data("context"),
  608. text: plainText
  609. },
  610. function(data) {
  611. preview.innerHTML = '<div class="markdown">' + data + "</div>";
  612. emojify.run($(".editor-preview")[0]);
  613. }
  614. );
  615. }, 0);
  616. return "Loading...";
  617. },
  618. renderingConfig: {
  619. singleLineBreaks: false
  620. },
  621. indentWithTabs: false,
  622. tabSize: 4,
  623. spellChecker: false,
  624. toolbar: [
  625. "bold",
  626. "italic",
  627. "strikethrough",
  628. "|",
  629. "heading-1",
  630. "heading-2",
  631. "heading-3",
  632. "heading-bigger",
  633. "heading-smaller",
  634. "|",
  635. "code",
  636. "quote",
  637. "|",
  638. "unordered-list",
  639. "ordered-list",
  640. "|",
  641. "link",
  642. "image",
  643. "table",
  644. "horizontal-rule",
  645. "|",
  646. "clean-block",
  647. "preview",
  648. "fullscreen"
  649. ]
  650. });
  651. }
  652. }
  653. var simpleMDEditor;
  654. var codeMirrorEditor;
  655. // For IE
  656. String.prototype.endsWith = function(pattern) {
  657. var d = this.length - pattern.length;
  658. return d >= 0 && this.lastIndexOf(pattern) === d;
  659. };
  660. // Adding function to get the cursor position in a text field to jQuery object.
  661. (function($, undefined) {
  662. $.fn.getCursorPosition = function() {
  663. var el = $(this).get(0);
  664. var pos = 0;
  665. if ("selectionStart" in el) {
  666. pos = el.selectionStart;
  667. } else if ("selection" in document) {
  668. el.focus();
  669. var Sel = document.selection.createRange();
  670. var SelLength = document.selection.createRange().text.length;
  671. Sel.moveStart("character", -el.value.length);
  672. pos = Sel.text.length - SelLength;
  673. }
  674. return pos;
  675. };
  676. })(jQuery);
  677. function setSimpleMDE($editArea) {
  678. if (codeMirrorEditor) {
  679. codeMirrorEditor.toTextArea();
  680. codeMirrorEditor = null;
  681. }
  682. if (simpleMDEditor) {
  683. return true;
  684. }
  685. simpleMDEditor = new SimpleMDE({
  686. autoDownloadFontAwesome: false,
  687. element: $editArea[0],
  688. forceSync: true,
  689. renderingConfig: {
  690. singleLineBreaks: false
  691. },
  692. indentWithTabs: false,
  693. tabSize: 4,
  694. spellChecker: false,
  695. previewRender: function(plainText, preview) {
  696. // Async method
  697. setTimeout(function() {
  698. // FIXME: still send render request when return back to edit mode
  699. $.post(
  700. $editArea.data("url"),
  701. {
  702. _csrf: csrf,
  703. mode: "gfm",
  704. context: $editArea.data("context"),
  705. text: plainText
  706. },
  707. function(data) {
  708. preview.innerHTML = '<div class="markdown">' + data + "</div>";
  709. emojify.run($(".editor-preview")[0]);
  710. }
  711. );
  712. }, 0);
  713. return "Loading...";
  714. },
  715. toolbar: [
  716. "bold",
  717. "italic",
  718. "strikethrough",
  719. "|",
  720. "heading-1",
  721. "heading-2",
  722. "heading-3",
  723. "heading-bigger",
  724. "heading-smaller",
  725. "|",
  726. "code",
  727. "quote",
  728. "|",
  729. "unordered-list",
  730. "ordered-list",
  731. "|",
  732. "link",
  733. "image",
  734. "table",
  735. "horizontal-rule",
  736. "|",
  737. "clean-block"
  738. ]
  739. });
  740. return true;
  741. }
  742. function setCodeMirror($editArea) {
  743. if (simpleMDEditor) {
  744. simpleMDEditor.toTextArea();
  745. simpleMDEditor = null;
  746. }
  747. if (codeMirrorEditor) {
  748. return true;
  749. }
  750. codeMirrorEditor = CodeMirror.fromTextArea($editArea[0], {
  751. lineNumbers: true
  752. });
  753. codeMirrorEditor.on("change", function(cm, change) {
  754. $editArea.val(cm.getValue());
  755. });
  756. return true;
  757. }
  758. function initEditor() {
  759. $(".js-quick-pull-choice-option").change(function() {
  760. if ($(this).val() == "commit-to-new-branch") {
  761. $(".quick-pull-branch-name").show();
  762. $(".quick-pull-branch-name input").prop("required", true);
  763. } else {
  764. $(".quick-pull-branch-name").hide();
  765. $(".quick-pull-branch-name input").prop("required", false);
  766. }
  767. });
  768. var $editFilename = $("#file-name");
  769. $editFilename
  770. .keyup(function(e) {
  771. var $section = $(".breadcrumb span.section");
  772. var $divider = $(".breadcrumb div.divider");
  773. if (e.keyCode == 8) {
  774. if ($(this).getCursorPosition() == 0) {
  775. if ($section.length > 0) {
  776. var value = $section
  777. .last()
  778. .find("a")
  779. .text();
  780. $(this).val(value + $(this).val());
  781. $(this)[0].setSelectionRange(value.length, value.length);
  782. $section.last().remove();
  783. $divider.last().remove();
  784. }
  785. }
  786. }
  787. if (e.keyCode == 191) {
  788. var parts = $(this)
  789. .val()
  790. .split("/");
  791. for (var i = 0; i < parts.length; ++i) {
  792. var value = parts[i];
  793. if (i < parts.length - 1) {
  794. if (value.length) {
  795. $(
  796. '<span class="section"><a href="#">' + value + "</a></span>"
  797. ).insertBefore($(this));
  798. $('<div class="divider"> / </div>').insertBefore($(this));
  799. }
  800. } else {
  801. $(this).val(value);
  802. }
  803. $(this)[0].setSelectionRange(0, 0);
  804. }
  805. }
  806. var parts = [];
  807. $(".breadcrumb span.section").each(function(i, element) {
  808. element = $(element);
  809. if (element.find("a").length) {
  810. parts.push(element.find("a").text());
  811. } else {
  812. parts.push(element.text());
  813. }
  814. });
  815. if ($(this).val()) {
  816. parts.push($(this).val());
  817. }
  818. var tree_path = parts.join("/");
  819. $("#tree_path").val(tree_path);
  820. $("#preview-tab").data(
  821. "context",
  822. $("#preview-tab").data("root-context") +
  823. tree_path.substring(0, tree_path.lastIndexOf("/") + 1)
  824. );
  825. })
  826. .trigger("keyup");
  827. var $editArea = $(".repository.editor textarea#edit_area");
  828. if (!$editArea.length) return;
  829. var markdownFileExts = $editArea.data("markdown-file-exts").split(",");
  830. var lineWrapExtensions = $editArea.data("line-wrap-extensions").split(",");
  831. $editFilename
  832. .on("keyup", function(e) {
  833. var val = $editFilename.val(),
  834. m,
  835. mode,
  836. spec,
  837. extension,
  838. extWithDot,
  839. previewLink,
  840. dataUrl,
  841. apiCall;
  842. extension = extWithDot = "";
  843. if ((m = /.+\.([^.]+)$/.exec(val))) {
  844. extension = m[1].toLowerCase();
  845. extWithDot = "." + extension;
  846. }
  847. var info = CodeMirror.findModeByExtension(extension);
  848. previewLink = $("a[data-tab=preview]");
  849. if (info) {
  850. mode = info.mode;
  851. spec = info.mime;
  852. apiCall = mode;
  853. } else {
  854. apiCall = extension;
  855. }
  856. if (
  857. previewLink.length &&
  858. apiCall &&
  859. previewFileModes &&
  860. previewFileModes.length &&
  861. previewFileModes.indexOf(apiCall) >= 0
  862. ) {
  863. dataUrl = previewLink.data("url");
  864. previewLink.data("url", dataUrl.replace(/(.*)\/.*/i, "$1/" + mode));
  865. previewLink.show();
  866. } else {
  867. previewLink.hide();
  868. }
  869. // If this file is a Markdown extensions, we will load that editor and return
  870. if (markdownFileExts.indexOf(extWithDot) >= 0) {
  871. if (setSimpleMDE($editArea)) {
  872. return;
  873. }
  874. }
  875. // Else we are going to use CodeMirror
  876. if (!codeMirrorEditor && !setCodeMirror($editArea)) {
  877. return;
  878. }
  879. if (mode) {
  880. codeMirrorEditor.setOption("mode", spec);
  881. CodeMirror.autoLoadMode(codeMirrorEditor, mode);
  882. }
  883. if (lineWrapExtensions.indexOf(extWithDot) >= 0) {
  884. codeMirrorEditor.setOption("lineWrapping", true);
  885. } else {
  886. codeMirrorEditor.setOption("lineWrapping", false);
  887. }
  888. // get the filename without any folder
  889. var value = $editFilename.val();
  890. if (value.length === 0) {
  891. return;
  892. }
  893. value = value.split("/");
  894. value = value[value.length - 1];
  895. $.getJSON($editFilename.data("ec-url-prefix") + value, function(
  896. editorconfig
  897. ) {
  898. if (editorconfig.indent_style === "tab") {
  899. codeMirrorEditor.setOption("indentWithTabs", true);
  900. codeMirrorEditor.setOption("extraKeys", {});
  901. } else {
  902. codeMirrorEditor.setOption("indentWithTabs", false);
  903. // required because CodeMirror doesn't seems to use spaces correctly for {"indentWithTabs": false}:
  904. // - https://github.com/codemirror/CodeMirror/issues/988
  905. // - https://codemirror.net/doc/manual.html#keymaps
  906. codeMirrorEditor.setOption("extraKeys", {
  907. Tab: function(cm) {
  908. var spaces = Array(parseInt(cm.getOption("indentUnit")) + 1).join(
  909. " "
  910. );
  911. cm.replaceSelection(spaces);
  912. }
  913. });
  914. }
  915. codeMirrorEditor.setOption("indentUnit", editorconfig.indent_size || 4);
  916. codeMirrorEditor.setOption("tabSize", editorconfig.tab_width || 4);
  917. });
  918. })
  919. .trigger("keyup");
  920. }
  921. function initOrganization() {
  922. if ($(".organization").length == 0) {
  923. return;
  924. }
  925. // Options
  926. if ($(".organization.settings.options").length > 0) {
  927. $("#org_name").keyup(function() {
  928. var $prompt = $("#org-name-change-prompt");
  929. if (
  930. $(this)
  931. .val()
  932. .toString()
  933. .toLowerCase() !=
  934. $(this)
  935. .data("org-name")
  936. .toString()
  937. .toLowerCase()
  938. ) {
  939. $prompt.show();
  940. } else {
  941. $prompt.hide();
  942. }
  943. });
  944. }
  945. }
  946. function initAdmin() {
  947. if ($(".admin").length == 0) {
  948. return;
  949. }
  950. // New user
  951. if ($(".admin.new.user").length > 0 || $(".admin.edit.user").length > 0) {
  952. $("#login_type").change(function() {
  953. if (
  954. $(this)
  955. .val()
  956. .substring(0, 1) == "0"
  957. ) {
  958. $("#login_name").removeAttr("required");
  959. $(".non-local").hide();
  960. $(".local").show();
  961. $("#user_name").focus();
  962. if ($(this).data("password") == "required") {
  963. $("#password").attr("required", "required");
  964. }
  965. } else {
  966. $("#login_name").attr("required", "required");
  967. $(".non-local").show();
  968. $(".local").hide();
  969. $("#login_name").focus();
  970. $("#password").removeAttr("required");
  971. }
  972. });
  973. }
  974. function onSecurityProtocolChange() {
  975. if ($("#security_protocol").val() > 0) {
  976. $(".has-tls").show();
  977. } else {
  978. $(".has-tls").hide();
  979. }
  980. }
  981. // New authentication
  982. if ($(".admin.new.authentication").length > 0) {
  983. $("#auth_type").change(function() {
  984. $(".ldap").hide();
  985. $(".dldap").hide();
  986. $(".smtp").hide();
  987. $(".pam").hide();
  988. $(".github").hide();
  989. $(".has-tls").hide();
  990. var authType = $(this).val();
  991. switch (authType) {
  992. case "2": // LDAP
  993. $(".ldap").show();
  994. break;
  995. case "3": // SMTP
  996. $(".smtp").show();
  997. $(".has-tls").show();
  998. break;
  999. case "4": // PAM
  1000. $(".pam").show();
  1001. break;
  1002. case "5": // LDAP
  1003. $(".dldap").show();
  1004. break;
  1005. case "6": //GITHUB
  1006. $(".github").show();
  1007. break;
  1008. }
  1009. if (authType == "2" || authType == "5") {
  1010. onSecurityProtocolChange();
  1011. }
  1012. });
  1013. $("#security_protocol").change(onSecurityProtocolChange);
  1014. }
  1015. // Edit authentication
  1016. if ($(".admin.edit.authentication").length > 0) {
  1017. var authType = $("#auth_type").val();
  1018. if (authType == "2" || authType == "5") {
  1019. $("#security_protocol").change(onSecurityProtocolChange);
  1020. }
  1021. }
  1022. // Notice
  1023. if ($(".admin.notice")) {
  1024. var $detailModal = $("#detail-modal");
  1025. // Attach view detail modals
  1026. $(".view-detail").click(function() {
  1027. $detailModal.find(".content p").text($(this).data("content"));
  1028. $detailModal.modal("show");
  1029. return false;
  1030. });
  1031. // Select actions
  1032. var $checkboxes = $(".select.table .ui.checkbox");
  1033. $(".select.action").click(function() {
  1034. switch ($(this).data("action")) {
  1035. case "select-all":
  1036. $checkboxes.checkbox("check");
  1037. break;
  1038. case "deselect-all":
  1039. $checkboxes.checkbox("uncheck");
  1040. break;
  1041. case "inverse":
  1042. $checkboxes.checkbox("toggle");
  1043. break;
  1044. }
  1045. });
  1046. $("#delete-selection").click(function() {
  1047. var $this = $(this);
  1048. $this.addClass("loading disabled");
  1049. var ids = [];
  1050. $checkboxes.each(function() {
  1051. if ($(this).checkbox("is checked")) {
  1052. ids.push($(this).data("id"));
  1053. }
  1054. });
  1055. $.post($this.data("link"), {
  1056. _csrf: csrf,
  1057. ids: ids
  1058. }).done(function() {
  1059. window.location.href = $this.data("redirect");
  1060. });
  1061. });
  1062. }
  1063. }
  1064. function buttonsClickOnEnter() {
  1065. $(".ui.button").keypress(function(e) {
  1066. if (e.keyCode == 13 || e.keyCode == 32)
  1067. // enter key or space bar
  1068. $(this).click();
  1069. });
  1070. }
  1071. function hideWhenLostFocus(body, parent) {
  1072. $(document).click(function(e) {
  1073. var target = e.target;
  1074. if (
  1075. !$(target).is(body) &&
  1076. !$(target)
  1077. .parents()
  1078. .is(parent)
  1079. ) {
  1080. $(body).hide();
  1081. }
  1082. });
  1083. }
  1084. function searchUsers() {
  1085. if (!$("#search-user-box .results").length) {
  1086. return;
  1087. }
  1088. var $searchUserBox = $("#search-user-box");
  1089. var $results = $searchUserBox.find(".results");
  1090. $searchUserBox.keyup(function() {
  1091. var $this = $(this);
  1092. var keyword = $this.find("input").val();
  1093. if (keyword.length < 2) {
  1094. $results.hide();
  1095. return;
  1096. }
  1097. $.ajax({
  1098. url: suburl + "/api/v1/users/search?q=" + keyword,
  1099. dataType: "json",
  1100. success: function(response) {
  1101. var notEmpty = function(str) {
  1102. return str && str.length > 0;
  1103. };
  1104. $results.html("");
  1105. if (response.ok && response.data.length) {
  1106. var html = "";
  1107. $.each(response.data, function(i, item) {
  1108. html +=
  1109. '<div class="item"><img class="ui avatar image" src="' +
  1110. item.avatar_url +
  1111. '"><span class="username">' +
  1112. item.username +
  1113. "</span>";
  1114. if (notEmpty(item.full_name)) {
  1115. html += " (" + item.full_name + ")";
  1116. }
  1117. html += "</div>";
  1118. });
  1119. $results.html(html);
  1120. $this.find(".results .item").click(function() {
  1121. $this.find("input").val(
  1122. $(this)
  1123. .find(".username")
  1124. .text()
  1125. );
  1126. $results.hide();
  1127. });
  1128. $results.show();
  1129. } else {
  1130. $results.hide();
  1131. }
  1132. }
  1133. });
  1134. });
  1135. $searchUserBox.find("input").focus(function() {
  1136. $searchUserBox.keyup();
  1137. });
  1138. hideWhenLostFocus("#search-user-box .results", "#search-user-box");
  1139. }
  1140. // FIXME: merge common parts in two functions
  1141. function searchRepositories() {
  1142. if (!$("#search-repo-box .results").length) {
  1143. return;
  1144. }
  1145. var $searchRepoBox = $("#search-repo-box");
  1146. var $results = $searchRepoBox.find(".results");
  1147. $searchRepoBox.keyup(function() {
  1148. var $this = $(this);
  1149. var keyword = $this.find("input").val();
  1150. if (keyword.length < 2) {
  1151. $results.hide();
  1152. return;
  1153. }
  1154. $.ajax({
  1155. url:
  1156. suburl +
  1157. "/api/v1/repos/search?q=" +
  1158. keyword +
  1159. "&uid=" +
  1160. $searchRepoBox.data("uid"),
  1161. dataType: "json",
  1162. success: function(response) {
  1163. var notEmpty = function(str) {
  1164. return str && str.length > 0;
  1165. };
  1166. $results.html("");
  1167. if (response.ok && response.data.length) {
  1168. var html = "";
  1169. $.each(response.data, function(i, item) {
  1170. html +=
  1171. '<div class="item"><i class="octicon octicon-repo"></i> <span class="fullname">' +
  1172. item.full_name +
  1173. "</span></div>";
  1174. });
  1175. $results.html(html);
  1176. $this.find(".results .item").click(function() {
  1177. $this.find("input").val(
  1178. $(this)
  1179. .find(".fullname")
  1180. .text()
  1181. .split("/")[1]
  1182. );
  1183. $results.hide();
  1184. });
  1185. $results.show();
  1186. } else {
  1187. $results.hide();
  1188. }
  1189. }
  1190. });
  1191. });
  1192. $searchRepoBox.find("input").focus(function() {
  1193. $searchRepoBox.keyup();
  1194. });
  1195. hideWhenLostFocus("#search-repo-box .results", "#search-repo-box");
  1196. }
  1197. function initCodeView() {
  1198. if ($(".code-view .linenums").length > 0) {
  1199. $(document).on("click", ".lines-num span", function(e) {
  1200. var $select = $(this);
  1201. var $list = $select
  1202. .parent()
  1203. .siblings(".lines-code")
  1204. .find("ol.linenums > li");
  1205. selectRange(
  1206. $list,
  1207. $list.filter("[rel=" + $select.attr("id") + "]"),
  1208. e.shiftKey ? $list.filter(".active").eq(0) : null
  1209. );
  1210. deSelect();
  1211. });
  1212. $(window)
  1213. .on("hashchange", function(e) {
  1214. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1215. var $list = $(".code-view ol.linenums > li");
  1216. var $first;
  1217. if (m) {
  1218. $first = $list.filter("." + m[1]);
  1219. selectRange($list, $first, $list.filter("." + m[2]));
  1220. $("html, body").scrollTop($first.offset().top - 200);
  1221. return;
  1222. }
  1223. m = window.location.hash.match(/^#(L\d+)$/);
  1224. if (m) {
  1225. $first = $list.filter("." + m[1]);
  1226. selectRange($list, $first);
  1227. $("html, body").scrollTop($first.offset().top - 200);
  1228. }
  1229. })
  1230. .trigger("hashchange");
  1231. }
  1232. }
  1233. function initUserSettings() {
  1234. console.log("initUserSettings");
  1235. // Options
  1236. if ($(".user.settings.profile").length > 0) {
  1237. $("#username").keyup(function() {
  1238. var $prompt = $("#name-change-prompt");
  1239. if (
  1240. $(this)
  1241. .val()
  1242. .toString()
  1243. .toLowerCase() !=
  1244. $(this)
  1245. .data("name")
  1246. .toString()
  1247. .toLowerCase()
  1248. ) {
  1249. $prompt.show();
  1250. } else {
  1251. $prompt.hide();
  1252. }
  1253. });
  1254. }
  1255. }
  1256. function initRepositoryCollaboration() {
  1257. console.log("initRepositoryCollaboration");
  1258. // Change collaborator access mode
  1259. $(".access-mode.menu .item").click(function() {
  1260. var $menu = $(this).parent();
  1261. $.post($menu.data("url"), {
  1262. _csrf: csrf,
  1263. uid: $menu.data("uid"),
  1264. mode: $(this).data("value")
  1265. });
  1266. });
  1267. }
  1268. function initWebhookSettings() {
  1269. $(".events.checkbox input").change(function() {
  1270. if ($(this).is(":checked")) {
  1271. $(".events.fields").show();
  1272. }
  1273. });
  1274. $(".non-events.checkbox input").change(function() {
  1275. if ($(this).is(":checked")) {
  1276. $(".events.fields").hide();
  1277. }
  1278. });
  1279. // Highlight payload on first click
  1280. $(".hook.history.list .toggle.button").click(function() {
  1281. $($(this).data("target") + " .nohighlight").each(function() {
  1282. var $this = $(this);
  1283. $this.removeClass("nohighlight");
  1284. setTimeout(function() {
  1285. hljs.highlightBlock($this[0]);
  1286. }, 500);
  1287. });
  1288. });
  1289. // Trigger delivery
  1290. $(".delivery.button, .redelivery.button").click(function() {
  1291. var $this = $(this);
  1292. $this.addClass("loading disabled");
  1293. $.post($this.data("link"), {
  1294. _csrf: csrf
  1295. }).done(
  1296. setTimeout(function() {
  1297. window.location.href = $this.data("redirect");
  1298. }, 5000)
  1299. );
  1300. });
  1301. }
  1302. $(document).ready(function() {
  1303. csrf = $("meta[name=_csrf]").attr("content");
  1304. suburl = $("meta[name=_suburl]").attr("content");
  1305. // Set cursor to the end of autofocus input string
  1306. $("input[autofocus]").each(function() {
  1307. $(this).val($(this).val());
  1308. });
  1309. // Show exact time
  1310. $(".time-since").each(function() {
  1311. $(this)
  1312. .addClass("poping up")
  1313. .attr("data-content", $(this).attr("title"))
  1314. .attr("data-variation", "inverted tiny")
  1315. .attr("title", "");
  1316. });
  1317. // Semantic UI modules.
  1318. $(".ui.dropdown").dropdown({
  1319. forceSelection: false
  1320. });
  1321. $(".jump.dropdown").dropdown({
  1322. action: "select",
  1323. onShow: function() {
  1324. $(".poping.up").popup("hide");
  1325. }
  1326. });
  1327. $(".slide.up.dropdown").dropdown({
  1328. transition: "slide up"
  1329. });
  1330. $(".upward.dropdown").dropdown({
  1331. direction: "upward"
  1332. });
  1333. $(".ui.accordion").accordion();
  1334. $(".ui.checkbox").checkbox();
  1335. $(".ui.progress").progress({
  1336. showActivity: false
  1337. });
  1338. $(".poping.up").popup();
  1339. $(".top.menu .poping.up").popup({
  1340. onShow: function() {
  1341. if ($(".top.menu .menu.transition").hasClass("visible")) {
  1342. return false;
  1343. }
  1344. }
  1345. });
  1346. $(".tabular.menu .item").tab();
  1347. $(".tabable.menu .item").tab();
  1348. $(".toggle.button").click(function() {
  1349. $($(this).data("target")).slideToggle(100);
  1350. });
  1351. // Dropzone
  1352. var $dropzone = $("#dropzone");
  1353. if ($dropzone.length > 0) {
  1354. var filenameDict = {};
  1355. $dropzone.dropzone({
  1356. url: $dropzone.data("upload-url"),
  1357. headers: { "X-CSRF-Token": csrf },
  1358. maxFiles: $dropzone.data("max-file"),
  1359. maxFilesize: $dropzone.data("max-size"),
  1360. acceptedFiles:
  1361. $dropzone.data("accepts") === "*/*" ? null : $dropzone.data("accepts"),
  1362. addRemoveLinks: true,
  1363. dictDefaultMessage: $dropzone.data("default-message"),
  1364. dictInvalidFileType: $dropzone.data("invalid-input-type"),
  1365. dictFileTooBig: $dropzone.data("file-too-big"),
  1366. dictRemoveFile: $dropzone.data("remove-file"),
  1367. init: function() {
  1368. this.on("success", function(file, data) {
  1369. filenameDict[file.name] = data.uuid;
  1370. var input = $(
  1371. '<input id="' + data.uuid + '" name="files" type="hidden">'
  1372. ).val(data.uuid);
  1373. $(".files").append(input);
  1374. });
  1375. this.on("removedfile", function(file) {
  1376. if (file.name in filenameDict) {
  1377. $("#" + filenameDict[file.name]).remove();
  1378. }
  1379. if ($dropzone.data("remove-url") && $dropzone.data("csrf")) {
  1380. $.post($dropzone.data("remove-url"), {
  1381. file: filenameDict[file.name],
  1382. _csrf: $dropzone.data("csrf")
  1383. });
  1384. }
  1385. });
  1386. }
  1387. });
  1388. }
  1389. // Emojify
  1390. emojify.setConfig({
  1391. img_dir: suburl + "/img/emoji",
  1392. ignore_emoticons: true
  1393. });
  1394. var hasEmoji = document.getElementsByClassName("has-emoji");
  1395. for (var i = 0; i < hasEmoji.length; i++) {
  1396. emojify.run(hasEmoji[i]);
  1397. }
  1398. // Clipboard JS
  1399. var clipboard = new ClipboardJS(".clipboard");
  1400. clipboard.on("success", function(e) {
  1401. e.clearSelection();
  1402. $("#" + e.trigger.getAttribute("id")).popup("destroy");
  1403. e.trigger.setAttribute(
  1404. "data-content",
  1405. e.trigger.getAttribute("data-success")
  1406. );
  1407. $("#" + e.trigger.getAttribute("id")).popup("show");
  1408. e.trigger.setAttribute(
  1409. "data-content",
  1410. e.trigger.getAttribute("data-original")
  1411. );
  1412. });
  1413. clipboard.on("error", function(e) {
  1414. $("#" + e.trigger.getAttribute("id")).popup("destroy");
  1415. e.trigger.setAttribute(
  1416. "data-content",
  1417. e.trigger.getAttribute("data-error")
  1418. );
  1419. $("#" + e.trigger.getAttribute("id")).popup("show");
  1420. e.trigger.setAttribute(
  1421. "data-content",
  1422. e.trigger.getAttribute("data-original")
  1423. );
  1424. });
  1425. // Autosize
  1426. if ($("#description.autosize").length > 0) {
  1427. autosize($("#description"));
  1428. showMessageMaxLength(512, "description", "descLength");
  1429. }
  1430. // AJAX load buttons
  1431. $(".ajax-load-button").click(function() {
  1432. var $this = $(this);
  1433. $this.addClass("disabled");
  1434. $.ajax({
  1435. url: $this.data("url"),
  1436. headers: {
  1437. "X-AJAX": "true"
  1438. }
  1439. }).done(function(data, status, request) {
  1440. $(data).insertBefore($this);
  1441. // Update new URL or remove self if no more feeds
  1442. var url = request.getResponseHeader("X-AJAX-URL");
  1443. if (url) {
  1444. $this.data("url", url);
  1445. $this.removeClass("disabled");
  1446. } else {
  1447. $this.remove();
  1448. }
  1449. });
  1450. });
  1451. // Helpers
  1452. $(".delete-button").click(function() {
  1453. var $this = $(this);
  1454. $(".delete.modal")
  1455. .modal({
  1456. closable: false,
  1457. onApprove: function() {
  1458. if ($this.data("type") == "form") {
  1459. $($this.data("form")).submit();
  1460. return;
  1461. }
  1462. $.post($this.data("url"), {
  1463. _csrf: csrf,
  1464. id: $this.data("id")
  1465. }).done(function(data) {
  1466. window.location.href = data.redirect;
  1467. });
  1468. }
  1469. })
  1470. .modal("show");
  1471. return false;
  1472. });
  1473. $(".show-panel.button").click(function() {
  1474. $($(this).data("panel")).show();
  1475. });
  1476. $(".show-modal.button").click(function() {
  1477. $($(this).data("modal")).modal("show");
  1478. });
  1479. $(".delete-post.button").click(function() {
  1480. var $this = $(this);
  1481. $.post($this.data("request-url"), {
  1482. _csrf: csrf
  1483. }).done(function() {
  1484. window.location.href = $this.data("done-url");
  1485. });
  1486. });
  1487. // To make arbitrary form element to behave like a submit button
  1488. $(".submit-button").click(function() {
  1489. $($(this).data("form")).submit();
  1490. });
  1491. // Check or select on option to enable/disable target region
  1492. $(".enable-system").change(function() {
  1493. if (this.checked) {
  1494. $($(this).data("target")).removeClass("disabled");
  1495. } else {
  1496. $($(this).data("target")).addClass("disabled");
  1497. $($(this).data("uncheck")).prop("checked", false);
  1498. }
  1499. });
  1500. $(".enable-system-radio").change(function() {
  1501. $($(this).data("enable")).removeClass("disabled");
  1502. $($(this).data("disable")).addClass("disabled");
  1503. $($(this).data("uncheck")).prop("checked", false);
  1504. });
  1505. // Set anchor.
  1506. $(".markdown").each(function() {
  1507. var headers = {};
  1508. $(this)
  1509. .find("h1, h2, h3, h4, h5, h6")
  1510. .each(function() {
  1511. var node = $(this);
  1512. var val = encodeURIComponent(
  1513. node
  1514. .text()
  1515. .toLowerCase()
  1516. .replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, "")
  1517. .replace(/[ ]/g, "-")
  1518. );
  1519. var name = val;
  1520. if (headers[val] > 0) {
  1521. name = val + "-" + headers[val];
  1522. }
  1523. if (headers[val] == undefined) {
  1524. headers[val] = 1;
  1525. } else {
  1526. headers[val] += 1;
  1527. }
  1528. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1529. node.append(
  1530. '<a class="anchor" href="#' +
  1531. name +
  1532. '"><span class="octicon octicon-link"></span></a>'
  1533. );
  1534. });
  1535. });
  1536. buttonsClickOnEnter();
  1537. searchUsers();
  1538. searchRepositories();
  1539. initCommentForm();
  1540. initRepository();
  1541. initWikiForm();
  1542. initEditForm();
  1543. initEditor();
  1544. initOrganization();
  1545. initAdmin();
  1546. initCodeView();
  1547. // Repo clone url.
  1548. if ($("#repo-clone-url").length > 0) {
  1549. switch (localStorage.getItem("repo-clone-protocol")) {
  1550. case "ssh":
  1551. if ($("#repo-clone-ssh").click().length === 0) {
  1552. $("#repo-clone-https").click();
  1553. }
  1554. break;
  1555. default:
  1556. $("#repo-clone-https").click();
  1557. break;
  1558. }
  1559. }
  1560. var routes = {
  1561. "div.user.settings": initUserSettings,
  1562. "div.repository.settings.collaboration": initRepositoryCollaboration,
  1563. "div.webhook.settings": initWebhookSettings
  1564. };
  1565. var selector;
  1566. for (selector in routes) {
  1567. if ($(selector).length > 0) {
  1568. routes[selector]();
  1569. break;
  1570. }
  1571. }
  1572. });
  1573. function changeHash(hash) {
  1574. if (history.pushState) {
  1575. history.pushState(null, null, hash);
  1576. } else {
  1577. location.hash = hash;
  1578. }
  1579. }
  1580. function deSelect() {
  1581. if (window.getSelection) {
  1582. window.getSelection().removeAllRanges();
  1583. } else {
  1584. document.selection.empty();
  1585. }
  1586. }
  1587. function selectRange($list, $select, $from) {
  1588. $list.removeClass("active");
  1589. if ($from) {
  1590. var a = parseInt($select.attr("rel").substr(1));
  1591. var b = parseInt($from.attr("rel").substr(1));
  1592. var c;
  1593. if (a != b) {
  1594. if (a > b) {
  1595. c = a;
  1596. a = b;
  1597. b = c;
  1598. }
  1599. var classes = [];
  1600. for (var i = a; i <= b; i++) {
  1601. classes.push(".L" + i);
  1602. }
  1603. $list.filter(classes.join(",")).addClass("active");
  1604. changeHash("#L" + a + "-" + "L" + b);
  1605. return;
  1606. }
  1607. }
  1608. $select.addClass("active");
  1609. changeHash("#" + $select.attr("rel"));
  1610. }
  1611. $(function() {
  1612. if ($(".user.signin").length > 0) return;
  1613. $("form").areYouSure();
  1614. });
  1615. // getByteLen counts bytes in a string's UTF-8 representation.
  1616. function getByteLen(normalVal) {
  1617. // Force string type
  1618. normalVal = String(normalVal);
  1619. var byteLen = 0;
  1620. for (var i = 0; i < normalVal.length; i++) {
  1621. var c = normalVal.charCodeAt(i);
  1622. byteLen +=
  1623. c < 1 << 7
  1624. ? 1
  1625. : c < 1 << 11
  1626. ? 2
  1627. : c < 1 << 16
  1628. ? 3
  1629. : c < 1 << 21
  1630. ? 4
  1631. : c < 1 << 26
  1632. ? 5
  1633. : c < 1 << 31
  1634. ? 6
  1635. : Number.NaN;
  1636. }
  1637. return byteLen;
  1638. }
  1639. function showMessageMaxLength(maxLen, textElemId, counterId) {
  1640. var $msg = $("#" + textElemId);
  1641. $("#" + counterId).html(maxLen - getByteLen($msg.val()));
  1642. var onMessageKey = function(e) {
  1643. var $msg = $(this);
  1644. var text = $msg.val();
  1645. var len = getByteLen(text);
  1646. var remainder = maxLen - len;
  1647. if (len >= maxLen) {
  1648. $msg.val($msg.val().substr(0, maxLen));
  1649. remainder = 0;
  1650. }
  1651. $("#" + counterId).html(remainder);
  1652. };
  1653. $msg.keyup(onMessageKey).keydown(onMessageKey);
  1654. }