gogs.js 49 KB

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