gogs.js 49 KB

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