gogs.js 49 KB

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