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

index.js 53KB

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