Code coverage report for code/models/selection-manager.coffee

Statements: 85.51% (59 / 69)      Branches: 92.86% (13 / 14)      Functions: 78.26% (18 / 23)      Lines: 86.44% (51 / 59)      Ignored: none     

All files » code/models/ » selection-manager.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 112 113 114 115 1161 1 1 1     1 1 1 1     42 42               143 143       66 66 65 65       36 36 36     7 7 7     4       76     14     2     170 170 170     170     14   14 12     12     21           14 14     14 12       12     22                         78 78 78 78 73 73 73 73   5 78  
Importer    = require '../utils/importer'
Link        = require './link'
DiagramNode = require './node'
tr          = require "../utils/translate"
 
 
module.exports = class SelectionManager
  @NodeTitleEditing   = "NodeTitleEditing"
  @NodeInpsection     = "NodeInpsection"
  @LinkSelection      = "LinkSelection"
 
  constructor: ->
    @selections = []
    @selectionListeners = []
 
  addSelectionListener: (listener) ->
    log.info("adding selection listener #{listener}")
    @selectionListeners.push listener
 
 
  _notifySelectionChange: ->
    log.info "notifiying listeners"
    for listener in @selectionListeners
      listener @
 
  addToSelection: (graphprimitive, context) ->
    entry = {graphprimitive: graphprimitive, context: context, key: graphprimitive.key}
    unless @isSelected(graphprimitive,context)
      @selections.push entry
      @_notifySelectionChange()
 
 
  selectOnly: (graphprimitive, context) ->
    Eunless @isSelected(graphprimitive, context)
      @clearSelection(context)
      @addToSelection(graphprimitive,context)
 
  selection: (context) ->
    where = {}
    where.context = context if context
    _.chain @selections
    .where where
    .map (obj,i) ->
      obj.graphprimitive
    .value()
 
  clearSelection: (context=null) ->
    @_deselect({context:context})
 
  clearLinkSelection: ->
    @clearSelection(SelectionManager.LinkSelection)
 
  clearSelectionFor:(graphprimitive, context=null) ->
    @_deselect({key:graphprimitive.key, context:context})
 
  isSelected: (graphprimitive, context) ->
    where = {key: graphprimitive.key}
    where.context = context if context
    found = _.chain @selections
    .where where
    .value()
    found.length > 0
 
  selectForTitleEditing: (graphprimitive) ->
    @selectOnly(graphprimitive,SelectionManager.NodeTitleEditing)
    # unselect the inspection selection, unless its this same graphprimitive.
    unless @isSelectedForInspection(graphprimitive)
      @clearInspection()
 
  clearTitleEditing: ->
    @clearSelection(SelectionManager.NodeTitleEditing)
 
  isSelectedForTitleEditing: (graphprimitive)->
    @isSelected(graphprimitive,SelectionManager.NodeTitleEditing)
 
  getTitleEditing: ->
    @selection(SelectionManager.NodeTitleEditing)
 
  selectForInspection: (graphprimitive) ->
    @selectOnly(graphprimitive, SelectionManager.NodeInpsection)
    @clearLinkSelection()
 
    # unselect the title selection, unless its this same graphprimitive.
    unless @isSelectedForTitleEditing(graphprimitive)
      @clearTitleEditing()
 
 
  clearInspection: ->
    @clearSelection(SelectionManager.NodeInpsection)
 
  isSelectedForInspection: (graphprimitive) ->
    @isSelected(graphprimitive,SelectionManager.NodeInpsection)
 
  getInspection: ->
    @selection(SelectionManager.NodeInpsection)
 
  getLinkSelection: ->
    @selection(SelectionManager.LinkSelection)
 
  selectLink: (graphprimitive)->
    @clearInspection()
    @selectOnly(graphprimitive, SelectionManager.LinkSelection)
 
  _deselect: (opts)->
    pickNonEmpty    = _.partial _.pick, _, _.identity
    removeCritereon = pickNonEmpty opts
    log.info removeCritereon
    if removeCritereon.context or removeCritereon.key
      log.info "removing #{removeCritereon.key}"
      log.info "in collection #{_.pluck @selections, 'key'}"
      _.remove @selections, removeCritereon
      log.info "in collection #{_.pluck @selections, 'key'}"
    else
      @selections = []
    @_notifySelectionChange()