Skip to content

fix(menu): change side when rtl - #11190

Closed
jlucfarias wants to merge 6 commits into
ionic-team:masterfrom
jlucfarias:flip-menu
Closed

jlucfarias wants to merge 6 commits into
ionic-team:masterfrom
jlucfarias:flip-menu

Conversation

@jlucfarias

@jlucfarias jlucfarias commented Apr 12, 2017 •

Copy link
Copy Markdown

Note: the menu will be in the better side of the html direction when the page load the first time.

Short description of what this resolves:

Change for the better side when direction is rtl

Ionic Version: 1.x / 2.x / 3.x

Fixes partially: #11115

for menus

*Note:* the menu will be in the better side of the html direction only when the page load the first time.
@jlucfarias

Copy link
Copy Markdown
Author

@AmitMY I will think later about your use case

Comment thread src/components/menu/menu.ts Outdated

// normalize the "side"
if (this.side !== 'left' && this.side !== 'right') {
if (this.side !== 'left' && this.side !== 'right' && !_plt.isRTL()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, what do you think about doing:

if (this.side !== 'left' && this.side !== 'right') {
	this.side = _plt.isRTL()? 'right': 'left';
}

It is cleaner imo.

else if (this.side !== 'left' && this.side !== 'right' && _plt.isRTL()) {
this.side = 'right';
}
this.setElementAttribute('side', this.side);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only when side is not previously set

@AmitMY

AmitMY commented Apr 12, 2017

Copy link
Copy Markdown
Contributor

If you already are changing the menu sides, how about this behaviour:

  • side="left" - always left (and deprecation message)
  • side="right" - always right (and deprecation message)
  • side="start" - left for ltr, right for rtl
  • side="end" - right for ltr, left for rtl

Also, ngOnChanges for the side attribute could help updating after init.

@brandyscarney

Copy link
Copy Markdown
Member

I like that idea @AmitMY. I think we should keep left and right as an option to always be left or right, then add start and end similar to how text-align works. cc @manucorporat @adamdbradley

@jlucfarias

Copy link
Copy Markdown
Author

Can you review the new changes? @AmitMY @brandyscarney

Comment thread src/components/menu/menu-gestures.ts Outdated
if (this.menu.side === 'right' || (this.menu.side === 'start' && this.plt.isRTL()) || (this.menu.side === 'end' && !this.plt.isRTL())) {
let z = slide.min;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 unnecessary break lines, it should be } else {.
Also, use else instead of else if as this is the exact opposite, and there is no default set.
This applies to lines: 62-64, 107-108
and on menu-types file as well: 89-91, 119-121, 153-154,
and on menu file: 344-345

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@AmitMY

AmitMY commented Apr 17, 2017 •

Copy link
Copy Markdown
Contributor

This looks pretty good. covering a lot of ground with the menu, well done.
Because of it's heavy usage, I think you should create a new helper function:

getSide(side: string) {
  if (['left', 'right', 'start', 'end'].indexOf(side) === -1) {
    throw new Error(`Side ${side} is unknown`);
  }

  // somehow we injected platform here as well, called `plt`

  if (side === 'right' || (side === 'start' && plt.isRTL()) || (side === 'end' && !plt.isRTL()) {
    return 'right';
  }

  return 'left';
}

Input: ['left', 'right', 'start', 'end'], Output: ['left', 'right']

This will basically standardise the side parameter that is pretty much everywhere.

Some work is missing for the menu.ts file. As the current code normalises the side value, without regarding it for the future (live change of platform direction).

else if (this.side !== 'left' && this.side !== 'right' && this.side !== 'start' && this.side !== 'end' && _plt.isRTL()) {
this.side = 'right';
}
this.setElementAttribute('side', this.side);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will break the change. Let's say I have 'start', after construct is done, it will be left.

this.side should remain with the same value, then, wherever there is usage of this.side in the menu, it should be only left and right. Take a look at the function I wrote, to make it nicer.

  • Note - this part of the code should not even exist (342-347). It should say:
if (['left', 'right', 'start', 'end'].indexOf(this.side) === -1) {
	this.side = 'start'; // default
	this.setElementAttribute('side', this.side);
}

instead of `else if` use the `else` condition
@jlucfarias

jlucfarias commented Apr 18, 2017 •

Copy link
Copy Markdown
Author

Do you think that I need change the @Input() side: string; to

  get side(): string {
    // code here
  }
  set side(val: string) {
    // code here
  }

@AmitMY

@AmitMY

AmitMY commented Apr 18, 2017

Copy link
Copy Markdown
Contributor

That looks like a great idea.
It's a bit different, here is a reference for how - http://stackoverflow.com/questions/36653678/angular2-input-to-a-property-with-get-set

Set saves the ideal side - left/right/start/end
Get gets the actual side - left/right

@jlucfarias

jlucfarias commented Apr 18, 2017 •

Copy link
Copy Markdown
Author

like this?

export class Menu implements RootNode {
  ...
  private _side: string;
  private _plt: Platform;
  ...
  @Input()
  get side(): string {
    return this._side;
  }

  set side(val: string) {
    if (val === 'right' || (val === 'start' && _plt.isRTL()) || (val === 'end' && !_plt.isRTL())) {
      this._side = 'right';
    }

    this._side = 'left';
  }

if it's ok what about this part?

ngOnInit() {
    ...
    // normalize the "side"
    if (this.side !== 'left' && this.side !== 'right' && this.side !== 'start' && this.side !== 'end' && !_plt.isRTL()) {
      this.side = 'left';
    } else {
      this.side = 'right';
    }
    this.setElementAttribute('side', this.side);

what I need to do here?

@AmitMY

AmitMY commented Apr 18, 2017

Copy link
Copy Markdown
Contributor

Well, opposite.
Because this is an Input, it should be for the set.
Also, save correct value, return bs value.

Something like:
type side = 'left' | 'right' | 'start' | 'end';
and

_side: side = 'start';
get side(): side {
  if (this._side === 'right' || (this._side === 'start' && this._plt.isRTL()) || (this._side === 'end' && !this._plt.isRTL())) {
    return 'right';

  return 'left';
}

@Input('side')
set side(side: side) {
  this._side = side;
}

Note that I set default value to be start

@jlucfarias

Copy link
Copy Markdown
Author

where are I put that type side = 'left' | 'right' | 'start' | 'end';?

@AmitMY

AmitMY commented Apr 18, 2017

Copy link
Copy Markdown
Contributor

Above the menu class, it will work. (Under imports)
I am not sure what is the ionic style guide for types though, so perhaps a member can give feedback.

@jlucfarias

jlucfarias commented Apr 18, 2017 •

Copy link
Copy Markdown
Author

ngOnInit should have any change?

@AmitMY

AmitMY commented Apr 18, 2017

Copy link
Copy Markdown
Contributor

Hmm.. I think should remove lines 342-346, and only keep the
this.setElementAttribute('side', this.side);, to make sure you are not breaking anything.

I think you should add a test to: menu.spec.ts.
Something like this but for start and end
(to make sure nothing was broken).

(and of course, side=start but expect it to be the left one on ltr, and right one on rtl)

As an overview, without running anything, this looks good for now in my opinion.

@brandyscarney Any additional feedback? this is the most important rtl "bug" currently, in my perspective. (Note, #11233 is completing it with the slide and the effect)

private _gestureBlocker: BlockerDelegate;
private _isPane: boolean = false;
private _side: side = 'start';
private _plt: Platform;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_plt is already injected in the constructor.

*/
@Input() side: string;
get side(): side {
if (val === 'right' || (val === 'start' && _plt.isRTL()) || (val === 'end' && !_plt.isRTL())) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._plt rather than _plt which is undefined

expect(menu).toEqual(someMenu);
});

it('should get the only right menu when is rtl', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how, but for this, and the end=left you need to inject Platform, and set direction to rtl
If you would run these tests, they won't pass

@jlucfarias jlucfarias Apr 18, 2017 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add the platform in mockMenu but I don't now how to access it. I think this

let someMenu = mockMenu();
    someMenu._plt.setDir('rtl', true);

but I don't know if it's right

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the platform is set to private, so no.
@brandyscarney or @jgw96, can you help on this point?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps if we change the platform from private to protected they will be accessed from the mockMenu

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@John-Luke that will indeed work, no doubt, but they have set it to private, and I guess they are knowing what they are doing better than us :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that they set it to private because didn't have the necessity to use in deriving classes

min: -this.menu.width(),
max: 0
};
} else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's only include the relevant changes in this PR. why did you refactor the "otherwise" condition?

menuClosedX = menu.width() + 'px';
menuOpenedX = '0px';

menuClosedX = menu.width() + 'px';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this

@manucorporat manucorporat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@John-Luke

this.menu.side === 'right' || (this.menu.side === 'start' && this.plt.isRTL()) || (this.menu.side === 'end' && !this.plt.isRTL())

this condition is repeated a lot across the code, we should add another auxiliar private function in menu.ts instead, for example, one that returns a boolean:

  _isRightSide(): boolean {
    switch (this.side) {
      case 'right': return true;
      case 'left': return false;
      case 'end': return !this._plt.isRTL();
      // 'start' by default
      default: return this._plt.isRTL();
    }
  }

where 'start' is the default value.

Have you tested your changes?

/**
* @input {string} Which side of the view the menu should be placed. Default `"left"`.
*/
@Input() side: string;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the point of this change

private _events: UIEventManager;
private _gestureBlocker: BlockerDelegate;
private _isPane: boolean = false;
private _side: side = 'start';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need a new '_side' variable either

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind having _side and @Input() side is that the _side has 4 possible values: ['left', 'right', 'start', 'end']
While the private magic variable side has only two values: ['left', 'right']
This makes it easier for the rest of that file. Every time there is a reference to side, it will evaluate _side and return the correct one for that specific call. start can return left or right, depending on platform, etc..

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AmitMY then why is:

  if (this._side === 'right' || (this._side === 'start' && this._plt.isRTL()) || (this._side === 'end' && !this._plt.isRTL())) {

repeated across menu-gesture?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manucorporat I can only guess that this PR derailed a bit, and went through many changes that some stuff got left behind..
I thing I will fork this, and create a new cleaner PR.


Can you please comment on the tests? How can you access Platform / how can you run specific tests on different directions?

onSlide(slide: SlideData, ev: any) {
let z = (this.menu.side === 'right' ? slide.min : slide.max);
if (this.menu.side === 'right' || (this.menu.side === 'start' && this.plt.isRTL()) || (this.menu.side === 'end' && !this.plt.isRTL())) {
let z = slide.min;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the scope of z should be the root of the function

@AmitMY

AmitMY commented Apr 19, 2017

Copy link
Copy Markdown
Contributor

@manucorporat
About having _isRightSide as a private member of menu -
I agree that it needs to be a separate method, but I don't think it should be a part of menu.
Because we are trying to have start and end work across ionic (item, sliding-options, menu, etc), it will be better if it will be a function somewhere else - I am not sure where.
(input 'left'|'right'|'start'|'end', output 'left'|'right')

@AmitMY AmitMY mentioned this pull request Apr 20, 2017
@AmitMY

AmitMY commented Apr 23, 2017

Copy link
Copy Markdown
Contributor

@manucorporat This can be closed as well, as previous merged PR was based on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants