gogs.js 35 KB

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