gogs.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. function onSecurityProtocolChange() {
  549. if ($('#security_protocol').val() > 0) {
  550. $('.has-tls').show();
  551. } else {
  552. $('.has-tls').hide();
  553. }
  554. }
  555. // New authentication
  556. if ($('.admin.new.authentication').length > 0) {
  557. $('#auth_type').change(function () {
  558. $('.ldap').hide();
  559. $('.dldap').hide();
  560. $('.smtp').hide();
  561. $('.pam').hide();
  562. $('.has-tls').hide();
  563. var auth_type = $(this).val();
  564. switch (auth_type) {
  565. case '2': // LDAP
  566. $('.ldap').show();
  567. break;
  568. case '3': // SMTP
  569. $('.smtp').show();
  570. $('.has-tls').show();
  571. break;
  572. case '4': // PAM
  573. $('.pam').show();
  574. break;
  575. case '5': // LDAP
  576. $('.dldap').show();
  577. break;
  578. }
  579. if (auth_type == '2' || auth_type == '5') {
  580. onSecurityProtocolChange()
  581. }
  582. });
  583. $('#security_protocol').change(onSecurityProtocolChange)
  584. }
  585. // Edit authentication
  586. if ($('.admin.edit.authentication').length > 0) {
  587. var auth_type = $('#auth_type').val();
  588. if (auth_type == '2' || auth_type == '5') {
  589. $('#security_protocol').change(onSecurityProtocolChange);
  590. }
  591. }
  592. // Notice
  593. if ($('.admin.notice')) {
  594. var $detail_modal = $('#detail-modal');
  595. // Attach view detail modals
  596. $('.view-detail').click(function () {
  597. $detail_modal.find('.content p').text($(this).data('content'));
  598. $detail_modal.modal('show');
  599. return false;
  600. });
  601. // Select actions
  602. var $checkboxes = $('.select.table .ui.checkbox');
  603. $('.select.action').click(function () {
  604. switch ($(this).data('action')) {
  605. case 'select-all':
  606. $checkboxes.checkbox('check');
  607. break;
  608. case 'deselect-all':
  609. $checkboxes.checkbox('uncheck');
  610. break;
  611. case 'inverse':
  612. $checkboxes.checkbox('toggle');
  613. break;
  614. }
  615. });
  616. $('#delete-selection').click(function () {
  617. var $this = $(this);
  618. $this.addClass("loading disabled");
  619. var ids = [];
  620. $checkboxes.each(function () {
  621. if ($(this).checkbox('is checked')) {
  622. ids.push($(this).data('id'));
  623. }
  624. });
  625. $.post($this.data('link'), {
  626. "_csrf": csrf,
  627. "ids": ids
  628. }).done(function () {
  629. window.location.href = $this.data('redirect');
  630. });
  631. });
  632. }
  633. }
  634. function buttonsClickOnEnter() {
  635. $('.ui.button').keypress(function (e) {
  636. if (e.keyCode == 13 || e.keyCode == 32) // enter key or space bar
  637. $(this).click();
  638. });
  639. }
  640. function hideWhenLostFocus(body, parent) {
  641. $(document).click(function (e) {
  642. var target = e.target;
  643. if (!$(target).is(body) && !$(target).parents().is(parent)) {
  644. $(body).hide();
  645. }
  646. });
  647. }
  648. function searchUsers() {
  649. if (!$('#search-user-box .results').length) {
  650. return;
  651. }
  652. var $search_user_box = $('#search-user-box');
  653. var $result_list = $search_user_box.find('.results');
  654. $search_user_box.keyup(function () {
  655. var $this = $(this);
  656. var keyword = $this.find('input').val();
  657. if (keyword.length < 2) {
  658. $result_list.hide();
  659. return;
  660. }
  661. $.ajax({
  662. url: suburl + '/api/v1/users/search?q=' + keyword,
  663. dataType: "json",
  664. success: function (response) {
  665. var notEmpty = function (str) {
  666. return str && str.length > 0;
  667. };
  668. $result_list.html('');
  669. if (response.ok && response.data.length) {
  670. var html = '';
  671. $.each(response.data, function (i, item) {
  672. html += '<div class="item"><img class="ui avatar image" src="' + item.avatar_url + '"><span class="username">' + item.username + '</span>';
  673. if (notEmpty(item.full_name)) {
  674. html += ' (' + item.full_name + ')';
  675. }
  676. html += '</div>';
  677. });
  678. $result_list.html(html);
  679. $this.find('.results .item').click(function () {
  680. $this.find('input').val($(this).find('.username').text());
  681. $result_list.hide();
  682. });
  683. $result_list.show();
  684. } else {
  685. $result_list.hide();
  686. }
  687. }
  688. });
  689. });
  690. $search_user_box.find('input').focus(function () {
  691. $search_user_box.keyup();
  692. });
  693. hideWhenLostFocus('#search-user-box .results', '#search-user-box');
  694. }
  695. // FIXME: merge common parts in two functions
  696. function searchRepositories() {
  697. if (!$('#search-repo-box .results').length) {
  698. return;
  699. }
  700. var $search_repo_box = $('#search-repo-box');
  701. var $result_list = $search_repo_box.find('.results');
  702. $search_repo_box.keyup(function () {
  703. var $this = $(this);
  704. var keyword = $this.find('input').val();
  705. if (keyword.length < 2) {
  706. $result_list.hide();
  707. return;
  708. }
  709. $.ajax({
  710. url: suburl + '/api/v1/repos/search?q=' + keyword + "&uid=" + $search_repo_box.data('uid'),
  711. dataType: "json",
  712. success: function (response) {
  713. var notEmpty = function (str) {
  714. return str && str.length > 0;
  715. };
  716. $result_list.html('');
  717. if (response.ok && response.data.length) {
  718. var html = '';
  719. $.each(response.data, function (i, item) {
  720. html += '<div class="item"><i class="icon octicon octicon-repo"></i> <span class="fullname">' + item.full_name + '</span></div>';
  721. });
  722. $result_list.html(html);
  723. $this.find('.results .item').click(function () {
  724. $this.find('input').val($(this).find('.fullname').text().split("/")[1]);
  725. $result_list.hide();
  726. });
  727. $result_list.show();
  728. } else {
  729. $result_list.hide();
  730. }
  731. }
  732. });
  733. });
  734. $search_repo_box.find('input').focus(function () {
  735. $search_repo_box.keyup();
  736. });
  737. hideWhenLostFocus('#search-repo-box .results', '#search-repo-box');
  738. }
  739. $(document).ready(function () {
  740. csrf = $('meta[name=_csrf]').attr("content");
  741. suburl = $('meta[name=_suburl]').attr("content");
  742. // Show exact time
  743. $('.time-since').each(function () {
  744. $(this).addClass('poping up').attr('data-content', $(this).attr('title')).attr('data-variation', 'inverted tiny').attr('title', '');
  745. });
  746. // Semantic UI modules.
  747. $('.dropdown').dropdown();
  748. $('.jump.dropdown').dropdown({
  749. action: 'hide',
  750. onShow: function () {
  751. $('.poping.up').popup('hide');
  752. }
  753. });
  754. $('.slide.up.dropdown').dropdown({
  755. transition: 'slide up'
  756. });
  757. $('.upward.dropdown').dropdown({
  758. direction: 'upward'
  759. });
  760. $('.ui.accordion').accordion();
  761. $('.ui.checkbox').checkbox();
  762. $('.ui.progress').progress({
  763. showActivity: false
  764. });
  765. $('.poping.up').popup();
  766. $('.top.menu .poping.up').popup({
  767. onShow: function () {
  768. if ($('.top.menu .menu.transition').hasClass('visible')) {
  769. return false;
  770. }
  771. }
  772. });
  773. $('.tabular.menu .item').tab();
  774. $('.tabable.menu .item').tab();
  775. $('.toggle.button').click(function () {
  776. $($(this).data('target')).slideToggle(100);
  777. });
  778. // Highlight JS
  779. if (typeof hljs != 'undefined') {
  780. hljs.initHighlightingOnLoad();
  781. }
  782. // Dropzone
  783. if ($('#dropzone').length > 0) {
  784. // Disable auto discover for all elements:
  785. Dropzone.autoDiscover = false;
  786. var filenameDict = {};
  787. var $dropz = $('#dropzone');
  788. $dropz.dropzone({
  789. url: $dropz.data('upload-url'),
  790. headers: {"X-Csrf-Token": csrf},
  791. maxFiles: $dropz.data('max-file'),
  792. maxFilesize: $dropz.data('max-size'),
  793. acceptedFiles: ($dropz.data('accepts') === '*/*') ? null : $dropz.data('accepts'),
  794. addRemoveLinks: true,
  795. dictDefaultMessage: $dropz.data('default-message'),
  796. dictInvalidFileType: $dropz.data('invalid-input-type'),
  797. dictFileTooBig: $dropz.data('file-too-big'),
  798. dictRemoveFile: $dropz.data('remove-file'),
  799. init: function () {
  800. this.on("success", function (file, data) {
  801. filenameDict[file.name] = data.uuid;
  802. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  803. });
  804. this.on("removedfile", function (file) {
  805. if (file.name in filenameDict) {
  806. $('#' + filenameDict[file.name]).remove();
  807. }
  808. })
  809. }
  810. });
  811. }
  812. // Emojify
  813. emojify.setConfig({
  814. img_dir: suburl + '/img/emoji',
  815. ignore_emoticons: true
  816. });
  817. var hasEmoji = document.getElementsByClassName('has-emoji');
  818. for (var i = 0; i < hasEmoji.length; i++) {
  819. emojify.run(hasEmoji[i]);
  820. }
  821. // Clipboard JS
  822. var clipboard = new Clipboard('.clipboard');
  823. clipboard.on('success', function (e) {
  824. e.clearSelection();
  825. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  826. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-success'))
  827. $('#' + e.trigger.getAttribute('id')).popup('show');
  828. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  829. });
  830. clipboard.on('error', function (e) {
  831. $('#' + e.trigger.getAttribute('id')).popup('destroy');
  832. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-error'))
  833. $('#' + e.trigger.getAttribute('id')).popup('show');
  834. e.trigger.setAttribute('data-content', e.trigger.getAttribute('data-original'))
  835. });
  836. // Helpers.
  837. $('.delete-button').click(function () {
  838. var $this = $(this);
  839. $('.delete.modal').modal({
  840. closable: false,
  841. onApprove: function () {
  842. if ($this.data('type') == "form") {
  843. $($this.data('form')).submit();
  844. return;
  845. }
  846. $.post($this.data('url'), {
  847. "_csrf": csrf,
  848. "id": $this.data("id")
  849. }).done(function (data) {
  850. window.location.href = data.redirect;
  851. });
  852. }
  853. }).modal('show');
  854. return false;
  855. });
  856. $('.show-panel.button').click(function () {
  857. $($(this).data('panel')).show();
  858. });
  859. $('.show-modal.button').click(function () {
  860. $($(this).data('modal')).modal('show');
  861. });
  862. $('.delete-post.button').click(function(){
  863. var $this = $(this);
  864. $.post($this.data('request-url'),{
  865. "_csrf": csrf
  866. }).done(function(){
  867. window.location.href = $this.data('done-url');
  868. });
  869. });
  870. // Set anchor.
  871. $('.markdown').each(function () {
  872. var headers = {};
  873. $(this).find('h1, h2, h3, h4, h5, h6').each(function () {
  874. var node = $(this);
  875. var val = encodeURIComponent(node.text().toLowerCase().replace(/[^\w\- ]/g, '').replace(/[ ]/g, '-'));
  876. var name = val;
  877. if (headers[val] > 0) {
  878. name = val + '-' + headers[val];
  879. }
  880. if (headers[val] == undefined) {
  881. headers[val] = 1;
  882. } else {
  883. headers[val] += 1;
  884. }
  885. node = node.wrap('<div id="' + name + '" class="anchor-wrap" ></div>');
  886. node.append('<a class="anchor" href="#' + name + '"><span class="octicon octicon-link"></span></a>');
  887. });
  888. });
  889. buttonsClickOnEnter();
  890. searchUsers();
  891. searchRepositories();
  892. initCommentForm();
  893. initInstall();
  894. initRepository();
  895. initWiki();
  896. initOrganization();
  897. initWebhook();
  898. initAdmin();
  899. var routes = {
  900. 'div.user.settings': initUserSettings,
  901. 'div.repository.settings.collaboration': initRepositoryCollaboration
  902. };
  903. var selector;
  904. for (selector in routes) {
  905. if ($(selector).length > 0) {
  906. routes[selector]();
  907. break;
  908. }
  909. }
  910. });
  911. $(window).load(function () {
  912. function changeHash(hash) {
  913. if (history.pushState) {
  914. history.pushState(null, null, hash);
  915. }
  916. else {
  917. location.hash = hash;
  918. }
  919. }
  920. function deSelect() {
  921. if (window.getSelection) {
  922. window.getSelection().removeAllRanges();
  923. } else {
  924. document.selection.empty();
  925. }
  926. }
  927. function selectRange($list, $select, $from) {
  928. $list.removeClass('active');
  929. if ($from) {
  930. var a = parseInt($select.attr('rel').substr(1));
  931. var b = parseInt($from.attr('rel').substr(1));
  932. var c;
  933. if (a != b) {
  934. if (a > b) {
  935. c = a;
  936. a = b;
  937. b = c;
  938. }
  939. var classes = [];
  940. for (i = a; i <= b; i++) {
  941. classes.push('.L' + i);
  942. }
  943. $list.filter(classes.join(',')).addClass('active');
  944. changeHash('#L' + a + '-' + 'L' + b);
  945. return
  946. }
  947. }
  948. $select.addClass('active');
  949. changeHash('#' + $select.attr('rel'));
  950. }
  951. // Code view.
  952. if ($('.code-view .linenums').length > 0) {
  953. var $block = $('.code-view .linenums');
  954. var lines = $block.html().split("\n");
  955. $block.html('');
  956. var $num_list = $('.code-view .lines-num');
  957. // Building blocks.
  958. var $toappendblock = [];
  959. var $toappendnum_list = [];
  960. for (var i = 0; i < lines.length; i++) {
  961. $toappendblock.push('<li class="L' + (i + 1) + '" rel="L' + (i + 1) + '">' + lines[i] + '</li>');
  962. $toappendnum_list.push('<span id="L' + (i + 1) + '">' + (i + 1) + '</span>');
  963. }
  964. $block.append($toappendblock.join(''));
  965. $num_list.append($toappendnum_list.join(''));
  966. $(document).on('click', '.lines-num span', function (e) {
  967. var $select = $(this);
  968. var $list = $select.parent().siblings('.lines-code').find('ol.linenums > li');
  969. selectRange($list, $list.filter('[rel=' + $select.attr('id') + ']'), (e.shiftKey ? $list.filter('.active').eq(0) : null));
  970. deSelect();
  971. });
  972. $(window).on('hashchange', function (e) {
  973. var m = window.location.hash.match(/^#(L\d+)\-(L\d+)$/);
  974. var $list = $('.code-view ol.linenums > li');
  975. var $first;
  976. if (m) {
  977. $first = $list.filter('.' + m[1]);
  978. selectRange($list, $first, $list.filter('.' + m[2]));
  979. $("html, body").scrollTop($first.offset().top - 200);
  980. return;
  981. }
  982. m = window.location.hash.match(/^#(L\d+)$/);
  983. if (m) {
  984. $first = $list.filter('.' + m[1]);
  985. selectRange($list, $first);
  986. $("html, body").scrollTop($first.offset().top - 200);
  987. }
  988. }).trigger('hashchange');
  989. }
  990. // Repo clone url.
  991. if ($('#repo-clone-url').length > 0) {
  992. switch (localStorage.getItem('repo-clone-protocol')) {
  993. case 'ssh':
  994. if ($('#repo-clone-ssh').click().length === 0) {
  995. $('#repo-clone-https').click();
  996. }
  997. ;
  998. break;
  999. default:
  1000. $('#repo-clone-https').click();
  1001. break;
  1002. }
  1003. }
  1004. });
  1005. $(function() {
  1006. $('form').areYouSure();
  1007. });