You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 50KB

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