gogs.js 49 KB

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