Skip to content

Commit fa10061

Browse files
Forbid jQuery .css and refactor all usage (#29852)
Tested all functionality. There is a [pre-existing bug](#29853) when moving a project panels which is not caused by this refactoring. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 5a8559e commit fa10061

File tree

7 files changed

+78
-73
lines changed

7 files changed

+78
-73
lines changed

.eslintrc.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ rules:
286286
jquery/no-class: [0]
287287
jquery/no-clone: [2]
288288
jquery/no-closest: [0]
289-
jquery/no-css: [0]
289+
jquery/no-css: [2]
290290
jquery/no-data: [0]
291291
jquery/no-deferred: [2]
292292
jquery/no-delegate: [2]
@@ -409,7 +409,7 @@ rules:
409409
no-jquery/no-constructor-attributes: [2]
410410
no-jquery/no-contains: [2]
411411
no-jquery/no-context-prop: [2]
412-
no-jquery/no-css: [0]
412+
no-jquery/no-css: [2]
413413
no-jquery/no-data: [0]
414414
no-jquery/no-deferred: [2]
415415
no-jquery/no-delegate: [2]

web_src/js/features/colorpicker.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
export async function createColorPicker($els) {
2-
if (!$els || !$els.length) return;
1+
import $ from 'jquery';
2+
3+
export async function createColorPicker(els) {
4+
if (!els.length) return;
35

46
await Promise.all([
57
import(/* webpackChunkName: "minicolors" */'@claviska/jquery-minicolors'),
68
import(/* webpackChunkName: "minicolors" */'@claviska/jquery-minicolors/jquery.minicolors.css'),
79
]);
810

9-
$els.minicolors();
11+
return $(els).minicolors();
1012
}

web_src/js/features/comp/ColorPicker.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import $ from 'jquery';
22
import {createColorPicker} from '../colorpicker.js';
33

44
export function initCompColorPicker() {
5-
createColorPicker($('.color-picker'));
5+
(async () => {
6+
await createColorPicker(document.querySelectorAll('.color-picker'));
67

7-
$('.precolors .color').on('click', function () {
8-
const color_hex = $(this).data('color-hex');
9-
$('.color-picker').val(color_hex);
10-
$('.minicolors-swatch-color').css('background-color', color_hex);
11-
});
8+
for (const el of document.querySelectorAll('.precolors .color')) {
9+
el.addEventListener('click', (e) => {
10+
const color = e.target.getAttribute('data-color-hex');
11+
const parent = e.target.closest('.color.picker');
12+
$(parent.querySelector('.color-picker')).minicolors('value', color);
13+
});
14+
}
15+
})();
1216
}

web_src/js/features/comp/LabelEdit.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export function initCompLabelEdit(selector) {
4343

4444
// Edit label
4545
$('.edit-label-button').on('click', function () {
46-
$('.edit-label .color-picker').minicolors('value', $(this).data('color'));
4746
$('#label-modal-id').val($(this).data('id'));
4847

4948
const $nameInput = $('.edit-label .label-name-input');
@@ -60,9 +59,8 @@ export function initCompLabelEdit(selector) {
6059
(!this.hasAttribute('data-exclusive') || !isExclusiveScopeName($nameInput.val())));
6160
updateExclusiveLabelEdit('.edit-label');
6261

63-
$('.edit-label .label-desc-input').val($(this).data('description'));
64-
$('.edit-label .color-picker').val($(this).data('color'));
65-
$('.edit-label .minicolors-swatch-color').css('background-color', $(this).data('color'));
62+
$('.edit-label .label-desc-input').val(this.getAttribute('data-description'));
63+
$('.edit-label .color-picker').minicolors('value', this.getAttribute('data-color'));
6664

6765
$('.edit-label.modal').modal({
6866
onApprove() {

web_src/js/features/imagediff.js

+54-54
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,25 @@ export function initImageDiff() {
133133
$container.find('.bounds-info-before .bounds-info-height').text(`${sizes.image2[0].naturalHeight}px`).addClass(heightChanged ? 'red' : '');
134134
}
135135

136-
sizes.image1.css({
137-
width: sizes.size1.width * factor,
138-
height: sizes.size1.height * factor
139-
});
140-
sizes.image1.parent().css({
141-
margin: `10px auto`,
142-
width: sizes.size1.width * factor + 2,
143-
height: sizes.size1.height * factor + 2
144-
});
145-
sizes.image2.css({
146-
width: sizes.size2.width * factor,
147-
height: sizes.size2.height * factor
148-
});
149-
sizes.image2.parent().css({
150-
margin: `10px auto`,
151-
width: sizes.size2.width * factor + 2,
152-
height: sizes.size2.height * factor + 2
153-
});
136+
const image1 = sizes.image1[0];
137+
if (image1) {
138+
const container = image1.parentNode;
139+
image1.style.width = `${sizes.size1.width * factor}px`;
140+
image1.style.height = `${sizes.size1.height * factor}px`;
141+
container.style.margin = '10px auto';
142+
container.style.width = `${sizes.size1.width * factor + 2}px`;
143+
container.style.height = `${sizes.size1.height * factor + 2}px`;
144+
}
145+
146+
const image2 = sizes.image2[0];
147+
if (image2) {
148+
const container = image2.parentNode;
149+
image2.style.width = `${sizes.size2.width * factor}px`;
150+
image2.style.height = `${sizes.size2.height * factor}px`;
151+
container.style.margin = '10px auto';
152+
container.style.width = `${sizes.size2.width * factor + 2}px`;
153+
container.style.height = `${sizes.size2.height * factor + 2}px`;
154+
}
154155
}
155156

156157
function initSwipe(sizes) {
@@ -159,36 +160,39 @@ export function initImageDiff() {
159160
factor = (diffContainerWidth - 12) / sizes.max.width;
160161
}
161162

162-
sizes.image1.css({
163-
width: sizes.size1.width * factor,
164-
height: sizes.size1.height * factor
165-
});
166-
sizes.image1.parent().css({
167-
margin: `0px ${sizes.ratio[0] * factor}px`,
168-
width: sizes.size1.width * factor + 2,
169-
height: sizes.size1.height * factor + 2
170-
});
171-
sizes.image1.parent().parent().css({
172-
padding: `${sizes.ratio[1] * factor}px 0 0 0`,
173-
width: sizes.max.width * factor + 2
174-
});
175-
sizes.image2.css({
176-
width: sizes.size2.width * factor,
177-
height: sizes.size2.height * factor
178-
});
179-
sizes.image2.parent().css({
180-
margin: `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`,
181-
width: sizes.size2.width * factor + 2,
182-
height: sizes.size2.height * factor + 2
183-
});
184-
sizes.image2.parent().parent().css({
185-
width: sizes.max.width * factor + 2,
186-
height: sizes.max.height * factor + 2
187-
});
188-
$container.find('.diff-swipe').css({
189-
width: sizes.max.width * factor + 2,
190-
height: sizes.max.height * factor + 30 /* extra height for inner "position: absolute" elements */,
191-
});
163+
const image1 = sizes.image1[0];
164+
if (image1) {
165+
const container = image1.parentNode;
166+
const swipeFrame = container.parentNode;
167+
image1.style.width = `${sizes.size1.width * factor}px`;
168+
image1.style.height = `${sizes.size1.height * factor}px`;
169+
container.style.margin = `0px ${sizes.ratio[0] * factor}px`;
170+
container.style.width = `${sizes.size1.width * factor + 2}px`;
171+
container.style.height = `${sizes.size1.height * factor + 2}px`;
172+
swipeFrame.style.padding = `${sizes.ratio[1] * factor}px 0 0 0`;
173+
swipeFrame.style.width = `${sizes.max.width * factor + 2}px`;
174+
}
175+
176+
const image2 = sizes.image2[0];
177+
if (image2) {
178+
const container = image2.parentNode;
179+
const swipeFrame = container.parentNode;
180+
image2.style.width = `${sizes.size2.width * factor}px`;
181+
image2.style.height = `${sizes.size2.height * factor}px`;
182+
container.style.margin = `${sizes.ratio[3] * factor}px ${sizes.ratio[2] * factor}px`;
183+
container.style.width = `${sizes.size2.width * factor + 2}px`;
184+
container.style.height = `${sizes.size2.height * factor + 2}px`;
185+
swipeFrame.style.width = `${sizes.max.width * factor + 2}px`;
186+
swipeFrame.style.height = `${sizes.max.height * factor + 2}px`;
187+
}
188+
189+
// extra height for inner "position: absolute" elements
190+
const swipe = $container.find('.diff-swipe')[0];
191+
if (swipe) {
192+
swipe.style.width = `${sizes.max.width * factor + 2}px`;
193+
swipe.style.height = `${sizes.max.height * factor + 30}px`;
194+
}
195+
192196
$container.find('.swipe-bar').on('mousedown', function(e) {
193197
e.preventDefault();
194198

@@ -200,13 +204,9 @@ export function initImageDiff() {
200204
e2.preventDefault();
201205

202206
const value = Math.max(0, Math.min(e2.clientX - $swipeFrame.offset().left, width));
207+
$swipeBar[0].style.left = `${value}px`;
208+
$container.find('.swipe-container')[0].style.width = `${$swipeFrame.width() - value}px`;
203209

204-
$swipeBar.css({
205-
left: value
206-
});
207-
$container.find('.swipe-container').css({
208-
width: $swipeFrame.width() - value
209-
});
210210
$(document).on('mouseup.diff-swipe', () => {
211211
$(document).off('.diff-swipe');
212212
});

web_src/js/features/repo-legacy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ async function onEditContent(event) {
389389
dz.emit('complete', attachment);
390390
dz.files.push(attachment);
391391
fileUuidDict[attachment.uuid] = {submitted: true};
392-
$dropzone.find(`img[src='${imgSrc}']`).css('max-width', '100%');
392+
$dropzone.find(`img[src='${imgSrc}']`)[0].style.maxWidth = '100%';
393393
const $input = $(`<input id="${attachment.uuid}" name="files" type="hidden">`).val(attachment.uuid);
394394
$dropzone.find('.files').append($input);
395395
}

web_src/js/features/repo-projects.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async function initRepoProjectSortable() {
7272
await PUT($(column).data('url'), {
7373
data: {
7474
sorting: i,
75-
color: rgbToHex($(column).css('backgroundColor')),
75+
color: rgbToHex(window.getComputedStyle($(column)[0]).backgroundColor),
7676
},
7777
});
7878
} catch (error) {
@@ -111,8 +111,9 @@ export function initRepoProject() {
111111
const $projectColorInput = $(this).find('#new_project_column_color');
112112
const $boardColumn = $(this).closest('.project-column');
113113

114-
if ($boardColumn.css('backgroundColor')) {
115-
setLabelColor($projectHeader, rgbToHex($boardColumn.css('backgroundColor')));
114+
const bgColor = $boardColumn[0].style.backgroundColor;
115+
if (bgColor) {
116+
setLabelColor($projectHeader, rgbToHex(bgColor));
116117
}
117118

118119
$(this).find('.edit-project-column-button').on('click', async function (e) {

0 commit comments

Comments
 (0)