mirror of
https://github.com/moodle/moodle.git
synced 2025-07-19 05:11:33 +02:00
MDL-57972 javascript: Add truncate.js third party lib
Part of MDL-55611
This commit is contained in:
125
lib/amd/src/truncate.js
Normal file
125
lib/amd/src/truncate.js
Normal file
@@ -0,0 +1,125 @@
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Module for text truncation.
|
||||
*
|
||||
* Implementation provided by Pathable (thanks!).
|
||||
* See: https://github.com/pathable/truncate
|
||||
*
|
||||
* @module core/truncate
|
||||
* @package core
|
||||
* @class truncate
|
||||
* @copyright 2017 Pathable
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
define(['jquery'], function($) {
|
||||
|
||||
// Matches trailing non-space characters.
|
||||
var chop = /(\s*\S+|\s)$/;
|
||||
|
||||
// Matches the first word in the string.
|
||||
var start = /^(\S*)/;
|
||||
|
||||
// Return a truncated html string. Delegates to $.fn.truncate.
|
||||
$.truncate = function(html, options) {
|
||||
return $('<div></div>').append(html).truncate(options).html();
|
||||
};
|
||||
|
||||
// Truncate the contents of an element in place.
|
||||
$.fn.truncate = function(options) {
|
||||
if ($.isNumeric(options)) options = {length: options};
|
||||
var o = $.extend({}, $.truncate.defaults, options);
|
||||
|
||||
return this.each(function() {
|
||||
var self = $(this);
|
||||
|
||||
if (o.noBreaks) self.find('br').replaceWith(' ');
|
||||
|
||||
var text = self.text();
|
||||
var excess = text.length - o.length;
|
||||
|
||||
if (o.stripTags) self.text(text);
|
||||
|
||||
// Chop off any partial words if appropriate.
|
||||
if (o.words && excess > 0) {
|
||||
var truncated = text.slice(0, o.length).replace(chop, '').length;
|
||||
|
||||
if (o.keepFirstWord && truncated === 0) {
|
||||
excess = text.length - start.exec(text)[0].length - 1;
|
||||
} else {
|
||||
excess = text.length - truncated - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (excess < 0 || !excess && !o.truncated) return;
|
||||
|
||||
// Iterate over each child node in reverse, removing excess text.
|
||||
$.each(self.contents().get().reverse(), function(i, el) {
|
||||
var $el = $(el);
|
||||
var text = $el.text();
|
||||
var length = text.length;
|
||||
|
||||
// If the text is longer than the excess, remove the node and continue.
|
||||
if (length <= excess) {
|
||||
o.truncated = true;
|
||||
excess -= length;
|
||||
$el.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove the excess text and append the ellipsis.
|
||||
if (el.nodeType === 3) {
|
||||
$(el.splitText(length - excess - 1)).replaceWith(o.ellipsis);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Recursively truncate child nodes.
|
||||
$el.truncate($.extend(o, {length: length - excess}));
|
||||
return false;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$.truncate.defaults = {
|
||||
|
||||
// Strip all html elements, leaving only plain text.
|
||||
stripTags: false,
|
||||
|
||||
// Only truncate at word boundaries.
|
||||
words: false,
|
||||
|
||||
// When 'words' is active, keeps the first word in the string
|
||||
// even if it's longer than a target length.
|
||||
keepFirstWord: false,
|
||||
|
||||
// Replace instances of <br> with a single space.
|
||||
noBreaks: false,
|
||||
|
||||
// The maximum length of the truncated html.
|
||||
length: Infinity,
|
||||
|
||||
// The character to use as the ellipsis. The word joiner (U+2060) can be
|
||||
// used to prevent a hanging ellipsis, but displays incorrectly in Chrome
|
||||
// on Windows 7.
|
||||
// http://code.google.com/p/chromium/issues/detail?id=68323
|
||||
ellipsis: '\u2026' // '\u2060\u2026'
|
||||
|
||||
};
|
||||
|
||||
return {
|
||||
truncate: $.truncate,
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user