]>
jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - js/datatables-ellipsis.js
1 /*! © SpryMedia Ltd - datatables.net/license */
4 if ( typeof define
=== 'function' && define
.amd
) {
6 define( ['jquery', 'datatables.net'], function ( $ ) {
7 return factory( $, window
, document
);
10 else if ( typeof exports
=== 'object' ) {
12 var jq
= require('jquery');
13 var cjsRequires = function (root
, $) {
14 if ( ! $.fn
.dataTable
) {
15 require('datatables.net')(root
, $);
19 if (typeof window
!== 'undefined') {
20 module
.exports = function (root
, $) {
22 // CommonJS environments without a window global must pass a
23 // root. This will give an error otherwise
31 cjsRequires( root
, $ );
32 return factory( $, root
, root
.document
);
36 cjsRequires( window
, jq
);
37 module
.exports
= factory( jq
, window
, window
.document
);
42 factory( jQuery
, window
, document
);
44 }(function( $, window
, document
, undefined ) {
46 var DataTable
= $.fn
.dataTable
;
50 * This data rendering helper method can be useful for cases where you have
51 * potentially large data strings to be shown in a column that is restricted by
52 * width. The data for the column is still fully searchable and sortable, but if
53 * it is longer than a give number of characters, it will be truncated and
54 * shown with ellipsis. A browser provided tooltip will show the full string
55 * to the end user on mouse hover of the cell.
57 * This function should be used with the `dt-init columns.render` configuration
58 * option of DataTables.
60 * It accepts three parameters:
62 * 1. `-type integer` - The number of characters to restrict the displayed data
64 * 2. `-type boolean` (optional - default `false`) - Indicate if the truncation
65 * of the string should not occur in the middle of a word (`true`) or if it
66 * can (`false`). This can allow the display of strings to look nicer, at the
67 * expense of showing less characters.
68 * 2. `-type boolean` (optional - default `false`) - Escape HTML entities
69 * (`true`) or not (`false` - default).
72 * @summary Restrict output data to a particular length, showing anything
73 * longer with ellipsis and a browser provided tooltip on hover.
74 * @author [Allan Jardine](http://datatables.net)
75 * @requires DataTables 1.10+
77 * @returns {Number} Calculated average
80 * // Restrict a column to 17 characters, don't split words
81 * $('#example').DataTable( {
84 * render: DataTable.render.ellipsis( 17, true )
89 * // Restrict a column to 10 characters, do split words
90 * $('#example').DataTable( {
93 * render: DataTable.render.ellipsis( 10 )
97 DataTable
.render
.ellipsis = function (cutoff
, wordbreak
, escapeHtml
) {
98 var esc = function (t
) {
100 .replace(/&/g
, '&')
101 .replace(/</g
, '<')
102 .replace(/>/g
, '>')
103 .replace(/"/g, '"');
105 return function (d, type, row) {
106 // Order, search and type get the original data
107 if (type !== 'display') {
110 if (typeof d !== 'number' && typeof d !== 'string') {
116 d = d.toString(); // cast numbers
117 if (d.length <= cutoff) {
123 var shortened = d.substr(0, cutoff - 1);
124 // Find the last white space character in the string
126 shortened = shortened.replace(/\s([^\s]*)$/, '');
128 // Protect against uncontrolled HTML input
130 shortened = esc(shortened);
132 return ('<span class="ellipsis
" title="' +