Skip to content

Instantly share code, notes, and snippets.

@scheibome
Created February 18, 2016 20:00
Show Gist options
  • Save scheibome/7b646fa7987faf6de00e to your computer and use it in GitHub Desktop.
Save scheibome/7b646fa7987faf6de00e to your computer and use it in GitHub Desktop.
QZ javascript
(function($) {
'use strict';
/**
* trim functions
*
* @class trim
*/
function Trim() {
/**
* @type {number}
* @private
*/
var trimLength = 100;
/**
* @type {number}
* @public
*/
this.trimStart = 0;
/**
* crop each element with data-trim="true"
*
* @return {void}
*/
this.start = function() {
var $elements = getTrimElements();
$elements.each(function() {
var $this = $(this);
var content = $this.html();
// trim content
$this.html(trim(content));
// if changed content, add title attribute
if ($this.html() !== $this.html(trim(content))) {
$this.prop('title', removeQuotes(content));
}
});
};
/**
* @returns {*|HTMLElement}
*/
var getTrimElements = function() {
return $('*[data-trim="true"]');
};
/**
* Trim string by 100 characters and append "..."
*
* @type {string}
* @returns {*|string}
*/
var trim = function(string) {
if (string.length > trimLength) {
return string.substr(this.trimStart, trimLength) + ' ...';
}
return string;
};
/**
* remove quotes
*
* @type {string}
* @returns {XML|void|*|string}
*/
var removeQuotes = function(string) {
return string.replace(/"/g, '');
};
}
var trimClass = new Trim();
trimClass.start();
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment