forked from oria/gridx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropDownSizer.js
112 lines (97 loc) · 2.98 KB
/
DropDownSizer.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
109
110
111
112
define([
"dojo/_base/declare",
"dojo/_base/lang",
"dijit/_WidgetBase",
"dijit/_FocusMixin",
"dijit/_TemplatedMixin",
"dojo/i18n",
"dijit/form/Select",
"dojo/i18n!../nls/PaginationBar"
], function(declare, lang, _WidgetBase, _FocusMixin, _TemplatedMixin, i18n, Select){
/*=====
return declare([_WidgetBase, _FocusMixin, _TemplatedMixin], {
// summary:
// This grid bar plugin is to switch page sizes using select widget.
// grid: [const] gridx.Grid
// The grid widget this plugin works for.
grid: null,
// sizes: Integer[]
// An array of available page sizes. Non-positive number means "all"
sizes: [10, 25, 50, 100, 0],
// sizerClass: Function
// The constructor of the select widget
sizerClass: Select,
// sizerProps: Object
// The properties passed to select widget when creating it.
sizerProps: null,
refresh: function(){}
});
=====*/
return declare([_WidgetBase, _FocusMixin, _TemplatedMixin], {
templateString: '<div class="gridxDropDownSizer"><label class="gridxPagerLabel">${pageSizeLabel}</label></div>',
constructor: function(args){
lang.mixin(this, i18n.getLocalization('gridx', 'PaginationBar', this.lang || args.grid.lang));
},
postCreate: function(){
var t = this;
t.connect(t.grid.pagination, 'onChangePageSize', '_onChange');
t.grid.pagination.loaded.then(function(){
t.refresh();
});
},
startup: function(){
this.inherited(arguments);
//Set initial page size after pagination module is ready.
this._onChange(this.grid.pagination.pageSize());
},
//Public-----------------------------------------------------------------------------
grid: null,
sizes: [10, 25, 50, 100, 0],
sizerClass: Select,
sizerProps: null,
refresh: function(){
var t = this,
options = [],
p = t.grid.pagination,
currentSize = p.pageSize(),
sizeSwitch = t._sizeSwitchSelect,
sizes = t.sizes;
for(var i = 0, len = sizes.length; i < len; ++i){
var pageSize = sizes[i],
isAll = !(pageSize > 0);
options.push({
label: String(isAll ? t.pageSizeAll : pageSize),
value: String(isAll ? -1 : pageSize),
selected: currentSize == pageSize || (isAll && p.isAll())
});
}
if(!sizeSwitch){
var cls = t.sizerClass,
props = lang.mixin({
options: options,
'class': 'gridxPagerSizeSwitchWidget',
'aria-label': 'switch page size',
onChange: function(ps){
p.setPageSize(ps < 0 ? 0 : ps);
}
}, t.sizerProps || {});
sizeSwitch = t._sizeSwitchSelect = new cls(props);
sizeSwitch.placeAt(t.domNode, "last");
sizeSwitch.startup();
}else{
sizeSwitch.removeOption(sizeSwitch.getOptions());
sizeSwitch.addOption(options);
}
},
//Private----------------------------------------------------------------------------
_onChange: function(size){
var select = this._sizeSwitchSelect;
if(this.grid.pagination.isAll()){
size = -1;
}
if(select && select.get('value') != size){
select.set('value', size);
}
}
});
});