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

index.js 49KB

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