You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.5 KiB

5 years ago
  1. /*! DataTables Bootstrap 3 integration
  2. * ©2011-2015 SpryMedia Ltd - datatables.net/license
  3. */
  4. /**
  5. * DataTables integration for Bootstrap 3. This requires Bootstrap 3 and
  6. * DataTables 1.10 or newer.
  7. *
  8. * This file sets the defaults and adds options to DataTables to style its
  9. * controls using Bootstrap. See http://datatables.net/manual/styling/bootstrap
  10. * for further information.
  11. */
  12. (function( factory ){
  13. if ( typeof define === 'function' && define.amd ) {
  14. // AMD
  15. define( ['jquery', 'datatables.net'], function ( $ ) {
  16. return factory( $, window, document );
  17. } );
  18. }
  19. else if ( typeof exports === 'object' ) {
  20. // CommonJS
  21. module.exports = function (root, $) {
  22. if ( ! root ) {
  23. root = window;
  24. }
  25. if ( ! $ || ! $.fn.dataTable ) {
  26. // Require DataTables, which attaches to jQuery, including
  27. // jQuery if needed and have a $ property so we can access the
  28. // jQuery object that is used
  29. $ = require('datatables.net')(root, $).$;
  30. }
  31. return factory( $, root, root.document );
  32. };
  33. }
  34. else {
  35. // Browser
  36. factory( jQuery, window, document );
  37. }
  38. }(function( $, window, document, undefined ) {
  39. 'use strict';
  40. var DataTable = $.fn.dataTable;
  41. /* Set the defaults for DataTables initialisation */
  42. $.extend( true, DataTable.defaults, {
  43. dom:
  44. "<'row'<'col-sm-6'l><'col-sm-6'f>>" +
  45. "<'row'<'col-sm-12'tr>>" +
  46. "<'row'<'col-sm-5'i><'col-sm-7'p>>",
  47. renderer: 'bootstrap'
  48. } );
  49. /* Default class modification */
  50. $.extend( DataTable.ext.classes, {
  51. sWrapper: "dataTables_wrapper form-inline dt-bootstrap",
  52. sFilterInput: "form-control input-sm",
  53. sLengthSelect: "form-control input-sm",
  54. sProcessing: "dataTables_processing panel panel-default"
  55. } );
  56. /* Bootstrap paging button renderer */
  57. DataTable.ext.renderer.pageButton.bootstrap = function ( settings, host, idx, buttons, page, pages ) {
  58. var api = new DataTable.Api( settings );
  59. var classes = settings.oClasses;
  60. var lang = settings.oLanguage.oPaginate;
  61. var aria = settings.oLanguage.oAria.paginate || {};
  62. var btnDisplay, btnClass, counter=0;
  63. var attach = function( container, buttons ) {
  64. var i, ien, node, button;
  65. var clickHandler = function ( e ) {
  66. e.preventDefault();
  67. if ( !$(e.currentTarget).hasClass('disabled') && api.page() != e.data.action ) {
  68. api.page( e.data.action ).draw( 'page' );
  69. }
  70. };
  71. for ( i=0, ien=buttons.length ; i<ien ; i++ ) {
  72. button = buttons[i];
  73. if ( $.isArray( button ) ) {
  74. attach( container, button );
  75. }
  76. else {
  77. btnDisplay = '';
  78. btnClass = '';
  79. switch ( button ) {
  80. case 'ellipsis':
  81. btnDisplay = '&#x2026;';
  82. btnClass = 'disabled';
  83. break;
  84. case 'first':
  85. btnDisplay = lang.sFirst;
  86. btnClass = button + (page > 0 ?
  87. '' : ' disabled');
  88. break;
  89. case 'previous':
  90. btnDisplay = lang.sPrevious;
  91. btnClass = button + (page > 0 ?
  92. '' : ' disabled');
  93. break;
  94. case 'next':
  95. btnDisplay = lang.sNext;
  96. btnClass = button + (page < pages-1 ?
  97. '' : ' disabled');
  98. break;
  99. case 'last':
  100. btnDisplay = lang.sLast;
  101. btnClass = button + (page < pages-1 ?
  102. '' : ' disabled');
  103. break;
  104. default:
  105. btnDisplay = button + 1;
  106. btnClass = page === button ?
  107. 'active' : '';
  108. break;
  109. }
  110. if ( btnDisplay ) {
  111. node = $('<li>', {
  112. 'class': classes.sPageButton+' '+btnClass,
  113. 'id': idx === 0 && typeof button === 'string' ?
  114. settings.sTableId +'_'+ button :
  115. null
  116. } )
  117. .append( $('<a>', {
  118. 'href': '#',
  119. 'aria-controls': settings.sTableId,
  120. 'aria-label': aria[ button ],
  121. 'data-dt-idx': counter,
  122. 'tabindex': settings.iTabIndex
  123. } )
  124. .html( btnDisplay )
  125. )
  126. .appendTo( container );
  127. settings.oApi._fnBindAction(
  128. node, {action: button}, clickHandler
  129. );
  130. counter++;
  131. }
  132. }
  133. }
  134. };
  135. // IE9 throws an 'unknown error' if document.activeElement is used
  136. // inside an iframe or frame.
  137. var activeEl;
  138. try {
  139. // Because this approach is destroying and recreating the paging
  140. // elements, focus is lost on the select button which is bad for
  141. // accessibility. So we want to restore focus once the draw has
  142. // completed
  143. activeEl = $(host).find(document.activeElement).data('dt-idx');
  144. }
  145. catch (e) {}
  146. attach(
  147. $(host).empty().html('<ul class="pagination"/>').children('ul'),
  148. buttons
  149. );
  150. if ( activeEl !== undefined ) {
  151. $(host).find( '[data-dt-idx='+activeEl+']' ).focus();
  152. }
  153. };
  154. return DataTable;
  155. }));