Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $.fn.initFlightsInputs = (options) ->
- return new FlightsInputs(this, options)
- class FlightsInputs
- constructor: (el, opt) ->
- @options =
- outletsSelect: '#cn1'
- arrivalsSelect: '#cn2'
- urlArrival: 'http://test.grossa.pl/?controller=ajax&action=get-city-destinations&city_id={{ID}}'
- urlOutlet: 'http://test.grossa.pl/?controller=ajax&action=get-city-origins&city_id={{ID}}'
- urlArrivalCities: [
- {
- "id": 1457,
- "name": "Katowice"
- },
- {
- "id": 1395,
- "name": "Kraków"
- },
- {
- "id": 1535,
- "name": "Wrocław"
- },
- {
- "id": 1498,
- "name": "Warszawa"
- }
- ]
- urlOutletCities: [
- {
- "id": 1469,
- "name": "Dublin"
- },
- {
- "id": 1582,
- "name": "Liverpool"
- }
- ]
- @temp =
- arrivalSelect: false
- outletSelect: false
- @options = $.extend(@options, opt)
- @reset()
- @_addAction()
- _addAction: ->
- @_getOutletSelect().on 'change', =>
- if !@temp.arrivalSelect
- id = `$(this).val()`
- @_getDataFromServer(@_getReplaceURL(@options.urlArrival, id), (data) =>
- @_setOptionInArrival(data)
- @temp.outletSelect = true
- )
- @_getArrivalSelect().on 'change', =>
- if !@temp.outletSelect
- id = `$(this).val()`
- @_getDataFromServer(@_getReplaceURL(@options.urlOutlet, id), (data) =>
- @_setOptionInOutlet(data)
- @temp.arrivalSelect = true
- )
- reset: () ->
- @_getDataFromVar(@options.urlArrivalCities, (data) =>
- @_setOptionInOutlet(data)
- )
- @_getDataFromVar(@options.urlOutletCities, (data) =>
- @_setOptionInArrival(data)
- )
- @temp =
- arrivalSelect: false
- outletSelect: false
- _setOptionInOutlet: (data) ->
- @_setOptionInInput(@_getOutletSelect(), data)
- _setOptionInArrival: (data) ->
- @_setOptionInInput(@_getArrivalSelect(), data)
- _setOptionInInput: (input, data) ->
- $inst = input.selectize(
- valueField: 'id'
- labelField: 'name'
- searchField: 'name'
- )
- inst = $inst[0].selectize
- inst.clearOptions()
- inst.addOption(data)
- inst.refreshOptions(false)
- _generateOptionsString: (data) ->
- html = ''
- for d in data
- do ->
- html += "<option value=\"#{d.id}\">#{d.name}</option>"
- return html
- _getDataFromServer: (url, callback) ->
- $.ajax(
- url: url
- method: 'GET'
- success: (data) ->
- callback(data) if $.isFunction(callback)
- error: ->
- console.log 'ERROR AJAX'
- )
- _getDataFromVar: (varName, callback) ->
- callback(varName) if $.isFunction(callback)
- _getReplaceURL: (url, id) ->
- url.replace /{{ID}}/i, id
- _getOutletSelect: ->
- $(@options.outletsSelect)
- _getArrivalSelect: ->
- $(@options.arrivalsSelect)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement