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.

gogs.js 49KB

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