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