Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add setOptions and setAxis method #198

Merged
merged 4 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 68 additions & 4 deletions packages/axes/src/Axes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
ObjectInterface,
UpdateAnimationOption,
} from "./types";
import { getInitialPos } from "./utils";
import { EasingManager } from "./animation/EasingManager";
import { AnimationManager } from "./animation/AnimationManager";

Expand Down Expand Up @@ -82,7 +81,7 @@ export interface AxesOption {
*
* @param {Object.<string, AxisOption>} axis Axis information managed by eg.Axes. The key of the axis specifies the name to use as the logical virtual coordinate system. <ko>eg.Axes가 관리하는 축 정보. 축의 키는 논리적인 가상 좌표계로 사용할 이름을 지정한다.</ko>
* @param {AxesOption} [options={}] The option object of the eg.Axes module<ko>eg.Axes 모듈의 옵션 객체</ko>
* @param {Object.<string, number>} [startPos=null] The coordinates to be moved when creating an instance. It is applied with higher priority than startPos of axisOption.<ko>인스턴스 생성시 이동할 좌표, axisOption의 startPos보다 높은 우선순위로 적용된다.</ko>
* @param {Object.<string, number>} [startPos={}] The coordinates to be moved when creating an instance. It is applied with higher priority than startPos of axisOption.<ko>인스턴스 생성시 이동할 좌표, axisOption의 startPos보다 높은 우선순위로 적용된다.</ko>
*
* @support {"ie": "10+", "ch" : "latest", "ff" : "latest", "sf" : "latest", "edge" : "latest", "ios" : "7+", "an" : "2.3+ (except 3.x)"}
* @example
Expand Down Expand Up @@ -244,7 +243,7 @@ class Axes extends Component<AxesEvents> {
public constructor(
public axis: ObjectInterface<AxisOption> = {},
options: AxesOption = {},
startPos: Axis = null
startPos: Axis = {}
) {
super();
this.options = {
Expand All @@ -261,14 +260,17 @@ class Axes extends Component<AxesEvents> {
},
...options,
};
Object.keys(startPos).forEach((key) => {
this.axis[key].startPos = startPos[key];
});

this.interruptManager = new InterruptManager(this.options);
this.axisManager = new AxisManager(this.axis);
this.eventManager = new EventManager(this);
this.animationManager = new EasingManager(this);
this.inputObserver = new InputObserver(this);
this.eventManager.setAnimationManager(this.animationManager);
this.eventManager.triggerChange(getInitialPos(axis, startPos));
this.eventManager.triggerChange(this.axisManager.get());
}

/**
Expand Down Expand Up @@ -453,6 +455,68 @@ class Axes extends Component<AxesEvents> {
return this;
}

/**
* Change the options of Axes instance.
* @ko 인스턴스의 옵션을 변경한다.
* @param {AxesOption} options Axes options to change <ko>변경할 옵션 목록</ko>
* @return {eg.Axes} An instance of a module itself <ko>모듈 자신의 인스턴스</ko>
* @example
* ```js
* const axes = new eg.Axes({
* "x": {
* range: [0, 100]
* },
* }, {
* round: 10,
* });
*
* axes.setTo({"x": 48});
* axes.get(); // {"x": 50}
*
* axes.setOptions({
* round: 1,
* });
*
* axes.setTo({"x": 48});
* axes.get(); // {"x": 48}
* ```
*/
public setOptions(options: AxesOption) {
Object.assign(this.options, options);
Copy link
Member

Choose a reason for hiding this comment

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

I think this will remain in the build, please check whether it does.
As Object.assign isn't supported in IE

Copy link
Member

Choose a reason for hiding this comment

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

Also, aren't there any internal updates required for this change?
Check this example of updating options in View360

Like, nested may require some immediate update of event handlers.

Copy link
Contributor Author

@malangfox malangfox Aug 16, 2022

Choose a reason for hiding this comment

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

It looks like this changes should avoid using Object.assign for browser compatibility. Confirmed that it remains after the build.
Also changing the nested option will not require any changes to the event handler.
We are using the following statement to add property to native event.

    if (!this.options.nested || !this._isEndofAxis(offset, depaPos, destPos)) {
      nativeEvent.__childrenAxesAlreadyChanged = true;
    }

Since the option is checked when each change is made, the changed option will be applied from the next input without changing any event handlers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there are parts that need immediate change, it may be current animation being played that duration needs to be changed by minimum/maximum duration option.
Also it will be necessary to discuss about action when the current range is outside of the new range.

return this;
}

/**
* Change the information of an existing axis.
* @ko 존재하는 축의 정보를 변경한다.
* @param {Object.<string, AxisOption>} axis Axis options to change <ko>변경할 축의 정보</ko>
* @return {eg.Axes} An instance of a module itself <ko>모듈 자신의 인스턴스</ko>
* @example
* ```js
* const axes = new eg.Axes({
* "x": {
* range: [0, 100]
* },
* });
*
* axes.setTo({"x": 150});
* axes.get(); // {"x": 100}
*
* axes.setAxis({
* "x": {
* range: [0, 200]
* },
* });
*
* axes.setTo({"x": 150});
* axes.get(); // {"x": 150}
* ```
*/
public setAxis(axis: ObjectInterface<AxisOption>) {
this.axisManager.setAxis(axis);
return this;
}

/**
* Stop an animation in progress.
* @ko 재생 중인 애니메이션을 정지한다.
Expand Down
16 changes: 13 additions & 3 deletions packages/axes/src/AxisManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export class AxisManager {
private _pos: Axis;
public constructor(private _axis: ObjectInterface<AxisOption>) {
this._complementOptions();
this._pos = Object.keys(this._axis).reduce((acc, v) => {
acc[v] = this._axis[v].range[0];
return acc;
this._pos = Object.keys(this._axis).reduce((pos, v) => {
pos[v] = this._axis[v].startPos;
return pos;
}, {});
}

Expand Down Expand Up @@ -109,6 +109,16 @@ export class AxisManager {
return this._axis[key];
}

public setAxis(axis: ObjectInterface<AxisOption>) {
Object.keys(axis).forEach((key) => {
if (!this._axis[key]) {
throw new Error(`Axis ${key} does not exist in Axes instance`);
}
Object.assign(this._axis[key], axis[key]);
});
this._complementOptions();
}

/**
* set up 'css' expression
* @private
Expand Down
2 changes: 2 additions & 0 deletions packages/axes/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const AXES_METHODS = [
"get",
"setTo",
"setBy",
"setOptions",
"setAxis",
"stopAnimation",
"updateAnimation",
"isBounceArea",
Expand Down
1 change: 0 additions & 1 deletion packages/axes/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ export { MoveKeyInput } from "./inputType/MoveKeyInput";
export default Axes;

export { AXES_METHODS, AXES_EVENTS } from "./const";
export { getInitialPos } from "./utils";
export * from "./types";
export * from "./reactive";
16 changes: 0 additions & 16 deletions packages/axes/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,22 +269,6 @@ export const getDirection = (
}
};

export const getInitialPos = (
axis: ObjectInterface<AxisOption>,
startPos: Axis
): Axis => {
return {
...Object.keys(axis).reduce(
(result, key) =>
Object.assign(result, {
[key]: axis[key].startPos ?? axis[key].range[0] ?? 0,
}),
{}
),
...startPos,
};
};

export const useDirection = (
checkType: number,
direction: number,
Expand Down
63 changes: 63 additions & 0 deletions packages/axes/test/unit/Axes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,69 @@ describe("Axes", () => {
// When
inst.setBy({ x: -10 }, 200);
});

it("should change Axes options via `setOptions` method", () => {
// Given
inst = new Axes(
{
x: {
range: [0, 100],
},
},
{
round: 10,
}
);

// When
inst.setTo({ x: 48 });

// Then
expect(inst.get()).to.be.eql({ x: 50 });

// When
inst.setOptions({
round: 1,
});
inst.setTo({ x: 48 });

// Then
expect(inst.get()).to.be.eql({ x: 48 });
});

it("should change options of each Axis via `setAxis` method", () => {
// Given
inst = new Axes(
{
x: {
range: [0, 100],
},
y: {
range: [0, 200],
},
},
);

// When
inst.setTo({ x: 150, y: 150 });

// Then
expect(inst.get()).to.be.eql({ x: 100, y: 150 });

// When
inst.setAxis({
x: {
range: [0, 200],
},
y: {
range: [0, 100],
},
});
inst.setTo({ x: 150, y: 150 });

// Then
expect(inst.get()).to.be.eql({ x: 150, y: 100 });
});
});

describe("Axes Test with InputType", () => {
Expand Down