* other settings for both its content and presentation.
*/
GraphModel = exports.GraphModel = BaseModel.extend do # {{{
- ctorName : 'GraphModel'
- urlRoot : '/graphs'
+ ctorName : 'GraphModel'
+ urlRoot : '/graphs'
+ idAttribute : 'slug'
+
initialize : ->
name = @get 'name'
- if name and not (@id or @has 'id')
- @id = @attributes.id = _.underscored name
+ if name and not (@id or @get('slug'))
+ @set 'slug', _.underscored name
defaults: ->
{
- id : 'graph'
- name : 'Kraken Graph'
+ slug : ''
+ name : ''
dataset : '/data/pageviews_by.timestamp.language.csv'
desc : ''
width : 'auto'
- height : 'auto'
+ height : 320
options : {}
}
GraphView = exports.GraphView = BaseView.extend do # {{{
- __bind__ : <[ render renderAll onReady ]>
+ __bind__ : <[ resizeViewport render renderAll onReady ]>
ctorName : 'GraphView'
tagName : 'section'
className : 'graph'
@model or= new GraphModel
BaseView::initialize ...
# console.log "#this.initialize!"
- @render = _.debounce @render, DEBOUNCE_RENDER
- @renderAll = _.debounce @renderAll, DEBOUNCE_RENDER
+
+ for name of <[ resizeViewport render renderAll ]>
+ @[name] = _.debounce @[name], DEBOUNCE_RENDER
+
+ # Resize graph on window resize
+ $ root .on 'resize', @resizeViewport
@model.on 'destroy', @remove, this
@model.on 'change', @render, this
_.delay @onReady, DEBOUNCE_RENDER
+
onReady: ->
@ready = @scaffold.ready = true
@renderAll()
+
chartOptions: (values, opts) ->
# Handle @chartOptions(k, v, opts)
if arguments.length > 1 and typeof values is 'string'
options.values()
- render: ->
- return this unless @ready
- options = @chartOptions()
- w = options.width or= @scaffold.get 'width' .getValue() or 480
- h = options.height or= @scaffold.get 'height' .getValue() or 320
- @viewport.css { width:w, height:h }
+ /**
+ * Resizes chart according to the model's width and height.
+ * @return { width, height }
+ */
+ resizeViewport: ->
+ return this unless @ready and @chart
# Remove old style, as it confuses dygraph after options update
@viewport.attr 'style', ''
+ if (width = @model.get 'width') is 'auto'
+ width = @viewport.width()
+ if (height = @model.get 'height') is 'auto'
+ height = @viewport.height()
+ size = { width, height }
+ @viewport.css size
+ console.log 'resizeViewport!', JSON.stringify size
+ # @chart.resize size if forceRedraw
+ size
+
+ render: ->
+ return this unless @ready
+
# console.log "#this"
# console.log do
# " .viewport.{ width=%s, height=%s, style=%s }"
# @viewport.attr 'style'
# console.log ' .options:', JSON.stringify options
+ size = @resizeViewport()
+ options = @chartOptions()
# @chart?.destroy()
unless @chart
@chart = new Dygraph do
options
else
@chart.updateOptions options
- @chart.resize w, h
+ @chart.resize size
path = root.location?.path or '/'
url = "#path?#{@toKVPairs()}"
-- var _ = require('kraken/underscore');
-section.graph(id=id)
+- var id = model.id || model.cid, graph_id = _.domize('graph', id)
+section.graph(id=graph_id)
.graph-label
.viewport
- //-
- form.details
- label.name(for='#{id}_name') Name
+ form.details.form-horizontal
+
+ .name-row.row-fluid.control-group
+ //- label.name.control-label(for="#{id}_name"): h3 Graph Name
+ .controls: input.span6.name(type='text', id="#{id}_name", placeholder='Graph Name', value=name)
+
+ .row-fluid
+ .half.control-group
+ label.slug.control-label(for='slug') Slug
+ .controls
+ input.span3.slug(type='text', id='slug', placeholder='graph_slug', value=slug)
+ p.help-block The slug uniquely identifies this graph and will be displayed in the URL.
+ .half.control-group
+ label.dataset.control-label(for='dataset') Data Set
+ .controls
+ input.span3.dataset(type='text', id='dataset', placeholder='URL to dataset file', value=dataset)
+ p.help-block This dataset filename will soon be replaced by a friendly UI.
+
+ .row-fluid
+ .half.control-group
+ label.width.control-label(for='width') Width
+ .controls
+ input.span1.width(type='text', id='width', value=width)
+ p.help-block Choosing 'auto' will size the graph to the viewport bounds.
+ .half.control-group
+ label.height.control-label(for='height') Height
+ .controls: input.span1.height(type='text', id='height', value=height)
+
fieldset.options
legend Graph Options