/
tables.js
215 lines (180 loc) · 6.06 KB
/
tables.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
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
/**
* @title WET-BOEW Tables
* @overview Integrates the DataTables plugin into WET providing searching, sorting, filtering, pagination and other advanced features for tables.
* @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html
* @author @jeresiv
*/
/*jshint scripturl:true*/
( function( $, window, wb ) {
"use strict";
/*
* Variable and function definitions.
* These are global to the plugin - meaning that they will be initialized once per page,
* not once per instance of plugin on the page. So, this is a good place to define
* variables that are common to all instances of the plugin on a page.
*/
var componentName = "wb-tables",
selector = "." + componentName,
initEvent = "wb-init" + selector,
$document = wb.doc,
idCount = 0,
i18n, i18nText, defaults,
/**
* @method init
* @param {jQuery Event} event Event that triggered the function call
*/
init = function( event ) {
// Start initialization
// returns DOM object = proceed with init
// returns undefined = do not proceed with init (e.g., already initialized)
var elm = wb.init( event, componentName, selector ),
elmId;
if ( elm ) {
elmId = elm.id;
// Ensure there is a unique id on the element
if ( !elmId ) {
elmId = componentName + "-id-" + idCount;
idCount += 1;
elm.id = elmId;
}
// Only initialize the i18nText once
if ( !i18nText ) {
i18n = wb.i18n;
i18nText = {
aria: {
sortAscending: i18n( "sortAsc" ),
sortDescending: i18n( "sortDesc" )
},
emptyTable: i18n( "emptyTbl" ),
info: i18n( "infoEntr" ),
infoEmpty: i18n( "infoEmpty" ),
infoFiltered: i18n( "infoFilt" ),
lengthMenu: i18n( "lenMenu" ),
loadingRecords: i18n( "load" ),
paginate: {
first: i18n( "first" ),
last: i18n( "last" ),
next: i18n( "nxt" ),
previous: i18n( "prv" ),
page: i18n( "page" )
},
processing: i18n( "process" ),
search: i18n( "filter" ),
thousands: i18n( "info1000" ),
zeroRecords: i18n( "infoEmpty" )
};
}
defaults = {
asStripeClasses: [],
language: i18nText,
dom: "<'top'fil>rt<'bottom'p><'clear'>"
};
Modernizr.load( {
load: [ "site!deps/jquery.dataTables" + wb.getMode() + ".js" ],
testReady: function() {
return ( $.fn.dataTable && $.fn.dataTable.version );
},
complete: function() {
var $elm = $( "#" + elmId ),
dataTableExt = $.fn.dataTableExt;
/*
* Extend sorting support
*/
$.extend( dataTableExt.type.order, {
// Enable internationalization support in the sorting
"html-pre": function( a ) {
return wb.normalizeDiacritics(
!a ? "" : a.replace ?
a.replace( /<.*?>/g, "" ).toLowerCase() : a + ""
);
},
"string-case-pre": function( a ) {
return wb.normalizeDiacritics( a );
},
"string-pre": function( a ) {
return wb.normalizeDiacritics( a );
},
// Formatted number sorting
"formatted-num-asc": function( a, b ) {
return wb.formattedNumCompare( a, b );
},
"formatted-num-desc": function( a, b ) {
return wb.formattedNumCompare( b, a );
}
} );
// Add the container or the sorting icons
$elm.find( "th" ).append( "<span class='sorting-cnt'><span class='sorting-icons'></span></span>" );
// Create the DataTable object
$elm.dataTable( $.extend( true, {}, defaults, window[ componentName ], wb.getData( $elm, componentName ) ) );
}
} );
}
};
// Bind the init event of the plugin
$document.on( "timerpoke.wb " + initEvent, selector, init );
// Handle the draw.dt event
$document.on( "init.dt draw.dt", selector, function( event, settings ) {
var $elm = $( event.target ),
pagination = $elm.next( ".bottom" ).find( "div:first-child" ),
paginate_buttons = $elm.next( ".bottom" ).find( ".paginate_button" ),
ol = document.createElement( "OL" ),
li = document.createElement( "LI" );
// Update Pagination List
for ( var i = 0; i < paginate_buttons.length; i++ ) {
var item = li.cloneNode( true );
item.appendChild( paginate_buttons[ i ] );
ol.appendChild( item );
}
ol.className = "pagination mrgn-tp-0 mrgn-bttm-0";
pagination.empty();
pagination.append( ol );
// Update the aria-pressed properties on the pagination buttons
// Should be pushed upstream to DataTables
$elm.next( ".bottom" ).find( ".paginate_button" )
.attr( {
"role": "button",
"href": "javascript:;"
} )
.not( ".previous, .next" )
.attr( "aria-pressed", "false" )
.html( function( index, oldHtml ) {
return "<span class='wb-inv'>" + i18nText.paginate.page + " </span>" + oldHtml;
} )
.filter( ".current" )
.attr( "aria-pressed", "true" );
if ( event.type === "init" ) {
// Identify that initialization has completed
wb.ready( $elm, componentName );
}
// Identify that the table has been updated
$elm.trigger( "wb-updated" + selector, [ settings ] );
} );
// Handle the draw.dt event
$document.on( "submit", ".wb-tables-filter", function( event ) {
event.preventDefault();
var $form = $( this ),
$datatable = $( "#" + $form.data( "bind-to" ) ).dataTable( { "retrieve": true } ).api();
// Lets reset the search;
$datatable.search( "" ).columns().search( "" );
// Lets loop throug all options
$form.find( "[name]" ).each( function() {
var $elm = $( this ),
$value = ( $elm.is( "select" ) ) ? $elm.find( "option:selected" ).val() : $elm.val();
if ( $value ) {
$datatable.column( parseInt( $elm.attr( "data-column" ), 10 ) ).search( $value ).draw();
}
} );
return false;
} );
$document.on( "click", ".wb-tables-filter [type='reset']", function( event ) {
event.preventDefault();
var $form = $( this ).closest( ".wb-tables-filter" ),
$datatable = $( "#" + $form.data( "bind-to" ) ).dataTable( { "retrieve": true } ).api();
$datatable.search( "" ).columns().search( "" ).draw();
$form.find( "select" ).prop( "selectedIndex", 0 );
$form.find( "input[type=date]" ).val( "" );
return false;
} );
// Add the timer poke to initialize the plugin
wb.add( selector );
} )( jQuery, window, wb );