Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.js 51KB

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