gogs.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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. }