Edit navigation bar

Edit navigation bar

Hello I am new here and i am new in JQuery. Can anyone help me how can i edit this file to make more steps and do it to work perfectly?

main.js
  1. jQuery(document).ready(function($){
  2. function ProductBuilder( element ) {
  3. this.element = element;
  4. this.stepsWrapper = this.element.children('.cd-builder-steps');
  5. this.steps = this.element.find('.builder-step');
  6. //store some specific bulider steps
  7. this.models = this.element.find('[data-selection="models"]'); 
  8. this.summary;
  9. this.optionsLists = this.element.find('.options-list');
  10. //bottom summary 
  11. this.fixedSummary = this.element.find('.cd-builder-footer');
  12. this.modelPreview = this.element.find('.selected-product').find('img');
  13. this.totPriceWrapper = this.element.find('.tot-price').find('b');
  14. //builder navigations
  15. this.mainNavigation = this.element.find('.cd-builder-main-nav');
  16. this.secondaryNavigation = this.element.find('.cd-builder-secondary-nav');
  17. //used to check if the builder content has been loaded properly
  18. this.loaded = true;
  19. // bind builder events
  20. this.bindEvents();
  21. }

  22. ProductBuilder.prototype.bindEvents = function() {
  23. var self = this;

  24. //detect click on the left navigation
  25. this.mainNavigation.on('click', 'li:not(.active)', function(event){
  26. event.preventDefault();
  27. self.loaded && self.newContentSelected($(this).index());
  28. });

  29. //detect click on bottom fixed navigation
  30. this.secondaryNavigation.on('click', '.nav-item li:not(.buy)', function(event){ 
  31. event.preventDefault();
  32. var stepNumber = ( $(this).parents('.next').length > 0 ) ? $(this).index() + 1 : $(this).index() - 1;
  33. self.loaded && self.newContentSelected(stepNumber);
  34. });
  35. //detect click on one element in an options list (e.g, models, accessories)
  36. this.optionsLists.on('click', '.js-option', function(event){
  37. self.updateListOptions($(this));
  38. });
  39. //detect clicks on customizer controls (e.g., colors ...)
  40. this.stepsWrapper.on('click', '.cd-product-customizer a', function(event){
  41. event.preventDefault();
  42. self.customizeModel($(this));
  43. });
  44. };

  45. ProductBuilder.prototype.newContentSelected = function(nextStep) {
  46. //first - check if a model has been selected - user can navigate through the builder
  47. if( this.fixedSummary.hasClass('disabled') ) {
  48. //no model has been selected - show alert
  49. this.fixedSummary.addClass('show-alert');
  50. } else 
  51. {
  52. //model has been selected so show new content 
  53. //first check if the color step has been completed - in this case update the product bottom preview
  54. if( this.steps.filter('.active').is('[data-selection="colors"]') ) {
  55. //in this case, color has been changed - update the preview image
  56. var imageSelected = this.steps.filter('.active').find('.cd-product-previews').children('.selected').children('img').attr('src');
  57. this.modelPreview.attr('src', imageSelected);
  58. }
  59. //if Summary is the selected step (new step to be revealed) -> update summary content
  60. if( nextStep + 1 >= this.steps.length ) {
  61. this.createSummary();
  62. }
  63. this.showNewContent(nextStep);
  64. this.updatePrimaryNav(nextStep);
  65. this.updateSecondaryNav(nextStep);
  66. }
  67. }

  68. ProductBuilder.prototype.showNewContent = function(nextStep) {
  69. var actualStep = this.steps.filter('.active').index() + 1;
  70. if( actualStep < nextStep + 1 ) {
  71. //go to next section
  72. this.steps.eq(actualStep-1).removeClass('active back').addClass('move-left');
  73. this.steps.eq(nextStep).addClass('active').removeClass('move-left back');
  74. } else {
  75. //go to previous section
  76. this.steps.eq(actualStep-1).removeClass('active back move-left');
  77. this.steps.eq(nextStep).addClass('active back').removeClass('move-left');
  78. }
  79. }

  80. ProductBuilder.prototype.updatePrimaryNav = function(nextStep) {
  81. this.mainNavigation.find('li').eq(nextStep).addClass('active').siblings('.active').removeClass('active');
  82. }

  83. ProductBuilder.prototype.updateSecondaryNav = function(nextStep) {
  84. ( nextStep == 0 ) ? this.fixedSummary.addClass('step-1') : this.fixedSummary.removeClass('step-1');

  85. this.secondaryNavigation.find('.nav-item.next').find('li').eq(nextStep).addClass('visible').removeClass('visited').prevAll().removeClass('visited').addClass('visited').end().nextAll().removeClass('visible visited');
  86. this.secondaryNavigation.find('.nav-item.prev').find('li').eq(nextStep).addClass('visible').removeClass('visited').prevAll().removeClass('visited').addClass('visited').end().nextAll().removeClass('visible visited');
  87. }

  88. ProductBuilder.prototype.createSummary = function() {
  89. var self = this;
  90. this.steps.each(function(){
  91. //this function may need to be updated according to your builder steps and summary
  92. var step = $(this);
  93. if( $(this).data('selection') == 'colors' ) {
  94. //create the Color summary
  95. var colorSelected = $(this).find('.cd-product-customizer').find('.selected'),
  96. color = colorSelected.children('a').data('color'),
  97. colorName = colorSelected.data('content'),
  98. imageSelected = $(this).find('.cd-product-previews').find('.selected img').attr('src');
  99. self.summary.find('.summary-color').find('.color-label').text(colorName).siblings('.color-swatch').attr('data-color', color);
  100. self.summary.find('.product-preview').attr('src', imageSelected);
  101. } else if( $(this).data('selection') == 'accessoies' ) {
  102. var selectedOptions = $(this).find('.js-option.selected'),
  103. optionsContent = '';

  104. if( selectedOptions.length == 0 ) {
  105. optionsContent = '<li><p>No Accessories selected;</p></li>';
  106. } else {
  107. selectedOptions.each(function(){
  108. optionsContent +='<li><p>'+$(this).find('p').text()+'</p></li>';
  109. });
  110. }

  111. self.summary.find('.summary-accessories').children('li').remove().end().append($(optionsContent));
  112. }
  113. });
  114. }

  115. ProductBuilder.prototype.updateListOptions = function(listItem) {
  116. var self = this;
  117. if( listItem.hasClass('js-radio') ) {
  118. //this means only one option can be selected (e.g., models) - so check if there's another option selected and deselect it
  119. var alreadySelectedOption = listItem.siblings('.selected'),
  120. price = (alreadySelectedOption.length > 0 ) ? -Number(alreadySelectedOption.data('price')) : 0;

  121. //if the option was already selected and you are deselecting it - price is the price of the option just clicked
  122. ( listItem.hasClass('selected') ) 
  123. ? price = -Number(listItem.data('price'))
  124. : price = Number(listItem.data('price')) + price;

  125. //now deselect all the other options
  126. alreadySelectedOption.removeClass('selected');
  127. //toggle the option just selected
  128. listItem.toggleClass('selected');
  129. //update totalPrice - only if the step is not the Models step
  130. (listItem.parents('[data-selection="models"]').length == 0) && self.updatePrice(price);
  131. } else {
  132. //more than one options can be selected - just need to add/remove the one just clicked
  133. var price = ( listItem.hasClass('selected') ) ? -Number(listItem.data('price')) : Number(listItem.data('price'));
  134. //toggle the option just selected
  135. listItem.toggleClass('selected');
  136. //update totalPrice
  137. self.updatePrice(price);
  138. }
  139. if( listItem.parents('[data-selection="models"]').length > 0 ) {
  140. //since a model has been selected/deselected, you need to update the builder content
  141. self.updateModelContent(listItem);
  142. }
  143. };

  144. ProductBuilder.prototype.updateModelContent = function(model) {
  145. var self = this;
  146. if( model.hasClass('selected') ) {
  147. var modelType = model.data('model'),
  148. modelImage = model.find('img').attr('src');

  149. //need to update the product image in the bottom fixed navigation
  150. this.modelPreview.attr('src', modelImage);

  151. //need to update the content of the builder according to the selected product
  152. //first - remove the contet which refers to a different model
  153. this.models.siblings('li').remove();
  154. //second - load the new content
  155. $.ajax({
  156.         type       : "GET",
  157.         dataType   : "html",
  158.         url        : modelType+".html",
  159.         beforeSend : function(){
  160.         self.loaded = false;
  161.         model.siblings().removeClass('loaded');
  162.         },
  163.         success    : function(data){
  164.         self.models.after(data);
  165.         self.loaded = true;
  166.         model.addClass('loaded');
  167.         //activate top and bottom navigations
  168.         self.fixedSummary.add(self.mainNavigation).removeClass('disabled show-alert');
  169.         //update properties of the object
  170. self.steps = self.element.find('.builder-step');
  171. self.summary = self.element.find('[data-selection="summary"]');
  172. //detect click on one element in an options list
  173. self.optionsLists.off('click', '.js-option');
  174. self.optionsLists = self.element.find('.options-list');
  175. self.optionsLists.on('click', '.js-option', function(event){
  176. self.updateListOptions($(this));
  177. });

  178. //this is used not to load the animation the first time new content is loaded
  179. self.element.find('.first-load').removeClass('first-load');
  180.         },
  181.         error     : function(jqXHR, textStatus, errorThrown) {
  182.             //you may want to show an error message here
  183.         }
  184. });

  185. //update price (no adding/removing)
  186. this.totPriceWrapper.text(model.data('price'));
  187. } else {
  188. //no model has been selected
  189. this.fixedSummary.add(this.mainNavigation).addClass('disabled');
  190. //update price
  191. this.totPriceWrapper.text('0');

  192. this.models.find('.loaded').removeClass('loaded');
  193. }
  194. };

  195. ProductBuilder.prototype.customizeModel = function(target) {
  196. var parent = target.parent('li')
  197. index = parent.index();
  198. //update final price
  199. var price = ( parent.hasClass('selected') )
  200. ? 0
  201. : Number(parent.data('price')) - parent.siblings('.selected').data('price');

  202. this.updatePrice(price);
  203. target.parent('li').addClass('selected').siblings().removeClass('selected').parents('.cd-product-customizer').siblings('.cd-product-previews').children('.selected').removeClass('selected').end().children('li').eq(index).addClass('selected');
  204. };

  205. ProductBuilder.prototype.updatePrice = function(price) {
  206. var actualPrice = Number(this.totPriceWrapper.text()) + price;
  207. this.totPriceWrapper.text(actualPrice);
  208. };

  209. if( $('.cd-product-builder').length > 0 ) {
  210. $('.cd-product-builder').each(function(){
  211. //create a productBuilder object for each .cd-product-builder
  212. new ProductBuilder($(this));
  213. });
  214. }
  215. });