gogs.js 49 KB

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