Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function(){
- $.widget('custom.contents', {
- /**
- * Default widget options
- */
- options : {
- element : 'h2', // The elements that are to be listed in the contents
- showTitle : true, // Whether or not to show a title above the contents
- titleWrap : 'h2', // The HTML element to wrap the title in
- titleID : 'contents-title', // The ID to add to the title element
- titleClass : '', // The classes to add to the title element
- titleText : 'Contents', // The text to display above the contents
- singleWrap : 'a', // The HTML element to wrap each contents line with
- singleID : 'content-{index}', // The ID to add to each contents line
- singleClass : 'content', // The class to add to each contents line
- linkID : 'link_to_{singleID}' // The ID to use for linking contents to the main document
- }, // options
- /**
- * Constructor
- */
- _create : function(){
- this.elements = this._getElemets(); // Grab the elements
- this.linkSingles = this._linkSingles(); // Check whether or not single
- this._doTitle(); // (Maybe) output the title
- this._doContents(); // Output the contents
- }, // _create
- /**
- * Return an object containing all of the requested elements
- *
- * @return jQuery
- */
- _getElemets : function(){
- return $(this.options.element);
- }, // _getQuestions
- /**
- * Whether or not to add a link to each element
- *
- * @return boolean
- */
- _linkSingles : function(){
- return (this.options.singleWrap == 'a');
- }, // _linkSingles
- /**
- * (Maybe) output the contents title
- */
- _doTitle : function(){
- if(!this.options.showTitle || this.elements.length <= 0) // Ensure the title should be shown
- return false;
- var title = $('<' + this.options.titleWrap + '>') // Create the title element...
- .attr('id', this.options.titleID) // ...Add the 'ID' attribute
- .addClass(this.options.titleClass) // ...Add the 'class' attribute
- .text(this.options.titleText); // ...Add the title text
- this.element.append(title); // Append the title to the element upon which this widget was instantiated
- }, // _doTitle
- /**
- * Output the contents
- */
- _doContents : function(){
- var t = this; // This object
- this.elements.each(function(index){ // Loop through each element that is to be added to the contents...
- var trueIndex = index + 1, // ...Start indexing at 1, not 0
- line = t._getSingle(trueIndex); // ...Construct the contnets line
- if(t.linkSingles){ // Check whether or not the contetns should be links
- line.attr('href', '#' + t._getOption(trueIndex, 'linkID')); // ...Insert the 'href' element in to the contents line
- $(this).attr('id', t._getOption(trueIndex, 'linkID')); // ...Insert the 'name' attribute in to the targeted element
- }
- line.text($(this).text()); // ...Add the relevant text to the contents line
- t.element.append(line); // ...Insert the contents line
- });
- }, // _doContents
- /**
- * Return a single contents line as an object
- *
- * @param required integer index The index of the current contents line
- * @return jQuery
- */
- _getSingle : function(index){
- var line = $('<' + this.options.singleWrap + '>')
- .attr('id', this._getOption(index, 'singleID'))
- .addClass(this.options.singleClass);
- return line;
- }, // _getContnetsLine
- /**
- * Get the extracted value of a given option
- *
- * @param required integer index The index of the current contents line
- * @param required string option The option to get the value of
- * @return string ID
- */
- _getOption : function(index, option){
- var t = this; // The object
- var optionValue = this.options[option].replace(/{(.+?)}/g, function(_, name){ // Search for all instances of '{(.+?)}'...
- return name === 'index' ? index : t.options[name]; // ...Replace all instance of '{(.+?)}' with the value of the associated variable
- });
- return optionValue;
- } // _getOption
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement