Skip to content

Commit c19fa79

Browse files
committed
refactor: keep options object immutable
1 parent 97bfca4 commit c19fa79

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/pagination.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function pagination (vm, { crossChapter, routerMode }) {
8282
const links = crossChapter ? all : group
8383

8484
return {
85+
route: vm.route,
8586
prev: new Link(links[index - 1]).toJSON(),
8687
next: new Link(links[index + 1]).toJSON(),
8788
}
@@ -96,6 +97,7 @@ const template = {
9697
},
9798

9899
inner (data, options) {
100+
const { previousText, nextText } = getLocalizationTexts(options, data.route.path)
99101
return [
100102
data.prev && `
101103
<div class="pagination-item pagination-item--previous">
@@ -104,7 +106,7 @@ const template = {
104106
<svg width="10" height="16" viewBox="0 0 10 16" xmlns="http://www.w3.org/2000/svg">
105107
<polyline fill="none" vector-effect="non-scaling-stroke" points="8,2 2,8 8,14"/>
106108
</svg>
107-
<span>${options.previousText}</span>
109+
<span>${previousText}</span>
108110
</div>
109111
<div class="pagination-item-title">${data.prev.name}</div>
110112
`,
@@ -116,7 +118,7 @@ const template = {
116118
<div class="pagination-item pagination-item--next">
117119
<a href="${data.next.href}">
118120
<div class="pagination-item-label">
119-
<span>${options.nextText}</span>
121+
<span>${nextText}</span>
120122
<svg width="10" height="16" viewBox="0 0 10 16" xmlns="http://www.w3.org/2000/svg">
121123
<polyline fill="none" vector-effect="non-scaling-stroke" points="2,2 8,8 2,14"/>
122124
</svg>
@@ -131,22 +133,24 @@ const template = {
131133
},
132134
}
133135

134-
function updateText (options, path) {
135-
['previousText', 'nextText'].forEach(key => {
136+
function getLocalizationTexts (options, path) {
137+
const texts = {}
138+
;['previousText', 'nextText'].forEach(key => {
136139
const text = options[key]
137140

138141
if (typeof text === 'string') {
139-
options[key] = text
142+
texts[key] = text
140143
} else {
141144
Object.keys(text).some(local => {
142145
const isMatch = path.indexOf(local) > -1
143146

144-
options[key] = isMatch ? text[local] : text
147+
texts[key] = isMatch ? text[local] : text
145148

146149
return isMatch
147-
});
150+
})
148151
}
149152
})
153+
return texts
150154
}
151155

152156
/**
@@ -164,11 +168,7 @@ export function install (hook, vm) {
164168

165169
if (!container) return
166170

167-
const i18n = JSON.parse(JSON.stringify(options))
168-
169-
updateText(i18n, vm.route.path);
170-
171-
container.innerHTML = template.inner(pagination(vm, i18n), i18n)
171+
container.innerHTML = template.inner(pagination(vm, options), options)
172172
}
173173

174174
hook.afterEach((html) => html + template.container())

0 commit comments

Comments
 (0)