OpenSencillo  2016.106
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
dataTables.material.js
1 
13 (function( factory ){
14  if ( typeof define === 'function' && define.amd ) {
15  // AMD
16  define( ['jquery', 'datatables.net'], function ( $ ) {
17  return factory( $, window, document );
18  } );
19  }
20  else if ( typeof exports === 'object' ) {
21  // CommonJS
22  module.exports = function (root, $) {
23  if ( ! root ) {
24  root = window;
25  }
26 
27  if ( ! $ || ! $.fn.dataTable ) {
28  // Require DataTables, which attaches to jQuery, including
29  // jQuery if needed and have a $ property so we can access the
30  // jQuery object that is used
31  $ = require('datatables.net')(root, $).$;
32  }
33 
34  return factory( $, root, root.document );
35  };
36  }
37  else {
38  // Browser
39  factory( jQuery, window, document );
40  }
41 }(function( $, window, document, undefined ) {
42 'use strict';
43 var DataTable = $.fn.dataTable;
44 
45 
46 /* Set the defaults for DataTables initialisation */
47 $.extend( true, DataTable.defaults, {
48  dom:
49  "<'mdl-grid'"+
50  "<'mdl-cell mdl-cell--6-col'l>"+
51  "<'mdl-cell mdl-cell--6-col'f>"+
52  ">"+
53  "<'mdl-grid dt-table'"+
54  "<'mdl-cell mdl-cell--12-col'tr>"+
55  ">"+
56  "<'mdl-grid'"+
57  "<'mdl-cell mdl-cell--4-col'i>"+
58  "<'mdl-cell mdl-cell--8-col'p>"+
59  ">",
60  renderer: 'material'
61 } );
62 
63 
64 /* Default class modification */
65 $.extend( DataTable.ext.classes, {
66  sWrapper: "dataTables_wrapper form-inline dt-material",
67  sFilterInput: "form-control input-sm",
68  sLengthSelect: "form-control input-sm",
69  sProcessing: "dataTables_processing panel panel-default"
70 } );
71 
72 
73 /* Bootstrap paging button renderer */
74 DataTable.ext.renderer.pageButton.material = function ( settings, host, idx, buttons, page, pages ) {
75  var api = new DataTable.Api( settings );
76  var classes = settings.oClasses;
77  var lang = settings.oLanguage.oPaginate;
78  var aria = settings.oLanguage.oAria.paginate || {};
79  var btnDisplay, btnClass, counter=0;
80 
81  var attach = function( container, buttons ) {
82  var i, ien, node, button, disabled, active;
83  var clickHandler = function ( e ) {
84  e.preventDefault();
85  if ( !$(e.currentTarget).hasClass('disabled') && api.page() != e.data.action ) {
86  api.page( e.data.action ).draw( 'page' );
87  }
88  };
89 
90  for ( i=0, ien=buttons.length ; i<ien ; i++ ) {
91  button = buttons[i];
92 
93  if ( $.isArray( button ) ) {
94  attach( container, button );
95  }
96  else {
97  btnDisplay = '';
98  active = false;
99 
100  switch ( button ) {
101  case 'ellipsis':
102  btnDisplay = '&#x2026;';
103  btnClass = 'disabled';
104  break;
105 
106  case 'first':
107  btnDisplay = lang.sFirst;
108  btnClass = button + (page > 0 ?
109  '' : ' disabled');
110  break;
111 
112  case 'previous':
113  btnDisplay = lang.sPrevious;
114  btnClass = button + (page > 0 ?
115  '' : ' disabled');
116  break;
117 
118  case 'next':
119  btnDisplay = lang.sNext;
120  btnClass = button + (page < pages-1 ?
121  '' : ' disabled');
122  break;
123 
124  case 'last':
125  btnDisplay = lang.sLast;
126  btnClass = button + (page < pages-1 ?
127  '' : ' disabled');
128  break;
129 
130  default:
131  btnDisplay = button + 1;
132  btnClass = '';
133  active = page === button;
134  break;
135  }
136 
137  if ( active ) {
138  btnClass += ' mdl-button--raised mdl-button--colored';
139  }
140 
141  if ( btnDisplay ) {
142  node = $('<button>', {
143  'class': 'mdl-button '+btnClass,
144  'id': idx === 0 && typeof button === 'string' ?
145  settings.sTableId +'_'+ button :
146  null,
147  'aria-controls': settings.sTableId,
148  'aria-label': aria[ button ],
149  'data-dt-idx': counter,
150  'tabindex': settings.iTabIndex,
151  'disabled': btnClass.indexOf('disabled') !== -1
152  } )
153  .html( btnDisplay )
154  .appendTo( container );
155 
156  settings.oApi._fnBindAction(
157  node, {action: button}, clickHandler
158  );
159 
160  counter++;
161  }
162  }
163  }
164  };
165 
166  // IE9 throws an 'unknown error' if document.activeElement is used
167  // inside an iframe or frame.
168  var activeEl;
169 
170  try {
171  // Because this approach is destroying and recreating the paging
172  // elements, focus is lost on the select button which is bad for
173  // accessibility. So we want to restore focus once the draw has
174  // completed
175  activeEl = $(host).find(document.activeElement).data('dt-idx');
176  }
177  catch (e) {}
178 
179  attach(
180  $(host).empty().html('<div class="pagination"/>').children(),
181  buttons
182  );
183 
184  if ( activeEl ) {
185  $(host).find( '[data-dt-idx='+activeEl+']' ).focus();
186  }
187 };
188 
189 
190 return DataTable;
191 }));