gogs.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. 'use strict';
  2. var csrf;
  3. var suburl;
  4. function initCommentPreviewTab($form) {
  5. var $tab_menu = $form.find('.tabular.menu');
  6. $tab_menu.find('.item').tab();
  7. $tab_menu.find('.item[data-tab="' + $tab_menu.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="' + $tab_menu.data('write') + '"] textarea').val()
  14. },
  15. function (data) {
  16. var $preview_tab = $form.find('.tab.segment[data-tab="' + $tab_menu.data('preview') + '"]');
  17. $preview_tab.html(data);
  18. emojify.run($preview_tab[0]);
  19. $('pre code', $preview_tab[0]).each(function (i, block) {
  20. hljs.highlightBlock(block);
  21. });
  22. }
  23. );
  24. });
  25. buttonsClickOnEnter();
  26. }
  27. function initCommentForm() {
  28. if ($('.comment.form').length == 0) {
  29. return
  30. }
  31. initCommentPreviewTab($('.comment.form'));
  32. // Labels
  33. var $list = $('.ui.labels.list');
  34. var $no_select = $list.find('.no-select');
  35. var $label_menu = $('.select-label .menu');
  36. var has_label_update_action = $label_menu.data('action') == 'update';
  37. function updateIssueMeta(url, action, id) {
  38. $.post(url, {
  39. "_csrf": csrf,
  40. "action": action,
  41. "id": id
  42. });
  43. }
  44. $label_menu.find('.item:not(.no-select)').click(function () {
  45. if ($(this).hasClass('checked')) {
  46. $(this).removeClass('checked');
  47. $(this).find('.octicon').removeClass('octicon-check');
  48. if (has_label_update_action) {
  49. updateIssueMeta($label_menu.data('update-url'), "detach", $(this).data('id'));
  50. }
  51. } else {
  52. $(this).addClass('checked');
  53. $(this).find('.octicon').addClass('octicon-check');
  54. if (has_label_update_action) {
  55. updateIssueMeta($label_menu.data('update-url'), "attach", $(this).data('id'));
  56. }
  57. }
  58. var label_ids = "";
  59. $(this).parent().find('.item').each(function () {
  60. if ($(this).hasClass('checked')) {
  61. label_ids += $(this).data('id') + ",";
  62. $($(this).data('id-selector')).removeClass('hide');
  63. } else {
  64. $($(this).data('id-selector')).addClass('hide');
  65. }
  66. });
  67. if (label_ids.length == 0) {
  68. $no_select.removeClass('hide');
  69. } else {
  70. $no_select.addClass('hide');
  71. }
  72. $($(this).parent().data('id')).val(label_ids);
  73. return false;
  74. });
  75. $label_menu.find('.no-select.item').click(function () {
  76. if (has_label_update_action) {
  77. updateIssueMeta($label_menu.data('update-url'), "clear", '');
  78. }
  79. $(this).parent().find('.item').each(function () {
  80. $(this).removeClass('checked');
  81. $(this).find('.octicon').removeClass('octicon-check');
  82. });
  83. $list.find('.item').each(function () {
  84. $(this).addClass('hide');
  85. });
  86. $no_select.removeClass('hide');
  87. $($(this).parent().data('id')).val('');
  88. });
  89. function selectItem(select_id, input_id) {
  90. var $menu = $(select_id + ' .menu');
  91. var $list = $('.ui' + select_id + '.list');
  92. var has_update_action = $menu.data('action') == 'update';
  93. $menu.find('.item:not(.no-select)').click(function () {
  94. $(this).parent().find('.item').each(function () {
  95. $(this).removeClass('selected active')
  96. });
  97. $(this).addClass('selected active');
  98. if (has_update_action) {
  99. updateIssueMeta($menu.data('update-url'), '', $(this).data('id'));
  100. }
  101. switch (input_id) {
  102. case '#milestone_id':
  103. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  104. $(this).text() + '</a>');
  105. break;
  106. case '#assignee_id':
  107. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  108. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  109. $(this).text() + '</a>');
  110. }
  111. $('.ui' + select_id + '.list .no-select').addClass('hide');
  112. $(input_id).val($(this).data('id'));
  113. });
  114. $menu.find('.no-select.item').click(function () {
  115. $(this).parent().find('.item:not(.no-select)').each(function () {
  116. $(this).removeClass('selected active')
  117. });
  118. if (has_update_action) {
  119. updateIssueMeta($menu.data('update-url'), '', '');
  120. }
  121. $list.find('.selected').html('');
  122. $list.find('.no-select').removeClass('hide');
  123. $(input_id).val('');
  124. });
  125. }
  126. // Milestone and assignee
  127. selectItem('.select-milestone', '#milestone_id');
  128. selectItem('.select-assignee', '#assignee_id');
  129. }
  130. function initInstall() {
  131. if ($('.install').length == 0) {
  132. return;
  133. }
  134. // Database type change detection.
  135. $("#db_type").change(function () {
  136. var sqlite_default = 'data/gogs.db';
  137. var tidb_default = 'data/gogs_tidb';
  138. var db_type = $(this).val();
  139. if (db_type === "SQLite3" || db_type === "TiDB") {
  140. $('#sql_settings').hide();
  141. $('#pgsql_settings').hide();
  142. $('#sqlite_settings').show();
  143. if (db_type === "SQLite3" && $('#db_path').val() == tidb_default) {
  144. $('#db_path').val(sqlite_default);
  145. } else if (db_type === "TiDB" && $('#db_path').val() == sqlite_default) {
  146. $('#db_path').val(tidb_default);
  147. }
  148. return;
  149. }
  150. var mysql_default = '127.0.0.1:3306';
  151. var postgres_default = '127.0.0.1:5432';
  152. $('#sqlite_settings').hide();
  153. $('#sql_settings').show();
  154. if (db_type === "PostgreSQL") {
  155. $('#pgsql_settings').show();
  156. if ($('#db_host').val() == mysql_default) {
  157. $('#db_host').val(postgres_default);
  158. }
  159. } else {
  160. $('#pgsql_settings').hide();
  161. if ($('#db_host').val() == postgres_default) {
  162. $('#db_host').val(mysql_default);
  163. }
  164. }
  165. });
  166. // TODO: better handling of exclusive relations.
  167. $('#offline-mode input').change(function () {
  168. if ($(this).is(':checked')) {
  169. $('#disable-gravatar').checkbox('check');
  170. $('#federated-avatar-lookup').checkbox('uncheck');
  171. }
  172. });
  173. $('#disable-gravatar input').change(function () {
  174. if ($(this).is(':checked')) {
  175. $('#federated-avatar-lookup').checkbox('uncheck');
  176. } else {
  177. $('#offline-mode').checkbox('uncheck');
  178. }
  179. });
  180. $('#federated-avatar-lookup input').change(function () {
  181. if ($(this).is(':checked')) {
  182. $('#disable-gravatar').checkbox('uncheck');
  183. $('#offline-mode').checkbox('uncheck');
  184. }
  185. });
  186. $('#disable-registration input').change(function () {
  187. if ($(this).is(':checked')) {
  188. $('#enable-captcha').checkbox('uncheck');
  189. }
  190. });
  191. $('#enable-captcha input').change(function () {
  192. if ($(this).is(':checked')) {
  193. $('#disable-registration').checkbox('uncheck');
  194. }
  195. });
  196. }
  197. function initRepository() {
  198. if ($('.repository').length == 0) {
  199. return;
  200. }
  201. function initFilterSearchDropdown(selector) {
  202. var $dropdown = $(selector);
  203. $dropdown.dropdown({
  204. fullTextSearch: true,
  205. onChange: function (text, value, $choice) {
  206. window.location.href = $choice.data('url');
  207. console.log($choice.data('url'))
  208. },
  209. message: {noResults: $dropdown.data('no-results')}
  210. });
  211. }
  212. // File list and commits
  213. if ($('.repository.file.list').length > 0 ||
  214. ('.repository.commits').length > 0) {
  215. initFilterSearchDropdown('.choose.reference .dropdown');
  216. $('.reference.column').click(function () {
  217. $('.choose.reference .scrolling.menu').css('display', 'none');
  218. $('.choose.reference .text').removeClass('black');
  219. $($(this).data('target')).css('display', 'block');
  220. $(this).find('.text').addClass('black');
  221. return false;
  222. });
  223. }
  224. // Wiki
  225. if ($('.repository.wiki.view').length > 0) {
  226. initFilterSearchDropdown('.choose.page .dropdown');
  227. }
  228. // Options
  229. if ($('.repository.settings.options').length > 0) {
  230. $('#repo_name').keyup(function () {
  231. var $prompt_span = $('#repo-name-change-prompt');
  232. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  233. $prompt_span.show();
  234. } else {
  235. $prompt_span.hide();
  236. }
  237. });
  238. // Enable or select internal/external wiki system and issue tracker.
  239. $('.enable-system').change(function () {
  240. if (this.checked) {
  241. $($(this).data('target')).removeClass('disabled');
  242. } else {
  243. $($(this).data('target')).addClass('disabled');
  244. }
  245. });
  246. $('.enable-system-radio').change(function () {
  247. if (this.value == 'false') {
  248. $($(this).data('target')).addClass('disabled');
  249. } else if (this.value == 'true') {
  250. $($(this).data('target')).removeClass('disabled');
  251. }
  252. });
  253. }
  254. // Labels
  255. if ($('.repository.labels').length > 0) {
  256. // Create label
  257. var $new_label_panel = $('.new-label.segment');
  258. $('.new-label.button').click(function () {
  259. $new_label_panel.show();
  260. });
  261. $('.new-label.segment .cancel').click(function () {
  262. $new_label_panel.hide();
  263. });
  264. $('.color-picker').each(function () {
  265. $(this).minicolors();
  266. });
  267. $('.precolors .color').click(function () {
  268. var color_hex = $(this).data('color-hex');
  269. $('.color-picker').val(color_hex);
  270. $('.minicolors-swatch-color').css("background-color", color_hex);
  271. });
  272. $('.edit-label-button').click(function () {
  273. $('#label-modal-id').val($(this).data('id'));
  274. $('.edit-label .new-label-input').val($(this).data('title'));
  275. $('.edit-label .color-picker').val($(this).data('color'));
  276. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  277. $('.edit-label.modal').modal({
  278. onApprove: function () {
  279. $('.edit-label.form').submit();
  280. }
  281. }).modal('show');
  282. return false;
  283. });
  284. }
  285. // Milestones
  286. if ($('.repository.milestones').length > 0) {
  287. }
  288. if ($('.repository.new.milestone').length > 0) {
  289. var $datepicker = $('.milestone.datepicker');
  290. $datepicker.datetimepicker({
  291. lang: $datepicker.data('lang'),
  292. inline: true,
  293. timepicker: false,
  294. startDate: $datepicker.data('start-date'),
  295. formatDate: 'Y-m-d',
  296. onSelectDate: function (ct) {
  297. $('#deadline').val(ct.dateFormat('Y-m-d'));
  298. }
  299. });
  300. $('#clear-date').click(function () {
  301. $('#deadline').val('');
  302. return false;
  303. });
  304. }
  305. // Issues
  306. if ($('.repository.view.issue').length > 0) {
  307. // Edit issue title
  308. var $issue_title = $('#issue-title');
  309. var $edit_input = $('#edit-title-input input');
  310. var editTitleToggle = function () {
  311. $issue_title.toggle();
  312. $('.not-in-edit').toggle();
  313. $('#edit-title-input').toggle();
  314. $('.in-edit').toggle();
  315. $edit_input.focus();
  316. return false;
  317. };
  318. $('#edit-title').click(editTitleToggle);
  319. $('#cancel-edit-title').click(editTitleToggle);
  320. $('#save-edit-title').click(editTitleToggle).click(function () {
  321. if ($edit_input.val().length == 0 ||
  322. $edit_input.val() == $issue_title.text()) {
  323. $edit_input.val($issue_title.text());
  324. return false;
  325. }
  326. $.post($(this).data('update-url'), {
  327. "_csrf": csrf,
  328. "title": $edit_input.val()
  329. },
  330. function (data) {
  331. $edit_input.val(data.title);
  332. $issue_title.text(data.title);
  333. });
  334. return false;
  335. });
  336. // Edit issue or comment content
  337. $('.edit-content').click(function () {
  338. var $segment = $(this).parent().parent().parent().next();
  339. var $edit_content_zone = $segment.find('.edit-content-zone');
  340. var $render_content = $segment.find('.render-content');
  341. var $raw_content = $segment.find('.raw-content');
  342. var $textarea;
  343. // Setup new form
  344. if ($edit_content_zone.html().length == 0) {
  345. $edit_content_zone.html($('#edit-content-form').html());
  346. $textarea = $segment.find('textarea');
  347. // Give new write/preview data-tab name to distinguish from others
  348. var $edit_content_form = $edit_content_zone.find('.ui.comment.form');
  349. var $tabular_menu = $edit_content_form.find('.tabular.menu');
  350. $tabular_menu.attr('data-write', $edit_content_zone.data('write'));
  351. $tabular_menu.attr('data-preview', $edit_content_zone.data('preview'));
  352. $tabular_menu.find('.write.item').attr('data-tab', $edit_content_zone.data('write'));
  353. $tabular_menu.find('.preview.item').attr('data-tab', $edit_content_zone.data('preview'));
  354. $edit_content_form.find('.write.segment').attr('data-tab', $edit_content_zone.data('write'));
  355. $edit_content_form.find('.preview.segment').attr('data-tab', $edit_content_zone.data('preview'));
  356. initCommentPreviewTab($edit_content_form);
  357. $edit_content_zone.find('.cancel.button').click(function () {
  358. $render_content.show();
  359. $edit_content_zone.hide();
  360. });
  361. $edit_content_zone.find('.save.button').click(function () {
  362. $render_content.show();
  363. $edit_content_zone.hide();
  364. $.post($edit_content_zone.data('update-url'), {
  365. "_csrf": csrf,
  366. "content": $textarea.val(),
  367. "context": $edit_content_zone.data('context')
  368. },
  369. function (data) {
  370. if (data.length == 0) {
  371. $render_content.html($('#no-content').html());
  372. } else {
  373. $render_content.html(data.content);
  374. emojify.run($render_content[0]);
  375. $('pre code', $render_content[0]).each(function (i, block) {
  376. hljs.highlightBlock(block);
  377. });
  378. }
  379. });
  380. });
  381. } else {
  382. $textarea = $segment.find('textarea');
  383. }
  384. // Show write/preview tab and copy raw content as needed
  385. $edit_content_zone.show();
  386. $render_content.hide();
  387. if ($textarea.val().length == 0) {
  388. $textarea.val($raw_content.text());
  389. }
  390. $textarea.focus();
  391. return false;
  392. });
  393. // Delete comment
  394. $('.delete-comment').click(function () {
  395. var $this = $(this);
  396. if (confirm($this.data('locale'))) {
  397. $.post($this.data('url'), {
  398. "_csrf": csrf
  399. }).success(function () {
  400. $('#' + $this.data('comment-id')).remove();
  401. });
  402. }
  403. return false;
  404. });
  405. // Change status
  406. var $status_btn = $('#status-button');
  407. $('#content').keyup(function () {
  408. if ($(this).val().length == 0) {
  409. $status_btn.text($status_btn.data('status'))
  410. } else {
  411. $status_btn.text($status_btn.data('status-and-comment'))
  412. }
  413. });
  414. $status_btn.click(function () {
  415. $('#status').val($status_btn.data('status-val'));
  416. $('#comment-form').submit();
  417. });
  418. }
  419. // Diff
  420. if ($('.repository.diff').length > 0) {
  421. var $counter = $('.diff-counter');
  422. if ($counter.length >= 1) {
  423. $counter.each(function (i, item) {
  424. var $item = $(item);
  425. var addLine = $item.find('span[data-line].add').data("line");
  426. var delLine = $item.find('span[data-line].del').data("line");
  427. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  428. $item.find(".bar .add").css("width", addPercent + "%");
  429. });
  430. }
  431. }
  432. // Quick start and repository home
  433. $('#repo-clone-ssh').click(function () {
  434. $('.clone-url').text($(this).data('link'));
  435. $('#repo-clone-url').val($(this).data('link'));
  436. $(this).addClass('blue');
  437. $('#repo-clone-https').removeClass('blue');
  438. localStorage.setItem('repo-clone-protocol', 'ssh');
  439. });
  440. $('#repo-clone-https').click(function () {
  441. $('.clone-url').text($(this).data('link'));
  442. $('#repo-clone-url').val($(this).data('link'));
  443. $(this).addClass('blue');
  444. $('#repo-clone-ssh').removeClass('blue');
  445. localStorage.setItem('repo-clone-protocol', 'https');
  446. });
  447. $('#repo-clone-url').click(function () {
  448. $(this).select();
  449. });
  450. // Pull request
  451. if ($('.repository.compare.pull').length > 0) {
  452. initFilterSearchDropdown('.choose.branch .dropdown');
  453. }
  454. }
  455. function initRepositoryCollaboration() {
  456. console.log('initRepositoryCollaboration');
  457. // Change collaborator access mode
  458. $('.access-mode.menu .item').click(function () {
  459. var $menu = $(this).parent();
  460. $.post($menu.data('url'), {
  461. "_csrf": csrf,
  462. "uid": $menu.data('uid'),
  463. "mode": $(this).data('value')
  464. })
  465. });
  466. }
  467. function initWiki() {
  468. if ($('.repository.wiki').length == 0) {
  469. return;
  470. }
  471. if ($('.repository.wiki.new').length > 0) {
  472. var $edit_area = $('#edit-area');
  473. var simplemde = new SimpleMDE({
  474. autoDownloadFontAwesome: false,
  475. element: $edit_area[0],
  476. forceSync: true,
  477. previewRender: function (plainText, preview) { // Async method
  478. setTimeout(function () {
  479. // FIXME: still send render request when return back to edit mode
  480. $.post($edit_area.data('url'), {
  481. "_csrf": csrf,
  482. "mode": "gfm",
  483. "context": $edit_area.data('context'),
  484. "text": plainText
  485. },
  486. function (data) {
  487. preview.innerHTML = '<div class="markdown">' + data + '</div>';
  488. emojify.run($('.editor-preview')[0]);
  489. }
  490. );
  491. }, 0);
  492. return "Loading...";
  493. },
  494. renderingConfig: {
  495. singleLineBreaks: false
  496. },
  497. spellChecker: false,
  498. tabSize: 4,
  499. toolbar: ["bold", "italic", "strikethrough", "|",
  500. "heading", "heading-1", "heading-2", "heading-3", "|",
  501. "code", "quote", "|",
  502. "unordered-list", "ordered-list", "|",
  503. "link", "image", "horizontal-rule", "|",
  504. "preview", "fullscreen"]
  505. })
  506. }
  507. }
  508. function initOrganization() {
  509. if ($('.organization').length == 0) {
  510. return;
  511. }
  512. // Options
  513. if ($('.organization.settings.options').length > 0) {
  514. $('#org_name').keyup(function () {
  515. var $prompt_span = $('#org-name-change-prompt');
  516. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  517. $prompt_span.show();
  518. } else {
  519. $prompt_span.hide();
  520. }
  521. });
  522. }
  523. }
  524. function initUserSettings() {
  525. console.log('initUserSettings');
  526. // Options
  527. if ($('.user.settings.profile').length > 0) {
  528. $('#username').keyup(function () {
  529. var $prompt_span = $('#name-change-prompt');
  530. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  531. $prompt_span.show();
  532. } else {
  533. $prompt_span.hide();
  534. }
  535. });
  536. }
  537. }
  538. function initWebhook() {
  539. if ($('.new.webhook').length == 0) {
  540. return;
  541. }
  542. $('.events.checkbox input').change(function () {
  543. if ($(this).is(':checked')) {
  544. $('.events.fields').show();
  545. }
  546. });
  547. $('.non-events.checkbox input').change(function () {
  548. if ($(this).is(':checked')) {
  549. $('.events.fields').hide();
  550. }
  551. });
  552. // Test delivery
  553. $('#test-delivery').click(function () {
  554. var $this = $(this);
  555. $this.addClass('loading disabled');
  556. $.post($this.data('link'), {
  557. "_csrf": csrf
  558. }).done(
  559. setTimeout(function () {
  560. window.location.href = $this.data('redirect');
  561. }, 5000)
  562. )
  563. });
  564. }
  565. function initAdmin() {
  566. if ($('.admin').length == 0) {
  567. return;
  568. }
  569. // New user
  570. if ($('.admin.new.user').length > 0 ||
  571. $('.admin.edit.user').length > 0) {
  572. $('#login_type').change(function () {
  573. if ($(this).val().substring(0, 1) == '0') {
  574. $('#login_name').removeAttr('required');
  575. $('.non-local').hide();
  576. $('.local').show();
  577. $('#user_name').focus();
  578. if ($(this).data('password') == "required") {
  579. $('#password').attr('required', 'required');
  580. }
  581. } else {
  582. $('#login_name').attr('required', 'required');
  583. $('.non-local').show();
  584. $('.local').hide();
  585. $('#login_name').focus();
  586. $('#password').removeAttr('required');
  587. }
  588. });
  589. }
  590. function onSecurityProtocolChange() {
  591. if ($('#security_protocol').val() > 0) {
  592. $('.has-tls').show();
  593. } else {
  594. $('.has-tls').hide();
  595. }
  596. }
  597. // New authentication
  598. if ($('.admin.new.authentication').length > 0) {
  599. $('#auth_type').change(function () {
  600. $('.ldap').hide();
  601. $('.dldap').hide();
  602. $('.smtp').hide();
  603. $('.pam').hide();
  604. $('.has-tls').hide();
  605. var auth_type = $(this).val();
  606. switch (auth_type) {
  607. case '2': // LDAP
  608. $('.ldap').show();
  609. break;
  610. case '3': // SMTP
  611. $('.smtp').show();
  612. $('.has-tls').show();
  613. break;
  614. case '4': // PAM
  615. $('.pam').show();
  616. break;
  617. case '5': // LDAP
  618. $('.dldap').show();
  619. break;
  620. }
  621. if (auth_type == '2' || auth_type == '5') {
  622. onSecurityProtocolChange()
  623. }
  624. });
  625. $('#security_protocol').change(onSecurityProtocolChange)
  626. }
  627. // Edit authentication
  628. if ($('.admin.edit.authentication').length > 0) {
  629. var auth_type = $('#auth_type').val();
  630. if (auth_type == '2' || auth_type == '5') {
  631. $('#security_protocol').change(onSecurityProtocolChange);
  632. }
  633. }
  634. // Notice
  635. if ($('.admin.notice')) {
  636. var $detail_modal = $('#detail-modal');
  637. // Attach view detail modals
  638. $('.view-detail').click(function () {
  639. $detail_modal.find('.content p').text($(this).data('content'));
  640. $detail_modal.modal('show');
  641. return false;
  642. });
  643. // Select actions
  644. var $checkboxes = $('.select.table .ui.checkbox');
  645. $('.select.action').click(function () {
  646. switch ($(this).data('action')) {
  647. case 'select-all':
  648. $checkboxes.checkbox('check');
  649. break;
  650. case 'deselect-all':
  651. $checkboxes.checkbox('uncheck');
  652. break;
  653. case 'inverse':
  654. $checkboxes.checkbox('toggle');
  655. break;
  656. }
  657. });
  658. $('#delete-selection').click(function () {
  659. var $this = $(this);
  660. $this.addClass("loading disabled");
  661. var ids = [];
  662. $checkboxes.each(function () {
  663. if ($(this).checkbox('is checked')) {
  664. ids.push($(this).data('id'));
  665. }
  666. });
  667. $.post($this.data('link'), {
  668. "_csrf": csrf,
  669. "ids": ids
  670. }).done(function () {
  671. window.location.href = $this.data('redirect');
  672. });
  673. });
  674. }
  675. }
  676. function buttonsClickOnEnter() {
  677. $('.ui.button').keypress(function (e) {
  678. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  679. $(this).click();
  680. });
  681. }
  682. function hideWhenLostFocus(body, parent) {
  683. $(document).click(function (e) {
  684. var target = e.target;
  685. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  686. $(body).hide();
  687. }
  688. });
  689. }
  690. function searchUsers() {
  691. if (!$('#search-user-box .results').length) {
  692. return;
  693. }
  694. var $search_user_box = $('#search-user-box');
  695. var $result_list = $search_user_box.find('.results');
  696. $search_user_box.keyup(function () {
  697. var $this = $(this);
  698. var keyword = $this.find('input').val();
  699. if (keyword.length < 2) {
  700. $result_list.hide();
  701. return;
  702. }
  703. $.ajax({
  704. url: suburl + '/api/v1/users/search?q=' + keyword,
  705. dataType: "json",
  706. success: function (response) {
  707. var notEmpty = function (str) {
  708. return str && str.length > 0;
  709. };
  710. $result_list.html('');
  711. if (response.ok && response.data.length) {
  712. var html = '';
  713. $.each(response.data, function (i, item) {
  714. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  715. if (notEmpty(item.full_name)) {
  716. html += ' (' + item.full_name + ')';
  717. }
  718. html += '</div>';
  719. });
  720. $result_list.html(html);
  721. $this.find('.results .item').click(function () {
  722. $this.find('input').val($(this).find('.username').text());
  723. $result_list.hide();
  724. });
  725. $result_list.show();
  726. } else {
  727. $result_list.hide();
  728. }
  729. }
  730. });
  731. });
  732. $search_user_box.find('input').focus(function () {
  733. $search_user_box.keyup();
  734. });
  735. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  736. }
  737. // FIXME: merge common parts in two functions
  738. function searchRepositories() {
  739. if (!$('#search-repo-box .results').length) {
  740. return;
  741. }
  742. var $search_repo_box = $('#search-repo-box');
  743. var $result_list = $search_repo_box.find('.results');
  744. $search_repo_box.keyup(function () {
  745. var $this = $(this);
  746. var keyword = $this.find('input').val();
  747. if (keyword.length < 2) {
  748. $result_list.hide();
  749. return;
  750. }
  751. $.ajax({
  752. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $search_repo_box.data('uid'),
  753. dataType: "json",
  754. success: function (response) {
  755. var notEmpty = function (str) {
  756. return str && str.length > 0;
  757. };
  758. $result_list.html('');
  759. if (response.ok && response.data.length) {
  760. var html = '';
  761. $.each(response.data, function (i, item) {
  762. html += '<div class="item"><i class="icon octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  763. });
  764. $result_list.html(html);
  765. $this.find('.results .item').click(function () {
  766. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  767. $result_list.hide();
  768. });
  769. $result_list.show();
  770. } else {
  771. $result_list.hide();
  772. }
  773. }
  774. });
  775. });
  776. $search_repo_box.find('input').focus(function () {
  777. $search_repo_box.keyup();
  778. });
  779. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  780. }
  781. $(document).ready(function () {
  782. csrf = $('meta[name=_csrf]').attr("content");
  783. suburl = $('meta[name=_suburl]').attr("content");
  784. // Show exact time
  785. $('.time-since').each(function () {
  786. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  787. });
  788. // Semantic UI modules.
  789. $('.dropdown').dropdown();
  790. $('.jump.dropdown').dropdown({
  791. action: 'hide',
  792. onShow: function () {
  793. $('.poping.up').popup('hide');
  794. }
  795. });
  796. $('.slide.up.dropdown').dropdown({
  797. transition: 'slide up'
  798. });
  799. $('.upward.dropdown').dropdown({
  800. direction: 'upward'
  801. });
  802. $('.ui.accordion').accordion();
  803. $('.ui.checkbox').checkbox();
  804. $('.ui.progress').progress({
  805. showActivity: false
  806. });
  807. $('.poping.up').popup();
  808. $('.top.menu .poping.up').popup({
  809. onShow: function () {
  810. if ($('.top.menu .menu.transition').hasClass('visible')) {
  811. return false;
  812. }
  813. }
  814. });
  815. $('.tabular.menu .item').tab();
  816. $('.tabable.menu .item').tab();
  817. $('.toggle.button').click(function () {
  818. $($(this).data('target')).slideToggle(100);
  819. });
  820. // Highlight JS
  821. if (typeof hljs != 'undefined') {
  822. hljs.initHighlightingOnLoad();
  823. }
  824. // Dropzone
  825. if ($('#dropzone').length > 0) {
  826. // Disable auto discover for all elements:
  827. Dropzone.autoDiscover = false;
  828. var filenameDict = {};
  829. var $dropz = $('#dropzone');
  830. $dropz.dropzone({
  831. url: $dropz.data('upload-url'),
  832. headers: {"X-Csrf-Token": csrf},
  833. maxFiles: $dropz.data('max-file'),
  834. maxFilesize: $dropz.data('max-size'),
  835. acceptedFiles: ($dropz.data('accepts') === '*/*') ? null : $dropz.data('accepts'),
  836. addRemoveLinks: true,
  837. dictDefaultMessage: $dropz.data('default-message'),
  838. dictInvalidFileType: $dropz.data('invalid-input-type'),
  839. dictFileTooBig: $dropz.data('file-too-big'),
  840. dictRemoveFile: $dropz.data('remove-file'),
  841. init: function () {
  842. this.on("success", function (file, data) {
  843. filenameDict[file.name] = data.uuid;
  844. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  845. });
  846. this.on("removedfile", function (file) {
  847. if (file.name in filenameDict) {
  848. $('#' + filenameDict[file.name]).remove();
  849. }
  850. })
  851. }
  852. });
  853. }
  854. // Emojify
  855. emojify.setConfig({
  856. img_dir: suburl + '/img/emoji',
  857. ignore_emoticons: true
  858. });
  859. var hasEmoji = document.getElementsByClassName('has-emoji');
  860. for (var i = 0; i < hasEmoji.length; i++) {
  861. emojify.run(hasEmoji[i]);
  862. }
  863. // Clipboard JS
  864. var clipboard = new Clipboard('.clipboard');
  865. clipboard.on('success', function (e) {
  866. e.clearSelection();
  867. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  868. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  869. $('#' + e.trigger.getAttribute('id')).popup('show');
  870. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  871. });
  872. clipboard.on('error', function (e) {
  873. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  874. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  875. $('#' + e.trigger.getAttribute('id')).popup('show');
  876. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  877. });
  878. // Helpers.
  879. $('.delete-button').click(function () {
  880. var $this = $(this);
  881. $('.delete.modal').modal({
  882. closable: false,
  883. onApprove: function () {
  884. if ($this.data('type') == "form") {
  885. $($this.data('form')).submit();
  886. return;
  887. }
  888. $.post($this.data('url'), {
  889. "_csrf": csrf,
  890. "id": $this.data("id")
  891. }).done(function (data) {
  892. window.location.href = data.redirect;
  893. });
  894. }
  895. }).modal('show');
  896. return false;
  897. });
  898. $('.show-panel.button').click(function () {
  899. $($(this).data('panel')).show();
  900. });
  901. $('.show-modal.button').click(function () {
  902. $($(this).data('modal')).modal('show');
  903. });
  904. $('.delete-post.button').click(function () {
  905. var $this = $(this);
  906. $.post($this.data('request-url'), {
  907. "_csrf": csrf
  908. }).done(function () {
  909. window.location.href = $this.data('done-url');
  910. });
  911. });
  912. // Set anchor.
  913. $('.markdown').each(function () {
  914. var headers = {};
  915. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  916. var node = $(this);
  917. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  918. var name = val;
  919. if (headers[val] > 0) {
  920. name = val + '-' + headers[val];
  921. }
  922. if (headers[val] == undefined) {
  923. headers[val] = 1;
  924. } else {
  925. headers[val] += 1;
  926. }
  927. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  928. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  929. });
  930. });
  931. buttonsClickOnEnter();
  932. searchUsers();
  933. searchRepositories();
  934. initCommentForm();
  935. initInstall();
  936. initRepository();
  937. initWiki();
  938. initOrganization();
  939. initWebhook();
  940. initAdmin();
  941. var routes = {
  942. 'div.user.settings': initUserSettings,
  943. 'div.repository.settings.collaboration': initRepositoryCollaboration
  944. };
  945. var selector;
  946. for (selector in routes) {
  947. if ($(selector).length > 0) {
  948. routes[selector]();
  949. break;
  950. }
  951. }
  952. });
  953. $(window).load(function () {
  954. function changeHash(hash) {
  955. if (history.pushState) {
  956. history.pushState(null, null, hash);
  957. }
  958. else {
  959. location.hash = hash;
  960. }
  961. }
  962. function deSelect() {
  963. if (window.getSelection) {
  964. window.getSelection().removeAllRanges();
  965. } else {
  966. document.selection.empty();
  967. }
  968. }
  969. function selectRange($list, $select, $from) {
  970. $list.removeClass('active');
  971. if ($from) {
  972. var a = parseInt($select.attr('rel').substr(1));
  973. var b = parseInt($from.attr('rel').substr(1));
  974. var c;
  975. if (a != b) {
  976. if (a > b) {
  977. c = a;
  978. a = b;
  979. b = c;
  980. }
  981. var classes = [];
  982. for (var i = a; i <= b; i++) {
  983. classes.push('.L' + i);
  984. }
  985. $list.filter(classes.join(',')).addClass('active');
  986. changeHash('#L' + a + '-' + 'L' + b);
  987. return
  988. }
  989. }
  990. $select.addClass('active');
  991. changeHash('#' + $select.attr('rel'));
  992. }
  993. // Code view.
  994. if ($('.code-view .linenums').length > 0) {
  995. $(document).on('click', '.lines-num span', function (e) {
  996. var $select = $(this);
  997. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  998. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  999. deSelect();
  1000. });
  1001. $(window).on('hashchange', function (e) {
  1002. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  1003. var $list = $('.code-view ol.linenums > li');
  1004. var $first;
  1005. if (m) {
  1006. $first = $list.filter('.' + m[1]);
  1007. selectRange($list, $first, $list.filter('.' + m[2]));
  1008. $("html, body").scrollTop($first.offset().top - 200);
  1009. return;
  1010. }
  1011. m = window.location.hash.match(/^#(L\d+)$/);
  1012. if (m) {
  1013. $first = $list.filter('.' + m[1]);
  1014. selectRange($list, $first);
  1015. $("html, body").scrollTop($first.offset().top - 200);
  1016. }
  1017. }).trigger('hashchange');
  1018. }
  1019. // Repo clone url.
  1020. if ($('#repo-clone-url').length > 0) {
  1021. switch (localStorage.getItem('repo-clone-protocol')) {
  1022. case 'ssh':
  1023. if ($('#repo-clone-ssh').click().length === 0) {
  1024. $('#repo-clone-https').click();
  1025. }
  1026. ;
  1027. break;
  1028. default:
  1029. $('#repo-clone-https').click();
  1030. break;
  1031. }
  1032. }
  1033. });
  1034. $(function () {
  1035. if ($('.user.signin').length > 0) return;
  1036. $('form').areYouSure();
  1037. });