Skip to content

Commit 1a8c4dd

Browse files
sobo-odooqsm-odoo
authored andcommitted
[IMP] web_editor: finish the cleaning of the grid code
The merge of [1] happened before its code was totally cleaned. This commit makes up for that: the variables have been correctly renamed, the comments have been cut at 80 characters and useless blank lines have been removed. [1]: odoo#93144 task-2973198 X-original-commit: 9874120 Part-of: odoo#102525
1 parent 782a1dd commit 1a8c4dd

File tree

2 files changed

+69
-68
lines changed

2 files changed

+69
-68
lines changed

addons/web_editor/static/src/js/editor/snippets.editor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3890,8 +3890,8 @@ var SnippetsMenu = Widget.extend({
38903890

38913891
// Reload images inside grid items so that no image disappears when
38923892
// activating mobile preview.
3893-
const gridItemEls = this.getEditableArea().find('div.o_grid_item');
3894-
for (const gridItemEl of gridItemEls) {
3893+
const $gridItemEls = this.getEditableArea().find('div.o_grid_item');
3894+
for (const gridItemEl of $gridItemEls) {
38953895
gridUtils._reloadLazyImages(gridItemEl);
38963896
}
38973897
for (const invisibleOverrideEl of this.getEditableArea().find('.o_snippet_mobile_invisible, .o_snippet_desktop_invisible')) {

addons/web_editor/static/src/js/editor/snippets.options.js

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,7 +3751,8 @@ const SnippetOptionWidget = Widget.extend({
37513751
const moveDownOrRight = widgetName === 'move_down_opt' || widgetName === 'move_right_opt';
37523752

37533753
if (moveUpOrLeft || moveDownOrRight) {
3754-
// The arrows are not displayed if the target is in a grid and if not in mobile view.
3754+
// The arrows are not displayed if the target is in a grid and if
3755+
// not in mobile view.
37553756
const isMobileView = this.$target[0].ownerDocument.defaultView.frameElement.clientWidth < 768;
37563757
if (this.$target[0].classList.contains('o_grid_item') && !isMobileView) {
37573758
return false;
@@ -4262,8 +4263,8 @@ registry.sizing = SnippetOptionWidget.extend({
42624263
return;
42634264
}
42644265

4265-
// If we are in grid mode, add a background grid and place the element
4266-
// we are resizing in front of it.
4266+
// If we are in grid mode, add a background grid and place the
4267+
// element we are resizing in front of it.
42674268
const rowEl = self.$target[0].parentNode;
42684269
let backgroundGridEl;
42694270
if (rowEl.classList.contains('o_grid_mode')) {
@@ -4274,8 +4275,9 @@ registry.sizing = SnippetOptionWidget.extend({
42744275
gridUtils._setElementToMaxZindex(self.$target[0], rowEl);
42754276
}
42764277

4277-
// For loop to handle the cases where it is ne, nw, se or sw. Since there are two
4278-
// directions, we compute for both directions and we store the values in an array.
4278+
// For loop to handle the cases where it is ne, nw, se or sw. Since
4279+
// there are two directions, we compute for both directions and we
4280+
// store the values in an array.
42794281
const directions = [];
42804282
for (const [i, resize] of resizeVal.entries()) {
42814283
const props = {};
@@ -4311,16 +4313,17 @@ registry.sizing = SnippetOptionWidget.extend({
43114313

43124314
let changeTotal = false;
43134315
for (const dir of directions) {
4314-
// dd is the number of pixels by which the mouse moved, compared to the
4315-
// initial position of the handle.
4316+
// dd is the number of pixels by which the mouse moved,
4317+
// compared to the initial position of the handle.
43164318
const dd = ev['page' + dir.XY] - dir.xy + dir.resize[1][dir.begin];
43174319
const next = dir.current + (dir.current + 1 === dir.resize[1].length ? 0 : 1);
43184320
const prev = dir.current ? (dir.current - 1) : 0;
43194321

43204322
let change = false;
4321-
// If the mouse moved to the right/down by at least 2/3 of the space between the
4322-
// previous and the next steps, the handle is snapped to the next step and the
4323-
// class is replaced by the one matching this step.
4323+
// If the mouse moved to the right/down by at least 2/3 of
4324+
// the space between the previous and the next steps, the
4325+
// handle is snapped to the next step and the class is
4326+
// replaced by the one matching this step.
43244327
if (dd > (2 * dir.resize[1][next] + dir.resize[1][dir.current]) / 3) {
43254328
self.$target.attr('class', (self.$target.attr('class') || '').replace(dir.regClass, ''));
43264329
self.$target.addClass(dir.resize[0][next]);
@@ -4354,8 +4357,8 @@ registry.sizing = SnippetOptionWidget.extend({
43544357
$handle.removeClass('o_active');
43554358

43564359
// If we are in grid mode, removes the background grid.
4357-
// Also sync the col-* class with the g-col-* class so the toggle to normal mode
4358-
// and the mobile view are well done.
4360+
// Also sync the col-* class with the g-col-* class so the
4361+
// toggle to normal mode and the mobile view are well done.
43594362
if (rowEl.classList.contains('o_grid_mode')) {
43604363
self.options.wysiwyg.odooEditor.observerUnactive('displayBackgroundGrid');
43614364
backgroundGridEl.remove();
@@ -4428,34 +4431,35 @@ registry.sizing = SnippetOptionWidget.extend({
44284431
const isMobileView = this.$target[0].ownerDocument.defaultView.frameElement.clientWidth < 768;
44294432
const isGrid = this.$target[0].classList.contains('o_grid_item');
44304433
if (this.$target[0].parentNode.classList.contains('row')) {
4431-
// Hiding/showing the correct resize handles if we are in grid mode or not.
4432-
for (const handle of this.$handles) {
4433-
const isGridHandle = handle.classList.contains('o_grid_handle');
4434-
handle.classList.toggle('d-none', isGrid ^ isGridHandle);
4434+
// Hiding/showing the correct resize handles if we are in grid mode
4435+
// or not.
4436+
for (const handleEl of this.$handles) {
4437+
const isGridHandle = handleEl.classList.contains('o_grid_handle');
4438+
handleEl.classList.toggle('d-none', isGrid ^ isGridHandle);
44354439
// Disabling the resize if we are in mobile view.
4436-
handle.classList.toggle('readonly', isMobileView && isGridHandle);
4440+
handleEl.classList.toggle('readonly', isMobileView && isGridHandle);
44374441
}
44384442

4439-
// Hiding the move handle for some snippets so we can't drag them and
4440-
// so we can't toggle grid mode.
4441-
const moveHandle = this.$overlay[0].querySelector('.o_move_handle');
4443+
// Hiding the move handle for some snippets so we can't drag them
4444+
// and so we can't toggle the grid mode.
4445+
const moveHandleEl = this.$overlay[0].querySelector('.o_move_handle');
44424446
const untoggleableColumns = '.s_masonry_block, .s_showcase, .s_features_grid, .s_website_form, .s_color_blocks_2';
44434447
const disableToggle = this.$target[0].closest(untoggleableColumns);
4444-
moveHandle.classList.toggle('d-none', disableToggle || isMobileView);
4448+
moveHandleEl.classList.toggle('d-none', disableToggle || isMobileView);
44454449

44464450
// Hiding/showing the arrows.
44474451
if (isGrid) {
4448-
const moveLeftArrow = this.$overlay[0].querySelector('.fa-angle-left');
4449-
const moveRightArrow = this.$overlay[0].querySelector('.fa-angle-right');
4452+
const moveLeftArrowEl = this.$overlay[0].querySelector('.fa-angle-left');
4453+
const moveRightArrowEl = this.$overlay[0].querySelector('.fa-angle-right');
44504454
const showLeft = await this._computeWidgetVisibility('move_left_opt');
44514455
const showRight = await this._computeWidgetVisibility('move_right_opt');
4452-
moveLeftArrow.classList.toggle('d-none', !showLeft);
4453-
moveRightArrow.classList.toggle('d-none', !showRight);
4456+
moveLeftArrowEl.classList.toggle('d-none', !showLeft);
4457+
moveRightArrowEl.classList.toggle('d-none', !showRight);
44544458
}
44554459

44564460
// Show/hide the buttons to send back/front a grid item.
4457-
const bringFrontBack = this.$overlay[0].querySelectorAll('.o_front_back');
4458-
bringFrontBack.forEach(button => button.classList.toggle('d-none', !isGrid || isMobileView));
4461+
const bringFrontBackEls = this.$overlay[0].querySelectorAll('.o_front_back');
4462+
bringFrontBackEls.forEach(button => button.classList.toggle('d-none', !isGrid || isMobileView));
44594463
}
44604464
},
44614465

@@ -4921,7 +4925,8 @@ registry.layout_column = SnippetOptionWidget.extend({
49214925
this.trigger_up('activate_snippet', {$snippet: this.$target});
49224926
}
49234927
} else {
4924-
// Toggle normal mode only if grid mode was activated (as it's in normal mode by default).
4928+
// Toggle normal mode only if grid mode was activated (as it's in
4929+
// normal mode by default).
49254930
const rowEl = this.$target[0].querySelector('.row');
49264931
if (rowEl && rowEl.classList.contains('o_grid_mode')) {
49274932
this._toggleNormalMode(rowEl);
@@ -4942,8 +4947,9 @@ registry.layout_column = SnippetOptionWidget.extend({
49424947
const rowEl = this.$target[0].querySelector('.row');
49434948
const elementType = widgetValue;
49444949

4945-
// If it has been less than 15 seconds that we have added an element, shift the new element
4946-
// right and down by one cell. Otherwise, put it on the top left corner.
4950+
// If it has been less than 15 seconds that we have added an element,
4951+
// shift the new element right and down by one cell. Otherwise, put it
4952+
// on the top left corner.
49474953
const currentTime = new Date().getTime();
49484954
if (this.lastAddTime && (currentTime - this.lastAddTime) / 1000 < 15) {
49494955
this.lastStartPosition = [this.lastStartPosition[0] + 1, this.lastStartPosition[1] + 1];
@@ -4953,49 +4959,47 @@ registry.layout_column = SnippetOptionWidget.extend({
49534959
this.lastAddTime = currentTime;
49544960

49554961
// Create the new column.
4956-
const newColumn = document.createElement('div');
4957-
newColumn.classList.add('o_grid_item');
4962+
const newColumnEl = document.createElement('div');
4963+
newColumnEl.classList.add('o_grid_item');
49584964
let numberColumns, numberRows;
49594965

49604966
if (elementType === 'image') {
49614967
// Set the columns properties.
4962-
newColumn.classList.add('col-lg-6', 'g-col-lg-6', 'g-height-6');
4968+
newColumnEl.classList.add('col-lg-6', 'g-col-lg-6', 'g-height-6');
49634969
numberColumns = 6;
49644970
numberRows = 6;
49654971

49664972
// Create a default image and add it to the new column.
4967-
const img = document.createElement('img');
4968-
img.classList.add('img', 'img-fluid', 'mx-auto');
4969-
img.src = '/web/image/website.s_text_image_default_image';
4970-
img.alt = '';
4971-
img.loading = 'lazy';
4972-
4973-
newColumn.appendChild(img);
4973+
const imgEl = document.createElement('img');
4974+
imgEl.classList.add('img', 'img-fluid', 'mx-auto');
4975+
imgEl.src = '/web/image/website.s_text_image_default_image';
4976+
imgEl.alt = '';
4977+
imgEl.loading = 'lazy';
49744978

4979+
newColumnEl.appendChild(imgEl);
49754980
} else if (elementType === 'text') {
4976-
newColumn.classList.add('col-lg-4', 'g-col-lg-4', 'g-height-2');
4981+
newColumnEl.classList.add('col-lg-4', 'g-col-lg-4', 'g-height-2');
49774982
numberColumns = 4;
49784983
numberRows = 2;
49794984

49804985
// Create default text content.
4981-
const p = document.createElement('p');
4982-
p.classList.add('o_default_snippet_text');
4983-
p.textContent = _t("Write something...");
4984-
4985-
newColumn.appendChild(p);
4986+
const pEl = document.createElement('p');
4987+
pEl.classList.add('o_default_snippet_text');
4988+
pEl.textContent = _t("Write something...");
49864989

4990+
newColumnEl.appendChild(pEl);
49874991
} else if (elementType === 'button') {
4988-
newColumn.classList.add('col-lg-2', 'g-col-lg-2', 'g-height-1');
4992+
newColumnEl.classList.add('col-lg-2', 'g-col-lg-2', 'g-height-1');
49894993
numberColumns = 2;
49904994
numberRows = 1;
49914995

49924996
// Create default button.
4993-
const a = document.createElement('a');
4994-
a.href = '#';
4995-
a.classList.add('mb-2', 'btn', 'btn-primary');
4996-
a.textContent = "Button";
4997+
const aEl = document.createElement('a');
4998+
aEl.href = '#';
4999+
aEl.classList.add('mb-2', 'btn', 'btn-primary');
5000+
aEl.textContent = "Button";
49975001

4998-
newColumn.appendChild(a);
5002+
newColumnEl.appendChild(aEl);
49995003
}
50005004
// Place the column in the grid.
50015005
const rowStart = this.lastStartPosition[0];
@@ -5004,15 +5008,15 @@ registry.layout_column = SnippetOptionWidget.extend({
50045008
columnStart = 1;
50055009
this.lastStartPosition[1] = columnStart;
50065010
}
5007-
newColumn.style.gridArea = `${rowStart} / ${columnStart} / ${rowStart + numberRows} / ${columnStart + numberColumns}`;
5011+
newColumnEl.style.gridArea = `${rowStart} / ${columnStart} / ${rowStart + numberRows} / ${columnStart + numberColumns}`;
50085012

50095013
// Setting the z-index to the maximum of the grid.
5010-
gridUtils._setElementToMaxZindex(newColumn, rowEl);
5014+
gridUtils._setElementToMaxZindex(newColumnEl, rowEl);
50115015

50125016
// Add the new column and update the grid height.
5013-
rowEl.appendChild(newColumn);
5017+
rowEl.appendChild(newColumnEl);
50145018
gridUtils._resizeGrid(rowEl);
5015-
this.trigger_up('activate_snippet', {$snippet: $(newColumn)});
5019+
this.trigger_up('activate_snippet', {$snippet: $(newColumnEl)});
50165020
},
50175021

50185022
//--------------------------------------------------------------------------
@@ -5025,7 +5029,6 @@ registry.layout_column = SnippetOptionWidget.extend({
50255029
_computeWidgetState: function (methodName, params) {
50265030
if (methodName === 'selectCount') {
50275031
return this.$('> .row').children().length;
5028-
50295032
} else if (methodName === 'selectLayout') {
50305033
const rowEl = this.$target[0].querySelector('.row');
50315034
if (rowEl && rowEl.classList.contains('o_grid_mode')) {
@@ -5097,18 +5100,16 @@ registry.layout_column = SnippetOptionWidget.extend({
50975100
_toggleNormalMode(rowEl) {
50985101
// Removing the grid class
50995102
rowEl.classList.remove('o_grid_mode');
5100-
5101-
const columns = rowEl.children;
5102-
for (const column of columns) {
5103-
// Reload the images.
5104-
gridUtils._reloadLazyImages(column);
5103+
const columnEls = rowEl.children;
5104+
for (const columnEl of columnEls) {
5105+
// Reloading the images.
5106+
gridUtils._reloadLazyImages(columnEl);
51055107

51065108
// Removing the grid properties.
5107-
column.classList.remove('o_grid_item');
5108-
column.style.removeProperty('grid-area');
5109-
column.style.removeProperty('z-index');
5109+
columnEl.classList.remove('o_grid_item');
5110+
columnEl.style.removeProperty('grid-area');
5111+
columnEl.style.removeProperty('z-index');
51105112
}
5111-
51125113
// Removing the grid properties.
51135114
delete rowEl.dataset.rowCount;
51145115

0 commit comments

Comments
 (0)