Commit d74d91f4 authored by Ryan Wyllie's avatar Ryan Wyllie
Browse files

MDL-57972 javascript: Add truncate.js third party lib

Part of MDL-55611
parent 3c45d26f
......@@ -56,6 +56,7 @@ lib/amd/src/chartjs-lazy.js
lib/maxmind/GeoIp2/
lib/maxmind/MaxMind/
lib/ltiprovider/
lib/amd/src/truncate.js
media/player/videojs/amd/src/video-lazy.js
media/player/videojs/amd/src/Youtube-lazy.js
media/player/videojs/videojs/
......
......@@ -57,6 +57,7 @@ lib/amd/src/chartjs-lazy.js
lib/maxmind/GeoIp2/
lib/maxmind/MaxMind/
lib/ltiprovider/
lib/amd/src/truncate.js
media/player/videojs/amd/src/video-lazy.js
media/player/videojs/amd/src/Youtube-lazy.js
media/player/videojs/videojs/
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
// 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,
};
});
......@@ -291,4 +291,10 @@
<version>3.0.2</version>
<licenseversion>2.0</licenseversion>
</library>
<library>
<location>amd/src/truncate.js</location>
<name>Truncate.js</name>
<license>MIT</license>
<version>0.0.1</version>
</library>
</libraries>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment