Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('paginator-core', function (Y, NAME) {
2
 
3
/**
4
 Paginator's core functionality consists of keeping track of the current page
5
 being displayed and providing information for previous and next pages.
6
 
7
 @module paginator
8
 @submodule paginator-core
9
 @since 3.11.0
10
 */
11
 
12
/**
13
 _API docs for this extension are included in the Paginator class._
14
 
15
 Class extension providing the core API and structure for the Paginator module.
16
 
17
 Use this class extension with Widget or another Base-based superclass to
18
 create the basic Paginator model API and composing class structure.
19
 
20
 @class Paginator.Core
21
 @for Paginator
22
 @since 3.11.0
23
 */
24
 
25
var PaginatorCore = Y.namespace('Paginator').Core = function () {};
26
 
27
PaginatorCore.ATTRS = {
28
    /**
29
     Current page count. First page is 1.
30
 
31
     @attribute page
32
     @type Number
33
     @default 1
34
     **/
35
    page: {
36
        value: 1
37
    },
38
 
39
    /**
40
     Total number of pages to display
41
 
42
     @readOnly
43
     @attribute totalPages
44
     @type Number
45
     **/
46
    totalPages: {
47
        readOnly: true,
48
        getter: '_getTotalPagesFn'
49
    },
50
 
51
    /**
52
     Maximum number of items per page. A value of negative one (-1) indicates
53
         all items on one page.
54
 
55
     @attribute itemsPerPage
56
     @type Number
57
     @default 10
58
     **/
59
    itemsPerPage: {
60
        value: 10
61
    },
62
 
63
    /**
64
     Total number of items in all pages.
65
 
66
     @attribute totalItems
67
     @type Number
68
     @default 0
69
     **/
70
    totalItems: {
71
        value: 0
72
    }
73
};
74
 
75
Y.mix(PaginatorCore.prototype, {
76
    /**
77
     Sets the page to the previous page in the set, if there is a previous page.
78
     @method prevPage
79
     @chainable
80
     */
81
    prevPage: function () {
82
        if (this.hasPrevPage()) {
83
            this.set('page', this.get('page') - 1);
84
        }
85
 
86
        return this;
87
    },
88
 
89
    /**
90
     Sets the page to the next page in the set, if there is a next page.
91
 
92
     @method nextPage
93
     @chainable
94
     */
95
    nextPage: function () {
96
        if (this.hasNextPage()) {
97
            this.set('page', this.get('page') + 1);
98
        }
99
 
100
        return this;
101
    },
102
 
103
    /**
104
     Returns True if there is a previous page in the set.
105
 
106
     @method hasPrevPage
107
     @return {Boolean} `true` if there is a previous page, `false` otherwise.
108
     */
109
    hasPrevPage: function () {
110
        return this.get('page') > 1;
111
    },
112
 
113
    /**
114
     Returns True if there is a next page in the set.
115
 
116
     If totalItems isn't set, assume there is always next page.
117
 
118
     @method hasNextPage
119
     @return {Boolean} `true` if there is a next page, `false` otherwise.
120
     */
121
    hasNextPage: function () {
122
        return (!this.get('totalItems') || this.get('page') < this.get('totalPages'));
123
    },
124
 
125
 
126
    //--- P R O T E C T E D
127
 
128
    /**
129
     Returns the total number of pages based on the total number of
130
       items provided and the number of items per page
131
 
132
     @protected
133
     @method _getTotalPagesFn
134
     @return {Number} Total number of pages based on total number of items and
135
       items per page or one if itemsPerPage is less than one
136
     */
137
    _getTotalPagesFn: function () {
138
        var itemsPerPage = this.get('itemsPerPage');
139
 
140
        return (itemsPerPage < 1) ? 1 : Math.ceil(this.get('totalItems') / itemsPerPage);
141
    }
142
});
143
 
144
 
145
 
146
}, '3.18.1', {"requires": ["base"]});