gogs.js 49 KB

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