gogs.js 35 KB

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