gogs.js 36 KB

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