ellipsis.js 4.1 KB

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