1
0

gogs.js 47 KB

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