Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $.fn.initFlightsInputs = (options) ->
  2.     return new FlightsInputs(this, options)
  3.  
  4.  
  5. class FlightsInputs
  6.  
  7.     constructor: (el, opt) ->
  8.         @options =
  9.             outletsSelect: '#cn1'
  10.             arrivalsSelect: '#cn2'
  11.             urlArrival: 'http://test.grossa.pl/?controller=ajax&action=get-city-destinations&city_id={{ID}}'
  12.             urlOutlet: 'http://test.grossa.pl/?controller=ajax&action=get-city-origins&city_id={{ID}}'
  13.  
  14.             urlArrivalCities: [
  15.                 {
  16.                     "id": 1457,
  17.                     "name": "Katowice"
  18.                 },
  19.                 {
  20.                     "id": 1395,
  21.                     "name": "Kraków"
  22.                 },
  23.                 {
  24.                     "id": 1535,
  25.                     "name": "Wrocław"
  26.                 },
  27.                 {
  28.                     "id": 1498,
  29.                     "name": "Warszawa"
  30.                 }
  31.             ]
  32.  
  33.  
  34.             urlOutletCities: [
  35.                 {
  36.                     "id": 1469,
  37.                     "name": "Dublin"
  38.                 },
  39.                 {
  40.                     "id": 1582,
  41.                     "name": "Liverpool"
  42.                 }
  43.             ]
  44.  
  45.         @temp =
  46.             arrivalSelect: false
  47.             outletSelect: false
  48.  
  49.  
  50.         @options = $.extend(@options, opt)
  51.  
  52.         @reset()
  53.         @_addAction()
  54.  
  55.  
  56.  
  57.     _addAction: ->
  58.         @_getOutletSelect().on 'change', =>
  59.             if !@temp.arrivalSelect
  60.                 id = `$(this).val()`
  61.                 @_getDataFromServer(@_getReplaceURL(@options.urlArrival, id), (data) =>
  62.                     @_setOptionInArrival(data)
  63.                     @temp.outletSelect = true
  64.                 )
  65.  
  66.         @_getArrivalSelect().on 'change', =>
  67.             if !@temp.outletSelect
  68.                 id = `$(this).val()`
  69.                 @_getDataFromServer(@_getReplaceURL(@options.urlOutlet, id), (data) =>
  70.                     @_setOptionInOutlet(data)
  71.                     @temp.arrivalSelect = true
  72.                 )
  73.  
  74.     reset: () ->
  75.         @_getDataFromVar(@options.urlArrivalCities, (data) =>
  76.             @_setOptionInOutlet(data)
  77.         )
  78.         @_getDataFromVar(@options.urlOutletCities, (data) =>
  79.             @_setOptionInArrival(data)
  80.         )
  81.         @temp =
  82.             arrivalSelect: false
  83.             outletSelect: false
  84.  
  85.  
  86.     _setOptionInOutlet: (data) ->
  87.         @_setOptionInInput(@_getOutletSelect(), data)
  88.  
  89.  
  90.     _setOptionInArrival: (data) ->
  91.         @_setOptionInInput(@_getArrivalSelect(), data)
  92.  
  93.  
  94.     _setOptionInInput: (input, data) ->
  95.         $inst = input.selectize(
  96.             valueField: 'id'
  97.             labelField: 'name'
  98.             searchField: 'name'
  99.         )
  100.         inst = $inst[0].selectize
  101.         inst.clearOptions()
  102.         inst.addOption(data)
  103.         inst.refreshOptions(false)
  104.  
  105.     _generateOptionsString: (data) ->
  106.         html = ''
  107.         for d in data
  108.             do ->
  109.                 html += "<option value=\"#{d.id}\">#{d.name}</option>"
  110.         return html
  111.  
  112.     _getDataFromServer: (url, callback) ->
  113.         $.ajax(
  114.             url: url
  115.             method: 'GET'
  116.             success: (data) ->
  117.                 callback(data) if $.isFunction(callback)
  118.             error: ->
  119.                 console.log 'ERROR AJAX'
  120.         )
  121.  
  122.     _getDataFromVar: (varName, callback) ->
  123.         callback(varName) if $.isFunction(callback)
  124.  
  125.  
  126.     _getReplaceURL: (url, id) ->
  127.         url.replace /{{ID}}/i, id
  128.  
  129.     _getOutletSelect: ->
  130.         $(@options.outletsSelect)
  131.     _getArrivalSelect: ->
  132.         $(@options.arrivalsSelect)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement