|
1
|
|
|
/** |
|
2
|
|
|
* @licence GNU GPL v2+ |
|
3
|
|
|
* @author Gabriel Birke <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Load and render current donation comments |
|
8
|
|
|
* |
|
9
|
|
|
* @constructor |
|
10
|
|
|
*/ |
|
11
|
|
|
var DonationComments = function ( commentContainer, paginationContainer ) { |
|
12
|
|
|
this.commentContainer = commentContainer; |
|
13
|
|
|
this.paginationContainer = paginationContainer; |
|
14
|
|
|
}; |
|
15
|
|
|
|
|
16
|
|
|
var DOM_SELECTORS = { |
|
17
|
|
|
data: { |
|
18
|
|
|
NO_COMMENTS: 'no-comments' |
|
19
|
|
|
} |
|
20
|
|
|
}; |
|
21
|
|
|
|
|
22
|
|
|
$.extend( DonationComments.prototype, { |
|
23
|
|
|
init: function () { |
|
24
|
|
|
this.paginationContainer.find( '.first' ).bind( 'click', $.proxy( this.goToFirstPage, this ) ); |
|
25
|
|
|
this.paginationContainer.find( '.last' ).bind( 'click', $.proxy( this.goToLastPage, this ) ); |
|
26
|
|
|
this.paginationContainer.find( '.prev' ).bind( 'click', $.proxy( this.goToPrevPage, this ) ); |
|
27
|
|
|
this.paginationContainer.find( '.next' ).bind( 'click', $.proxy( this.goToNextPage, this ) ); |
|
28
|
|
|
this.currentPage = 0; |
|
29
|
|
|
this.itemsPerPage = 10; |
|
30
|
|
|
this.numPages = 1; |
|
31
|
|
|
this.update(); |
|
32
|
|
|
}, |
|
33
|
|
|
|
|
34
|
|
|
update: function () { |
|
35
|
|
|
var self = this; |
|
36
|
|
|
$.ajax( '../list-comments.json?n=100&anon=1', { |
|
37
|
|
|
dataType: 'json', |
|
38
|
|
|
success: function ( data ) { |
|
39
|
|
|
self.numPages = Math.ceil( data.length / self.itemsPerPage ); |
|
40
|
|
|
self.commentContainer.html( self.renderHtml( data ) ); |
|
41
|
|
|
self.updatePagination(); |
|
42
|
|
|
} |
|
43
|
|
|
} ); |
|
44
|
|
|
}, |
|
45
|
|
|
|
|
46
|
|
|
renderHtml: function ( data ) { |
|
47
|
|
|
var html = $('<div></div>'), |
|
48
|
|
|
currentPage, pageContainer, |
|
49
|
|
|
dataPages = this.paginateData( data ), |
|
50
|
|
|
self = this; |
|
51
|
|
|
if ( !data.length ) { |
|
52
|
|
|
return '<div class="noDonationComments">' + this.commentContainer.data( DOM_SELECTORS.data.NO_COMMENTS) + '</div>'; |
|
53
|
|
|
} |
|
54
|
|
|
for ( currentPage = 0; currentPage < this.numPages; currentPage++ ) { |
|
55
|
|
|
pageContainer = $( '<div class="wrap-items comment-page comment-page-' + currentPage + '"></div>' ); |
|
56
|
|
|
$.each( dataPages[currentPage], function( index, item ) { |
|
57
|
|
|
pageContainer.append( |
|
|
|
|
|
|
58
|
|
|
'<article class="comment-item">' + |
|
59
|
|
|
'<span class="field-amount-name">' + item.betrag + ' € von ' + item.spender + '</span>' + |
|
60
|
|
|
'<span class="date-time">' + self._renderDate( item.datum ) + '</span>' + |
|
61
|
|
|
'<p>' + item.kommentar + '</p></article>' |
|
62
|
|
|
); |
|
63
|
|
|
} ); |
|
64
|
|
|
html.append( pageContainer ); |
|
65
|
|
|
} |
|
66
|
|
|
return html; |
|
67
|
|
|
}, |
|
68
|
|
|
|
|
69
|
|
|
paginateData: function ( data ) { |
|
70
|
|
|
if ( !data.length ) { |
|
71
|
|
|
return []; |
|
72
|
|
|
} |
|
73
|
|
|
var pages = [], |
|
74
|
|
|
i = 0, |
|
75
|
|
|
n = data.length; |
|
76
|
|
|
|
|
77
|
|
|
while (i < n) { |
|
78
|
|
|
pages.push( data.slice( i, i += this.itemsPerPage ) ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return pages; |
|
82
|
|
|
}, |
|
83
|
|
|
|
|
84
|
|
|
goToFirstPage: function () { |
|
85
|
|
|
this.currentPage = 0; |
|
86
|
|
|
this.updatePagination(); |
|
87
|
|
|
}, |
|
88
|
|
|
|
|
89
|
|
|
goToLastPage: function () { |
|
90
|
|
|
this.currentPage = this.numPages - 1; |
|
91
|
|
|
this.updatePagination(); |
|
92
|
|
|
}, |
|
93
|
|
|
|
|
94
|
|
|
goToPrevPage: function () { |
|
95
|
|
|
if ( this.currentPage > 0 ) { |
|
96
|
|
|
this.currentPage--; |
|
97
|
|
|
} |
|
98
|
|
|
this.updatePagination(); |
|
99
|
|
|
}, |
|
100
|
|
|
|
|
101
|
|
|
goToNextPage: function () { |
|
102
|
|
|
if ( this.currentPage < this.numPages - 1 ) { |
|
103
|
|
|
this.currentPage++; |
|
104
|
|
|
} |
|
105
|
|
|
this.updatePagination(); |
|
106
|
|
|
}, |
|
107
|
|
|
|
|
108
|
|
|
updatePagination: function () { |
|
109
|
|
|
// Show current page |
|
110
|
|
|
$( '.comment-page', this.commentContainer ).hide(); |
|
111
|
|
|
$( '.comment-page-' + this.currentPage ).show(); |
|
112
|
|
|
|
|
113
|
|
|
// Show page numbers |
|
114
|
|
|
this.paginationContainer.find( '.current-page' ).text( this.currentPage + 1 ); |
|
115
|
|
|
this.paginationContainer.find( '.num-pages' ).text( this.numPages ); |
|
116
|
|
|
|
|
117
|
|
|
// set visibility of back and forward arrows depending on this.numPages and this.currentPage |
|
118
|
|
|
this.paginationContainer.find( '> span' ).hide(); |
|
119
|
|
|
if ( this.numPages > 1 ) { |
|
120
|
|
|
this.paginationContainer.find( '.pages' ).show(); |
|
121
|
|
|
} |
|
122
|
|
|
if ( this.currentPage > 0 ) { |
|
123
|
|
|
this.paginationContainer.find( '.prev' ).show(); |
|
124
|
|
|
this.paginationContainer.find( '.first' ).show(); |
|
125
|
|
|
} |
|
126
|
|
|
if ( this.currentPage < this.numPages - 1 ) { |
|
127
|
|
|
this.paginationContainer.find( '.next' ).show(); |
|
128
|
|
|
this.paginationContainer.find( '.last' ).show(); |
|
129
|
|
|
} |
|
130
|
|
|
}, |
|
131
|
|
|
|
|
132
|
|
|
_formatTwoDigitNumber: function ( n ) { |
|
133
|
|
|
return n < 10 ? '0' + n : n; |
|
134
|
|
|
}, |
|
135
|
|
|
|
|
136
|
|
|
_renderDate: function ( dateString ) { |
|
137
|
|
|
var donationDate = new Date( dateString ), |
|
138
|
|
|
donationDateParts = [ |
|
139
|
|
|
this._formatTwoDigitNumber( donationDate.getDate() ), |
|
140
|
|
|
'.', |
|
141
|
|
|
this._formatTwoDigitNumber( donationDate.getMonth() + 1 ), |
|
142
|
|
|
'.', |
|
143
|
|
|
donationDate.getFullYear(), |
|
144
|
|
|
' um ', |
|
145
|
|
|
donationDate.getHours(), |
|
146
|
|
|
':', |
|
147
|
|
|
this._formatTwoDigitNumber( donationDate.getMinutes() ), |
|
148
|
|
|
' Uhr' |
|
149
|
|
|
]; |
|
150
|
|
|
return donationDateParts.join( '' ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
} ); |
|
154
|
|
|
|
|
155
|
|
|
$( function () { |
|
156
|
|
|
var comments = new DonationComments( |
|
157
|
|
|
$( '.comment-commentContainer' ), |
|
158
|
|
|
$( '.comment-paginationContainer') |
|
159
|
|
|
); |
|
160
|
|
|
comments.init(); |
|
161
|
|
|
} ); |
|
162
|
|
|
|