gogs.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. }
  494. function initAdmin() {
  495. if ($('.admin').length == 0) {
  496. return;
  497. }
  498. // New user
  499. if ($('.admin.new.user').length > 0 ||
  500. $('.admin.edit.user').length > 0) {
  501. $('#login_type').change(function () {
  502. if ($(this).val().substring(0, 1) == '0') {
  503. $('#login_name').removeAttr('required');
  504. $('.non-local').hide();
  505. $('.local').show();
  506. $('#user_name').focus();
  507. if ($(this).data('password') == "required") {
  508. $('#password').attr('required', 'required');
  509. }
  510. } else {
  511. $('#login_name').attr('required', 'required');
  512. $('.non-local').show();
  513. $('.local').hide();
  514. $('#login_name').focus();
  515. $('#password').removeAttr('required');
  516. }
  517. });
  518. }
  519. // New authentication
  520. if ($('.admin.new.authentication').length > 0) {
  521. $('#auth_type').change(function () {
  522. $('.ldap').hide();
  523. $('.dldap').hide();
  524. $('.smtp').hide();
  525. $('.pam').hide();
  526. var auth_type = $(this).val();
  527. switch (auth_type) {
  528. case '2': // LDAP
  529. $('.ldap').show();
  530. break;
  531. case '3': // SMTP
  532. $('.smtp').show();
  533. break;
  534. case '4': // PAM
  535. $('.pam').show();
  536. break;
  537. case '5': // LDAP
  538. $('.dldap').show();
  539. break;
  540. }
  541. });
  542. }
  543. // Notice
  544. if ($('.admin.notice')) {
  545. var $detail_modal = $('#detail-modal');
  546. // Attach view detail modals
  547. $('.view-detail').click(function () {
  548. $detail_modal.find('.content p').text($(this).data('content'));
  549. $detail_modal.modal('show');
  550. return false;
  551. });
  552. // Select actions
  553. var $checkboxes = $('.select.table .ui.checkbox');
  554. $('.select.action').click(function () {
  555. switch ($(this).data('action')) {
  556. case 'select-all':
  557. $checkboxes.checkbox('check');
  558. break;
  559. case 'deselect-all':
  560. $checkboxes.checkbox('uncheck');
  561. break;
  562. case 'inverse':
  563. $checkboxes.checkbox('toggle');
  564. break;
  565. }
  566. });
  567. $('#delete-selection').click(function () {
  568. var $this = $(this);
  569. $this.addClass("loading disabled");
  570. var ids = [];
  571. $checkboxes.each(function () {
  572. if ($(this).checkbox('is checked')) {
  573. ids.push($(this).data('id'));
  574. }
  575. });
  576. $.post($this.data('link'), {
  577. "_csrf": csrf,
  578. "ids": ids
  579. }).done(function () {
  580. window.location.href = $this.data('redirect');
  581. });
  582. });
  583. }
  584. }
  585. function buttonsClickOnEnter() {
  586. $('.ui.button').keypress(function (e) {
  587. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  588. $(this).click();
  589. });
  590. }
  591. function hideWhenLostFocus(body, parent) {
  592. $(document).click(function (e) {
  593. var target = e.target;
  594. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  595. $(body).hide();
  596. }
  597. });
  598. }
  599. function searchUsers() {
  600. if (!$('#search-user-box .results').length) {
  601. return;
  602. }
  603. var $search_user_box = $('#search-user-box');
  604. var $result_list = $search_user_box.find('.results');
  605. $search_user_box.keyup(function () {
  606. var $this = $(this);
  607. var keyword = $this.find('input').val();
  608. if (keyword.length < 2) {
  609. $result_list.hide();
  610. return;
  611. }
  612. $.ajax({
  613. url: suburl + '/api/v1/users/search?q=' + keyword,
  614. dataType: "json",
  615. success: function (response) {
  616. var notEmpty = function (str) {
  617. return str && str.length > 0;
  618. };
  619. $result_list.html('');
  620. if (response.ok && response.data.length) {
  621. var html = '';
  622. $.each(response.data, function (i, item) {
  623. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  624. if (notEmpty(item.full_name)) {
  625. html += ' (' + item.full_name + ')';
  626. }
  627. html += '</div>';
  628. });
  629. $result_list.html(html);
  630. $this.find('.results .item').click(function () {
  631. $this.find('input').val($(this).find('.username').text());
  632. $result_list.hide();
  633. });
  634. $result_list.show();
  635. } else {
  636. $result_list.hide();
  637. }
  638. }
  639. });
  640. });
  641. $search_user_box.find('input').focus(function () {
  642. $search_user_box.keyup();
  643. });
  644. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  645. }
  646. // FIXME: merge common parts in two functions
  647. function searchRepositories() {
  648. if (!$('#search-repo-box .results').length) {
  649. return;
  650. }
  651. var $search_repo_box = $('#search-repo-box');
  652. var $result_list = $search_repo_box.find('.results');
  653. $search_repo_box.keyup(function () {
  654. var $this = $(this);
  655. var keyword = $this.find('input').val();
  656. if (keyword.length < 2) {
  657. $result_list.hide();
  658. return;
  659. }
  660. $.ajax({
  661. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $search_repo_box.data('uid'),
  662. dataType: "json",
  663. success: function (response) {
  664. var notEmpty = function (str) {
  665. return str && str.length > 0;
  666. };
  667. $result_list.html('');
  668. if (response.ok && response.data.length) {
  669. var html = '';
  670. $.each(response.data, function (i, item) {
  671. html += '<div class="item"><i class="icon octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  672. });
  673. $result_list.html(html);
  674. $this.find('.results .item').click(function () {
  675. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  676. $result_list.hide();
  677. });
  678. $result_list.show();
  679. } else {
  680. $result_list.hide();
  681. }
  682. }
  683. });
  684. });
  685. $search_repo_box.find('input').focus(function () {
  686. $search_repo_box.keyup();
  687. });
  688. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  689. }
  690. $(document).ready(function () {
  691. csrf = $('meta[name=_csrf]').attr("content");
  692. suburl = $('meta[name=_suburl]').attr("content");
  693. // Show exact time
  694. $('.time-since').each(function () {
  695. $(this).addClass('poping up').
  696. attr('data-content', $(this).attr('title')).
  697. attr('data-variation', 'inverted tiny').
  698. attr('title', '');
  699. });
  700. // Semantic UI modules.
  701. $('.dropdown').dropdown();
  702. $('.jump.dropdown').dropdown({
  703. action: 'hide',
  704. onShow: function () {
  705. $('.poping.up').popup('hide');
  706. }
  707. });
  708. $('.slide.up.dropdown').dropdown({
  709. transition: 'slide up'
  710. });
  711. $('.upward.dropdown').dropdown({
  712. direction: 'upward'
  713. });
  714. $('.ui.accordion').accordion();
  715. $('.ui.checkbox').checkbox();
  716. $('.ui.progress').progress({
  717. showActivity: false
  718. });
  719. $('.poping.up').popup();
  720. $('.top.menu .poping.up').popup({
  721. onShow: function () {
  722. if ($('.top.menu .menu.transition').hasClass('visible')) {
  723. return false;
  724. }
  725. }
  726. });
  727. $('.tabular.menu .item').tab();
  728. $('.tabable.menu .item').tab();
  729. $('.toggle.button').click(function () {
  730. $($(this).data('target')).slideToggle(100);
  731. });
  732. // Highlight JS
  733. if (typeof hljs != 'undefined') {
  734. hljs.initHighlightingOnLoad();
  735. }
  736. // Dropzone
  737. if ($('#dropzone').length > 0) {
  738. // Disable auto discover for all elements:
  739. Dropzone.autoDiscover = false;
  740. var filenameDict = {};
  741. var $dropz = $('#dropzone');
  742. $dropz.dropzone({
  743. url: $dropz.data('upload-url'),
  744. headers: {"X-Csrf-Token": csrf},
  745. maxFiles: $dropz.data('max-file'),
  746. maxFilesize: $dropz.data('max-size'),
  747. acceptedFiles: ($dropz.data('accepts') === '*/*') ? null : $dropz.data('accepts'),
  748. addRemoveLinks: true,
  749. dictDefaultMessage: $dropz.data('default-message'),
  750. dictInvalidFileType: $dropz.data('invalid-input-type'),
  751. dictFileTooBig: $dropz.data('file-too-big'),
  752. dictRemoveFile: $dropz.data('remove-file'),
  753. init: function () {
  754. this.on("success", function (file, data) {
  755. filenameDict[file.name] = data.uuid;
  756. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  757. });
  758. this.on("removedfile", function (file) {
  759. if (file.name in filenameDict) {
  760. $('#' + filenameDict[file.name]).remove();
  761. }
  762. })
  763. }
  764. });
  765. }
  766. // Emojify
  767. emojify.setConfig({
  768. img_dir: suburl + '/img/emoji'
  769. });
  770. emojify.run();
  771. // Clipboard JS
  772. var clipboard = new Clipboard('.clipboard');
  773. clipboard.on('success', function (e) {
  774. e.clearSelection();
  775. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  776. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  777. $('#' + e.trigger.getAttribute('id')).popup('show');
  778. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  779. });
  780. clipboard.on('error', function (e) {
  781. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  782. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  783. $('#' + e.trigger.getAttribute('id')).popup('show');
  784. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  785. });
  786. // Helpers.
  787. $('.delete-button').click(function () {
  788. var $this = $(this);
  789. $('.delete.modal').modal({
  790. closable: false,
  791. onApprove: function () {
  792. if ($this.data('type') == "form") {
  793. $($this.data('form')).submit();
  794. return;
  795. }
  796. $.post($this.data('url'), {
  797. "_csrf": csrf,
  798. "id": $this.data("id")
  799. }).done(function (data) {
  800. window.location.href = data.redirect;
  801. });
  802. }
  803. }).modal('show');
  804. return false;
  805. });
  806. $('.show-panel.button').click(function () {
  807. $($(this).data('panel')).show();
  808. });
  809. $('.show-modal.button').click(function () {
  810. $($(this).data('modal')).modal('show');
  811. });
  812. // Set anchor.
  813. $('.markdown').each(function () {
  814. var headers = {};
  815. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  816. var node = $(this);
  817. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  818. var name = val;
  819. if (headers[val] > 0) {
  820. name = val + '-' + headers[val];
  821. }
  822. if (headers[val] == undefined) {
  823. headers[val] = 1;
  824. } else {
  825. headers[val] += 1;
  826. }
  827. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  828. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  829. });
  830. });
  831. buttonsClickOnEnter();
  832. searchUsers();
  833. searchRepositories();
  834. initCommentForm();
  835. initInstall();
  836. initRepository();
  837. initWiki();
  838. initOrganization();
  839. initUser();
  840. initWebhook();
  841. initAdmin();
  842. });
  843. $(window).load(function () {
  844. function changeHash(hash) {
  845. if (history.pushState) {
  846. history.pushState(null, null, hash);
  847. }
  848. else {
  849. location.hash = hash;
  850. }
  851. }
  852. function deSelect() {
  853. if (window.getSelection) {
  854. window.getSelection().removeAllRanges();
  855. } else {
  856. document.selection.empty();
  857. }
  858. }
  859. function selectRange($list, $select, $from) {
  860. $list.removeClass('active');
  861. if ($from) {
  862. var a = parseInt($select.attr('rel').substr(1));
  863. var b = parseInt($from.attr('rel').substr(1));
  864. var c;
  865. if (a != b) {
  866. if (a > b) {
  867. c = a;
  868. a = b;
  869. b = c;
  870. }
  871. var classes = [];
  872. for (i = a; i <= b; i++) {
  873. classes.push('.L' + i);
  874. }
  875. $list.filter(classes.join(',')).addClass('active');
  876. changeHash('#L' + a + '-' + 'L' + b);
  877. return
  878. }
  879. }
  880. $select.addClass('active');
  881. changeHash('#' + $select.attr('rel'));
  882. }
  883. // Code view.
  884. if ($('.code-view .linenums').length > 0) {
  885. var $block = $('.code-view .linenums');
  886. var lines = $block.html().split("\n");
  887. $block.html('');
  888. var $num_list = $('.code-view .lines-num');
  889. // Building blocks.
  890. for (var i = 0; i < lines.length; i++) {
  891. $block.append('<li class="L' + (i + 1) + '" rel="L' + (i + 1) + '">' + lines[i] + '</li>');
  892. $num_list.append('<span id="L' + (i + 1) + '">' + (i + 1) + '</span>');
  893. }
  894. $(document).on('click', '.lines-num span', function (e) {
  895. var $select = $(this);
  896. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  897. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  898. deSelect();
  899. });
  900. $(window).on('hashchange', function (e) {
  901. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  902. var $list = $('.code-view ol.linenums > li');
  903. var $first;
  904. if (m) {
  905. $first = $list.filter('.' + m[1]);
  906. selectRange($list, $first, $list.filter('.' + m[2]));
  907. $("html, body").scrollTop($first.offset().top - 200);
  908. return;
  909. }
  910. m = window.location.hash.match(/^#(L\d+)$/);
  911. if (m) {
  912. $first = $list.filter('.' + m[1]);
  913. selectRange($list, $first);
  914. $("html, body").scrollTop($first.offset().top - 200);
  915. }
  916. }).trigger('hashchange');
  917. }
  918. });