1. Udi Bauman
  2. model

Commits

Udi Bauman  committed 760e55a

Auto-convergion to CoffeeScript

  • Participants
  • Parent commits 93f0ce2
  • Branches default

Comments (0)

Files changed (4)

File app.html

View file
  • Ignore whitespace
     <script src="js/d3.geo.min.js"></script>
     <script src="js/d3.geom.min.js"></script>
     <script src="js/d3.time.min.js"></script>
-    <script src="js/model-models.js"></script>
-    <script src="js/model-widgets.js"></script>
-    <script src="js/model-controllers.js"></script>
+    <!-- CoffeeScript output files -->
+    <script src="js/models.js"></script>
+    <script src="js/views.js"></script>
+    <script src="js/controllers.js"></script>
 
   </body>
 </html>

File model_app/controllers.coffee

View file
  • Ignore whitespace
+create_parser = ->
+  previous_source = ""
+  current_class = ""
+  current_super_classes = []
+  members_map =
+    "+": []
+    "-": []
+
+  add_class = ->
+    cls = new Class(
+      name: current_class
+      super_classes: _.clone(current_super_classes)
+      public_methods: _.clone(members_map["+"])
+      private_methods: _.clone(members_map["-"])
+    )
+    existing_class = current_diagram.find((c) ->
+        c.get("name") is cls.name
+    )
+    if existing_class
+      existing_class.set cls
+    else
+      current_diagram.add cls
+    current_class = ""
+    current_super_classes = []
+    members_map["+"] = []
+    members_map["-"] = []
+
+  ->
+    source = $("#source_input").val()
+    unless source is previous_source
+      previous_source = source
+      lines = source.split("\n")
+      _.each lines, (line) ->
+        return  if line.trim().length is 0
+        first_char = line.charAt(0)
+        if members_map[first_char]
+          members_map[first_char].push line.substr(1)
+        else
+          add_class()  unless current_class is ""
+          if line.indexOf(":") >= 0
+            parts = line.split(":")
+            current_class = parts[0].trim()
+            current_super_classes = parts[1].trim().split(",")
+          else
+            current_class = line
+
+      add_class()
+
+$ ->
+  drawer = new ClassDiagramDrawer
+  drawer.init()
+  parse = create_parser()
+  $("a[data-toggle=\"tab\"]").on "shown", (e) ->
+    parse()
+
+  current_diagram.on "add", drawer.draw_class, drawer

File model_app/models.coffee

View file
  • Ignore whitespace
+class DiagramElement extends Backbone.Model
+  defaults:
+    x: 0
+    y: 0
+    width: 40
+    height: 100
+
+class Class extends DiagramElement
+  defaults:
+    name: ""
+    super_classes: []
+    public_methods: []
+    private_methods: []
+
+  get_all_members: =>
+    result = []
+    name = @get("name")
+    if @get("super_classes").length > 0
+      name += " : "
+      name += @get("super_classes").join(",")
+    result.push name
+    _.each @get("public_methods"), (n) ->
+      result.push n
+
+    _.each @get("private_methods"), (n) ->
+      result.push n
+
+    result
+
+class ClassDiagram extends Backbone.Collection
+  model: Class
+  source: ""
+  get_classes: =>
+    (model.get 'name' for model in @models)
+
+exports = this
+exports.Class = Class
+exports.current_diagram = new ClassDiagram

File model_app/views.coffee

View file
  • Ignore whitespace
+class ClassDiagramDrawer
+  svg: null
+  current_offset: 0
+  default_class_width: 150
+  default_member_height: 20
+  default_member_spacing: 20
+  class_containers: {}
+  init: ->
+    w = 675
+    h = 360
+    pack = d3.layout.pack().size([ w - 4, h - 4 ]).value((d) ->
+        d.size
+    )
+    @svg = d3.select("#chart").append("svg").attr("width", w).attr("height", h).attr("class", "pack").append("g").attr("transform", "translate(2, 2)")
+
+  draw_class: (model) ->
+    data = model.get_all_members()
+    x = @current_offset
+    y = 50
+    margin = 10
+    g = @svg.append("g").attr("x", x).attr("y", y)
+    @class_containers[model.get("name")] = g
+    g.append("rect").attr("x", x).attr("y", y).attr("width", @default_class_width).attr "height", data.length * @default_member_height
+    that = this
+    g.selectAll("text").data(data).enter().append("text").attr("x", x + margin).attr("y", (d, i) ->
+        y + margin + i * that.default_member_height
+    ).attr("dy", ".35em").text String
+    g.append("line").attr("x1", x).attr("y1", y + @default_member_height).attr("x2", x + @default_class_width).attr "y2", y + @default_member_height
+    @current_offset += @default_class_width + @default_member_spacing
+
+exports = this
+exports.ClassDiagramDrawer = ClassDiagramDrawer