gogs.js 37 KB

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