JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
cleanup hover/filter stuff
authorJason Woofenden <jason@jasonwoof.com>
Sun, 20 Sep 2015 20:57:15 +0000 (16:57 -0400)
committerJason Woofenden <jason@jasonwoof.com>
Sun, 20 Sep 2015 20:57:15 +0000 (16:57 -0400)
auto.coffee

index 89dfb86..2bd4bfa 100644 (file)
@@ -12,6 +12,17 @@ STYLE_HOVER = 2
 STYLE_EDITING = 3
 STYLE_DRAGGING = 4
 
+STYLE_TO_CLASS = [
+       "normal"
+       "selected"
+       "hover"
+       "editing"
+       "dragging"
+]
+
+set_style_class = (args) ->
+       args.el.setAttribute 'class', "#{args.class} #{STYLE_TO_CLASS[args.style]}"
+
 # json (compiled to javascript and minified) is ~8% smaller than the resulting xml
 json_to_svg = (json) ->
        for tag, attrs of json
@@ -20,49 +31,40 @@ json_to_svg = (json) ->
                        if k is 'children'
                                for child in v
                                        el.appendChild json_to_svg child
+                       else if k is 'contents'
+                               el.appendChild document.createTextNode v
                        else
                                el.setAttribute k, v
        return el
 
 next_widget_id = 0
 # public vars: x, y, width, height, el
-class Widget
+class Visible
        # required args: svg
        constructor: (args) ->
                @id = next_widget_id
                next_widget_id += 1
                @svg = args.svg
-               @style = args.style ? STYLE_NORMAL
                @x = args.x ? 1
                @y = args.y ? 1
                @width = args.width ? 50
                @height = args.height ? 34
+               @style = args.style ? STYLE_NORMAL
        destruct: ->
-       clone: ->
-               return new Widget @
        move: (args) ->
                @x = args.x
                @y = args.y
        proximity: (xy) -> # return the square of the distance to your visible bits
                return PROX_MAX + 1
        set_style: (style) ->
-               return if @style is style
-               if style is STYLE_NORMAL
-                       @el.setAttribute 'style', 'filter: url(#crayon)'
-               else
-                       if @style is STYLE_NORMAL
-                               @el.setAttribute 'style', ''
-               switch style
-                       when STYLE_NORMAL
-                               @el.setAttribute 'class', "#{@css_class}"
-                       when STYLE_SELECTED
-                               @el.setAttribute 'class', "#{@css_class} selected"
-                       when STYLE_HOVER
-                               @el.setAttribute 'class', "#{@css_class} hover"
-                       when STYLE_DRAGGING
-                               @el.setAttribute 'class', "#{@css_class} dragging"
-                       # FIXME when STYLE_EDITING
                @style = style
+
+class Control extends Visible
+
+class Widget extends Visible
+       #sub-classes are expected to implement all of these:
+       clone: ->
+               return new Widget @
        controls: -> # create controls, return them
                return []
        hide_controls: ->
@@ -76,8 +78,7 @@ class RectWidget extends Widget
                        y: @y + 1
                        width: @width - 2
                        height: @height - 2
-                       class: 'box'
-                       style: if @style is STYLE_NORMAL then 'filter: url(#crayon)' else ''
+                       class: 'box normal'
                @svg.appendChild @el
        destruct: ->
                super()
@@ -85,6 +86,9 @@ class RectWidget extends Widget
                        @svg.removeChild @el
        clone: ->
                return new RectWidget @
+       set_style: (style) ->
+               super style
+               set_style_class el: @el, class: 'box', style: style
        move: (args) ->
                super args
                @el.setAttribute 'x', @x + 1
@@ -134,6 +138,9 @@ init = ->
                        { feTurbulence: baseFrequency: '.3', numOctaves: '2', type: 'fractalNoise' }
                        { feDisplacementMap: scale: '6', xChannelSelector: 'R', in: 'SourceGraphic' }
                ]
+       svg.appendChild json_to_svg style:
+               type: 'text/css'
+               contents: '.box.normal,.box.hover,.box.selected{filter: url(#crayon)}'
 
        # create canvas border
        svg.appendChild json_to_svg rect: