gogs.js 49 KB

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