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. $(".has-tls").show();
  1008. break;
  1009. }
  1010. if (authType == "2" || authType == "5") {
  1011. onSecurityProtocolChange();
  1012. }
  1013. });
  1014. $("#security_protocol").change(onSecurityProtocolChange);
  1015. }
  1016. // Edit authentication
  1017. if ($(".admin.edit.authentication").length > 0) {
  1018. var authType = $("#auth_type").val();
  1019. if (authType == "2" || authType == "5") {
  1020. $("#security_protocol").change(onSecurityProtocolChange);
  1021. }
  1022. }
  1023. // Notice
  1024. if ($(".admin.notice")) {
  1025. var $detailModal = $("#detail-modal");
  1026. // Attach view detail modals
  1027. $(".view-detail").click(function() {
  1028. $detailModal.find(".content p").text($(this).data("content"));
  1029. $detailModal.modal("show");
  1030. return false;
  1031. });
  1032. // Select actions
  1033. var $checkboxes = $(".select.table .ui.checkbox");
  1034. $(".select.action").click(function() {
  1035. switch ($(this).data("action")) {
  1036. case "select-all":
  1037. $checkboxes.checkbox("check");
  1038. break;
  1039. case "deselect-all":
  1040. $checkboxes.checkbox("uncheck");
  1041. break;
  1042. case "inverse":
  1043. $checkboxes.checkbox("toggle");
  1044. break;
  1045. }
  1046. });
  1047. $("#delete-selection").click(function() {
  1048. var $this = $(this);
  1049. $this.addClass("loading disabled");
  1050. var ids = [];
  1051. $checkboxes.each(function() {
  1052. if ($(this).checkbox("is checked")) {
  1053. ids.push($(this).data("id"));
  1054. }
  1055. });
  1056. $.post($this.data("link"), {
  1057. _csrf: csrf,
  1058. ids: ids
  1059. }).done(function() {
  1060. window.location.href = $this.data("redirect");
  1061. });
  1062. });
  1063. }
  1064. }
  1065. function buttonsClickOnEnter() {
  1066. $(".ui.button").keypress(function(e) {
  1067. if (e.keyCode == 13 || e.keyCode == 32)
  1068. // enter key or space bar
  1069. $(this).click();
  1070. });
  1071. }
  1072. function hideWhenLostFocus(body, parent) {
  1073. $(document).click(function(e) {
  1074. var target = e.target;
  1075. if (
  1076. !$(target).is(body) &&
  1077. !$(target)
  1078. .parents()
  1079. .is(parent)
  1080. ) {
  1081. $(body).hide();
  1082. }
  1083. });
  1084. }
  1085. function searchUsers() {
  1086. if (!$("#search-user-box .results").length) {
  1087. return;
  1088. }
  1089. var $searchUserBox = $("#search-user-box");
  1090. var $results = $searchUserBox.find(".results");
  1091. $searchUserBox.keyup(function() {
  1092. var $this = $(this);
  1093. var keyword = $this.find("input").val();
  1094. if (keyword.length < 2) {
  1095. $results.hide();
  1096. return;
  1097. }
  1098. $.ajax({
  1099. url: suburl + "/api/v1/users/search?q=" + keyword,
  1100. dataType: "json",
  1101. success: function(response) {
  1102. var notEmpty = function(str) {
  1103. return str && str.length > 0;
  1104. };
  1105. $results.html("");
  1106. if (response.ok && response.data.length) {
  1107. var html = "";
  1108. $.each(response.data, function(i, item) {
  1109. html +=
  1110. '<div class="item"><img class="ui avatar image" src="' +
  1111. item.avatar_url +
  1112. '"><span class="username">' +
  1113. item.username +
  1114. "</span>";
  1115. if (notEmpty(item.full_name)) {
  1116. html += " (" + item.full_name + ")";
  1117. }
  1118. html += "</div>";
  1119. });
  1120. $results.html(html);
  1121. $this.find(".results .item").click(function() {
  1122. $this.find("input").val(
  1123. $(this)
  1124. .find(".username")
  1125. .text()
  1126. );
  1127. $results.hide();
  1128. });
  1129. $results.show();
  1130. } else {
  1131. $results.hide();
  1132. }
  1133. }
  1134. });
  1135. });
  1136. $searchUserBox.find("input").focus(function() {
  1137. $searchUserBox.keyup();
  1138. });
  1139. hideWhenLostFocus("#search-user-box .results", "#search-user-box");
  1140. }
  1141. // FIXME: merge common parts in two functions
  1142. function searchRepositories() {
  1143. if (!$("#search-repo-box .results").length) {
  1144. return;
  1145. }
  1146. var $searchRepoBox = $("#search-repo-box");
  1147. var $results = $searchRepoBox.find(".results");
  1148. $searchRepoBox.keyup(function() {
  1149. var $this = $(this);
  1150. var keyword = $this.find("input").val();
  1151. if (keyword.length < 2) {
  1152. $results.hide();
  1153. return;
  1154. }
  1155. $.ajax({
  1156. url:
  1157. suburl +
  1158. "/api/v1/repos/search?q=" +
  1159. keyword +
  1160. "&uid=" +
  1161. $searchRepoBox.data("uid"),
  1162. dataType: "json",
  1163. success: function(response) {
  1164. var notEmpty = function(str) {
  1165. return str && str.length > 0;
  1166. };
  1167. $results.html("");
  1168. if (response.ok && response.data.length) {
  1169. var html = "";
  1170. $.each(response.data, function(i, item) {
  1171. html +=
  1172. '<div class="item"><i class="octicon octicon-repo"></i> <span class="fullname">' +
  1173. item.full_name +
  1174. "</span></div>";
  1175. });
  1176. $results.html(html);
  1177. $this.find(".results .item").click(function() {
  1178. $this.find("input").val(
  1179. $(this)
  1180. .find(".fullname")
  1181. .text()
  1182. .split("/")[1]
  1183. );
  1184. $results.hide();
  1185. });
  1186. $results.show();
  1187. } else {
  1188. $results.hide();
  1189. }
  1190. }
  1191. });
  1192. });
  1193. $searchRepoBox.find("input").focus(function() {
  1194. $searchRepoBox.keyup();
  1195. });
  1196. hideWhenLostFocus("#search-repo-box .results", "#search-repo-box");
  1197. }
  1198. function initCodeView() {
  1199. if ($(".code-view .linenums").length > 0) {
  1200. $(document).on("click", ".lines-num span", function(e) {
  1201. var $select = $(this);
  1202. var $list = $select
  1203. .parent()
  1204. .siblings(".lines-code")
  1205. .find("ol.linenums > li");
  1206. selectRange(
  1207. $list,
  1208. $list.filter("[rel=" + $select.attr("id") + "]"),
  1209. e.shiftKey ? $list.filter(".active").eq(0) : null
  1210. );
  1211. deSelect();
  1212. });
  1213. $(window)
  1214. .on("hashchange", function(e) {
  1215. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1216. var $list = $(".code-view ol.linenums > li");
  1217. var $first;
  1218. if (m) {
  1219. $first = $list.filter("." + m[1]);
  1220. selectRange($list, $first, $list.filter("." + m[2]));
  1221. $("html, body").scrollTop($first.offset().top - 200);
  1222. return;
  1223. }
  1224. m = window.location.hash.match(/^#(L\d+)$/);
  1225. if (m) {
  1226. $first = $list.filter("." + m[1]);
  1227. selectRange($list, $first);
  1228. $("html, body").scrollTop($first.offset().top - 200);
  1229. }
  1230. })
  1231. .trigger("hashchange");
  1232. }
  1233. }
  1234. function initUserSettings() {
  1235. console.log("initUserSettings");
  1236. // Options
  1237. if ($(".user.settings.profile").length > 0) {
  1238. $("#username").keyup(function() {
  1239. var $prompt = $("#name-change-prompt");
  1240. if (
  1241. $(this)
  1242. .val()
  1243. .toString()
  1244. .toLowerCase() !=
  1245. $(this)
  1246. .data("name")
  1247. .toString()
  1248. .toLowerCase()
  1249. ) {
  1250. $prompt.show();
  1251. } else {
  1252. $prompt.hide();
  1253. }
  1254. });
  1255. }
  1256. }
  1257. function initRepositoryCollaboration() {
  1258. console.log("initRepositoryCollaboration");
  1259. // Change collaborator access mode
  1260. $(".access-mode.menu .item").click(function() {
  1261. var $menu = $(this).parent();
  1262. $.post($menu.data("url"), {
  1263. _csrf: csrf,
  1264. uid: $menu.data("uid"),
  1265. mode: $(this).data("value")
  1266. });
  1267. });
  1268. }
  1269. function initWebhookSettings() {
  1270. $(".events.checkbox input").change(function() {
  1271. if ($(this).is(":checked")) {
  1272. $(".events.fields").show();
  1273. }
  1274. });
  1275. $(".non-events.checkbox input").change(function() {
  1276. if ($(this).is(":checked")) {
  1277. $(".events.fields").hide();
  1278. }
  1279. });
  1280. // Highlight payload on first click
  1281. $(".hook.history.list .toggle.button").click(function() {
  1282. $($(this).data("target") + " .nohighlight").each(function() {
  1283. var $this = $(this);
  1284. $this.removeClass("nohighlight");
  1285. setTimeout(function() {
  1286. hljs.highlightBlock($this[0]);
  1287. }, 500);
  1288. });
  1289. });
  1290. // Trigger delivery
  1291. $(".delivery.button, .redelivery.button").click(function() {
  1292. var $this = $(this);
  1293. $this.addClass("loading disabled");
  1294. $.post($this.data("link"), {
  1295. _csrf: csrf
  1296. }).done(
  1297. setTimeout(function() {
  1298. window.location.href = $this.data("redirect");
  1299. }, 5000)
  1300. );
  1301. });
  1302. }
  1303. $(document).ready(function() {
  1304. csrf = $("meta[name=_csrf]").attr("content");
  1305. suburl = $("meta[name=_suburl]").attr("content");
  1306. // Set cursor to the end of autofocus input string
  1307. $("input[autofocus]").each(function() {
  1308. $(this).val($(this).val());
  1309. });
  1310. // Show exact time
  1311. $(".time-since").each(function() {
  1312. $(this)
  1313. .addClass("poping up")
  1314. .attr("data-content", $(this).attr("title"))
  1315. .attr("data-variation", "inverted tiny")
  1316. .attr("title", "");
  1317. });
  1318. // Semantic UI modules.
  1319. $(".ui.dropdown").dropdown({
  1320. forceSelection: false
  1321. });
  1322. $(".jump.dropdown").dropdown({
  1323. action: "select",
  1324. onShow: function() {
  1325. $(".poping.up").popup("hide");
  1326. }
  1327. });
  1328. $(".slide.up.dropdown").dropdown({
  1329. transition: "slide up"
  1330. });
  1331. $(".upward.dropdown").dropdown({
  1332. direction: "upward"
  1333. });
  1334. $(".ui.accordion").accordion();
  1335. $(".ui.checkbox").checkbox();
  1336. $(".ui.progress").progress({
  1337. showActivity: false
  1338. });
  1339. $(".poping.up").popup();
  1340. $(".top.menu .poping.up").popup({
  1341. onShow: function() {
  1342. if ($(".top.menu .menu.transition").hasClass("visible")) {
  1343. return false;
  1344. }
  1345. }
  1346. });
  1347. $(".tabular.menu .item").tab();
  1348. $(".tabable.menu .item").tab();
  1349. $(".toggle.button").click(function() {
  1350. $($(this).data("target")).slideToggle(100);
  1351. });
  1352. // Dropzone
  1353. var $dropzone = $("#dropzone");
  1354. if ($dropzone.length > 0) {
  1355. var filenameDict = {};
  1356. $dropzone.dropzone({
  1357. url: $dropzone.data("upload-url"),
  1358. headers: { "X-CSRF-Token": csrf },
  1359. maxFiles: $dropzone.data("max-file"),
  1360. maxFilesize: $dropzone.data("max-size"),
  1361. acceptedFiles:
  1362. $dropzone.data("accepts") === "*/*" ? null : $dropzone.data("accepts"),
  1363. addRemoveLinks: true,
  1364. dictDefaultMessage: $dropzone.data("default-message"),
  1365. dictInvalidFileType: $dropzone.data("invalid-input-type"),
  1366. dictFileTooBig: $dropzone.data("file-too-big"),
  1367. dictRemoveFile: $dropzone.data("remove-file"),
  1368. init: function() {
  1369. this.on("success", function(file, data) {
  1370. filenameDict[file.name] = data.uuid;
  1371. var input = $(
  1372. '<input id="' + data.uuid + '" name="files" type="hidden">'
  1373. ).val(data.uuid);
  1374. $(".files").append(input);
  1375. });
  1376. this.on("removedfile", function(file) {
  1377. if (file.name in filenameDict) {
  1378. $("#" + filenameDict[file.name]).remove();
  1379. }
  1380. if ($dropzone.data("remove-url") && $dropzone.data("csrf")) {
  1381. $.post($dropzone.data("remove-url"), {
  1382. file: filenameDict[file.name],
  1383. _csrf: $dropzone.data("csrf")
  1384. });
  1385. }
  1386. });
  1387. }
  1388. });
  1389. }
  1390. // Emojify
  1391. emojify.setConfig({
  1392. img_dir: suburl + "/img/emoji",
  1393. ignore_emoticons: true
  1394. });
  1395. var hasEmoji = document.getElementsByClassName("has-emoji");
  1396. for (var i = 0; i < hasEmoji.length; i++) {
  1397. emojify.run(hasEmoji[i]);
  1398. }
  1399. // Clipboard JS
  1400. var clipboard = new ClipboardJS(".clipboard");
  1401. clipboard.on("success", function(e) {
  1402. e.clearSelection();
  1403. $("#" + e.trigger.getAttribute("id")).popup("destroy");
  1404. e.trigger.setAttribute(
  1405. "data-content",
  1406. e.trigger.getAttribute("data-success")
  1407. );
  1408. $("#" + e.trigger.getAttribute("id")).popup("show");
  1409. e.trigger.setAttribute(
  1410. "data-content",
  1411. e.trigger.getAttribute("data-original")
  1412. );
  1413. });
  1414. clipboard.on("error", function(e) {
  1415. $("#" + e.trigger.getAttribute("id")).popup("destroy");
  1416. e.trigger.setAttribute(
  1417. "data-content",
  1418. e.trigger.getAttribute("data-error")
  1419. );
  1420. $("#" + e.trigger.getAttribute("id")).popup("show");
  1421. e.trigger.setAttribute(
  1422. "data-content",
  1423. e.trigger.getAttribute("data-original")
  1424. );
  1425. });
  1426. // Autosize
  1427. if ($("#description.autosize").length > 0) {
  1428. autosize($("#description"));
  1429. showMessageMaxLength(512, "description", "descLength");
  1430. }
  1431. // AJAX load buttons
  1432. $(".ajax-load-button").click(function() {
  1433. var $this = $(this);
  1434. $this.addClass("disabled");
  1435. $.ajax({
  1436. url: $this.data("url"),
  1437. headers: {
  1438. "X-AJAX": "true"
  1439. }
  1440. }).done(function(data, status, request) {
  1441. $(data).insertBefore($this);
  1442. // Update new URL or remove self if no more feeds
  1443. var url = request.getResponseHeader("X-AJAX-URL");
  1444. if (url) {
  1445. $this.data("url", url);
  1446. $this.removeClass("disabled");
  1447. } else {
  1448. $this.remove();
  1449. }
  1450. });
  1451. });
  1452. // Helpers
  1453. $(".delete-button").click(function() {
  1454. var $this = $(this);
  1455. $(".delete.modal")
  1456. .modal({
  1457. closable: false,
  1458. onApprove: function() {
  1459. if ($this.data("type") == "form") {
  1460. $($this.data("form")).submit();
  1461. return;
  1462. }
  1463. $.post($this.data("url"), {
  1464. _csrf: csrf,
  1465. id: $this.data("id")
  1466. }).done(function(data) {
  1467. window.location.href = data.redirect;
  1468. });
  1469. }
  1470. })
  1471. .modal("show");
  1472. return false;
  1473. });
  1474. $(".show-panel.button").click(function() {
  1475. $($(this).data("panel")).show();
  1476. });
  1477. $(".show-modal.button").click(function() {
  1478. $($(this).data("modal")).modal("show");
  1479. });
  1480. $(".delete-post.button").click(function() {
  1481. var $this = $(this);
  1482. $.post($this.data("request-url"), {
  1483. _csrf: csrf
  1484. }).done(function() {
  1485. window.location.href = $this.data("done-url");
  1486. });
  1487. });
  1488. // To make arbitrary form element to behave like a submit button
  1489. $(".submit-button").click(function() {
  1490. $($(this).data("form")).submit();
  1491. });
  1492. // Check or select on option to enable/disable target region
  1493. $(".enable-system").change(function() {
  1494. if (this.checked) {
  1495. $($(this).data("target")).removeClass("disabled");
  1496. } else {
  1497. $($(this).data("target")).addClass("disabled");
  1498. $($(this).data("uncheck")).prop("checked", false);
  1499. }
  1500. });
  1501. $(".enable-system-radio").change(function() {
  1502. $($(this).data("enable")).removeClass("disabled");
  1503. $($(this).data("disable")).addClass("disabled");
  1504. $($(this).data("uncheck")).prop("checked", false);
  1505. });
  1506. // Set anchor.
  1507. $(".markdown").each(function() {
  1508. var headers = {};
  1509. $(this)
  1510. .find("h1, h2, h3, h4, h5, h6")
  1511. .each(function() {
  1512. var node = $(this);
  1513. var val = encodeURIComponent(
  1514. node
  1515. .text()
  1516. .toLowerCase()
  1517. .replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, "")
  1518. .replace(/[ ]/g, "-")
  1519. );
  1520. var name = val;
  1521. if (headers[val] > 0) {
  1522. name = val + "-" + headers[val];
  1523. }
  1524. if (headers[val] == undefined) {
  1525. headers[val] = 1;
  1526. } else {
  1527. headers[val] += 1;
  1528. }
  1529. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1530. node.append(
  1531. '<a class="anchor" href="#' +
  1532. name +
  1533. '"><span class="octicon octicon-link"></span></a>'
  1534. );
  1535. });
  1536. });
  1537. buttonsClickOnEnter();
  1538. searchUsers();
  1539. searchRepositories();
  1540. initCommentForm();
  1541. initRepository();
  1542. initWikiForm();
  1543. initEditForm();
  1544. initEditor();
  1545. initOrganization();
  1546. initAdmin();
  1547. initCodeView();
  1548. // Repo clone url.
  1549. if ($("#repo-clone-url").length > 0) {
  1550. switch (localStorage.getItem("repo-clone-protocol")) {
  1551. case "ssh":
  1552. if ($("#repo-clone-ssh").click().length === 0) {
  1553. $("#repo-clone-https").click();
  1554. }
  1555. break;
  1556. default:
  1557. $("#repo-clone-https").click();
  1558. break;
  1559. }
  1560. }
  1561. var routes = {
  1562. "div.user.settings": initUserSettings,
  1563. "div.repository.settings.collaboration": initRepositoryCollaboration,
  1564. "div.webhook.settings": initWebhookSettings
  1565. };
  1566. var selector;
  1567. for (selector in routes) {
  1568. if ($(selector).length > 0) {
  1569. routes[selector]();
  1570. break;
  1571. }
  1572. }
  1573. });
  1574. function changeHash(hash) {
  1575. if (history.pushState) {
  1576. history.pushState(null, null, hash);
  1577. } else {
  1578. location.hash = hash;
  1579. }
  1580. }
  1581. function deSelect() {
  1582. if (window.getSelection) {
  1583. window.getSelection().removeAllRanges();
  1584. } else {
  1585. document.selection.empty();
  1586. }
  1587. }
  1588. function selectRange($list, $select, $from) {
  1589. $list.removeClass("active");
  1590. if ($from) {
  1591. var a = parseInt($select.attr("rel").substr(1));
  1592. var b = parseInt($from.attr("rel").substr(1));
  1593. var c;
  1594. if (a != b) {
  1595. if (a > b) {
  1596. c = a;
  1597. a = b;
  1598. b = c;
  1599. }
  1600. var classes = [];
  1601. for (var i = a; i <= b; i++) {
  1602. classes.push(".L" + i);
  1603. }
  1604. $list.filter(classes.join(",")).addClass("active");
  1605. changeHash("#L" + a + "-" + "L" + b);
  1606. return;
  1607. }
  1608. }
  1609. $select.addClass("active");
  1610. changeHash("#" + $select.attr("rel"));
  1611. }
  1612. $(function() {
  1613. if ($(".user.signin").length > 0) return;
  1614. $("form").areYouSure();
  1615. });
  1616. // getByteLen counts bytes in a string's UTF-8 representation.
  1617. function getByteLen(normalVal) {
  1618. // Force string type
  1619. normalVal = String(normalVal);
  1620. var byteLen = 0;
  1621. for (var i = 0; i < normalVal.length; i++) {
  1622. var c = normalVal.charCodeAt(i);
  1623. byteLen +=
  1624. c < 1 << 7
  1625. ? 1
  1626. : c < 1 << 11
  1627. ? 2
  1628. : c < 1 << 16
  1629. ? 3
  1630. : c < 1 << 21
  1631. ? 4
  1632. : c < 1 << 26
  1633. ? 5
  1634. : c < 1 << 31
  1635. ? 6
  1636. : Number.NaN;
  1637. }
  1638. return byteLen;
  1639. }
  1640. function showMessageMaxLength(maxLen, textElemId, counterId) {
  1641. var $msg = $("#" + textElemId);
  1642. $("#" + counterId).html(maxLen - getByteLen($msg.val()));
  1643. var onMessageKey = function(e) {
  1644. var $msg = $(this);
  1645. var text = $msg.val();
  1646. var len = getByteLen(text);
  1647. var remainder = maxLen - len;
  1648. if (len >= maxLen) {
  1649. $msg.val($msg.val().substr(0, maxLen));
  1650. remainder = 0;
  1651. }
  1652. $("#" + counterId).html(remainder);
  1653. };
  1654. $msg.keyup(onMessageKey).keydown(onMessageKey);
  1655. }