gogs.js 48 KB

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