scinote-web/app/assets/javascripts/sitewide/string_utils.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

/*
* Truncate long strings where is necessary.
*/
function truncateLongString( el, chars ) {
if($.type(el) !== 'string'){
var input = $.trim(el.text());
} else {
var input = $.trim(el);
}
var html = "";
if( $.type(el) !== 'string' &&
el.children().hasClass('fas')) {
html = el.children()[0];
}
2016-09-29 20:12:52 +08:00
if( input.length >= chars ){
if($.type(el) != 'string') {
var newText = el.text().slice(0, chars);
}else {
var newText = el.slice(0, chars);
}
for( var i = newText.length; i > 0; i--){
if(newText[i] === ' ' && i > 10){
newText = newText.slice(0, i);
break;
}
}
if ( html ) {
2018-06-11 20:47:25 +08:00
el.text(html.outerHTML + newText + '...' );
} else {
if($.type(el) === 'string'){
return newText + '...';
} else {
2018-06-11 20:47:25 +08:00
el.text(newText + '...' );
}
}
} else {
return el;
}
}
/*
* Usefull for converting locals messages to error format
* (i.e. no dot at the end).
*/
String.prototype.strToErrorFormat = function() {
var length = this.length;
if (this[length - 1] === ".") {
length -= 1;
}
return this.slice(0, length);
}