JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
save/load from url hash
authorJason Woofenden <jason@jasonwoof.com>
Sun, 8 Nov 2015 03:51:34 +0000 (22:51 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Sun, 8 Nov 2015 03:51:34 +0000 (22:51 -0500)
main.coffee

index d8f75b5..3b896dd 100644 (file)
@@ -29,12 +29,7 @@ STATES = {
        DRAGGING: { txt: 'dragging' }
        EDITING:  { txt: 'editing' }
 }
        DRAGGING: { txt: 'dragging' }
        EDITING:  { txt: 'editing' }
 }
-TYPE_WIDGET = 1
-TYPE_CONTROL = 2
-CSS_CLASS_TO_PICKLE_TYPE = {
-       box: '0'
-       polyline: '1'
-}
+
 # json (compiled to javascript and minified) is ~8% smaller than the resulting xml
 json_to_svg = (json) ->
        for tag, attrs of json
 # json (compiled to javascript and minified) is ~8% smaller than the resulting xml
 json_to_svg = (json) ->
        for tag, attrs of json
@@ -133,7 +128,6 @@ class Visible
 class Control extends Visible
        constructor: (args) ->
                super args
 class Control extends Visible
        constructor: (args) ->
                super args
-               @type = TYPE_CONTROL
                @on_drag = args.drag
                @on_destruct = args.done ? null
        destruct: ->
                @on_drag = args.drag
                @on_destruct = args.done ? null
        destruct: ->
@@ -168,7 +162,6 @@ class Widget extends Visible
        constructor: (args) ->
                super args
                @controls = []
        constructor: (args) ->
                super args
                @controls = []
-               @type = TYPE_WIDGET
        destruct: ->
                @kill_controls()
        clone: ->
        destruct: ->
                @kill_controls()
        clone: ->
@@ -211,8 +204,12 @@ class RectWidget extends Widget
                return new RectWidget @
        as_array: ->
                return [@x, @y, @width, @height]
                return new RectWidget @
        as_array: ->
                return [@x, @y, @width, @height]
-       from_array: (a) ->
-               return new RectWidget x: a[0], y: a[1], width: a[2], height: a[3]
+       from_array: (args, a) ->
+               args.x = a[0]
+               args.y = a[1]
+               args.width = a[2]
+               args.height = a[3]
+               return new RectWidget args
        set_state: (state) ->
                super state
                @update_class()
        set_state: (state) ->
                super state
                @update_class()
@@ -315,14 +312,15 @@ class PolylineWidget extends Widget
                        ret.push @x + n.x
                        ret.push @y + n.y
                return ret
                        ret.push @x + n.x
                        ret.push @y + n.y
                return ret
-       from_array: (a) ->
-               args = {
-                       x: a[0]
-                       y: a[1]
-                       nodes: []
-               }
+       from_array: (args, a) ->
+               args.x = a[0]
+               args.y = a[1]
+               args.nodes = []
                while a.length
                while a.length
-                       args.nodes.push x: a.shift() - args.x, y: a.shift() - args.x
+                       xy = {}
+                       xy.x = a.shift() - args.x
+                       xy.y = a.shift() - args.y
+                       args.nodes.push xy
                return new PolylineWidget args
        clone: ->
                return new PolylineWidget @
                return new PolylineWidget args
        clone: ->
                return new PolylineWidget @
@@ -395,9 +393,9 @@ class PolylineWidget extends Widget
        node_dragger: (i) ->
                if i is 0
                        return (dxy) =>
        node_dragger: (i) ->
                if i is 0
                        return (dxy) =>
-                               for i in [1...@nodes.length]
-                                       @nodes[i].x -= dxy.x
-                                       @nodes[i].y -= dxy.y
+                               for j in [1...@nodes.length]
+                                       @nodes[j].x -= dxy.x
+                                       @nodes[j].y -= dxy.y
                                @move x: @x + dxy.x, y: @y + dxy.y
                return (dxy) =>
                        @nodes[i].x += dxy.x
                                @move x: @x + dxy.x, y: @y + dxy.y
                return (dxy) =>
                        @nodes[i].x += dxy.x
