gogs.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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 initWikiForm() {
  464. var $editArea = $('.repository.wiki textarea#edit_area');
  465. if ($editArea.length > 0) {
  466. new SimpleMDE({
  467. autoDownloadFontAwesome: false,
  468. element: $editArea[0],
  469. forceSync: true,
  470. previewRender: function (plainText, preview) { // Async method
  471. setTimeout(function () {
  472. // FIXME: still send render request when return back to edit mode
  473. $.post($editArea.data('url'), {
  474. "_csrf": csrf,
  475. "mode": "gfm",
  476. "context": $editArea.data('context'),
  477. "text": plainText
  478. },
  479. function (data) {
  480. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  481. emojify.run($('.editor-preview')[0]);
  482. }
  483. );
  484. }, 0);
  485. return "Loading...";
  486. },
  487. renderingConfig: {
  488. singleLineBreaks: false
  489. },
  490. indentWithTabs: false,
  491. tabSize: 4,
  492. spellChecker: false,
  493. toolbar: ["bold", "italic", "strikethrough", "|",
  494. "heading-1", "heading-2", "heading-3", "heading-bigger", "heading-smaller", "|",
  495. "code", "quote", "|",
  496. "unordered-list", "ordered-list", "|",
  497. "link", "image", "table", "horizontal-rule", "|",
  498. "clean-block", "preview", "fullscreen"]
  499. })
  500. }
  501. }
  502. var simpleMDEditor;
  503. var codeMirrorEditor;
  504. // For IE
  505. String.prototype.endsWith = function (pattern) {
  506. var d = this.length - pattern.length;
  507. return d >= 0 && this.lastIndexOf(pattern) === d;
  508. };
  509. // Adding function to get the cursor position in a text field to jQuery object.
  510. (function ($, undefined) {
  511. $.fn.getCursorPosition = function () {
  512. var el = $(this).get(0);
  513. var pos = 0;
  514. if ('selectionStart' in el) {
  515. pos = el.selectionStart;
  516. } else if ('selection' in document) {
  517. el.focus();
  518. var Sel = document.selection.createRange();
  519. var SelLength = document.selection.createRange().text.length;
  520. Sel.moveStart('character', -el.value.length);
  521. pos = Sel.text.length - SelLength;
  522. }
  523. return pos;
  524. }
  525. })(jQuery);
  526. function setSimpleMDE($editArea) {
  527. if (codeMirrorEditor) {
  528. codeMirrorEditor.toTextArea();
  529. codeMirrorEditor = null;
  530. }
  531. if (simpleMDEditor) {
  532. return true;
  533. }
  534. simpleMDEditor = new SimpleMDE({
  535. autoDownloadFontAwesome: false,
  536. element: $editArea[0],
  537. forceSync: true,
  538. renderingConfig: {
  539. singleLineBreaks: false
  540. },
  541. indentWithTabs: false,
  542. tabSize: 4,
  543. spellChecker: false,
  544. previewRender: function (plainText, preview) { // Async method
  545. setTimeout(function () {
  546. // FIXME: still send render request when return back to edit mode
  547. $.post($editArea.data('url'), {
  548. "_csrf": csrf,
  549. "mode": "gfm",
  550. "context": $editArea.data('context'),
  551. "text": plainText
  552. },
  553. function (data) {
  554. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  555. emojify.run($('.editor-preview')[0]);
  556. }
  557. );
  558. }, 0);
  559. return "Loading...";
  560. },
  561. toolbar: ["bold", "italic", "strikethrough", "|",
  562. "heading-1", "heading-2", "heading-3", "heading-bigger", "heading-smaller", "|",
  563. "code", "quote", "|",
  564. "unordered-list", "ordered-list", "|",
  565. "link", "image", "table", "horizontal-rule", "|",
  566. "clean-block", "preview", "fullscreen", "side-by-side"]
  567. });
  568. return true;
  569. }
  570. function setCodeMirror($editArea) {
  571. if (simpleMDEditor) {
  572. simpleMDEditor.toTextArea();
  573. simpleMDEditor = null;
  574. }
  575. if (codeMirrorEditor) {
  576. return true;
  577. }
  578. codeMirrorEditor = CodeMirror.fromTextArea($editArea[0], {
  579. lineNumbers: true
  580. });
  581. codeMirrorEditor.on("change", function (cm, change) {
  582. $editArea.val(cm.getValue());
  583. });
  584. return true;
  585. }
  586. function initEditor() {
  587. $('.js-quick-pull-choice-option').change(function () {
  588. if ($(this).val() == 'commit-to-new-branch') {
  589. $('.quick-pull-branch-name').show();
  590. $('.quick-pull-branch-name input').prop('required',true);
  591. } else {
  592. $('.quick-pull-branch-name').hide();
  593. $('.quick-pull-branch-name input').prop('required',false);
  594. }
  595. });
  596. var $editFilename = $("#file-name");
  597. $editFilename.keyup(function (e) {
  598. var $section = $('.breadcrumb span.section');
  599. var $divider = $('.breadcrumb div.divider');
  600. if (e.keyCode == 8) {
  601. if ($(this).getCursorPosition() == 0) {
  602. if ($section.length > 0) {
  603. var value = $section.last().find('a').text();
  604. $(this).val(value + $(this).val());
  605. $(this)[0].setSelectionRange(value.length, value.length);
  606. $section.last().remove();
  607. $divider.last().remove();
  608. }
  609. }
  610. }
  611. if (e.keyCode == 191) {
  612. var parts = $(this).val().split('/');
  613. for (var i = 0; i < parts.length; ++i) {
  614. var value = parts[i];
  615. if (i < parts.length - 1) {
  616. if (value.length) {
  617. $('<span class="section"><a href="#">' + value + '</a></span>').insertBefore($(this));
  618. $('<div class="divider"> / </div>').insertBefore($(this));
  619. }
  620. }
  621. else {
  622. $(this).val(value);
  623. }
  624. $(this)[0].setSelectionRange(0, 0);
  625. }
  626. }
  627. var parts = [];
  628. $('.breadcrumb span.section').each(function (i, element) {
  629. element = $(element);
  630. if (element.find('a').length) {
  631. parts.push(element.find('a').text());
  632. } else {
  633. parts.push(element.text());
  634. }
  635. });
  636. if ($(this).val())
  637. parts.push($(this).val());
  638. $('#tree_path').val(parts.join('/'));
  639. }).trigger('keyup');
  640. var $editArea = $('.repository.editor textarea#edit_area');
  641. if (!$editArea.length)
  642. return;
  643. var markdownFileExts = $editArea.data("markdown-file-exts").split(",");
  644. var lineWrapExtensions = $editArea.data("line-wrap-extensions").split(",");
  645. $editFilename.on("keyup", function (e) {
  646. var val = $editFilename.val(), m, mode, spec, extension, extWithDot, previewLink, dataUrl, apiCall;
  647. extension = extWithDot = "";
  648. if (m = /.+\.([^.]+)$/.exec(val)) {
  649. extension = m[1];
  650. extWithDot = "." + extension;
  651. }
  652. var info = CodeMirror.findModeByExtension(extension);
  653. previewLink = $('a[data-tab=preview]');
  654. if (info) {
  655. mode = info.mode;
  656. spec = info.mime;
  657. apiCall = mode;
  658. }
  659. else {
  660. apiCall = extension
  661. }
  662. if (previewLink.length && apiCall && previewFileModes && previewFileModes.length && previewFileModes.indexOf(apiCall) >= 0) {
  663. dataUrl = previewLink.data('url');
  664. previewLink.data('url', dataUrl.replace(/(.*)\/.*/i, '$1/' + mode));
  665. previewLink.show();
  666. }
  667. else {
  668. previewLink.hide();
  669. }
  670. // If this file is a Markdown extensions, we will load that editor and return
  671. if (markdownFileExts.indexOf(extWithDot) >= 0) {
  672. if (setSimpleMDE($editArea)) {
  673. return;
  674. }
  675. }
  676. // Else we are going to use CodeMirror
  677. if (!codeMirrorEditor && !setCodeMirror($editArea)) {
  678. return;
  679. }
  680. if (mode) {
  681. codeMirrorEditor.setOption("mode", spec);
  682. CodeMirror.autoLoadMode(codeMirrorEditor, mode);
  683. }
  684. if (lineWrapExtensions.indexOf(extWithDot) >= 0) {
  685. codeMirrorEditor.setOption("lineWrapping", true);
  686. }
  687. else {
  688. codeMirrorEditor.setOption("lineWrapping", false);
  689. }
  690. // get the filename without any folder
  691. var value = $editFilename.val();
  692. if (value.length === 0) {
  693. return;
  694. }
  695. value = value.split('/');
  696. value = value[value.length - 1];
  697. $.getJSON($editFilename.data('ec-url-prefix')+value, function(editorconfig) {
  698. if (editorconfig.indent_style === 'tab') {
  699. codeMirrorEditor.setOption("indentWithTabs", true);
  700. codeMirrorEditor.setOption('extraKeys', {});
  701. } else {
  702. codeMirrorEditor.setOption("indentWithTabs", false);
  703. // required because CodeMirror doesn't seems to use spaces correctly for {"indentWithTabs": false}:
  704. // - https://github.com/codemirror/CodeMirror/issues/988
  705. // - https://codemirror.net/doc/manual.html#keymaps
  706. codeMirrorEditor.setOption('extraKeys', {
  707. Tab: function(cm) {
  708. var spaces = Array(parseInt(cm.getOption("indentUnit")) + 1).join(" ");
  709. cm.replaceSelection(spaces);
  710. }
  711. });
  712. }
  713. codeMirrorEditor.setOption("indentUnit", editorconfig.indent_size || 4);
  714. codeMirrorEditor.setOption("tabSize", editorconfig.tab_width || 4);
  715. });
  716. }).trigger('keyup');
  717. }
  718. function initOrganization() {
  719. if ($('.organization').length == 0) {
  720. return;
  721. }
  722. // Options
  723. if ($('.organization.settings.options').length > 0) {
  724. $('#org_name').keyup(function () {
  725. var $prompt = $('#org-name-change-prompt');
  726. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  727. $prompt.show();
  728. } else {
  729. $prompt.hide();
  730. }
  731. });
  732. }
  733. }
  734. function initAdmin() {
  735. if ($('.admin').length == 0) {
  736. return;
  737. }
  738. // New user
  739. if ($('.admin.new.user').length > 0 ||
  740. $('.admin.edit.user').length > 0) {
  741. $('#login_type').change(function () {
  742. if ($(this).val().substring(0, 1) == '0') {
  743. $('#login_name').removeAttr('required');
  744. $('.non-local').hide();
  745. $('.local').show();
  746. $('#user_name').focus();
  747. if ($(this).data('password') == "required") {
  748. $('#password').attr('required', 'required');
  749. }
  750. } else {
  751. $('#login_name').attr('required', 'required');
  752. $('.non-local').show();
  753. $('.local').hide();
  754. $('#login_name').focus();
  755. $('#password').removeAttr('required');
  756. }
  757. });
  758. }
  759. function onSecurityProtocolChange() {
  760. if ($('#security_protocol').val() > 0) {
  761. $('.has-tls').show();
  762. } else {
  763. $('.has-tls').hide();
  764. }
  765. }
  766. // New authentication
  767. if ($('.admin.new.authentication').length > 0) {
  768. $('#auth_type').change(function () {
  769. $('.ldap').hide();
  770. $('.dldap').hide();
  771. $('.smtp').hide();
  772. $('.pam').hide();
  773. $('.has-tls').hide();
  774. var authType = $(this).val();
  775. switch (authType) {
  776. case '2': // LDAP
  777. $('.ldap').show();
  778. break;
  779. case '3': // SMTP
  780. $('.smtp').show();
  781. $('.has-tls').show();
  782. break;
  783. case '4': // PAM
  784. $('.pam').show();
  785. break;
  786. case '5': // LDAP
  787. $('.dldap').show();
  788. break;
  789. }
  790. if (authType == '2' || authType == '5') {
  791. onSecurityProtocolChange()
  792. }
  793. });
  794. $('#security_protocol').change(onSecurityProtocolChange)
  795. }
  796. // Edit authentication
  797. if ($('.admin.edit.authentication').length > 0) {
  798. var authType = $('#auth_type').val();
  799. if (authType == '2' || authType == '5') {
  800. $('#security_protocol').change(onSecurityProtocolChange);
  801. }
  802. }
  803. // Notice
  804. if ($('.admin.notice')) {
  805. var $detailModal = $('#detail-modal');
  806. // Attach view detail modals
  807. $('.view-detail').click(function () {
  808. $detailModal.find('.content p').text($(this).data('content'));
  809. $detailModal.modal('show');
  810. return false;
  811. });
  812. // Select actions
  813. var $checkboxes = $('.select.table .ui.checkbox');
  814. $('.select.action').click(function () {
  815. switch ($(this).data('action')) {
  816. case 'select-all':
  817. $checkboxes.checkbox('check');
  818. break;
  819. case 'deselect-all':
  820. $checkboxes.checkbox('uncheck');
  821. break;
  822. case 'inverse':
  823. $checkboxes.checkbox('toggle');
  824. break;
  825. }
  826. });
  827. $('#delete-selection').click(function () {
  828. var $this = $(this);
  829. $this.addClass("loading disabled");
  830. var ids = [];
  831. $checkboxes.each(function () {
  832. if ($(this).checkbox('is checked')) {
  833. ids.push($(this).data('id'));
  834. }
  835. });
  836. $.post($this.data('link'), {
  837. "_csrf": csrf,
  838. "ids": ids
  839. }).done(function () {
  840. window.location.href = $this.data('redirect');
  841. });
  842. });
  843. }
  844. }
  845. function buttonsClickOnEnter() {
  846. $('.ui.button').keypress(function (e) {
  847. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  848. $(this).click();
  849. });
  850. }
  851. function hideWhenLostFocus(body, parent) {
  852. $(document).click(function (e) {
  853. var target = e.target;
  854. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  855. $(body).hide();
  856. }
  857. });
  858. }
  859. function searchUsers() {
  860. if (!$('#search-user-box .results').length) {
  861. return;
  862. }
  863. var $searchUserBox = $('#search-user-box');
  864. var $results = $searchUserBox.find('.results');
  865. $searchUserBox.keyup(function () {
  866. var $this = $(this);
  867. var keyword = $this.find('input').val();
  868. if (keyword.length < 2) {
  869. $results.hide();
  870. return;
  871. }
  872. $.ajax({
  873. url: suburl + '/api/v1/users/search?q=' + keyword,
  874. dataType: "json",
  875. success: function (response) {
  876. var notEmpty = function (str) {
  877. return str && str.length > 0;
  878. };
  879. $results.html('');
  880. if (response.ok && response.data.length) {
  881. var html = '';
  882. $.each(response.data, function (i, item) {
  883. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  884. if (notEmpty(item.full_name)) {
  885. html += ' (' + item.full_name + ')';
  886. }
  887. html += '</div>';
  888. });
  889. $results.html(html);
  890. $this.find('.results .item').click(function () {
  891. $this.find('input').val($(this).find('.username').text());
  892. $results.hide();
  893. });
  894. $results.show();
  895. } else {
  896. $results.hide();
  897. }
  898. }
  899. });
  900. });
  901. $searchUserBox.find('input').focus(function () {
  902. $searchUserBox.keyup();
  903. });
  904. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  905. }
  906. // FIXME: merge common parts in two functions
  907. function searchRepositories() {
  908. if (!$('#search-repo-box .results').length) {
  909. return;
  910. }
  911. var $searchRepoBox = $('#search-repo-box');
  912. var $results = $searchRepoBox.find('.results');
  913. $searchRepoBox.keyup(function () {
  914. var $this = $(this);
  915. var keyword = $this.find('input').val();
  916. if (keyword.length < 2) {
  917. $results.hide();
  918. return;
  919. }
  920. $.ajax({
  921. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $searchRepoBox.data('uid'),
  922. dataType: "json",
  923. success: function (response) {
  924. var notEmpty = function (str) {
  925. return str && str.length > 0;
  926. };
  927. $results.html('');
  928. if (response.ok && response.data.length) {
  929. var html = '';
  930. $.each(response.data, function (i, item) {
  931. html += '<div class="item"><i class="octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  932. });
  933. $results.html(html);
  934. $this.find('.results .item').click(function () {
  935. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  936. $results.hide();
  937. });
  938. $results.show();
  939. } else {
  940. $results.hide();
  941. }
  942. }
  943. });
  944. });
  945. $searchRepoBox.find('input').focus(function () {
  946. $searchRepoBox.keyup();
  947. });
  948. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  949. }
  950. function initCodeView() {
  951. if ($('.code-view .linenums').length > 0) {
  952. $(document).on('click', '.lines-num span', function (e) {
  953. var $select = $(this);
  954. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  955. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  956. deSelect();
  957. });
  958. $(window).on('hashchange', function (e) {
  959. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  960. var $list = $('.code-view ol.linenums > li');
  961. var $first;
  962. if (m) {
  963. $first = $list.filter('.' + m[1]);
  964. selectRange($list, $first, $list.filter('.' + m[2]));
  965. $("html, body").scrollTop($first.offset().top - 200);
  966. return;
  967. }
  968. m = window.location.hash.match(/^#(L\d+)$/);
  969. if (m) {
  970. $first = $list.filter('.' + m[1]);
  971. selectRange($list, $first);
  972. $("html, body").scrollTop($first.offset().top - 200);
  973. }
  974. }).trigger('hashchange');
  975. }
  976. }
  977. function initUserSettings() {
  978. console.log('initUserSettings');
  979. // Options
  980. if ($('.user.settings.profile').length > 0) {
  981. $('#username').keyup(function () {
  982. var $prompt = $('#name-change-prompt');
  983. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  984. $prompt.show();
  985. } else {
  986. $prompt.hide();
  987. }
  988. });
  989. }
  990. }
  991. function initRepositoryCollaboration() {
  992. console.log('initRepositoryCollaboration');
  993. // Change collaborator access mode
  994. $('.access-mode.menu .item').click(function () {
  995. var $menu = $(this).parent();
  996. $.post($menu.data('url'), {
  997. "_csrf": csrf,
  998. "uid": $menu.data('uid'),
  999. "mode": $(this).data('value')
  1000. })
  1001. });
  1002. }
  1003. function initWebhookSettings() {
  1004. $('.events.checkbox input').change(function () {
  1005. if ($(this).is(':checked')) {
  1006. $('.events.fields').show();
  1007. }
  1008. });
  1009. $('.non-events.checkbox input').change(function () {
  1010. if ($(this).is(':checked')) {
  1011. $('.events.fields').hide();
  1012. }
  1013. });
  1014. // Highlight payload on first click
  1015. $('.hook.history.list .toggle.button').click(function () {
  1016. $($(this).data('target') + ' .nohighlight').each(function () {
  1017. var $this = $(this);
  1018. $this.removeClass('nohighlight');
  1019. setTimeout(function(){ hljs.highlightBlock($this[0]) }, 500);
  1020. })
  1021. })
  1022. // Trigger delivery
  1023. $('.delivery.button, .redelivery.button').click(function () {
  1024. var $this = $(this);
  1025. $this.addClass('loading disabled');
  1026. $.post($this.data('link'), {
  1027. "_csrf": csrf
  1028. }).done(
  1029. setTimeout(function () {
  1030. window.location.href = $this.data('redirect');
  1031. }, 5000)
  1032. );
  1033. });
  1034. }
  1035. $(document).ready(function () {
  1036. csrf = $('meta[name=_csrf]').attr("content");
  1037. suburl = $('meta[name=_suburl]').attr("content");
  1038. // Show exact time
  1039. $('.time-since').each(function () {
  1040. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  1041. });
  1042. // Semantic UI modules.
  1043. $('.ui.dropdown').dropdown({
  1044. forceSelection: false
  1045. });
  1046. $('.jump.dropdown').dropdown({
  1047. action: 'select',
  1048. onShow: function () {
  1049. $('.poping.up').popup('hide');
  1050. }
  1051. });
  1052. $('.slide.up.dropdown').dropdown({
  1053. transition: 'slide up'
  1054. });
  1055. $('.upward.dropdown').dropdown({
  1056. direction: 'upward'
  1057. });
  1058. $('.ui.accordion').accordion();
  1059. $('.ui.checkbox').checkbox();
  1060. $('.ui.progress').progress({
  1061. showActivity: false
  1062. });
  1063. $('.poping.up').popup();
  1064. $('.top.menu .poping.up').popup({
  1065. onShow: function () {
  1066. if ($('.top.menu .menu.transition').hasClass('visible')) {
  1067. return false;
  1068. }
  1069. }
  1070. });
  1071. $('.tabular.menu .item').tab();
  1072. $('.tabable.menu .item').tab();
  1073. $('.toggle.button').click(function () {
  1074. $($(this).data('target')).slideToggle(100);
  1075. });
  1076. // Highlight JS
  1077. if (typeof hljs != 'undefined') {
  1078. hljs.initHighlightingOnLoad();
  1079. }
  1080. // Dropzone
  1081. var $dropzone = $('#dropzone');
  1082. if ($dropzone.length > 0) {
  1083. // Disable auto discover for all elements:
  1084. Dropzone.autoDiscover = false;
  1085. var filenameDict = {};
  1086. $dropzone.dropzone({
  1087. url: $dropzone.data('upload-url'),
  1088. headers: {"X-Csrf-Token": csrf},
  1089. maxFiles: $dropzone.data('max-file'),
  1090. maxFilesize: $dropzone.data('max-size'),
  1091. acceptedFiles: ($dropzone.data('accepts') === '*/*') ? null : $dropzone.data('accepts'),
  1092. addRemoveLinks: true,
  1093. dictDefaultMessage: $dropzone.data('default-message'),
  1094. dictInvalidFileType: $dropzone.data('invalid-input-type'),
  1095. dictFileTooBig: $dropzone.data('file-too-big'),
  1096. dictRemoveFile: $dropzone.data('remove-file'),
  1097. init: function () {
  1098. this.on("success", function (file, data) {
  1099. filenameDict[file.name] = data.uuid;
  1100. var input = $('<input id="' + data.uuid + '" name="files" type="hidden">').val(data.uuid);
  1101. $('.files').append(input);
  1102. });
  1103. this.on("removedfile", function (file) {
  1104. if (file.name in filenameDict) {
  1105. $('#' + filenameDict[file.name]).remove();
  1106. }
  1107. if ($dropzone.data('remove-url') && $dropzone.data('csrf')) {
  1108. $.post($dropzone.data('remove-url'), {
  1109. file: filenameDict[file.name],
  1110. _csrf: $dropzone.data('csrf')
  1111. });
  1112. }
  1113. })
  1114. }
  1115. });
  1116. }
  1117. // Emojify
  1118. emojify.setConfig({
  1119. img_dir: suburl + '/img/emoji',
  1120. ignore_emoticons: true
  1121. });
  1122. var hasEmoji = document.getElementsByClassName('has-emoji');
  1123. for (var i = 0; i < hasEmoji.length; i++) {
  1124. emojify.run(hasEmoji[i]);
  1125. }
  1126. // Clipboard JS
  1127. var clipboard = new Clipboard('.clipboard');
  1128. clipboard.on('success', function (e) {
  1129. e.clearSelection();
  1130. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1131. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  1132. $('#' + e.trigger.getAttribute('id')).popup('show');
  1133. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1134. });
  1135. clipboard.on('error', function (e) {
  1136. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  1137. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  1138. $('#' + e.trigger.getAttribute('id')).popup('show');
  1139. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  1140. });
  1141. // AJAX load buttons
  1142. $('.ajax-load-button').click(function () {
  1143. var $this = $(this);
  1144. $this.addClass('disabled');
  1145. $.ajax({
  1146. url: $this.data('url'),
  1147. headers: {
  1148. 'X-AJAX': "true"
  1149. }
  1150. }).success(function (data, status, request) {
  1151. $(data).insertBefore($this);
  1152. // Update new URL or remove self if no more feeds
  1153. var url = request.getResponseHeader('X-AJAX-URL');
  1154. if (url) {
  1155. $this.data('url', url);
  1156. $this.removeClass('disabled');
  1157. } else {
  1158. $this.remove();
  1159. }
  1160. });
  1161. });
  1162. // Helpers.
  1163. $('.delete-button').click(function () {
  1164. var $this = $(this);
  1165. $('.delete.modal').modal({
  1166. closable: false,
  1167. onApprove: function () {
  1168. if ($this.data('type') == "form") {
  1169. $($this.data('form')).submit();
  1170. return;
  1171. }
  1172. $.post($this.data('url'), {
  1173. "_csrf": csrf,
  1174. "id": $this.data("id")
  1175. }).done(function (data) {
  1176. window.location.href = data.redirect;
  1177. });
  1178. }
  1179. }).modal('show');
  1180. return false;
  1181. });
  1182. $('.show-panel.button').click(function () {
  1183. $($(this).data('panel')).show();
  1184. });
  1185. $('.show-modal.button').click(function () {
  1186. $($(this).data('modal')).modal('show');
  1187. });
  1188. $('.delete-post.button').click(function () {
  1189. var $this = $(this);
  1190. $.post($this.data('request-url'), {
  1191. "_csrf": csrf
  1192. }).done(function () {
  1193. window.location.href = $this.data('done-url');
  1194. });
  1195. });
  1196. // Set anchor.
  1197. $('.markdown').each(function () {
  1198. var headers = {};
  1199. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  1200. var node = $(this);
  1201. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, '').replace(/[ ]/g, '-'));
  1202. var name = val;
  1203. if (headers[val] > 0) {
  1204. name = val + '-' + headers[val];
  1205. }
  1206. if (headers[val] == undefined) {
  1207. headers[val] = 1;
  1208. } else {
  1209. headers[val] += 1;
  1210. }
  1211. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  1212. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  1213. });
  1214. });
  1215. buttonsClickOnEnter();
  1216. searchUsers();
  1217. searchRepositories();
  1218. initCommentForm();
  1219. initRepository();
  1220. initWikiForm();
  1221. initEditForm();
  1222. initEditor();
  1223. initOrganization();
  1224. initAdmin();
  1225. initCodeView();
  1226. // Repo clone url.
  1227. if ($('#repo-clone-url').length > 0) {
  1228. switch (localStorage.getItem('repo-clone-protocol')) {
  1229. case 'ssh':
  1230. if ($('#repo-clone-ssh').click().length === 0) {
  1231. $('#repo-clone-https').click();
  1232. }
  1233. break;
  1234. default:
  1235. $('#repo-clone-https').click();
  1236. break;
  1237. }
  1238. }
  1239. var routes = {
  1240. 'div.user.settings': initUserSettings,
  1241. 'div.repository.settings.collaboration': initRepositoryCollaboration,
  1242. 'div.webhook.settings': initWebhookSettings
  1243. };
  1244. var selector;
  1245. for (selector in routes) {
  1246. if ($(selector).length > 0) {
  1247. routes[selector]();
  1248. break;
  1249. }
  1250. }
  1251. });
  1252. function changeHash(hash) {
  1253. if (history.pushState) {
  1254. history.pushState(null, null, hash);
  1255. }
  1256. else {
  1257. location.hash = hash;
  1258. }
  1259. }
  1260. function deSelect() {
  1261. if (window.getSelection) {
  1262. window.getSelection().removeAllRanges();
  1263. } else {
  1264. document.selection.empty();
  1265. }
  1266. }
  1267. function selectRange($list, $select, $from) {
  1268. $list.removeClass('active');
  1269. if ($from) {
  1270. var a = parseInt($select.attr('rel').substr(1));
  1271. var b = parseInt($from.attr('rel').substr(1));
  1272. var c;
  1273. if (a != b) {
  1274. if (a > b) {
  1275. c = a;
  1276. a = b;
  1277. b = c;
  1278. }
  1279. var classes = [];
  1280. for (var i = a; i <= b; i++) {
  1281. classes.push('.L' + i);
  1282. }
  1283. $list.filter(classes.join(',')).addClass('active');
  1284. changeHash('#L' + a + '-' + 'L' + b);
  1285. return
  1286. }
  1287. }
  1288. $select.addClass('active');
  1289. changeHash('#' + $select.attr('rel'));
  1290. }
  1291. $(function () {
  1292. if ($('.user.signin').length > 0) return;
  1293. $('form').areYouSure();
  1294. });