Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($) {
- /**
- * trim functions
- *
- * @class trim
- */
- function trim() {
- 'use strict';
- /**
- * @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 "..."
- *
- * @param string
- * @returns {*|string}
- */
- var trim = function(string) {
- if (string.length > trimLength) {
- return string.substr(this.trimStart, trimLength) + ' ...';
- }
- return string;
- };
- /**
- * remove quotes
- *
- * @param string
- * @returns {XML|void|*|string}
- */
- var removeQuotes = function(string) {
- return string.replace(/"/g, '');
- };
- }
- var trimClass = new trim();
- trimClass.start();
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement