Advertisement
einpraegsam

Example javascript for private/public properties/functions

Feb 15th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.     /**
  3.      * trim functions
  4.      *
  5.      * @class trim
  6.      */
  7.     function trim() {
  8.         'use strict';
  9.  
  10.         /**
  11.          * @type {number}
  12.          * @private
  13.          */
  14.         var trimLength = 100;
  15.  
  16.         /**
  17.          * @type {number}
  18.          * @public
  19.          */
  20.         this.trimStart = 0;
  21.  
  22.         /**
  23.          * crop each element with data-trim="true"
  24.          *
  25.          * @return {void}
  26.          */
  27.         this.start = function() {
  28.             var $elements = getTrimElements();
  29.             $elements.each(function() {
  30.                 var $this = $(this);
  31.                 var content = $this.html();
  32.  
  33.                 // trim content
  34.                 $this.html(trim(content));
  35.  
  36.                 // if changed content, add title attribute
  37.                 if ($this.html() !== $this.html(trim(content))) {
  38.                     $this.prop('title', removeQuotes(content));
  39.                 }
  40.             });
  41.         };
  42.  
  43.         /**
  44.          * @returns {*|HTMLElement}
  45.          */
  46.         var getTrimElements = function() {
  47.             return $('*[data-trim="true"]');
  48.         };
  49.  
  50.         /**
  51.          * Trim string by 100 characters and append "..."
  52.          *
  53.          * @param string
  54.          * @returns {*|string}
  55.          */
  56.         var trim = function(string) {
  57.             if (string.length > trimLength) {
  58.                 return string.substr(this.trimStart, trimLength) + ' ...';
  59.             }
  60.             return string;
  61.         };
  62.  
  63.         /**
  64.          * remove quotes
  65.          *
  66.          * @param string
  67.          * @returns {XML|void|*|string}
  68.          */
  69.         var removeQuotes = function(string) {
  70.             return string.replace(/"/g, '');
  71.         };
  72.     }
  73.  
  74.     var trimClass = new trim();
  75.     trimClass.start();
  76. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement