Code coverage report for code/models/node.coffee

Statements: 88.14% (52 / 59)      Branches: 70% (7 / 10)      Functions: 83.33% (15 / 18)      Lines: 87.5% (35 / 40)      Ignored: none     

All files » code/models/ » node.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 751 1   1   1     118 118 46 118 118 118 118 118       111 110 1   109   1                   9     762     2     1 2 2 1     1   1 3 3 3 2 2 2 1 1     10                      
GraphPrimitive = require './graph-primitive'
Colors = require '../utils/colors'
 
tr = require '../utils/translate'
 
module.exports = class Node extends GraphPrimitive
 
  constructor: (nodeSpec={x:0,y:0,title:"untitled",image:null,initialValue:50, isAccumulator:false}, key) ->
    super()
    if key
      @key = key
    @links = []
    {@x, @y, @title, @image, @initialValue, @isAccumulator} = nodeSpec
    @initialValue  ?= 50
    @isAccumulator ?= false
    @color ?= Colors[0].value
 
  type: 'Node'
  addLink: (link) ->
    if link.sourceNode is @ or link.targetNode is @
      if _.contains @links, link
        throw new Error "Duplicate link for Node:#{@.id}"
      else
        @links.push link
    else
      throw new Error "Bad link for Node:#{@.id}"
 
  removeLink: (link) ->
    if link.sourceNode is @ or link.targetNode is @
      _.remove @links, (testLink) ->
        return testLink is link
    else
      throw new Error "Bad link for Node:#{@.id}"
 
  outLinks: ->
    _.filter @links, (link) => link.sourceNode is @
 
  inLinks: ->
    _.filter @links, (link) => link.targetNode is @
 
  inNodes: ->
    _.map @inLinks(), (link) -> link.sourceNode
 
  infoString: ->
    linkNamer = (link) ->
      " --#{link.title}-->[#{link.targetNode.title}]"
    outs = (linkNamer link for link in @outLinks())
    "#{@title} #{outs}"
 
  downstreamNodes: ->
    visitedNodes = []
 
    visit = (node) ->
      log.info("visiting node: #{node.id}")
      visitedNodes.push node
      _.each node.outLinks(), (link) ->
        downstreamNode = link.targetNode
        Eunless _.contains visitedNodes, downstreamNode
          visit downstreamNode
    visit @
    _.without visitedNodes, @ # remove ourself from the results.
 
  toExport: ->
    data:
      title: @title
      x: @x
      y: @y
      image: @image
      initialValue: @initialValue
      isAccumulator: @isAccumulator
    key: @key
 
  paletteItemIs: (paletteItem) ->
    paletteItem.image is @image