gogs.js 35 KB

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