aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/controller/paged-array-controller.js
blob: 8064246752e483abf06d10d5a5f47967f433a949 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */
/**
	@module montage/ui/controller/paged-array-controller
    @requires montage/core/core
    @requires ui/controller/array-controller
    @requires core/event/mutable-event
*/
var Montage = require("montage").Montage,
    ArrayController = require("ui/controller/array-controller").ArrayController,
    MutableEvent = require("core/event/mutable-event").MutableEvent;
/**
    @class module:montage/ui/controller/paged-array-controller.PagedArrayController
    @extends module:montage/ui/controller/array-controller.ArrayController
*/
exports.PagedArrayController = Montage.create(ArrayController,/** @lends module:montage/ui/controller/paged-array-controller.PagedArrayController# */ {
/**
  Description TODO
  @private
*/
    _pageSize: {
        value: 10
    },
        //changing this should recalculate pageIndex to contain first item of previous current page
        // via gotoOffset ?
/**
        Description TODO
        @type {Function}
        @default {Number} 10
    */
    pageSize: {
        get: function() {
            return this._pageSize;
        },
        set: function(value) {
            if (value !== this._pageSize) {
                this._calculate(value, this._pageIndex, this._padding);
            }
        }
    },

/**
  Description TODO
  @private
*/
    _padding: {
        value: 0
    },
    // changing this should change the organized object to have the new padding
/**
        Description TODO
        @type {Function}
        @default {Number} 0
    */
    padding: {
        get: function() {
            return this._padding;
        },
        set: function(value) {
            if (value !== this._padding) {
                this._calculate(this._pageSize, this._pageIndex, value);
            }
        }
    },
/**
  Description TODO
  @private
*/
    _pageIndex: {
        value: 0,
        enumerable: false
    },
        // changing this should change the start and endIndex
/**
        Description TODO
        @type {Function}
        @default {Number} 0
    */
    pageIndex: {
        get: function() {
            return this._pageIndex;
        },
        set: function(pageIndex) {
            if (pageIndex !== this._pageIndex) {
                pageIndex = pageIndex >= this.pageCount ? this.pageCount - 1 : pageIndex < 0 ? 0 : pageIndex;
                this._calculate(this._pageSize, pageIndex, this._padding);
            }
        }
    },

 /**
  Description TODO
  @private
*/
    _pageCount: {
        value: null,
        enumerable: false
    },
/**
        Description TODO
        @type {Function}
        @default null
    */
    pageCount: {
        get: function() {
            return this._pageCount;
        }
    },

/**
        Description TODO
        @type {Function}
        @default {Array} array
    */
    pageNumber: {
        dependencies: ["pageIndex"],
        get: function() {
            return this._pageIndex + 1;
        }
    },

 /**
  Description TODO
  @private
*/
    _calculate: {
        value: function(pageSize, pageIndex, padding) {
            var contentLength = this.content.length,
                pageCount = this._pageCount,
                startIndex, endIndex,
                pageSizeChangeEvent,
                pageIndexChangeEvent,
                paddingChangeEvent,
                pageCountChangeEvent,
                startIndexChangeEvent,
                endIndexChangeEvent;

            if (pageSize !== this._pageSize) {
                pageSizeChangeEvent = MutableEvent.changeEventForKeyAndValue("pageSize", this._pageSize);
                this._pageSize = pageSize;
            }
            if (pageIndex !== this._pageIndex) {
                pageIndexChangeEvent = MutableEvent.changeEventForKeyAndValue("pageIndex", this._pageIndex);
                this._pageIndex = pageIndex;
            }
            if (padding !== this._padding) {
                paddingChangeEvent = MutableEvent.changeEventForKeyAndValue("padding", this._padding);
                this._padding = padding;
            }

            pageCountChangeEvent = MutableEvent.changeEventForKeyAndValue("pageCount", pageCount);
            this._pageCount = pageCount = Math.ceil(contentLength / pageSize);

            startIndexChangeEvent = MutableEvent.changeEventForKeyAndValue("startIndex", this._startIndex);
            startIndex = pageIndex * pageSize - padding;
            this._startIndex = startIndex = startIndex < 0 ? 0 : startIndex;

            endIndexChangeEvent = MutableEvent.changeEventForKeyAndValue("endIndex", this._endIndex);
            endIndex = pageIndex * pageSize + pageSize + padding;
            this._endIndex = endIndex = endIndex > contentLength ? contentLength : endIndex;

            if (pageCountChangeEvent.minus !== pageCount) {
                this.dispatchEvent(pageCountChangeEvent.withPlusValue(pageCount));
            }
            if (startIndexChangeEvent.minus !== startIndex) {
                this.dispatchEvent(startIndexChangeEvent.withPlusValue(startIndex));
            }
            if (endIndexChangeEvent.minus !== endIndex) {
                this.dispatchEvent(endIndexChangeEvent.withPlusValue(endIndex));
            }
            if (pageSizeChangeEvent) {
                this.dispatchEvent(pageSizeChangeEvent.withPlusValue(pageSize));
            }
            if (pageIndexChangeEvent) {
                this.dispatchEvent(pageIndexChangeEvent.withPlusValue(pageIndex));
            }
            if (paddingChangeEvent) {
                this.dispatchEvent(paddingChangeEvent.withPlusValue(padding));
            }
        }
    },
/**
    Description TODO
    @function
    */
    gotoFirstPage: {
        value: function() {
            this.gotoPage(0);
        }
    },
/**
    Description TODO
    @function
    */
    gotoLastPage: {
        value: function() {
            this.gotoPage(this.pageCount - 1);
        }
    },
/**
    Description TODO
    @function
    */
    gotoNextPage: {
        value: function() {
            this.gotoPage(this.pageIndex + 1);
        }
    },
/**
    Description TODO
    @function
    */
    gotoPreviousPage: {
        value: function() {
            this.gotoPage(this.pageIndex - 1);
        }
    },
/**
    Sets the currentPage so that the desired item is contained in that page.
    @function
    @param {Array} pageIndex The index of the item to be shown.
    */
    gotoPage: {
        value: function(pageIndex) {
            this.pageIndex = pageIndex;
        }
    },
/**
    Sets the currentPage so that the desired item is contained in that page.
    @function
    @param {Math} offset The index of the item to be shown.
    */
    gotoContentIndex: {
        value: function(offset) {
            this.gotoPage(Math.floor(offset / this.pageSize));
        }
    },

 /**
        Description TODO
        @type {Function}
        @default null
    */
    content: {
        get: function() {
            return Object.getPropertyDescriptor(ArrayController, "content").get.call(this);
        },
        set: function(value) {
            if (value !== this.content) {
                Object.getPropertyDescriptor(ArrayController, "content").set.call(this, value);
                this._calculate(this._pageSize, this._pageIndex, this._padding);
            }
        }
    }


});