     @zoom = d3.behavior.zoom().scaleExtent([.1,8]).on "zoom", @redraw
 
     # init visual
-    me = @
     @canvas = d3
       .select element
       .append "svg"

# ===================================

$ ->
	$('body').on 'click.graphEdit.data-api', '[data-pluginNameAction^=Action]', ( e ) ->
		$(e.target).graphEdit()

      @drawSelection()

  redraw : =>
    if not @mousedown_node
      @svg.attr "transform", "translate(" + d3.event.translate + ")" + "scale(" + d3.event.scale + ")"

  resetForce : =>
    @force
      .links @link_data
      .nodes @node_data
      .start()

  restart : =>
    me = @
    @links = @links.data(@link_data)

    # update existing links
    @links
      .style "stroke", "#F00"
      .attr "class", "link"

    # new links
    @links.enter()
      .append "line"
      .attr "class", "link"
      .style "stroke", "#000"
      .style "stroke-width", () -> 1

    @links.exit().remove()

    @nodes = @nodes.data(@node_data, (d)-> d.id)

    # update existing nodes
    @nodes
      .style "fill", (d) -> me.colors(d.id)

    # new nodes
    @nodes.enter()

      .append "circle"
      .attr "class", "node"
      .attr "r", 5
      .style "fill", (d) -> me.colors(d.id)
      .call me.force.drag

      .on "mousedown", () ->
        me.mousedown_node = true
        me.scale = me.zoom.scale()
        me.translate = me.zoom.translate()
        me.select this
      .on "mouseup", () ->
        me.mousedown_node = false
        me.zoom.scale me.scale
        me.zoom.translate me.translate

    # clear old ones
    @nodes.exit().remove()

    # update force
    @resetForce()



# GRAPHEDIT PLUGIN DEFINITION
# ==========================

$.fn.graphEdit = ( option, params ) ->
  this.each ->
    $this = $(@)
    data = $this.data 'graphEdit'
    if !data then $this.data 'graphEdit', (data = new GraphEdit @, option)
    if typeof option is 'string' then data[option].call $this, params

$.fn.graphEdit.Constructor = GraphEdit

