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

refactor: declare methods in methods instead of created #790

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
95 changes: 47 additions & 48 deletions packages/vue3-flicking/src/Flicking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,54 +42,6 @@ const Flicking = defineComponent({
this.renderEmitter = new Component();
this.diffResult = null;

this.getPanels = () => {
const componentInstance = getCurrentInstance() as unknown as { ctx: Flicking } | null;
const vueFlicking = componentInstance?.ctx;
const flicking = this.vanillaFlicking;
const defaultSlots = this.getSlots();
const diffResult = vueFlicking?.diffResult;

const slots = diffResult
? getRenderingPanels(flicking, diffResult)
: defaultSlots;

const panelComponent = resolveComponent("Panel");
const panels = slots.map((slot, idx) => h(panelComponent as any, {
key: slot.key!,
ref: idx.toString()
}, () => slot));

return panels;
};
this.getVirtualPanels = () => {
const options = this.options;
const {
panelClass = "flicking-panel"
} = options.virtual!;
const panelsPerView = options.panelsPerView as number;
const flicking = this.vanillaFlicking;
const initialized = flicking && flicking.initialized;

const renderingIndexes = initialized
? flicking.renderer.strategy.getRenderingIndexesByOrder(flicking)
: range(panelsPerView + 1);

const firstPanel = initialized && flicking.panels[0];
const size = firstPanel
? flicking.horizontal
? { width: firstPanel.size }
: { height: firstPanel.size }
: {};

return renderingIndexes.map(idx => h("div", {
key: idx,
ref: idx.toString(),
class: panelClass,
style: size,
"data-element-index": idx
}));
};

withFlickingMethods(this, "vanillaFlicking");
},
mounted() {
Expand Down Expand Up @@ -233,6 +185,53 @@ const Flicking = defineComponent({
this.vanillaFlicking!.addPlugins(...added.map(index => list[index]));
this.vanillaFlicking!.removePlugins(...removed.map(index => prevList[index]));
},
getPanels() {
const componentInstance = getCurrentInstance() as unknown as { ctx: Flicking } | null;
const vueFlicking = componentInstance?.ctx;
const flicking = this.vanillaFlicking;
const defaultSlots = this.getSlots();
const diffResult = vueFlicking?.diffResult;

const slots = diffResult
? getRenderingPanels(flicking, diffResult)
: defaultSlots;

const panelComponent = resolveComponent("Panel");
const panels = slots.map((slot, idx) => h(panelComponent as any, {
key: slot.key!,
ref: idx.toString()
}, () => slot));

return panels;
},
getVirtualPanels() {
const options = this.options;
const {
panelClass = "flicking-panel"
} = options.virtual!;
const panelsPerView = options.panelsPerView as number;
const flicking = this.vanillaFlicking;
const initialized = flicking && flicking.initialized;

const renderingIndexes = initialized
? flicking.renderer.strategy.getRenderingIndexesByOrder(flicking)
: range(panelsPerView + 1);

const firstPanel = initialized && flicking.panels[0];
const size = firstPanel
? flicking.horizontal
? { width: firstPanel.size }
: { height: firstPanel.size }
: {};

return renderingIndexes.map(idx => h("div", {
key: idx,
ref: idx.toString(),
class: panelClass,
style: size,
"data-element-index": idx
}));
},
fillKeys() {
const vnodes = this.getSlots();

Expand Down