gogs.js 38 KB

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