You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

644 lines
21 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. 'use strict';
  2. var csrf;
  3. var suburl;
  4. function initCommentPreviewTab($form) {
  5. var $tab_menu = $form.find('.tabular.menu');
  6. $tab_menu.find('.item').tab();
  7. $tab_menu.find('.item[data-tab="' + $tab_menu.data('preview') + '"]').click(function () {
  8. var $this = $(this);
  9. $.post($this.data('url'), {
  10. "_csrf": csrf,
  11. "mode": "gfm",
  12. "context": $this.data('context'),
  13. "text": $form.find('.tab.segment[data-tab="' + $tab_menu.data('write') + '"] textarea').val()
  14. },
  15. function (data) {
  16. var $preview_tab = $form.find('.tab.segment[data-tab="' + $tab_menu.data('preview') + '"]');
  17. $preview_tab.html(data);
  18. emojify.run($preview_tab[0]);
  19. }
  20. );
  21. });
  22. }
  23. function initCommentForm() {
  24. if ($('.comment.form').length == 0) {
  25. return
  26. }
  27. initCommentPreviewTab($('.comment.form'));
  28. // Labels
  29. var $list = $('.ui.labels.list');
  30. var $no_select = $list.find('.no-select');
  31. var $label_menu = $('.select-label .menu');
  32. var has_label_update_action = $label_menu.data('action') == 'update';
  33. function updateIssueMeta(url, action, id) {
  34. $.post(url, {
  35. "_csrf": csrf,
  36. "action": action,
  37. "id": id
  38. });
  39. }
  40. $label_menu.find('.item:not(.no-select)').click(function () {
  41. if ($(this).hasClass('checked')) {
  42. $(this).removeClass('checked');
  43. $(this).find('.octicon').removeClass('octicon-check');
  44. if (has_label_update_action) {
  45. updateIssueMeta($label_menu.data('update-url'), "detach", $(this).data('id'));
  46. }
  47. } else {
  48. $(this).addClass('checked');
  49. $(this).find('.octicon').addClass('octicon-check');
  50. if (has_label_update_action) {
  51. updateIssueMeta($label_menu.data('update-url'), "attach", $(this).data('id'));
  52. }
  53. }
  54. var label_ids = "";
  55. $(this).parent().find('.item').each(function () {
  56. if ($(this).hasClass('checked')) {
  57. label_ids += $(this).data('id') + ",";
  58. $($(this).data('id-selector')).removeClass('hide');
  59. } else {
  60. $($(this).data('id-selector')).addClass('hide');
  61. }
  62. });
  63. if (label_ids.length == 0) {
  64. $no_select.removeClass('hide');
  65. } else {
  66. $no_select.addClass('hide');
  67. }
  68. $($(this).parent().data('id')).val(label_ids);
  69. return false;
  70. });
  71. $label_menu.find('.no-select.item').click(function () {
  72. if (has_label_update_action) {
  73. updateIssueMeta($label_menu.data('update-url'), "clear", '');
  74. }
  75. $(this).parent().find('.item').each(function () {
  76. $(this).removeClass('checked');
  77. $(this).find('.octicon').removeClass('octicon-check');
  78. });
  79. $list.find('.item').each(function () {
  80. $(this).addClass('hide');
  81. });
  82. $no_select.removeClass('hide');
  83. $($(this).parent().data('id')).val('');
  84. });
  85. function selectItem(select_id, input_id) {
  86. var $menu = $(select_id + ' .menu');
  87. var $list = $('.ui' + select_id + '.list');
  88. var has_update_action = $menu.data('action') == 'update';
  89. $menu.find('.item:not(.no-select)').click(function () {
  90. $(this).parent().find('.item').each(function () {
  91. $(this).removeClass('selected active')
  92. });
  93. $(this).addClass('selected active');
  94. if (has_update_action) {
  95. updateIssueMeta($menu.data('update-url'), '', $(this).data('id'));
  96. }
  97. switch (input_id) {
  98. case '#milestone_id':
  99. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  100. $(this).text() + '</a>');
  101. break;
  102. case '#assignee_id':
  103. $list.find('.selected').html('<a class="item" href=' + $(this).data('href') + '>' +
  104. '<img class="ui avatar image" src=' + $(this).data('avatar') + '>' +
  105. $(this).text() + '</a>');
  106. }
  107. $('.ui' + select_id + '.list .no-select').addClass('hide');
  108. $(input_id).val($(this).data('id'));
  109. });
  110. $menu.find('.no-select.item').click(function () {
  111. $(this).parent().find('.item:not(.no-select)').each(function () {
  112. $(this).removeClass('selected active')
  113. });
  114. if (has_update_action) {
  115. updateIssueMeta($menu.data('update-url'), '', '');
  116. }
  117. $list.find('.selected').html('');
  118. $list.find('.no-select').removeClass('hide');
  119. $(input_id).val('');
  120. });
  121. }
  122. // Milestone and assignee
  123. selectItem('.select-milestone', '#milestone_id');
  124. selectItem('.select-assignee', '#assignee_id');
  125. }
  126. function initInstall() {
  127. if ($('.install').length == 0) {
  128. return;
  129. }
  130. // Database type change detection.
  131. $("#db_type").change(function () {
  132. var sqlite_default = 'data/gogs.db';
  133. var tidb_default = 'data/gogs_tidb';
  134. var db_type = $(this).val();
  135. if (db_type === "SQLite3" || db_type === "TiDB") {
  136. $('#sql_settings').hide();
  137. $('#pgsql_settings').hide();
  138. $('#sqlite_settings').show();
  139. if (db_type === "SQLite3" && $('#db_path').val() == tidb_default) {
  140. $('#db_path').val(sqlite_default);
  141. } else if (db_type === "TiDB" && $('#db_path').val() == sqlite_default) {
  142. $('#db_path').val(tidb_default);
  143. }
  144. return;
  145. }
  146. var mysql_default = '127.0.0.1:3306';
  147. var postgres_default = '127.0.0.1:5432';
  148. $('#sqlite_settings').hide();
  149. $('#sql_settings').show();
  150. if (db_type === "PostgreSQL") {
  151. $('#pgsql_settings').show();
  152. if ($('#db_host').val() == mysql_default) {
  153. $('#db_host').val(postgres_default);
  154. }
  155. } else {
  156. $('#pgsql_settings').hide();
  157. if ($('#db_host').val() == postgres_default) {
  158. $('#db_host').val(mysql_default);
  159. }
  160. }
  161. });
  162. $('#offline-mode input').change(function () {
  163. if ($(this).is(':checked')) {
  164. $('#disable-gravatar').checkbox('check');
  165. }
  166. });
  167. $('#disable-registration input').change(function () {
  168. if ($(this).is(':checked')) {
  169. $('#enable-captcha').checkbox('uncheck');
  170. }
  171. });
  172. $('#enable-captcha input').change(function () {
  173. if ($(this).is(':checked')) {
  174. $('#disable-registration').checkbox('uncheck');
  175. }
  176. });
  177. }
  178. function initRepository() {
  179. if ($('.repository').length == 0) {
  180. return;
  181. }
  182. // Options
  183. if ($('.repository.settings.options').length > 0) {
  184. $('#repo_name').keyup(function () {
  185. var $prompt_span = $('#repo-name-change-prompt');
  186. if ($(this).val().toString().toLowerCase() != $(this).data('repo-name').toString().toLowerCase()) {
  187. $prompt_span.show();
  188. } else {
  189. $prompt_span.hide();
  190. }
  191. });
  192. }
  193. // Labels
  194. if ($('.repository.labels').length > 0) {
  195. // Create label
  196. var $new_label_panel = $('.new-label.segment');
  197. $('.new-label.button').click(function () {
  198. $new_label_panel.show();
  199. });
  200. $('.new-label.segment .cancel').click(function () {
  201. $new_label_panel.hide();
  202. });
  203. $('.color-picker').each(function () {
  204. $(this).minicolors();
  205. });
  206. $('.precolors .color').click(function () {
  207. var color_hex = $(this).data('color-hex');
  208. $('.color-picker').val(color_hex);
  209. $('.minicolors-swatch-color').css("background-color", color_hex);
  210. });
  211. $('.edit-label-button').click(function () {
  212. $('#label-modal-id').val($(this).data('id'));
  213. $('.edit-label .new-label-input').val($(this).data('title'));
  214. $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
  215. $('.edit-label.modal').modal({
  216. onApprove: function () {
  217. $('.edit-label.form').submit();
  218. }
  219. }).modal('show');
  220. return false;
  221. });
  222. }
  223. // Milestones
  224. if ($('.repository.milestones').length > 0) {
  225. }
  226. if ($('.repository.new.milestone').length > 0) {
  227. var $datepicker = $('.milestone.datepicker');
  228. $datepicker.datetimepicker({
  229. lang: $datepicker.data('lang'),
  230. inline: true,
  231. timepicker: false,
  232. startDate: $datepicker.data('start-date'),
  233. formatDate: 'Y-m-d',
  234. onSelectDate: function (ct) {
  235. $('#deadline').val(ct.dateFormat('Y-m-d'));
  236. }
  237. });
  238. $('#clear-date').click(function () {
  239. $('#deadline').val('');
  240. return false;
  241. });
  242. }
  243. // Issues
  244. if ($('.repository.view.issue').length > 0) {
  245. // Edit issue title
  246. var $issue_title = $('#issue-title');
  247. var $edit_input = $('#edit-title-input input');
  248. var editTitleToggle = function () {
  249. $issue_title.toggle();
  250. $('.not-in-edit').toggle();
  251. $('#edit-title-input').toggle();
  252. $('.in-edit').toggle();
  253. $edit_input.focus();
  254. return false;
  255. };
  256. $('#edit-title').click(editTitleToggle);
  257. $('#cancel-edit-title').click(editTitleToggle);
  258. $('#save-edit-title').click(editTitleToggle).
  259. click(function () {
  260. if ($edit_input.val().length == 0 ||
  261. $edit_input.val() == $issue_title.text()) {
  262. $edit_input.val($issue_title.text());
  263. return false;
  264. }
  265. $.post($(this).data('update-url'), {
  266. "_csrf": csrf,
  267. "title": $edit_input.val()
  268. },
  269. function (data) {
  270. $edit_input.val(data.title);
  271. $issue_title.text(data.title);
  272. });
  273. return false;
  274. });
  275. // Edit issue or comment content
  276. $('.edit-content').click(function () {
  277. var $segment = $(this).parent().parent().next();
  278. var $edit_content_zone = $segment.find('.edit-content-zone');
  279. var $render_content = $segment.find('.render-content');
  280. var $raw_content = $segment.find('.raw-content');
  281. var $textarea;
  282. // Setup new form
  283. if ($edit_content_zone.html().length == 0) {
  284. $edit_content_zone.html($('#edit-content-form').html());
  285. $textarea = $segment.find('textarea');
  286. // Give new write/preview data-tab name to distinguish from others
  287. var $edit_content_form = $edit_content_zone.find('.ui.comment.form');
  288. var $tabular_menu = $edit_content_form.find('.tabular.menu');
  289. $tabular_menu.attr('data-write', $edit_content_zone.data('write'));
  290. $tabular_menu.attr('data-preview', $edit_content_zone.data('preview'));
  291. $tabular_menu.find('.write.item').attr('data-tab', $edit_content_zone.data('write'));
  292. $tabular_menu.find('.preview.item').attr('data-tab', $edit_content_zone.data('preview'));
  293. $edit_content_form.find('.write.segment').attr('data-tab', $edit_content_zone.data('write'));
  294. $edit_content_form.find('.preview.segment').attr('data-tab', $edit_content_zone.data('preview'));
  295. initCommentPreviewTab($edit_content_form);
  296. $edit_content_zone.find('.cancel.button').click(function () {
  297. $render_content.show();
  298. $edit_content_zone.hide();
  299. });
  300. $edit_content_zone.find('.save.button').click(function () {
  301. $render_content.show();
  302. $edit_content_zone.hide();
  303. $.post($edit_content_zone.data('update-url'), {
  304. "_csrf": csrf,
  305. "content": $textarea.val(),
  306. "context": $edit_content_zone.data('context')
  307. },
  308. function (data) {
  309. if (data.length == 0) {
  310. $render_content.html($('#no-content').html());
  311. } else {
  312. $render_content.html(data.content);
  313. emojify.run($render_content[0]);
  314. }
  315. });
  316. });
  317. } else {
  318. $textarea = $segment.find('textarea');
  319. }
  320. // Show write/preview tab and copy raw content as needed
  321. $edit_content_zone.show();
  322. $render_content.hide();
  323. if ($textarea.val().length == 0) {
  324. $textarea.val($raw_content.text());
  325. }
  326. $textarea.focus();
  327. return false;
  328. });
  329. // Change status
  330. var $status_btn = $('#status-button');
  331. $('#content').keyup(function () {
  332. if ($(this).val().length == 0) {
  333. $status_btn.text($status_btn.data('status'))
  334. } else {
  335. $status_btn.text($status_btn.data('status-and-comment'))
  336. }
  337. });
  338. $status_btn.click(function () {
  339. $('#status').val($status_btn.data('status-val'));
  340. $('#comment-form').submit();
  341. })
  342. }
  343. // Diff
  344. if ($('.repository.diff').length > 0) {
  345. var $counter = $('.diff-counter');
  346. if ($counter.length < 1) {
  347. return;
  348. }
  349. $counter.each(function (i, item) {
  350. var $item = $(item);
  351. var addLine = $item.find('span[data-line].add').data("line");
  352. var delLine = $item.find('span[data-line].del').data("line");
  353. var addPercent = parseFloat(addLine) / (parseFloat(addLine) + parseFloat(delLine)) * 100;
  354. $item.find(".bar .add").css("width", addPercent + "%");
  355. });
  356. }
  357. // Pull request
  358. if ($('.repository.compare.pull').length > 0) {
  359. var $branch_dropdown = $('.choose.branch .dropdown');
  360. $branch_dropdown.dropdown({
  361. fullTextSearch: true,
  362. onChange: function (text, value, $choice) {
  363. window.location.href = $choice.data('url');
  364. },
  365. message: {noResults: $branch_dropdown.data('no-results')}
  366. });
  367. }
  368. }
  369. function initOrganization() {
  370. if ($('.organization').length == 0) {
  371. return;
  372. }
  373. // Options
  374. if ($('.organization.settings.options').length > 0) {
  375. $('#org_name').keyup(function () {
  376. var $prompt_span = $('#org-name-change-prompt');
  377. if ($(this).val().toString().toLowerCase() != $(this).data('org-name').toString().toLowerCase()) {
  378. $prompt_span.show();
  379. } else {
  380. $prompt_span.hide();
  381. }
  382. });
  383. }
  384. }
  385. function initUser() {
  386. if ($('.user').length == 0) {
  387. return;
  388. }
  389. // Options
  390. if ($('.user.settings.profile').length > 0) {
  391. $('#username').keyup(function () {
  392. var $prompt_span = $('#name-change-prompt');
  393. if ($(this).val().toString().toLowerCase() != $(this).data('name').toString().toLowerCase()) {
  394. $prompt_span.show();
  395. } else {
  396. $prompt_span.hide();
  397. }
  398. });
  399. }
  400. }
  401. function initWebhook() {
  402. if ($('.new.webhook').length == 0) {
  403. return;
  404. }
  405. $('.events.checkbox input').change(function () {
  406. if ($(this).is(':checked')) {
  407. $('.events.fields').show();
  408. }
  409. });
  410. $('.non-events.checkbox input').change(function () {
  411. if ($(this).is(':checked')) {
  412. $('.events.fields').hide();
  413. }
  414. });
  415. }
  416. function initAdmin() {
  417. if ($('.admin').length == 0) {
  418. return;
  419. }
  420. // New user
  421. if ($('.admin.new.user').length > 0 ||
  422. $('.admin.edit.user').length > 0) {
  423. $('#login_type').change(function () {
  424. if ($(this).val().substring(0, 1) == '0') {
  425. $('#login_name').removeAttr('required');
  426. $('.non-local').hide();
  427. $('.local').show();
  428. $('#user_name').focus();
  429. if($(this).data('password')=="required"){
  430. $('#password').attr('required', 'required');
  431. }
  432. } else {
  433. $('#login_name').attr('required', 'required');
  434. $('.non-local').show();
  435. $('.local').hide();
  436. $('#login_name').focus();
  437. $('#password').removeAttr('required');
  438. }
  439. });
  440. }
  441. // New authentication
  442. if ($('.admin.new.authentication').length > 0) {
  443. $('#auth_type').change(function () {
  444. $('.ldap').hide();
  445. $('.dldap').hide();
  446. $('.smtp').hide();
  447. $('.pam').hide();
  448. var auth_type = $(this).val();
  449. switch (auth_type) {
  450. case '2': // LDAP
  451. $('.ldap').show();
  452. break;
  453. case '3': // SMTP
  454. $('.smtp').show();
  455. break;
  456. case '4': // PAM
  457. $('.pam').show();
  458. break;
  459. case '5': // LDAP
  460. $('.dldap').show();
  461. break;
  462. }
  463. });
  464. }
  465. }
  466. $(document).ready(function () {
  467. csrf = $('meta[name=_csrf]').attr("content");
  468. suburl = $('meta[name=_suburl]').attr("content");
  469. // Show exact time
  470. $('.time-since').each(function () {
  471. $(this).addClass('poping up').
  472. attr('data-content', $(this).attr('title')).
  473. attr('data-variation', 'inverted tiny').
  474. attr('title', '');
  475. });
  476. // Semantic UI modules.
  477. $('.dropdown').dropdown();
  478. $('.jump.dropdown').dropdown({
  479. action: 'hide',
  480. onShow: function () {
  481. $('.poping.up').popup('hide');
  482. }
  483. });
  484. $('.slide.up.dropdown').dropdown({
  485. transition: 'slide up'
  486. });
  487. $('.ui.accordion').accordion();
  488. $('.ui.checkbox').checkbox();
  489. $('.ui.progress').progress({
  490. showActivity: false
  491. });
  492. $('.poping.up').popup();
  493. $('.top.menu .poping.up').popup({
  494. onShow: function () {
  495. if ($('.top.menu .menu.transition').hasClass('visible')) {
  496. return false;
  497. }
  498. }
  499. });
  500. $('.tabular.menu .item').tab();
  501. $('.toggle.button').click(function () {
  502. $($(this).data('target')).slideToggle(100);
  503. });
  504. // Highlight JS
  505. if (typeof hljs != 'undefined') {
  506. hljs.initHighlightingOnLoad();
  507. }
  508. // Dropzone
  509. if ($('#dropzone').length > 0) {
  510. // Disable auto discover for all elements:
  511. Dropzone.autoDiscover = false;
  512. var filenameDict = {};
  513. var $dropz = $('#dropzone');
  514. $dropz.dropzone({
  515. url: $dropz.data('upload-url'),
  516. headers: {"X-Csrf-Token": csrf},
  517. maxFiles: $dropz.data('max-file'),
  518. maxFilesize: $dropz.data('max-size'),
  519. acceptedFiles: $dropz.data('accepts'),
  520. addRemoveLinks: true,
  521. dictDefaultMessage: $dropz.data('default-message'),
  522. dictInvalidFileType: $dropz.data('invalid-input-type'),
  523. dictFileTooBig: $dropz.data('file-too-big'),
  524. dictRemoveFile: $dropz.data('remove-file'),
  525. init: function () {
  526. this.on("success", function (file, data) {
  527. filenameDict[file.name] = data.uuid;
  528. $('.attachments').append('<input id="' + data.uuid + '" name="attachments" type="hidden" value="' + data.uuid + '">');
  529. });
  530. this.on("removedfile", function (file) {
  531. if (file.name in filenameDict) {
  532. $('#' + filenameDict[file.name]).remove();
  533. }
  534. })
  535. }
  536. });
  537. }
  538. // Emojify
  539. emojify.setConfig({
  540. img_dir: suburl + '/img/emoji'
  541. });
  542. $('.emojify').each(function () {
  543. emojify.run($(this)[0]);
  544. });
  545. // Helpers.
  546. $('.delete-button').click(function () {
  547. var $this = $(this);
  548. $('.delete.modal').modal({
  549. closable: false,
  550. onApprove: function () {
  551. if ($this.data('type') == "form") {
  552. $($this.data('form')).submit();
  553. return;
  554. }
  555. $.post($this.data('url'), {
  556. "_csrf": csrf,
  557. "id": $this.data("id")
  558. }).done(function (data) {
  559. window.location.href = data.redirect;
  560. });
  561. }
  562. }).modal('show');
  563. return false;
  564. });
  565. $('.show-panel.button').click(function () {
  566. $($(this).data('panel')).show();
  567. });
  568. $('.show-modal.button').click(function () {
  569. $($(this).data('modal')).modal('show');
  570. });
  571. initCommentForm();
  572. initInstall();
  573. initRepository();
  574. initOrganization();
  575. initUser();
  576. initWebhook();
  577. initAdmin();
  578. });