gogs.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  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. $("#repo-clone-gin").removeClass("blue"); // Custom GIN code
  566. localStorage.setItem("repo-clone-protocol", "ssh");
  567. });
  568. $("#repo-clone-https").click(function() {
  569. $(".clone-url").text($(this).data("link"));
  570. $("#repo-clone-url").val($(this).data("link"));
  571. $(this).addClass("blue");
  572. $("#repo-clone-ssh").removeClass("blue");
  573. $("#repo-clone-gin").removeClass("blue"); // Custom GIN code
  574. localStorage.setItem("repo-clone-protocol", "https");
  575. });
  576. // Custom GIN code: #repo-clone-gin
  577. $("#repo-clone-gin").click(function () {
  578. $(".clone-url").text($(this).data("link"));
  579. $("#repo-clone-url").val($(this).data("link"));
  580. $(this).addClass("blue");
  581. $("#repo-clone-ssh").removeClass("blue");
  582. $("#repo-clone-https").removeClass("blue");
  583. localStorage.setItem("repo-clone-protocol", "gin");
  584. });
  585. $("#repo-clone-url").click(function() {
  586. $(this).select();
  587. });
  588. // Pull request
  589. if ($(".repository.compare.pull").length > 0) {
  590. initFilterSearchDropdown(".choose.branch .dropdown");
  591. }
  592. if ($(".repository.view.pull").length > 0) {
  593. $(".comment.merge.box input[name=merge_style]").change(function() {
  594. if ($(this).val() === "create_merge_commit") {
  595. $(".commit.description.field").show();
  596. } else {
  597. $(".commit.description.field").hide();
  598. }
  599. });
  600. }
  601. // Custom GIN code: #download-repo-button
  602. if ($("#download-repo-button")) {
  603. $("#download-repo-button").click(function () {
  604. $("#download_modal")
  605. .modal("show");
  606. })
  607. }
  608. }
  609. function initWikiForm() {
  610. var $editArea = $(".repository.wiki textarea#edit_area");
  611. if ($editArea.length > 0) {
  612. new SimpleMDE({
  613. autoDownloadFontAwesome: false,
  614. element: $editArea[0],
  615. forceSync: true,
  616. previewRender: function(plainText, preview) {
  617. // Async method
  618. setTimeout(function() {
  619. // FIXME: still send render request when return back to edit mode
  620. $.post(
  621. $editArea.data("url"),
  622. {
  623. _csrf: csrf,
  624. mode: "gfm",
  625. context: $editArea.data("context"),
  626. text: plainText
  627. },
  628. function(data) {
  629. preview.innerHTML = '<div class="markdown">' + data + "</div>";
  630. emojify.run($(".editor-preview")[0]);
  631. }
  632. );
  633. }, 0);
  634. return "Loading...";
  635. },
  636. renderingConfig: {
  637. singleLineBreaks: false
  638. },
  639. indentWithTabs: false,
  640. tabSize: 4,
  641. spellChecker: false,
  642. toolbar: [
  643. "bold",
  644. "italic",
  645. "strikethrough",
  646. "|",
  647. "heading-1",
  648. "heading-2",
  649. "heading-3",
  650. "heading-bigger",
  651. "heading-smaller",
  652. "|",
  653. "code",
  654. "quote",
  655. "|",
  656. "unordered-list",
  657. "ordered-list",
  658. "|",
  659. "link",
  660. "image",
  661. "table",
  662. "horizontal-rule",
  663. "|",
  664. "clean-block",
  665. "preview",
  666. "fullscreen"
  667. ]
  668. });
  669. }
  670. }
  671. var simpleMDEditor;
  672. var codeMirrorEditor;
  673. // For IE
  674. String.prototype.endsWith = function(pattern) {
  675. var d = this.length - pattern.length;
  676. return d >= 0 && this.lastIndexOf(pattern) === d;
  677. };
  678. // Adding function to get the cursor position in a text field to jQuery object.
  679. (function($, undefined) {
  680. $.fn.getCursorPosition = function() {
  681. var el = $(this).get(0);
  682. var pos = 0;
  683. if ("selectionStart" in el) {
  684. pos = el.selectionStart;
  685. } else if ("selection" in document) {
  686. el.focus();
  687. var Sel = document.selection.createRange();
  688. var SelLength = document.selection.createRange().text.length;
  689. Sel.moveStart("character", -el.value.length);
  690. pos = Sel.text.length - SelLength;
  691. }
  692. return pos;
  693. };
  694. })(jQuery);
  695. function setSimpleMDE($editArea) {
  696. if (codeMirrorEditor) {
  697. codeMirrorEditor.toTextArea();
  698. codeMirrorEditor = null;
  699. }
  700. if (simpleMDEditor) {
  701. return true;
  702. }
  703. simpleMDEditor = new SimpleMDE({
  704. autoDownloadFontAwesome: false,
  705. element: $editArea[0],
  706. forceSync: true,
  707. renderingConfig: {
  708. singleLineBreaks: false
  709. },
  710. indentWithTabs: false,
  711. tabSize: 4,
  712. spellChecker: false,
  713. previewRender: function(plainText, preview) {
  714. // Async method
  715. setTimeout(function() {
  716. // FIXME: still send render request when return back to edit mode
  717. $.post(
  718. $editArea.data("url"),
  719. {
  720. _csrf: csrf,
  721. mode: "gfm",
  722. context: $editArea.data("context"),
  723. text: plainText
  724. },
  725. function(data) {
  726. preview.innerHTML = '<div class="markdown">' + data + "</div>";
  727. emojify.run($(".editor-preview")[0]);
  728. }
  729. );
  730. }, 0);
  731. return "Loading...";
  732. },
  733. toolbar: [
  734. "bold",
  735. "italic",
  736. "strikethrough",
  737. "|",
  738. "heading-1",
  739. "heading-2",
  740. "heading-3",
  741. "heading-bigger",
  742. "heading-smaller",
  743. "|",
  744. "code",
  745. "quote",
  746. "|",
  747. "unordered-list",
  748. "ordered-list",
  749. "|",
  750. "link",
  751. "image",
  752. "table",
  753. "horizontal-rule",
  754. "|",
  755. "clean-block"
  756. ]
  757. });
  758. return true;
  759. }
  760. function setCodeMirror($editArea) {
  761. if (simpleMDEditor) {
  762. simpleMDEditor.toTextArea();
  763. simpleMDEditor = null;
  764. }
  765. if (codeMirrorEditor) {
  766. return true;
  767. }
  768. codeMirrorEditor = CodeMirror.fromTextArea($editArea[0], {
  769. lineNumbers: true
  770. });
  771. codeMirrorEditor.on("change", function(cm, change) {
  772. $editArea.val(cm.getValue());
  773. });
  774. // GIN specific code: codeMirrorEditor.on("keyup"...)
  775. codeMirrorEditor.on("keyup", function (cm, change) {
  776. if (typeof (jsonEditor) !== "undefined") {
  777. try {
  778. jsonEditor.set(JSON.parse(cm.getValue()));
  779. document.getElementById("parseerr").innerHTML = "Valid JSON";
  780. document.getElementById("parseerr").className = "ui positive message"
  781. }
  782. catch (err) {
  783. document.getElementById("parseerr").innerHTML = err.message;
  784. document.getElementById("parseerr").className = "ui negative message"
  785. }
  786. }
  787. if (typeof (yamleditor) !== "undefined") {
  788. try {
  789. yamleditor.set(YAML.parse(cm.getValue()));
  790. document.getElementById("parseerr").innerHTML = "Valid YAML";
  791. document.getElementById("parseerr").className = "ui positive message"
  792. }
  793. catch (err) {
  794. document.getElementById("parseerr").innerHTML = err.message;
  795. document.getElementById("parseerr").className = "ui negative message"
  796. }
  797. }
  798. if (typeof (Xonomy) !== "undefined") {
  799. OdmlEditor();
  800. }
  801. });
  802. return true;
  803. }
  804. function initEditor() {
  805. $(".js-quick-pull-choice-option").change(function() {
  806. if ($(this).val() == "commit-to-new-branch") {
  807. $(".quick-pull-branch-name").show();
  808. $(".quick-pull-branch-name input").prop("required", true);
  809. } else {
  810. $(".quick-pull-branch-name").hide();
  811. $(".quick-pull-branch-name input").prop("required", false);
  812. }
  813. });
  814. var $editFilename = $("#file-name");
  815. $editFilename
  816. .keyup(function(e) {
  817. var $section = $(".breadcrumb span.section");
  818. var $divider = $(".breadcrumb div.divider");
  819. if (e.keyCode == 8) {
  820. if ($(this).getCursorPosition() == 0) {
  821. if ($section.length > 0) {
  822. var value = $section
  823. .last()
  824. .find("a")
  825. .text();
  826. $(this).val(value + $(this).val());
  827. $(this)[0].setSelectionRange(value.length, value.length);
  828. $section.last().remove();
  829. $divider.last().remove();
  830. }
  831. }
  832. }
  833. if (e.keyCode == 191) {
  834. var parts = $(this)
  835. .val()
  836. .split("/");
  837. for (var i = 0; i < parts.length; ++i) {
  838. var value = parts[i];
  839. if (i < parts.length - 1) {
  840. if (value.length) {
  841. $(
  842. '<span class="section"><a href="#">' + value + "</a></span>"
  843. ).insertBefore($(this));
  844. $('<div class="divider"> / </div>').insertBefore($(this));
  845. }
  846. } else {
  847. $(this).val(value);
  848. }
  849. $(this)[0].setSelectionRange(0, 0);
  850. }
  851. }
  852. var parts = [];
  853. $(".breadcrumb span.section").each(function(i, element) {
  854. element = $(element);
  855. if (element.find("a").length) {
  856. parts.push(element.find("a").text());
  857. } else {
  858. parts.push(element.text());
  859. }
  860. });
  861. if ($(this).val()) {
  862. parts.push($(this).val());
  863. }
  864. var tree_path = parts.join("/");
  865. $("#tree_path").val(tree_path);
  866. $("#preview-tab").data(
  867. "context",
  868. $("#preview-tab").data("root-context") +
  869. tree_path.substring(0, tree_path.lastIndexOf("/") + 1)
  870. );
  871. })
  872. .trigger("keyup");
  873. var $editArea = $(".repository.editor textarea#edit_area");
  874. if (!$editArea.length) return;
  875. var markdownFileExts = $editArea.data("markdown-file-exts").split(",");
  876. var lineWrapExtensions = $editArea.data("line-wrap-extensions").split(",");
  877. $editFilename
  878. .on("keyup", function(e) {
  879. var val = $editFilename.val(),
  880. m,
  881. mode,
  882. spec,
  883. extension,
  884. extWithDot,
  885. previewLink,
  886. dataUrl,
  887. apiCall;
  888. extension = extWithDot = "";
  889. if ((m = /.+\.([^.]+)$/.exec(val))) {
  890. extension = m[1].toLowerCase();
  891. extWithDot = "." + extension;
  892. }
  893. var info = CodeMirror.findModeByExtension(extension);
  894. previewLink = $("a[data-tab=preview]");
  895. if (info) {
  896. mode = info.mode;
  897. spec = info.mime;
  898. apiCall = mode;
  899. } else {
  900. apiCall = extension;
  901. }
  902. if (
  903. previewLink.length &&
  904. apiCall &&
  905. previewFileModes &&
  906. previewFileModes.length &&
  907. previewFileModes.indexOf(apiCall) >= 0
  908. ) {
  909. dataUrl = previewLink.data("url");
  910. previewLink.data("url", dataUrl.replace(/(.*)\/.*/i, "$1/" + mode));
  911. previewLink.show();
  912. } else {
  913. previewLink.hide();
  914. }
  915. // If this file is a Markdown extensions, we will load that editor and return
  916. if (markdownFileExts.indexOf(extWithDot) >= 0) {
  917. if (setSimpleMDE($editArea)) {
  918. return;
  919. }
  920. }
  921. // Else we are going to use CodeMirror
  922. if (!codeMirrorEditor && !setCodeMirror($editArea)) {
  923. return;
  924. }
  925. if (mode) {
  926. codeMirrorEditor.setOption("mode", spec);
  927. CodeMirror.autoLoadMode(codeMirrorEditor, mode);
  928. }
  929. if (lineWrapExtensions.indexOf(extWithDot) >= 0) {
  930. codeMirrorEditor.setOption("lineWrapping", true);
  931. } else {
  932. codeMirrorEditor.setOption("lineWrapping", false);
  933. }
  934. // get the filename without any folder
  935. var value = $editFilename.val();
  936. if (value.length === 0) {
  937. return;
  938. }
  939. value = value.split("/");
  940. value = value[value.length - 1];
  941. $.getJSON($editFilename.data("ec-url-prefix") + value, function(
  942. editorconfig
  943. ) {
  944. if (editorconfig.indent_style === "tab") {
  945. codeMirrorEditor.setOption("indentWithTabs", true);
  946. codeMirrorEditor.setOption("extraKeys", {});
  947. } else {
  948. codeMirrorEditor.setOption("indentWithTabs", false);
  949. // required because CodeMirror doesn't seems to use spaces correctly for {"indentWithTabs": false}:
  950. // - https://github.com/codemirror/CodeMirror/issues/988
  951. // - https://codemirror.net/doc/manual.html#keymaps
  952. codeMirrorEditor.setOption("extraKeys", {
  953. Tab: function(cm) {
  954. var spaces = Array(parseInt(cm.getOption("indentUnit")) + 1).join(
  955. " "
  956. );
  957. cm.replaceSelection(spaces);
  958. }
  959. });
  960. }
  961. codeMirrorEditor.setOption("indentUnit", editorconfig.indent_size || 4);
  962. codeMirrorEditor.setOption("tabSize", editorconfig.tab_width || 4);
  963. });
  964. })
  965. .trigger("keyup");
  966. }
  967. function initOrganization() {
  968. if ($(".organization").length == 0) {
  969. return;
  970. }
  971. // Options
  972. if ($(".organization.settings.options").length > 0) {
  973. $("#org_name").keyup(function() {
  974. var $prompt = $("#org-name-change-prompt");
  975. if (
  976. $(this)
  977. .val()
  978. .toString()
  979. .toLowerCase() !=
  980. $(this)
  981. .data("org-name")
  982. .toString()
  983. .toLowerCase()
  984. ) {
  985. $prompt.show();
  986. } else {
  987. $prompt.hide();
  988. }
  989. });
  990. }
  991. }
  992. function initAdmin() {
  993. if ($(".admin").length == 0) {
  994. return;
  995. }
  996. // New user
  997. if ($(".admin.new.user").length > 0 || $(".admin.edit.user").length > 0) {
  998. $("#login_type").change(function() {
  999. if (
  1000. $(this)
  1001. .val()
  1002. .substring(0, 1) == "0"
  1003. ) {
  1004. $("#login_name").removeAttr("required");
  1005. $(".non-local").hide();
  1006. $(".local").show();
  1007. $("#user_name").focus();
  1008. if ($(this).data("password") == "required") {
  1009. $("#password").attr("required", "required");
  1010. }
  1011. } else {
  1012. $("#login_name").attr("required", "required");
  1013. $(".non-local").show();
  1014. $(".local").hide();
  1015. $("#login_name").focus();
  1016. $("#password").removeAttr("required");
  1017. }
  1018. });
  1019. }
  1020. function onSecurityProtocolChange() {
  1021. if ($("#security_protocol").val() > 0) {
  1022. $(".has-tls").show();
  1023. } else {
  1024. $(".has-tls").hide();
  1025. }
  1026. }
  1027. // New authentication
  1028. if ($(".admin.new.authentication").length > 0) {
  1029. $("#auth_type").change(function() {
  1030. $(".ldap").hide();
  1031. $(".dldap").hide();
  1032. $(".smtp").hide();
  1033. $(".pam").hide();
  1034. $(".github").hide();
  1035. $(".has-tls").hide();
  1036. var authType = $(this).val();
  1037. switch (authType) {
  1038. case "2": // LDAP
  1039. $(".ldap").show();
  1040. break;
  1041. case "3": // SMTP
  1042. $(".smtp").show();
  1043. $(".has-tls").show();
  1044. break;
  1045. case "4": // PAM
  1046. $(".pam").show();
  1047. break;
  1048. case "5": // LDAP
  1049. $(".dldap").show();
  1050. break;
  1051. case "6": //GITHUB
  1052. $(".github").show();
  1053. break;
  1054. }
  1055. if (authType == "2" || authType == "5") {
  1056. onSecurityProtocolChange();
  1057. }
  1058. });
  1059. $("#security_protocol").change(onSecurityProtocolChange);
  1060. }
  1061. // Edit authentication
  1062. if ($(".admin.edit.authentication").length > 0) {
  1063. var authType = $("#auth_type").val();
  1064. if (authType == "2" || authType == "5") {
  1065. $("#security_protocol").change(onSecurityProtocolChange);
  1066. }
  1067. }
  1068. // Notice
  1069. if ($(".admin.notice")) {
  1070. var $detailModal = $("#detail-modal");
  1071. // Attach view detail modals
  1072. $(".view-detail").click(function() {
  1073. $detailModal.find(".content p").text($(this).data("content"));
  1074. $detailModal.modal("show");
  1075. return false;
  1076. });
  1077. // Select actions
  1078. var $checkboxes = $(".select.table .ui.checkbox");
  1079. $(".select.action").click(function() {
  1080. switch ($(this).data("action")) {
  1081. case "select-all":
  1082. $checkboxes.checkbox("check");
  1083. break;
  1084. case "deselect-all":
  1085. $checkboxes.checkbox("uncheck");
  1086. break;
  1087. case "inverse":
  1088. $checkboxes.checkbox("toggle");
  1089. break;
  1090. }
  1091. });
  1092. $("#delete-selection").click(function() {
  1093. var $this = $(this);
  1094. $this.addClass("loading disabled");
  1095. var ids = [];
  1096. $checkboxes.each(function() {
  1097. if ($(this).checkbox("is checked")) {
  1098. ids.push($(this).data("id"));
  1099. }
  1100. });
  1101. $.post($this.data("link"), {
  1102. _csrf: csrf,
  1103. ids: ids
  1104. }).done(function() {
  1105. window.location.href = $this.data("redirect");
  1106. });
  1107. });
  1108. }
  1109. }
  1110. function buttonsClickOnEnter() {
  1111. $(".ui.button").keypress(function(e) {
  1112. if (e.keyCode == 13 || e.keyCode == 32)
  1113. // enter key or space bar
  1114. $(this).click();
  1115. });
  1116. }
  1117. function hideWhenLostFocus(body, parent) {
  1118. $(document).click(function(e) {
  1119. var target = e.target;
  1120. if (
  1121. !$(target).is(body) &&
  1122. !$(target)
  1123. .parents()
  1124. .is(parent)
  1125. ) {
  1126. $(body).hide();
  1127. }
  1128. });
  1129. }
  1130. function searchUsers() {
  1131. if (!$("#search-user-box .results").length) {
  1132. return;
  1133. }
  1134. var $searchUserBox = $("#search-user-box");
  1135. var $results = $searchUserBox.find(".results");
  1136. $searchUserBox.keyup(function() {
  1137. var $this = $(this);
  1138. var keyword = $this.find("input").val();
  1139. if (keyword.length < 2) {
  1140. $results.hide();
  1141. return;
  1142. }
  1143. $.ajax({
  1144. url: suburl + "/api/v1/users/search?q=" + keyword,
  1145. dataType: "json",
  1146. success: function(response) {
  1147. var notEmpty = function(str) {
  1148. return str && str.length > 0;
  1149. };
  1150. $results.html("");
  1151. if (response.ok && response.data.length) {
  1152. var html = "";
  1153. $.each(response.data, function(i, item) {
  1154. html +=
  1155. '<div class="item"><img class="ui avatar image" src="' +
  1156. item.avatar_url +
  1157. '"><span class="username">' +
  1158. item.username +
  1159. "</span>";
  1160. if (notEmpty(item.full_name)) {
  1161. html += " (" + item.full_name + ")";
  1162. }
  1163. html += "</div>";
  1164. });
  1165. $results.html(html);
  1166. $this.find(".results .item").click(function() {
  1167. $this.find("input").val(
  1168. $(this)
  1169. .find(".username")
  1170. .text()
  1171. );
  1172. $results.hide();
  1173. });
  1174. $results.show();
  1175. } else {
  1176. $results.hide();
  1177. }
  1178. }
  1179. });
  1180. });
  1181. $searchUserBox.find("input").focus(function() {
  1182. $searchUserBox.keyup();
  1183. });
  1184. hideWhenLostFocus("#search-user-box .results", "#search-user-box");
  1185. }
  1186. // FIXME: merge common parts in two functions
  1187. function searchRepositories() {
  1188. if (!$("#search-repo-box .results").length) {
  1189. return;
  1190. }
  1191. var $searchRepoBox = $("#search-repo-box");
  1192. var $results = $searchRepoBox.find(".results");
  1193. $searchRepoBox.keyup(function() {
  1194. var $this = $(this);
  1195. var keyword = $this.find("input").val();
  1196. if (keyword.length < 2) {
  1197. $results.hide();
  1198. return;
  1199. }
  1200. $.ajax({
  1201. url:
  1202. suburl +
  1203. "/api/v1/repos/search?q=" +
  1204. keyword +
  1205. "&uid=" +
  1206. $searchRepoBox.data("uid"),
  1207. dataType: "json",
  1208. success: function(response) {
  1209. var notEmpty = function(str) {
  1210. return str && str.length > 0;
  1211. };
  1212. $results.html("");
  1213. if (response.ok && response.data.length) {
  1214. var html = "";
  1215. $.each(response.data, function(i, item) {
  1216. html +=
  1217. '<div class="item"><i class="octicon octicon-repo"></i> <span class="fullname">' +
  1218. item.full_name +
  1219. "</span></div>";
  1220. });
  1221. $results.html(html);
  1222. $this.find(".results .item").click(function() {
  1223. $this.find("input").val(
  1224. $(this)
  1225. .find(".fullname")
  1226. .text()
  1227. .split("/")[1]
  1228. );
  1229. $results.hide();
  1230. });
  1231. $results.show();
  1232. } else {
  1233. $results.hide();
  1234. }
  1235. }
  1236. });
  1237. });
  1238. $searchRepoBox.find("input").focus(function() {
  1239. $searchRepoBox.keyup();
  1240. });
  1241. hideWhenLostFocus("#search-repo-box .results", "#search-repo-box");
  1242. }
  1243. function initCodeView() {
  1244. if ($(".code-view .linenums").length > 0) {
  1245. $(document).on("click", ".lines-num span", function(e) {
  1246. var $select = $(this);
  1247. var $list = $select
  1248. .parent()
  1249. .siblings(".lines-code")
  1250. .find("ol.linenums > li");
  1251. selectRange(
  1252. $list,
  1253. $list.filter("[rel=" + $select.attr("id") + "]"),
  1254. e.shiftKey ? $list.filter(".active").eq(0) : null
  1255. );
  1256. deSelect();
  1257. });
  1258. $(window)
  1259. .on("hashchange", function(e) {
  1260. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1261. var $list = $(".code-view ol.linenums > li");
  1262. var $first;
  1263. if (m) {
  1264. $first = $list.filter("." + m[1]);
  1265. selectRange($list, $first, $list.filter("." + m[2]));
  1266. $("html, body").scrollTop($first.offset().top - 200);
  1267. return;
  1268. }
  1269. m = window.location.hash.match(/^#(L\d+)$/);
  1270. if (m) {
  1271. $first = $list.filter("." + m[1]);
  1272. selectRange($list, $first);
  1273. $("html, body").scrollTop($first.offset().top - 200);
  1274. }
  1275. })
  1276. .trigger("hashchange");
  1277. }
  1278. }
  1279. // GIN specific code
  1280. function initCookieWarn() {
  1281. // Automatically shows on init if cookie isnt set
  1282. if (Cookies.get("cookieok") === undefined) {
  1283. $(".cookie.nag")
  1284. .nag({
  1285. key: "accepts-cookies",
  1286. value: true
  1287. });
  1288. $(".nag.close").click(function () {
  1289. Cookies.set("cookieok", true, {expires: 365})
  1290. })
  1291. }
  1292. }
  1293. // GIN specific code
  1294. function initSuggest() {
  1295. $(".ui.ginsearch")
  1296. .search({
  1297. apiSettings: {
  1298. url: "/explore/_suggest/{query}"
  1299. },
  1300. fields: {
  1301. results: "Items",
  1302. title: "Keyword",
  1303. },
  1304. minCharacters: 3,
  1305. showNoResults: false
  1306. });
  1307. }
  1308. function initUserSettings() {
  1309. console.log("initUserSettings");
  1310. // Options
  1311. if ($(".user.settings.profile").length > 0) {
  1312. $("#username").keyup(function() {
  1313. var $prompt = $("#name-change-prompt");
  1314. if (
  1315. $(this)
  1316. .val()
  1317. .toString()
  1318. .toLowerCase() !=
  1319. $(this)
  1320. .data("name")
  1321. .toString()
  1322. .toLowerCase()
  1323. ) {
  1324. $prompt.show();
  1325. } else {
  1326. $prompt.hide();
  1327. }
  1328. });
  1329. }
  1330. }
  1331. function initRepositoryCollaboration() {
  1332. console.log("initRepositoryCollaboration");
  1333. // Change collaborator access mode
  1334. $(".access-mode.menu .item").click(function() {
  1335. var $menu = $(this).parent();
  1336. $.post($menu.data("url"), {
  1337. _csrf: csrf,
  1338. uid: $menu.data("uid"),
  1339. mode: $(this).data("value")
  1340. });
  1341. });
  1342. }
  1343. function initWebhookSettings() {
  1344. $(".events.checkbox input").change(function() {
  1345. if ($(this).is(":checked")) {
  1346. $(".events.fields").show();
  1347. }
  1348. });
  1349. $(".non-events.checkbox input").change(function() {
  1350. if ($(this).is(":checked")) {
  1351. $(".events.fields").hide();
  1352. }
  1353. });
  1354. // Highlight payload on first click
  1355. $(".hook.history.list .toggle.button").click(function() {
  1356. $($(this).data("target") + " .nohighlight").each(function() {
  1357. var $this = $(this);
  1358. $this.removeClass("nohighlight");
  1359. setTimeout(function() {
  1360. hljs.highlightBlock($this[0]);
  1361. }, 500);
  1362. });
  1363. });
  1364. // Trigger delivery
  1365. $(".delivery.button, .redelivery.button").click(function() {
  1366. var $this = $(this);
  1367. $this.addClass("loading disabled");
  1368. $.post($this.data("link"), {
  1369. _csrf: csrf
  1370. }).done(
  1371. setTimeout(function() {
  1372. window.location.href = $this.data("redirect");
  1373. }, 5000)
  1374. );
  1375. });
  1376. }
  1377. $(document).ready(function() {
  1378. csrf = $("meta[name=_csrf]").attr("content");
  1379. suburl = $("meta[name=_suburl]").attr("content");
  1380. // Set cursor to the end of autofocus input string
  1381. $("input[autofocus]").each(function() {
  1382. $(this).val($(this).val());
  1383. });
  1384. // Show exact time
  1385. $(".time-since").each(function() {
  1386. $(this)
  1387. .addClass("poping up")
  1388. .attr("data-content", $(this).attr("title"))
  1389. .attr("data-variation", "inverted tiny")
  1390. .attr("title", "");
  1391. });
  1392. // Semantic UI modules.
  1393. $(".ui.dropdown").dropdown({
  1394. forceSelection: false
  1395. });
  1396. $(".jump.dropdown").dropdown({
  1397. action: "select",
  1398. onShow: function() {
  1399. $(".poping.up").popup("hide");
  1400. }
  1401. });
  1402. $(".slide.up.dropdown").dropdown({
  1403. transition: "slide up"
  1404. });
  1405. $(".upward.dropdown").dropdown({
  1406. direction: "upward"
  1407. });
  1408. $(".ui.accordion").accordion();
  1409. $(".ui.checkbox").checkbox();
  1410. $(".ui.progress").progress({
  1411. showActivity: false
  1412. });
  1413. $(".poping.up").popup();
  1414. $(".top.menu .poping.up").popup({
  1415. onShow: function() {
  1416. if ($(".top.menu .menu.transition").hasClass("visible")) {
  1417. return false;
  1418. }
  1419. }
  1420. });
  1421. $(".tabular.menu .item").tab();
  1422. $(".tabable.menu .item").tab();
  1423. $(".toggle.button").click(function() {
  1424. $($(this).data("target")).slideToggle(100);
  1425. });
  1426. // Dropzone
  1427. var $dropzone = $("#dropzone");
  1428. if ($dropzone.length > 0) {
  1429. var filenameDict = {};
  1430. $dropzone.dropzone({
  1431. url: $dropzone.data("upload-url"),
  1432. headers: { "X-CSRF-Token": csrf },
  1433. maxFiles: $dropzone.data("max-file"),
  1434. maxFilesize: $dropzone.data("max-size"),
  1435. acceptedFiles:
  1436. $dropzone.data("accepts") === "*/*" ? null : $dropzone.data("accepts"),
  1437. addRemoveLinks: true,
  1438. dictDefaultMessage: $dropzone.data("default-message"),
  1439. dictInvalidFileType: $dropzone.data("invalid-input-type"),
  1440. dictFileTooBig: $dropzone.data("file-too-big"),
  1441. dictRemoveFile: $dropzone.data("remove-file"),
  1442. init: function() {
  1443. this.on("success", function(file, data) {
  1444. filenameDict[file.name] = data.uuid;
  1445. var input = $(
  1446. '<input id="' + data.uuid + '" name="files" type="hidden">'
  1447. ).val(data.uuid);
  1448. $(".files").append(input);
  1449. });
  1450. this.on("removedfile", function(file) {
  1451. if (file.name in filenameDict) {
  1452. $("#" + filenameDict[file.name]).remove();
  1453. }
  1454. if ($dropzone.data("remove-url") && $dropzone.data("csrf")) {
  1455. $.post($dropzone.data("remove-url"), {
  1456. file: filenameDict[file.name],
  1457. _csrf: $dropzone.data("csrf")
  1458. });
  1459. }
  1460. });
  1461. // GIN specific code: this.on("sending", ...)
  1462. // Used for directory tree structure upload; tree would be flattened otherwise
  1463. this.on("sending", function (file, xhr, formData) {
  1464. formData.append("full_path", file.fullPath);
  1465. });
  1466. // GIN specific code: this.on("addedfile", ...)
  1467. // Used for file display in directory trees
  1468. this.on("addedfile", function (file) {
  1469. var preview = document.getElementsByClassName("dz-filename");
  1470. preview = preview[preview.length - 1];
  1471. preview.removeChild(preview.firstChild);
  1472. var imageName = document.createElement("span");
  1473. imageName.innerHTML = file.name;
  1474. if (file.fullPath !== undefined) {
  1475. imageName.innerHTML = file.fullPath;
  1476. }
  1477. preview.insertBefore(imageName, preview.firstChild);
  1478. });
  1479. }
  1480. });
  1481. }
  1482. // Emojify
  1483. emojify.setConfig({
  1484. img_dir: suburl + "/img/emoji",
  1485. ignore_emoticons: true
  1486. });
  1487. var hasEmoji = document.getElementsByClassName("has-emoji");
  1488. for (var i = 0; i < hasEmoji.length; i++) {
  1489. emojify.run(hasEmoji[i]);
  1490. }
  1491. // Clipboard JS
  1492. var clipboard = new ClipboardJS(".clipboard");
  1493. clipboard.on("success", function(e) {
  1494. e.clearSelection();
  1495. $("#" + e.trigger.getAttribute("id")).popup("destroy");
  1496. e.trigger.setAttribute(
  1497. "data-content",
  1498. e.trigger.getAttribute("data-success")
  1499. );
  1500. $("#" + e.trigger.getAttribute("id")).popup("show");
  1501. e.trigger.setAttribute(
  1502. "data-content",
  1503. e.trigger.getAttribute("data-original")
  1504. );
  1505. });
  1506. clipboard.on("error", function(e) {
  1507. $("#" + e.trigger.getAttribute("id")).popup("destroy");
  1508. e.trigger.setAttribute(
  1509. "data-content",
  1510. e.trigger.getAttribute("data-error")
  1511. );
  1512. $("#" + e.trigger.getAttribute("id")).popup("show");
  1513. e.trigger.setAttribute(
  1514. "data-content",
  1515. e.trigger.getAttribute("data-original")
  1516. );
  1517. });
  1518. // Autosize
  1519. if ($("#description.autosize").length > 0) {
  1520. autosize($("#description"));
  1521. showMessageMaxLength(512, "description", "descLength");
  1522. }
  1523. // AJAX load buttons
  1524. $(".ajax-load-button").click(function() {
  1525. var $this = $(this);
  1526. $this.addClass("disabled");
  1527. $.ajax({
  1528. url: $this.data("url"),
  1529. headers: {
  1530. "X-AJAX": "true"
  1531. }
  1532. }).done(function(data, status, request) {
  1533. $(data).insertBefore($this);
  1534. // Update new URL or remove self if no more feeds
  1535. var url = request.getResponseHeader("X-AJAX-URL");
  1536. if (url) {
  1537. $this.data("url", url);
  1538. $this.removeClass("disabled");
  1539. } else {
  1540. $this.remove();
  1541. }
  1542. });
  1543. });
  1544. // Helpers
  1545. $(".delete-button").click(function() {
  1546. var $this = $(this);
  1547. $(".delete.modal")
  1548. .modal({
  1549. closable: false,
  1550. onApprove: function() {
  1551. if ($this.data("type") == "form") {
  1552. $($this.data("form")).submit();
  1553. return;
  1554. }
  1555. $.post($this.data("url"), {
  1556. _csrf: csrf,
  1557. id: $this.data("id")
  1558. }).done(function(data) {
  1559. window.location.href = data.redirect;
  1560. });
  1561. }
  1562. })
  1563. .modal("show");
  1564. return false;
  1565. });
  1566. $(".show-panel.button").click(function() {
  1567. $($(this).data("panel")).show();
  1568. });
  1569. $(".show-modal.button").click(function() {
  1570. $($(this).data("modal")).modal("show");
  1571. });
  1572. $(".delete-post.button").click(function() {
  1573. var $this = $(this);
  1574. $.post($this.data("request-url"), {
  1575. _csrf: csrf
  1576. }).done(function() {
  1577. window.location.href = $this.data("done-url");
  1578. });
  1579. });
  1580. // To make arbitrary form element to behave like a submit button
  1581. $(".submit-button").click(function() {
  1582. $($(this).data("form")).submit();
  1583. });
  1584. // Check or select on option to enable/disable target region
  1585. $(".enable-system").change(function() {
  1586. if (this.checked) {
  1587. $($(this).data("target")).removeClass("disabled");
  1588. } else {
  1589. $($(this).data("target")).addClass("disabled");
  1590. $($(this).data("uncheck")).prop("checked", false);
  1591. }
  1592. });
  1593. $(".enable-system-radio").change(function() {
  1594. $($(this).data("enable")).removeClass("disabled");
  1595. $($(this).data("disable")).addClass("disabled");
  1596. $($(this).data("uncheck")).prop("checked", false);
  1597. });
  1598. // Set anchor.
  1599. $(".markdown").each(function() {
  1600. var headers = {};
  1601. $(this)
  1602. .find("h1, h2, h3, h4, h5, h6")
  1603. .each(function() {
  1604. var node = $(this);
  1605. var val = encodeURIComponent(
  1606. node
  1607. .text()
  1608. .toLowerCase()
  1609. .replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, "")
  1610. .replace(/[ ]/g, "-")
  1611. );
  1612. var name = val;
  1613. if (headers[val] > 0) {
  1614. name = val + "-" + headers[val];
  1615. }
  1616. if (headers[val] == undefined) {
  1617. headers[val] = 1;
  1618. } else {
  1619. headers[val] += 1;
  1620. }
  1621. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1622. node.append(
  1623. '<a class="anchor" href="#' +
  1624. name +
  1625. '"><span class="octicon octicon-link"></span></a>'
  1626. );
  1627. });
  1628. });
  1629. buttonsClickOnEnter();
  1630. searchUsers();
  1631. searchRepositories();
  1632. initCommentForm();
  1633. initRepository();
  1634. initWikiForm();
  1635. initEditForm();
  1636. initEditor();
  1637. initOrganization();
  1638. initAdmin();
  1639. initCodeView();
  1640. initCookieWarn(); // GIN specific code
  1641. initSuggest(); // GIN specific code
  1642. // Repo clone url.
  1643. if ($("#repo-clone-url").length > 0) {
  1644. switch (localStorage.getItem("repo-clone-protocol")) {
  1645. case "ssh":
  1646. if ($("#repo-clone-ssh").click().length === 0) {
  1647. $("#repo-clone-https").click();
  1648. }
  1649. break;
  1650. default:
  1651. $('#repo-clone-gin').click(); // GIN specific code; shows "gin get ..." as default repo clone command.
  1652. break;
  1653. }
  1654. }
  1655. var routes = {
  1656. "div.user.settings": initUserSettings,
  1657. "div.repository.settings.collaboration": initRepositoryCollaboration,
  1658. "div.webhook.settings": initWebhookSettings
  1659. };
  1660. var selector;
  1661. for (selector in routes) {
  1662. if ($(selector).length > 0) {
  1663. routes[selector]();
  1664. break;
  1665. }
  1666. }
  1667. });
  1668. function changeHash(hash) {
  1669. if (history.pushState) {
  1670. history.pushState(null, null, hash);
  1671. } else {
  1672. location.hash = hash;
  1673. }
  1674. }
  1675. function deSelect() {
  1676. if (window.getSelection) {
  1677. window.getSelection().removeAllRanges();
  1678. } else {
  1679. document.selection.empty();
  1680. }
  1681. }
  1682. function selectRange($list, $select, $from) {
  1683. $list.removeClass("active");
  1684. if ($from) {
  1685. var a = parseInt($select.attr("rel").substr(1));
  1686. var b = parseInt($from.attr("rel").substr(1));
  1687. var c;
  1688. if (a != b) {
  1689. if (a > b) {
  1690. c = a;
  1691. a = b;
  1692. b = c;
  1693. }
  1694. var classes = [];
  1695. for (var i = a; i <= b; i++) {
  1696. classes.push(".L" + i);
  1697. }
  1698. $list.filter(classes.join(",")).addClass("active");
  1699. changeHash("#L" + a + "-" + "L" + b);
  1700. return;
  1701. }
  1702. }
  1703. $select.addClass("active");
  1704. changeHash("#" + $select.attr("rel"));
  1705. }
  1706. $(function() {
  1707. if ($(".user.signin").length > 0) return;
  1708. $("form").areYouSure();
  1709. });
  1710. // getByteLen counts bytes in a string's UTF-8 representation.
  1711. function getByteLen(normalVal) {
  1712. // Force string type
  1713. normalVal = String(normalVal);
  1714. var byteLen = 0;
  1715. for (var i = 0; i < normalVal.length; i++) {
  1716. var c = normalVal.charCodeAt(i);
  1717. byteLen +=
  1718. c < 1 << 7
  1719. ? 1
  1720. : c < 1 << 11
  1721. ? 2
  1722. : c < 1 << 16
  1723. ? 3
  1724. : c < 1 << 21
  1725. ? 4
  1726. : c < 1 << 26
  1727. ? 5
  1728. : c < 1 << 31
  1729. ? 6
  1730. : Number.NaN;
  1731. }
  1732. return byteLen;
  1733. }
  1734. function showMessageMaxLength(maxLen, textElemId, counterId) {
  1735. var $msg = $("#" + textElemId);
  1736. $("#" + counterId).html(maxLen - getByteLen($msg.val()));
  1737. var onMessageKey = function(e) {
  1738. var $msg = $(this);
  1739. var text = $msg.val();
  1740. var len = getByteLen(text);
  1741. var remainder = maxLen - len;
  1742. if (len >= maxLen) {
  1743. $msg.val($msg.val().substr(0, maxLen));
  1744. remainder = 0;
  1745. }
  1746. $("#" + counterId).html(remainder);
  1747. };
  1748. $msg.keyup(onMessageKey).keydown(onMessageKey);
  1749. }
  1750. // GIN specific code
  1751. function OdmlEditor() {
  1752. var docSpec = {
  1753. allowModeSwitching: true,
  1754. elements: {
  1755. "odML": {
  1756. collapsible: true,
  1757. oneliner: false,
  1758. menu: [{
  1759. caption: "Add <section>",
  1760. action: Xonomy.newElementChild,
  1761. actionParameter: "<section/>"
  1762. }, {
  1763. caption: "Add <author>",
  1764. action: Xonomy.newElementChild,
  1765. actionParameter: "<author>...</author>"
  1766. }, {
  1767. caption: "Add <date>",
  1768. action: Xonomy.newElementChild,
  1769. actionParameter: "<date>...</date>"
  1770. }, {
  1771. caption: "Add <repository>",
  1772. action: Xonomy.newElementChild,
  1773. actionParameter: "<repository>...</repository>"
  1774. },
  1775. {
  1776. caption: "Add <version>",
  1777. action: Xonomy.newElementChild,
  1778. actionParameter: "<version>...</version>"
  1779. }]
  1780. },
  1781. "author": {
  1782. collapsible: false,
  1783. oneliner: true
  1784. },
  1785. "value": {
  1786. collapsible: false,
  1787. menu: [{
  1788. caption: "Delete this <value>",
  1789. action: Xonomy.deleteElement
  1790. }, {
  1791. caption: "Add <unit>",
  1792. action: Xonomy.newElementChild,
  1793. actionParameter: "<unit>...</unit>"
  1794. }]
  1795. },
  1796. "unit": {
  1797. collapsible: false
  1798. },
  1799. "name": {
  1800. collapsible: false
  1801. },
  1802. "property": {
  1803. oneliner: true,
  1804. collapsible: true,
  1805. collapsed: true,
  1806. collapsoid: function (jsElement) {
  1807. return jsElement.getChildElements("name")[0].getText() + ": " + jsElement.getChildElements("value")[0].getText();
  1808. },
  1809. menu: [{
  1810. caption: "Delete this <property>",
  1811. action: Xonomy.deleteElement
  1812. }, {
  1813. caption: "New <property> before this",
  1814. action: Xonomy.newElementBefore,
  1815. actionParameter: "<property><name>...</name><value>...</value></property>"
  1816. }, {
  1817. caption: "New <property> after this",
  1818. action: Xonomy.newElementAfter,
  1819. actionParameter: "<property><name>...</name><value>...</value></property>"
  1820. }, {
  1821. caption: "Add <uncertainty>",
  1822. action: Xonomy.newElementChild,
  1823. actionParameter: "<uncertainty>...</uncertainty>"
  1824. }, {
  1825. caption: "Add <type>",
  1826. action: Xonomy.newElementChild,
  1827. actionParameter: "<type>...</type>"
  1828. }, {
  1829. caption: "Add <reference>",
  1830. action: Xonomy.newElementChild,
  1831. actionParameter: "<reference>...</reference>"
  1832. },
  1833. {
  1834. caption: "Add <definition>",
  1835. action: Xonomy.newElementChild,
  1836. actionParameter: "<definition>...</definition>"
  1837. },
  1838. {
  1839. caption: "Add <dependency>",
  1840. action: Xonomy.newElementChild,
  1841. actionParameter: "<dependency>...</dependency>"
  1842. },
  1843. {
  1844. caption: "Add <value_origin>",
  1845. action: Xonomy.newElementChild,
  1846. actionParameter: "<value_origin>...</value_origin>"
  1847. }],
  1848. },
  1849. "section": {
  1850. oneliner: false,
  1851. collapsible: true,
  1852. collapsed: true,
  1853. collapsoid: function (jsElement) {
  1854. if (jsElement.hasChildElement("name")) {
  1855. return jsElement.getChildElements("name")[0].getText();
  1856. }
  1857. else {
  1858. return "..."
  1859. }
  1860. },
  1861. menu: [{
  1862. caption: "Delete this <section>",
  1863. action: Xonomy.deleteElement
  1864. }, {
  1865. caption: "New <section> before this",
  1866. action: Xonomy.newElementBefore,
  1867. actionParameter: "<section/>"
  1868. }, {
  1869. caption: "New <section> after this",
  1870. action: Xonomy.newElementAfter,
  1871. actionParameter: "<section/>"
  1872. }, {
  1873. caption: "Add <name>",
  1874. action: Xonomy.newElementChild,
  1875. actionParameter: "<name>...</name>"
  1876. }, {
  1877. caption: "Add <type>",
  1878. action: Xonomy.newElementChild,
  1879. actionParameter: "<type>...</type>"
  1880. }, {
  1881. caption: "Add <reference>",
  1882. action: Xonomy.newElementChild,
  1883. actionParameter: "<reference>...</reference>"
  1884. },
  1885. {
  1886. caption: "Add <definition>",
  1887. action: Xonomy.newElementChild,
  1888. actionParameter: "<definition>...</definition>"
  1889. },
  1890. {
  1891. caption: "Add <link>",
  1892. action: Xonomy.newElementChild,
  1893. actionParameter: "<link>...</link>"
  1894. },
  1895. {
  1896. caption: "Add <repository>",
  1897. action: Xonomy.newElementChild,
  1898. actionParameter: "<repository>...</link>"
  1899. },
  1900. {
  1901. caption: "Add <property>",
  1902. action: Xonomy.newElementChild,
  1903. actionParameter: "<property><name>...</name><value>...</value></property>"
  1904. }
  1905. ],
  1906. }
  1907. },
  1908. onchange: function () {
  1909. var ct = Xonomy.harvest();
  1910. ct = ct.replace(/ xml:space='preserve'/g, "");
  1911. $("#edit_area").val(ct);
  1912. codeMirrorEditor.setValue(ct);
  1913. }
  1914. }
  1915. var xml = $("#edit_area").val();
  1916. var editor = document.getElementById("xonomy_edit");
  1917. Xonomy.render(xml, editor, docSpec);
  1918. }