@@ -432,6 +430,14 @@ class PolylineWidget extends Widget
                        }
                return @controls
 
                        }
                return @controls
 
+CSS_CLASS_TO_PICKLE_TYPE = {
+       box: '0'
+       polyline: '1'
+}
+PICKLE_TYPE_TO_WIDGET_CLASS = [
+       RectWidget
+       PolylineWidget
+]
 # called automatically on domcontentloaded
 init = ->
        svg_offset = null
 # called automatically on domcontentloaded
 init = ->
        svg_offset = null
@@ -473,7 +479,7 @@ init = ->
        supply_add PolylineWidget, y: 25, nodes: [{x: 0, y: 0}, {x: 50, y: 0}]
        supply_add PolylineWidget, x: 25, nodes: [{x: 0, y: 0}, {x: 0, y: 50}]
        supply_add PolylineWidget, x: 10, nodes: [{x: 0, y: 0}, {x: 15, y: 50}, {x: 30, y: 0}]
        supply_add PolylineWidget, y: 25, nodes: [{x: 0, y: 0}, {x: 50, y: 0}]
        supply_add PolylineWidget, x: 25, nodes: [{x: 0, y: 0}, {x: 0, y: 50}]
        supply_add PolylineWidget, x: 10, nodes: [{x: 0, y: 0}, {x: 15, y: 50}, {x: 30, y: 0}]
-       supply_add PolylineWidget, nodes: [{x: 0, y: 50}, {x: 17, y: 0}, {x: 33, y: 50}, {x: 50, y: 0}]
+       supply_add PolylineWidget, y: 50, nodes: [{x: 0, y: 0}, {x: 17, y: -50}, {x: 33, y: 0}, {x: 50, y: -50}]
 
        # editor state
        controls_layer = { all: {}, selected: {} }
 
        # editor state
        controls_layer = { all: {}, selected: {} }
@@ -487,6 +493,12 @@ init = ->
 
        lc = "abcdefghijklmnopqrstuvwxyz"
        uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
        lc = "abcdefghijklmnopqrstuvwxyz"
        uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+       cc_lca = lc.charCodeAt 0
+       cc_lcz = lc.charCodeAt 25
+       cc_uca = uc.charCodeAt 0
+       cc_ucz = uc.charCodeAt 25
+       cc_0 = '0'.charCodeAt 0
+       cc_9 = '9'.charCodeAt 0
        # takes an array of positive integers, and encodes it as a string
        pickle = (a) ->
                ret = ''
        # takes an array of positive integers, and encodes it as a string
        pickle = (a) ->
                ret = ''
@@ -510,6 +522,34 @@ init = ->
                return ret
        save = ->
                window.location.hash = pickle_widgets()
                return ret
        save = ->
                window.location.hash = pickle_widgets()
+       load = (str) ->
+               return if str.charAt(1) isnt '0'
+               wtype = null
+               args = []
+               ii = 0
+               load_1 = (next_type) ->
+                       unless wtype?
+                               if next_type?
+                                       wtype = next_type
+                               return
+                       w = PICKLE_TYPE_TO_WIDGET_CLASS[wtype]::from_array svg: svg, args
+                       widget_layer.all[w.id] = w
+                       wtype = next_type
+                       args = []
+                       ii = 0
+               for i in [2...str.length]
+                       c = str.charCodeAt(i)
+                       if cc_0 <= c <= cc_9
+                               load_1 c - cc_0
+                       else if cc_lca <= c <= cc_lcz
+                               ii *= lc.length
+                               ii += c - cc_lca
+                               args.push ii
+                               ii = 0
+                       else if cc_uca <= c <= cc_ucz
+                               ii *= lc.length
+                               ii += c - cc_uca
+               load_1 null
        stop_editing = ->
                if widget_layer.editing
                        widget_layer.editing.kill_controls()
        stop_editing = ->
                if widget_layer.editing
                        widget_layer.editing.kill_controls()
@@ -657,4 +697,7 @@ init = ->
                return true
        #($ document).keydown (e) ->
 
                return true
        #($ document).keydown (e) ->
 
+       if window.location.hash
+               load window.location.hash
+
 $ init
 $ init