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

1060 lines
35 KiB

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