Advertisement
Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createVehicleWidget(params, name) {
  2.     var widget = createWidget(params, name)
  3.  
  4.     widget.useSelectize = params.useSelectize
  5.     widget.htmlSelectors = {
  6.         container: '#vehicle-search',
  7.         type: '#vehicle-type',
  8.         brand: '#vehicle-brand',
  9.         year: '#vehicle-year',
  10.         model: '#vehicle-model'
  11.     }
  12.  
  13.     // Methods
  14.     widget.onInit = function () {
  15.         if (this.useSelectize === true) {
  16.             // Initialize select lists (selectize)                                                      onDropdownClose = Temp fix to remove "selected" class (see issue: https://github.com/selectize/selectize.js/issues/1191)
  17.             $(this.htmlSelectors.type).selectize({ placeholder: '- Select Type -', selectOnTab: true, onDropdownClose: function (dd) { $(dd).find('.selected:not(.active)').removeClass('selected') } })
  18.             $(this.htmlSelectors.brand).selectize({ placeholder: '- Select Brand -', selectOnTab: true, onDropdownClose: function (dd) { $(dd).find('.selected:not(.active)').removeClass('selected') } })
  19.             $(this.htmlSelectors.year).selectize({ placeholder: '- Select Year -', selectOnTab: true, onDropdownClose: function (dd) { $(dd).find('.selected:not(.active)').removeClass('selected') } })
  20.             $(this.htmlSelectors.model).selectize({ placeholder: '- Select Model -', selectOnTab: true, onDropdownClose: function (dd) { $(dd).find('.selected:not(.active)').removeClass('selected') } })
  21.             document.querySelector(this.htmlSelectors.type).selectize.on('change', this.onTypeSelected.bind(this))
  22.             document.querySelector(this.htmlSelectors.brand).selectize.on('change', this.onBrandSelected.bind(this))
  23.             document.querySelector(this.htmlSelectors.year).selectize.on('change', this.onYearSelected.bind(this))
  24.             document.querySelector(this.htmlSelectors.model).selectize.on('change', this.onModelSelected.bind(this))
  25.         } else {
  26.             document.querySelector(this.htmlSelectors.type).onchange = this.onTypeSelected.bind(this)
  27.             document.querySelector(this.htmlSelectors.brand).onchange = this.onBrandSelected.bind(this)
  28.             document.querySelector(this.htmlSelectors.year).onchange = this.onYearSelected.bind(this)
  29.             document.querySelector(this.htmlSelectors.model).onchange = this.onModelSelected.bind(this)
  30.         }
  31.        
  32.         // Update with data
  33.         this.update(this.data, true)
  34.     }
  35.     widget.onUpdate = function (data, isInit) {
  36.         if (data == null) {
  37.             data = {
  38.                 Page: 1,
  39.                 Engine: {
  40.                     SearchType: 2,
  41.                     Type: null,
  42.                     TypeName: null,
  43.                     Brand: null,
  44.                     BrandName: null,
  45.                     Year: null,
  46.                     Model: null,
  47.                 }
  48.             }
  49.         }
  50.         this.data = data
  51.         var nbItemsLoaded = 0
  52.         var htmlType = document.querySelector(this.htmlSelectors.type)
  53.         var htmlBrand = document.querySelector(this.htmlSelectors.brand)
  54.         var htmlYear = document.querySelector(this.htmlSelectors.year)
  55.         var htmlModel = document.querySelector(this.htmlSelectors.model)
  56.  
  57.         // Load types
  58.         if (isInit === true) {
  59.             this.setLoading(true)
  60.             this.loadingMethods.loadTypes().done(function (data) {
  61.                 this.refreshList(htmlType, data, this.data.Type)
  62.                 this.onItemLoaded(++nbItemsLoaded)
  63.             })
  64.         } else {
  65.             this.selectValue(htmlType, this.data.Engine.Type, true)
  66.             //htmlType.selectize.setValue(this.data.Type, true)
  67.             this.onItemLoaded(++nbItemsLoaded)
  68.         }
  69.  
  70.         if (this.data.Engine.Type !== null) {
  71.             // Load brands
  72.             this.loadingMethods.loadBrands(this.data.Engine.Type).done(function (data) {
  73.                 this.refreshList(htmlBrand, data, this.data.Engine.Brand)
  74.                 this.onItemLoaded(++nbItemsLoaded)
  75.             })
  76.         } else {
  77.             // Clear brands
  78.             this.refreshList(htmlBrand, null, null)
  79.             this.refreshList(htmlYear, null, null)
  80.             this.refreshList(htmlModel, null, null)
  81.             return
  82.         }
  83.  
  84.         if (this.data.Engine.Brand !== null) {
  85.             // Load years
  86.             this.loadingMethods.loadYears(this.data.Engine.Type, this.data.Engine.Brand).done(function (data) {
  87.  
  88.                 this.refreshList(htmlYear, data, this.data.Engine.Year)
  89.                 this.onItemLoaded(++nbItemsLoaded)
  90.             })
  91.         } else {
  92.             // Clear years
  93.             this.refreshList(htmlYear, null, null)
  94.             this.refreshList(htmlModel, null, null)
  95.             return
  96.         }
  97.  
  98.         if (this.data.Engine.year !== null) {
  99.             // Load models
  100.             this.loadingMethods.loadModels(this.data.Engine.Type, this.data.Engine.Brand, this.data.Engine.Year).done(function (data) {
  101.                 this.refreshList(htmlModel, data, this.data.Engine.Model)
  102.                 this.onItemLoaded(++nbItemsLoaded)
  103.             })
  104.         } else {
  105.             // Clear model
  106.             this.refreshList(htmlModel, null, null)
  107.             return
  108.         }
  109.     }
  110.     widget.onClear = function () {
  111.         var htmlType = document.querySelector(this.htmlSelectors.type)
  112.         var htmlBrand = document.querySelector(this.htmlSelectors.brand)
  113.         var htmlYear = document.querySelector(this.htmlSelectors.year)
  114.         var htmlModel = document.querySelector(this.htmlSelectors.model)
  115.         if (this.useSelectize === true) {
  116.             htmlType.selectize.clear(true)
  117.         } else {
  118.             htmlType.selectedIndex = 0
  119.         }
  120.         this.refreshList(htmlBrand, null, null)
  121.         this.refreshList(htmlYear, null, null)
  122.         this.refreshList(htmlModel, null, null)
  123.     }
  124.  
  125.     widget.onTypeSelected = function (value) {
  126.         value = this.useSelectize === true ? value : value.target.value
  127.         this.data.Engine.Type = value
  128.         this.data.Engine.TypeName = this.useSelectize === true ?
  129.             document.querySelector(this.htmlSelectors.type).innerText :
  130.             document.querySelector(this.htmlSelectors.type).querySelector('[value="' + value + '"]').innerText
  131.  
  132.         if (value === '')
  133.             return
  134.  
  135.         this.loadingMethods.loadBrands(value).done(function (data) {
  136.             var htmlBrand = document.querySelector(this.htmlSelectors.brand)
  137.             var htmlYear = document.querySelector(this.htmlSelectors.year)
  138.             var htmlModel = document.querySelector(this.htmlSelectors.model)
  139.             this.refreshList(htmlBrand, data, null)
  140.             this.refreshList(htmlYear, null, null)
  141.             this.refreshList(htmlModel, null, null)
  142.             this.openList(htmlBrand)
  143.         })
  144.     }
  145.     widget.onBrandSelected = function (value) {
  146.         value = this.useSelectize === true ? value : value.target.value
  147.         this.data.Engine.Brand = value
  148.         this.data.Engine.BrandName = this.useSelectize === true ?
  149.             document.querySelector(this.htmlSelectors.brand).innerText :
  150.             document.querySelector(this.htmlSelectors.brand).querySelector('[value="' + value + '"]').innerText
  151.  
  152.         if (value === '')
  153.             return
  154.  
  155.         this.loadingMethods.loadYears(this.data.Engine.Type, value).done(function (data) {
  156.             var htmlYear = document.querySelector(this.htmlSelectors.year)
  157.             var htmlModel = document.querySelector(this.htmlSelectors.model)
  158.             this.refreshList(htmlYear, data, null)
  159.             this.refreshList(htmlModel, null, null)
  160.             this.openList(htmlYear)
  161.         })
  162.     }
  163.     widget.onYearSelected = function (value) {
  164.         value = this.useSelectize === true ? value : value.target.value
  165.         this.data.Engine.Year = value
  166.  
  167.         if (value === '')
  168.             return
  169.  
  170.         this.loadingMethods.loadModels(this.data.Engine.Type, this.data.Engine.Brand, value).done(function (data) {
  171.             var htmlModel = document.querySelector(this.htmlSelectors.model)
  172.             this.refreshList(htmlModel, data, null)
  173.             this.openList(htmlModel)
  174.         })
  175.     }
  176.     widget.onModelSelected = function (value) {
  177.         value = this.useSelectize === true ? value : value.target.value
  178.         this.data.Engine.Model = value
  179.  
  180.         if (value === '' || this.loaded === false)
  181.             return
  182.  
  183.         searchGTM.push(searchGTM.events.Vehicle, {
  184.             vehicleType: this.data.Engine.TypeName,
  185.             vehicleBrand: this.data.Engine.BrandName,
  186.             vehicleYear: this.data.Engine.Year,
  187.             vehicleModel: this.data.Engine.Model
  188.         })
  189.  
  190.         this.submit()
  191.     }
  192.  
  193.     widget.openList = function (element) {
  194.         if (this.useSelectize === true) {
  195.             element.selectize.open()
  196.         } else {
  197.             element.focus()
  198.         }
  199.     }
  200.     widget.refreshList = function (element, data, preSelection) {
  201.         if (this.useSelectize === true) {
  202.             this.refreshSelectize(element, data, preSelection)
  203.         } else {
  204.             this.refreshSelect(element, data, preSelection)
  205.         }
  206.     }
  207.     widget.selectValue = function (element, value, silent) {
  208.         if (this.useSelectize === true) {
  209.             element.selectize.setValue(this.data.Engine.Type, silent)
  210.         } else {
  211.             element.value = value
  212.         }
  213.     }
  214.     widget.refreshSelectize = function (element, data, preSelection) {
  215.         var selectize = element.selectize
  216.         selectize.clear(true)
  217.         selectize.clearOptions()
  218.  
  219.         if (data !== null) {
  220.             for (var i = 0; i < data.length; i++) {
  221.                 selectize.addOption({ value: data[i].Id, text: data[i].Value })
  222.             }
  223.         }
  224.  
  225.         selectize.setValue(preSelection === null ? '' : preSelection, true)
  226.         selectize.refreshOptions(false)
  227.     }
  228.     widget.refreshSelect = function (element, data, preSelection) {
  229.         var options = '<option disabled selected>- Select -</option>'
  230.         if (data !== null) {
  231.             for (var i = 0; i < data.length; i++) {
  232.                 var option = data[i]
  233.                 options += '<option value="' + option.Id + '">' + option.Value + '</option>'
  234.             }
  235.         }
  236.         element.innerHTML = options
  237.  
  238.         if (preSelection !== null) {
  239.             element.value = preSelection
  240.         }
  241.     }
  242.     widget.onItemLoaded = function (nbItemsLoaded) {
  243.         var count =
  244.                 this.data.Engine.Type === null ? 1 :
  245.                 this.data.Engine.Brand === null ? 2 :
  246.                 this.data.Engine.Year === null ? 3 : 4
  247.  
  248.         if (nbItemsLoaded == count) {
  249.             this.setLoading(false)
  250.             this.onLoaded(this)
  251.         }
  252.     }
  253.     return widget
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement