gogs.js 35 KB

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