Skip to content

Commit ffe4556

Browse files
[CCI] Replace jquery usage in console plugin with native methods (opensearch-project#3733)
* Remove jquery import and unused mock test * Removed jquery imports and replaced jquery functions and methods to native js in console plugin tests * Removed jquery imports and replaced jquery functions to native js in console plugin * Adding a changelog entry * Accept changes from new mappings * Update to template string Co-authored-by: Josh Romero <rmerqg@amazon.com> Signed-off-by: Alexei Karikov <karikov.alist.ru@gmail.com> --------- Signed-off-by: Alexei Karikov <karikov.alist.ru@gmail.com> Co-authored-by: Josh Romero <rmerqg@amazon.com>
1 parent 1edb195 commit ffe4556

File tree

9 files changed

+33
-52
lines changed

9 files changed

+33
-52
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
183183
- [Console] Replace jQuery.ajax with core.http when calling OSD APIs in console ([#3080](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3080))
184184
- [I18n] Fix Listr type errors and error handlers ([#3629](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3629))
185185
- [Multiple DataSource] Present the authentication type choices in a drop-down ([#3693](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3693))
186+
- [Console] Replace jQuery usage in console plugin with native methods ([#3733](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3733))
186187

187188
### 🔩 Tests
188189

src/plugins/console/public/application/models/legacy_core_editor/__tests__/input.test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import '../legacy_core_editor.test.mocks';
3232
import RowParser from '../../../../lib/row_parser';
3333
import { createTokenIterator } from '../../../factories';
34-
import $ from 'jquery';
3534
import { create } from '../create';
3635

3736
describe('Input', () => {
@@ -46,10 +45,10 @@ describe('Input', () => {
4645

4746
coreEditor = create(document.querySelector('#ConAppEditor'));
4847

49-
$(coreEditor.getContainer()).show();
48+
coreEditor.getContainer().style.display = '';
5049
});
5150
afterEach(() => {
52-
$(coreEditor.getContainer()).hide();
51+
coreEditor.getContainer().style.display = 'none';
5352
});
5453

5554
describe('.getLineCount', () => {

src/plugins/console/public/application/models/legacy_core_editor/__tests__/output_tokenization.test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030

3131
import '../legacy_core_editor.test.mocks';
32-
import $ from 'jquery';
3332
import RowParser from '../../../../lib/row_parser';
3433
import ace from 'brace';
3534
import { createReadOnlyAceEditor } from '../create_readonly';
@@ -39,11 +38,11 @@ const tokenIterator = ace.acequire('ace/token_iterator');
3938
describe('Output Tokenization', () => {
4039
beforeEach(() => {
4140
output = createReadOnlyAceEditor(document.querySelector('#ConAppOutput'));
42-
$(output.container).show();
41+
output.container.style.display = '';
4342
});
4443

4544
afterEach(() => {
46-
$(output.container).hide();
45+
output.container.style.display = 'none';
4746
});
4847

4948
function tokensAsList() {

src/plugins/console/public/application/models/legacy_core_editor/legacy_core_editor.ts

+13-18
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import ace from 'brace';
3232
import { Editor as IAceEditor, IEditSession as IAceEditSession } from 'brace';
33-
import $ from 'jquery';
3433
import {
3534
CoreEditor,
3635
Position,
@@ -54,11 +53,11 @@ const rangeToAceRange = ({ start, end }: Range) =>
5453

5554
export class LegacyCoreEditor implements CoreEditor {
5655
private _aceOnPaste: any;
57-
$actions: any;
56+
actions: any;
5857
resize: () => void;
5958

6059
constructor(private readonly editor: IAceEditor, actions: HTMLElement) {
61-
this.$actions = $(actions);
60+
this.actions = actions;
6261
this.editor.setShowPrintMargin(false);
6362

6463
const session = this.editor.getSession();
@@ -274,20 +273,16 @@ export class LegacyCoreEditor implements CoreEditor {
274273

275274
private setActionsBar = (value?: any, topOrBottom: 'top' | 'bottom' = 'top') => {
276275
if (value === null) {
277-
this.$actions.css('visibility', 'hidden');
276+
this.actions.style.visibility = 'hidden';
278277
} else {
279278
if (topOrBottom === 'top') {
280-
this.$actions.css({
281-
bottom: 'auto',
282-
top: value,
283-
visibility: 'visible',
284-
});
279+
this.actions.style.bottom = 'auto';
280+
this.actions.style.top = value;
281+
this.actions.style.visibility = 'visible';
285282
} else {
286-
this.$actions.css({
287-
top: 'auto',
288-
bottom: value,
289-
visibility: 'visible',
290-
});
283+
this.actions.style.top = 'auto';
284+
this.actions.style.bottom = value;
285+
this.actions.style.visibility = 'visible';
291286
}
292287
}
293288
};
@@ -318,14 +313,14 @@ export class LegacyCoreEditor implements CoreEditor {
318313
}
319314

320315
legacyUpdateUI(range: any) {
321-
if (!this.$actions) {
316+
if (!this.actions) {
322317
return;
323318
}
324319
if (range) {
325320
// elements are positioned relative to the editor's container
326321
// pageY is relative to page, so subtract the offset
327322
// from pageY to get the new top value
328-
const offsetFromPage = $(this.editor.container).offset()!.top;
323+
const offsetFromPage = this.editor.container.offsetTop;
329324
const startLine = range.start.lineNumber;
330325
const startColumn = range.start.column;
331326
const firstLine = this.getLineValue(startLine);
@@ -345,11 +340,11 @@ export class LegacyCoreEditor implements CoreEditor {
345340
let offset = 0;
346341
if (isWrapping) {
347342
// Try get the line height of the text area in pixels.
348-
const textArea = $(this.editor.container.querySelector('textArea')!);
343+
const textArea = this.editor.container.querySelector('textArea');
349344
const hasRoomOnNextLine = this.getLineValue(startLine).length < maxLineLength;
350345
if (textArea && hasRoomOnNextLine) {
351346
// Line height + the number of wraps we have on a line.
352-
offset += this.getLineValue(startLine).length * textArea.height()!;
347+
offset += this.getLineValue(startLine).length * textArea.getBoundingClientRect().height;
353348
} else {
354349
if (startLine > 1) {
355350
this.setActionsBar(getScreenCoords(startLine - 1));

src/plugins/console/public/application/models/sense_editor/__tests__/integration.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ describe('Integration', () => {
4444
'<div><div id="ConAppEditor" /><div id="ConAppEditorActions" /><div id="ConCopyAsCurl" /></div>';
4545

4646
senseEditor = create(document.querySelector('#ConAppEditor'));
47-
$(senseEditor.getCoreEditor().getContainer()).show();
47+
senseEditor.getCoreEditor().getContainer().style.display = '';
4848
senseEditor.autocomplete._test.removeChangeListener();
4949
});
5050
afterEach(() => {
51-
$(senseEditor.getCoreEditor().getContainer()).hide();
51+
senseEditor.getCoreEditor().getContainer().style.display = 'none';
5252
senseEditor.autocomplete._test.addChangeListener();
5353
});
5454

src/plugins/console/public/application/models/sense_editor/__tests__/sense_editor.test.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import '../sense_editor.test.mocks';
3232

33-
import $ from 'jquery';
3433
import _ from 'lodash';
3534

3635
import { create } from '../create';
@@ -51,11 +50,11 @@ describe('Editor', () => {
5150
</div>`;
5251

5352
input = create(document.querySelector('#ConAppEditor'));
54-
$(input.getCoreEditor().getContainer()).show();
53+
input.getCoreEditor().getContainer().style.display = '';
5554
input.autocomplete._test.removeChangeListener();
5655
});
5756
afterEach(function () {
58-
$(input.getCoreEditor().getContainer()).hide();
57+
input.getCoreEditor().getContainer().style.display = 'none';
5958
input.autocomplete._test.addChangeListener();
6059
});
6160

src/plugins/console/public/application/models/sense_editor/sense_editor.test.mocks.ts

-8
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,3 @@
3131
/* eslint no-undef: 0 */
3232

3333
import '../legacy_core_editor/legacy_core_editor.test.mocks';
34-
35-
import jQuery from 'jquery';
36-
jest.spyOn(jQuery, 'ajax').mockImplementation(
37-
() =>
38-
new Promise(() => {
39-
// never resolve
40-
}) as any
41-
);

src/plugins/console/public/lib/ace_token_provider/token_provider.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030

3131
import '../../application/models/sense_editor/sense_editor.test.mocks';
3232

33-
import $ from 'jquery';
34-
3533
// TODO:
3634
// We import from application models as a convenient way to bootstrap loading up of an editor using
3735
// this lib. We also need to import application specific mocks which is not ideal.
@@ -59,14 +57,14 @@ describe('Ace (legacy) token provider', () => {
5957

6058
senseEditor = create(document.querySelector<HTMLElement>('#ConAppEditor')!);
6159

62-
$(senseEditor.getCoreEditor().getContainer())!.show();
60+
senseEditor.getCoreEditor().getContainer().style.display = '';
6361

6462
(senseEditor as any).autocomplete._test.removeChangeListener();
6563
tokenProvider = senseEditor.getCoreEditor().getTokenProvider();
6664
});
6765

6866
afterEach(async () => {
69-
$(senseEditor.getCoreEditor().getContainer())!.hide();
67+
senseEditor.getCoreEditor().getContainer().style.display = 'none';
7068
(senseEditor as any).autocomplete._test.addChangeListener();
7169
await senseEditor.update('', true);
7270
});

src/plugins/console/public/lib/osd/osd.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import {
3838
UsernameAutocompleteComponent,
3939
} from '../autocomplete/components';
4040

41-
import $ from 'jquery';
4241
import _ from 'lodash';
4342

4443
import Api from './api';
@@ -174,20 +173,19 @@ function loadApisFromJson(
174173
// like this, it looks like a minor security issue.
175174
export function setActiveApi(api) {
176175
if (!api) {
177-
$.ajax({
178-
url: '../api/console/api_server',
179-
dataType: 'json', // disable automatic guessing
176+
fetch('../api/console/api_server', {
177+
method: 'GET',
180178
headers: {
181179
'osd-xsrf': 'opensearch-dashboards',
182180
},
183-
}).then(
184-
function (data) {
181+
})
182+
.then(function (response) {
183+
response.json();
184+
})
185+
.then(function (data) {
185186
setActiveApi(loadApisFromJson(data));
186-
},
187-
function (jqXHR) {
188-
console.log("failed to load API '" + api + "': " + jqXHR.responseText);
189-
}
190-
);
187+
})
188+
.catch((error) => console.log(`failed to load API '${api}': ${error}`));
191189
return;
192190
}
193191

0 commit comments

Comments
 (0)