Refactors chart defs into their own package.
authordsc <dsc@wikimedia.org>
Fri, 9 Mar 2012 19:41:26 +0000 (11:41 -0800)
committerdsc <dsc@wikimedia.org>
Fri, 9 Mar 2012 19:41:26 +0000 (11:41 -0800)
lib/chart/chart-library.co
lib/chart/dygraphs-library.co
lib/main.co
lib/vis/vis-model.co

index 04c053c..edba6e3 100644 (file)
@@ -4,6 +4,8 @@ op = require 'kraken/util/op'
 {Parsers, ParserMixin}  = require 'kraken/util/parser'
 
 
+KNOWN_LIBRARIES = exports.KNOWN_LIBRARIES = {}
+
 /**
  * @class Specification for an option.
  */
@@ -39,6 +41,10 @@ class exports.ChartOption
  * @class Abstraction of a charting library, encapsulating its logic and options.
  */
 class exports.ChartLibrary extends EventEmitter
+    
+    @lookupLibrary = (name) ->
+        KNOWN_LIBRARIES[name]
+    
     /**
      * Ordered ChartOption objects.
      * @type ChartOption[]
@@ -61,7 +67,10 @@ class exports.ChartLibrary extends EventEmitter
      */
     (@name, options) ->
         @options_ordered = _.map options, (opt) ~> new ChartOption this, opt
-        @options = _.synthesize @options, -> [it.name, it]
+        @options = _.synthesize @options_ordered, -> [it.name, it]
+        
+        # register library
+        KNOWN_LIBRARIES[@name] = this
     
     
     /**
@@ -112,7 +121,8 @@ class exports.ChartLibrary extends EventEmitter
     getParserFor: (name) ->
         @getParser @get(name).type
     
-    
+    parseOption: (name, value) ->
+        @getParserFor(name)(value)
     
 
 
index fc80796..2487107 100644 (file)
@@ -4,9 +4,10 @@ _ = require 'kraken/underscore'
 
 
 class exports.DygraphsLibrary extends ChartLibrary
+    name : 'dygraphs'
     
-    (name, options) ->
-        super ...
+    (options) ->
+        super 'dygraphs', options
     
     render: ->
         ...
index e2d6793..0d45e25 100644 (file)
@@ -1,12 +1,14 @@
 Seq = require 'seq'
 { _, op,
 } = require 'kraken/util'
+{ ChartLibrary, DygraphsLibrary,
+} = require 'kraken/chart'
 { BaseView, BaseModel, BaseList,
 } = require 'kraken/base'
-{   Field, FieldList, FieldView, Scaffold,
+{ Field, FieldList, FieldView, Scaffold,
 } = require 'kraken/scaffold'
-{   GraphOption, GraphOptionList, GraphOptionView,
-    GraphOptionsScaffold, TagSet,
+{ GraphOption, GraphOptionList, GraphOptionView,
+  GraphOptionsScaffold, TagSet,
 } = require 'kraken/graph'
 { VisView, VisModel,
 } = require 'kraken/vis'
@@ -20,9 +22,11 @@ ROOT_VIS_OPTIONS      = {}
 
 # Create the Graph Scaffold
 main = ->
-    opts = root.CHART_DEFAULT_OPTIONS = {}
-    for opt of root.CHART_OPTIONS_SPEC
-        opts[opt.name] = opt.default
+    # opts = root.CHART_DEFAULT_OPTIONS = {}
+    # for opt of root.CHART_OPTIONS_SPEC
+    #     opts[opt.name] = opt.default
+    
+    dyglib = new DygraphsLibrary CHART_OPTIONS_SPEC
     
     # TODO: create a preset manager
     # Remove chart options from data so we don't have to deepcopy
@@ -41,6 +45,9 @@ main = ->
     # If we got querystring args, apply them to the graph
     if loc.split '?' .1
         data = _.uncollapseObject _.fromKV that.replace('#', '%23')
+        data.options = _.synthesize do
+            data.options or {}
+            (v, k) -> [ k, dyglib.parseOption(k,v) ]
     
     # Extract slug from URL
     if match = /\/graph\/(?!view)([^\/?]+)/i.exec loc
index 0049853..59365e8 100644 (file)
@@ -82,10 +82,10 @@ VisModel = exports.VisModel = BaseModel.extend do # {{{
         json = _.clone(@attributes) import { options:_.clone(@attributes.options) }
         return json if options.keepDefaults
         
-        defs = root.CHART_DEFAULT_OPTIONS
+        dyglib = ChartLibrary.lookupLibrary 'dygraphs'
         opts = json.options
         for k, v in opts
-            delete opts[k] if v is defs[k]
+            delete opts[k] if dyglib.isDefault k, v
         json