gogs.js 49 KB

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