gogs.js 35 KB

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