Code coverage report for code/stores/image-dialog-store.coffee

Statements: 79.59% (39 / 49)      Branches: 66.67% (4 / 6)      Functions: 64.71% (11 / 17)      Lines: 81.25% (39 / 48)      Ignored: none     

All files » code/stores/ » image-dialog-store.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111    1   1         1       1 1     1 1 1   1 1     13     1     5 5 5   5 5 3 3 5             1 1     2 2                   3 3     3 3     3 3 3 3 3 3 2     14           14     1                                     1        
# TODO:  This should be split up into and ImageDialogStore and a DialogStore…
 
PaletteStore = require './palette-store'
 
imageDialogActions = Reflux.createActions([
    "open", "close", "update", "cancel"
  ])
 
 
store = Reflux.createStore
  listenables: [ imageDialogActions ]
 
  init: ->
    @enableListening()
    @initValues()
 
  initValues: ->
    @showingDialog    = false
    @keepShowing      = false
    @callback         = -> undefined
 
    @resetPaletteItem()
    @_updateChanges()
 
  resetPaletteItem: ->
    @paletteItem = null
 
  enableListening: ->
    PaletteStore.store.listen @onPaletteSelect
 
  onOpen: (callback=false)->
    @keepShowing = true
    @resetPaletteItem()
    @showingDialog = true
 
    @callback = null
    if callback
      @callback = callback
      @keepShowing = false
    @_updateChanges()
 
  onPaletteSelect: (status) ->
    @paletteItem = status.selectedPaletteItem
    @finish()  # Incase we need to trigger window closing
 
  close: ->
    @showingDialog = false
    @resetPaletteItem()
 
  onClose: ->
    @close()
    @_updateChanges()
 
  onUpdate: (data) ->
    if @paletteItem
      @paletteItem = _.merge @paletteItem, data
    else
    @paletteItem ||= data
    @_updateChanges()
 
  onCancel: ->
    @resetPaletteItem()
    @finish()
 
  invoke_callback: ->
    @callback?(@paletteItem)
    @callback = null # once only
 
  finish: ->
    @_updateChanges()
    @invoke_callback()
    @callback = null
    @resetPaletteItem()
    @_updateChanges()
    unless @keepShowing
      imageDialogActions.close.trigger()
 
  _updateChanges: ->
    data =
      showingDialog: @showingDialog
      keepShowing: @keepShowing
      paletteItem: @paletteItem
 
    # log.info "Sending changes to listeners: #{JSON.stringify(data)}"
    @trigger(data)
 
 
listenerMixin =
  actions: imageDialogActions
 
  getInitialState: ->
    showingDialog: store.showingDialog
    keepShowing: store.keepShowing
    paletteItem: store.paletteItem
    selectedImage: store.paletteItem
 
  componentDidMount: ->
    store.listen @onChange
 
  onChange: (status) ->
    @setState
      showingDialog: status.showingDialog
      keepShowing: status.keepShowing
      paletteItem: status.paletteItem
      selectedImage: status.paletteItem
 
module.exports =
  store: store
  actions: imageDialogActions
  mixin: listenerMixin