-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathpager.js
108 lines (106 loc) · 2.63 KB
/
pager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const VUE_VIEW = '__vueVNodeRef__';
module.exports = function pager(Vue) {
return {
model: {
prop: 'selectedIndex',
event: 'selectedIndexChange'
},
props: {
items: {
type: Array | Object,
},
'+alias': {
type: String,
default: 'item'
},
'+index': {
type: String,
default: '$index'
},
selectedIndex: {
type: Number,
default: 0
}
},
template: `
<NativePager
ref="pagerView"
:items="items"
v-bind="$attrs"
v-on="listeners"
:selectedIndex="selectedIndex"
@itemLoading="onItemLoading"
@itemDisposing="onItemDisposing">
<slot />
</NativePager>
`,
watch: {
items: {
handler(newVal) {
this.$refs.pagerView.setAttribute('items', newVal);
this.$refs.pagerView.nativeView.refresh();
},
deep: true
}
},
computed: {
listeners() {
return Object.assign({}, this.$listeners, {
selectedIndexChange: this.onSelectedIndexChange
})
}
},
mounted() {
if (!this.items) return;
this.getItemContext = (item, index) =>
getItemContext(item, index, this.$props[ '+alias' ], this.$props[ '+index' ]);
this.$refs.pagerView.setAttribute('items', this.items);
this.$refs.pagerView.setAttribute(
'_itemTemplatesInternal',
this.$templates.getKeyedTemplates()
);
this.$refs.pagerView.setAttribute(
'_itemTemplateSelector',
(item, index) => {
return this.$templates.selectorFn(this.getItemContext(item, index));
}
);
},
methods: {
onItemLoading(args) {
if (!this.items) return;
const index = args.index;
const items = args.object.items;
const currentItem =
typeof items.getItem === 'function'
? items.getItem(index)
: items[ index ];
const name = args.object._itemTemplateSelector(currentItem, index, items);
const context = this.getItemContext(currentItem, index);
const oldVnode = args.view && args.view[ VUE_VIEW ];
args.view = this.$templates.patchTemplate(name, context, oldVnode);
},
onItemDisposing(args) {
// TODO: handle disposing template
// const oldVnode = args.view && args.view[ VUE_VIEW ];
// console.log("disposing", !!oldVnode, VUE_VIEW);
// if (oldVnode) {
// Vue.prototype.__patch__(oldVnode, null);
// }
},
onSelectedIndexChange({ value }) {
this.$emit('selectedIndexChange', {
object: { selectedIndex: value },
});
}
}
};
function getItemContext(item, index, alias, index_alias) {
return {
[ alias ]: item,
[ index_alias ]: index,
$even: index % 2 === 0,
$odd: index % 2 !== 0
};
}
}