Advertisement
Guest User

Contetns jQuery widget

a guest
Feb 3rd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 4.16 KB | None | 0 0
  1. $(function(){
  2.  
  3.     $.widget('custom.contents', {
  4.  
  5.         /**
  6.          * Default widget options
  7.          */
  8.         options : {
  9.        
  10.             element     : 'h2',                 // The elements that are to be listed in the contents
  11.            
  12.             showTitle   : true,                 // Whether or not to show a title above the contents
  13.             titleWrap   : 'h2',                 // The HTML element to wrap the title in
  14.             titleID     : 'contents-title',     // The ID to add to the title element
  15.             titleClass  : '',                   // The classes to add to the title element
  16.             titleText   : 'Contents',           // The text to display above the contents
  17.            
  18.             singleWrap  : 'a',                  // The HTML element to wrap each contents line with
  19.             singleID    : 'content-{index}',    // The ID to add to each contents line
  20.             singleClass : 'content',            // The class to add to each contents line
  21.            
  22.             linkID      : 'link_to_{singleID}'  // The ID to use for linking contents to the main document
  23.            
  24.         }, // options
  25.        
  26.         /**
  27.          * Constructor
  28.          */
  29.         _create : function(){
  30.        
  31.             this.elements = this._getElemets();     // Grab the elements
  32.             this.linkSingles = this._linkSingles(); // Check whether or not single
  33.            
  34.             this._doTitle();    // (Maybe) output the title
  35.             this._doContents(); // Output the contents
  36.            
  37.         }, // _create
  38.        
  39.         /**
  40.          * Return an object containing all of the requested elements
  41.          *
  42.          * @return jQuery
  43.          */
  44.         _getElemets : function(){
  45.        
  46.             return $(this.options.element);
  47.            
  48.         }, // _getQuestions
  49.        
  50.         /**
  51.          * Whether or not to add a link to each element
  52.          *
  53.          * @return boolean
  54.          */
  55.         _linkSingles : function(){
  56.        
  57.             return (this.options.singleWrap == 'a');
  58.            
  59.         }, // _linkSingles
  60.        
  61.         /**
  62.          * (Maybe) output the contents title
  63.          */
  64.         _doTitle : function(){
  65.        
  66.             if(!this.options.showTitle || this.elements.length <= 0)    // Ensure the title should be shown
  67.                 return false;
  68.            
  69.             var title = $('<' + this.options.titleWrap + '>')   // Create the title element...
  70.                 .attr('id', this.options.titleID)               // ...Add the 'ID' attribute
  71.                 .addClass(this.options.titleClass)              // ...Add the 'class' attribute
  72.                 .text(this.options.titleText);                  // ...Add the title text
  73.            
  74.             this.element.append(title); // Append the title to the element upon which this widget was instantiated
  75.            
  76.         }, // _doTitle
  77.        
  78.         /**
  79.          * Output the contents
  80.          */
  81.         _doContents : function(){
  82.        
  83.             var t = this;   // This object
  84.            
  85.             this.elements.each(function(index){ // Loop through each element that is to be added to the contents...
  86.            
  87.                 var trueIndex = index + 1,          // ...Start indexing at 1, not 0
  88.                     line = t._getSingle(trueIndex); // ...Construct the contnets line
  89.                
  90.                 if(t.linkSingles){                                              // Check whether or not the contetns should be links
  91.                     line.attr('href', '#' + t._getOption(trueIndex, 'linkID')); // ...Insert the 'href' element in to the contents line
  92.                     $(this).attr('id', t._getOption(trueIndex, 'linkID'));      // ...Insert the 'name' attribute in to the targeted element
  93.                 }
  94.                
  95.                 line.text($(this).text());      // ...Add the relevant text to the contents line
  96.                 t.element.append(line);         // ...Insert the contents line
  97.                
  98.             });
  99.            
  100.         }, // _doContents
  101.        
  102.         /**
  103.          * Return a single contents line as an object
  104.          *
  105.          * @param required integer index    The index of the current contents line
  106.          * @return jQuery
  107.          */
  108.         _getSingle : function(index){
  109.        
  110.             var line = $('<' + this.options.singleWrap + '>')
  111.                 .attr('id', this._getOption(index, 'singleID'))
  112.                 .addClass(this.options.singleClass);
  113.            
  114.             return line;
  115.            
  116.         }, // _getContnetsLine
  117.        
  118.         /**
  119.          * Get the extracted value of a given option
  120.          *
  121.          * @param required integer index    The index of the current contents line
  122.          * @param required string option    The option to get the value of
  123.          * @return string ID
  124.          */
  125.         _getOption : function(index, option){
  126.        
  127.             var t = this;   // The object
  128.            
  129.             var optionValue = this.options[option].replace(/{(.+?)}/g, function(_, name){   // Search for all instances of '{(.+?)}'...
  130.                 return name === 'index' ? index : t.options[name];                          // ...Replace all instance of '{(.+?)}' with the value of the associated variable
  131.             });
  132.            
  133.             return optionValue;
  134.            
  135.         } // _getOption
  136.        
  137.     });
  138.    
  139. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement