jam-cloud/web/app/assets/javascripts/react-components/mixins/ICheckMixin.js.coffee

62 lines
1.5 KiB
CoffeeScript

context = window
teacherActions = window.JK.Actions.Teacher
@ICheckMixin = {
iCheckIgnore: false
checkboxes: []
iCheckify: () ->
@setCheckboxState()
@enableICheck()
setCheckboxState: () ->
for checkbox in @checkboxes
selector = checkbox.selector
if checkbox.stateKey?
choice = @state[checkbox.stateKey]
else
choice = @props[checkbox.propsKey]
$candidate = @root.find(selector)
@iCheckIgnore = true
if $candidate.attr('type') == 'radio'
if choice?
$found = @root.find(selector + '[value="' + choice + '"]')
$found.iCheck('check').attr('checked', true)
else
$candidate.iCheck('uncheck').attr('checked', false)
else
if choice
$candidate.iCheck('check').attr('checked', true);
else
$candidate.iCheck('uncheck').attr('checked', false);
@iCheckIgnore = false
enableICheck: (e) ->
if !@root?
return
checkBoxes = @root.find('input[type="checkbox"]')
if checkBoxes.length > 0
context.JK.checkbox(checkBoxes)
checkBoxes.on('ifChanged', (e) => @checkIfCanFire(e))
radioBoxes = @root.find('input[type="radio"]')
if radioBoxes.length > 0
context.JK.checkbox(radioBoxes)
radioBoxes.on('ifChanged', (e) => @checkIfCanFire(e))
true
checkIfCanFire: (e) ->
if @iCheckIgnore
return
if @checkboxChanged?
@checkboxChanged(e)
else
logger.error("no checkbox changed defined")
}