ellipsis.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*! © SpryMedia Ltd - datatables.net/license */
  2. (function( factory ){
  3. if ( typeof define === 'function' && define.amd ) {
  4. // AMD
  5. define( ['datatables.net'], function ( $ ) {
  6. return factory( $, window, document );
  7. } );
  8. }
  9. else if ( typeof exports === 'object' ) {
  10. // CommonJS
  11. module.exports = function (root, $) {
  12. if ( ! root ) {
  13. // CommonJS environments without a window global must pass a
  14. // root. This will give an error otherwise
  15. root = window;
  16. }
  17. if ( ! $.fn.dataTable ) {
  18. require('datatables.net')(root, $);
  19. }
  20. return factory( $, root, root.document );
  21. };
  22. }
  23. else {
  24. // Browser
  25. factory( jQuery, window, document );
  26. }
  27. }(function( $, window, document, undefined ) {
  28. 'use strict';
  29. var DataTable = $.fn.dataTable;
  30. /**
  31. * This data rendering helper method can be useful for cases where you have
  32. * potentially large data strings to be shown in a column that is restricted by
  33. * width. The data for the column is still fully searchable and sortable, but if
  34. * it is longer than a give number of characters, it will be truncated and
  35. * shown with ellipsis. A browser provided tooltip will show the full string
  36. * to the end user on mouse hover of the cell.
  37. *
  38. * This function should be used with the `dt-init columns.render` configuration
  39. * option of DataTables.
  40. *
  41. * It accepts three parameters:
  42. *
  43. * 1. `-type integer` - The number of characters to restrict the displayed data
  44. * to.
  45. * 2. `-type boolean` (optional - default `false`) - Indicate if the truncation
  46. * of the string should not occur in the middle of a word (`true`) or if it
  47. * can (`false`). This can allow the display of strings to look nicer, at the
  48. * expense of showing less characters.
  49. * 2. `-type boolean` (optional - default `false`) - Escape HTML entities
  50. * (`true`) or not (`false` - default).
  51. *
  52. * @name ellipsis
  53. * @summary Restrict output data to a particular length, showing anything
  54. * longer with ellipsis and a browser provided tooltip on hover.
  55. * @author [Allan Jardine](http://datatables.net)
  56. * @requires DataTables 1.10+
  57. *
  58. * @returns {Number} Calculated average
  59. *
  60. * @example
  61. * // Restrict a column to 17 characters, don't split words
  62. * $('#example').DataTable( {
  63. * columnDefs: [ {
  64. * targets: 1,
  65. * render: DataTable.render.ellipsis( 17, true )
  66. * } ]
  67. * } );
  68. *
  69. * @example
  70. * // Restrict a column to 10 characters, do split words
  71. * $('#example').DataTable( {
  72. * columnDefs: [ {
  73. * targets: 2,
  74. * render: DataTable.render.ellipsis( 10 )
  75. * } ]
  76. * } );
  77. */
  78. DataTable.render.ellipsis = function (cutoff, wordbreak, escapeHtml) {
  79. var esc = function (t) {
  80. return ('' + t)
  81. .replace(/&/g, '&')
  82. .replace(/</g, '&lt;')
  83. .replace(/>/g, '&gt;')
  84. .replace(/"/g, '&quot;');
  85. };
  86. return function (d, type, row) {
  87. // Order, search and type get the original data
  88. if (type !== 'display') {
  89. return d;
  90. }
  91. if (typeof d !== 'number' && typeof d !== 'string') {
  92. if (escapeHtml) {
  93. return esc(d);
  94. }
  95. return d;
  96. }
  97. d = d.toString(); // cast numbers
  98. if (d.length <= cutoff) {
  99. if (escapeHtml) {
  100. return esc(d);
  101. }
  102. return d;
  103. }
  104. var shortened = d.substr(0, cutoff - 1);
  105. // Find the last white space character in the string
  106. if (wordbreak) {
  107. shortened = shortened.replace(/\s([^\s]*)$/, '');
  108. }
  109. // Protect against uncontrolled HTML input
  110. if (escapeHtml) {
  111. shortened = esc(shortened);
  112. }
  113. return ('<span class="ellipsis" title="' +
  114. esc(d) +
  115. '">' +
  116. shortened +
  117. '&#8230;</span>');
  118. };
  119. };
  120. return DataTable;
  121. }));