1 /*globals $*/
  2 
  3 /**
  4   Copyright 2012 Christopher Meiklejohn and Basho Technologies, Inc.
  5 
  6   Licensed under the Apache License, Version 2.0 (the "License");
  7   you may not use this file except in compliance with the License.
  8   You may obtain a copy of the License at
  9 
 10   http://www.apache.org/licenses/LICENSE-2.0
 11 
 12   Unless required by applicable law or agreed to in writing, software
 13   distributed under the License is distributed on an "AS IS" BASIS,
 14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   See the License for the specific language governing permissions and
 16   limitations under the License.
 17 
 18   All of the files in this project are under the project-wide license
 19   unless they are otherwise marked.
 20 **/
 21 
 22 /**
 23   @class
 24 
 25   CanvasView provides a responsive container for containing your SVG elements.
 26 
 27   A CanvasView containing a SVG ensure that the visualization scales up at a particular
 28   aspect ratio, and scales down properly at a minimum width.
 29 
 30   To use, wrap around your graph like so:
 31 
 32       {{#view Ember.CanvasView}}
 33         {{view Ember.HistogramView ...}}
 34       {{/view}}
 35 
 36   CanvasView provides sane defaults, but you may override the presentation using the following
 37   attributes: containerWidth, containerHeight, xMargin, yMargin, and aspectRatio.
 38 
 39 **/
 40 Ember.CanvasView = Ember.View.extend(
 41 /** @scope Ember.CanvasView.prototype */ {
 42 
 43   xMargin: 70,
 44 
 45   yMargin: 20,
 46 
 47   containerWidth: 980,
 48 
 49   containerHeight: 300,
 50 
 51   aspectRatio: 980 / 300,
 52 
 53   viewBox: function() {
 54     var containerWidth  = this.get('containerWidth'),
 55         containerHeight = this.get('containerHeight');
 56 
 57     return "0 0 " + containerWidth + " " + containerHeight;
 58   }.property('containerWidth').cacheable(),
 59 
 60   preserveAspectRatio: function() {
 61     return "xMinYMid";
 62   }.property('containerWidth').cacheable(),
 63 
 64   width: function() {
 65     return this.get('containerWidth');
 66   }.property('containerWidth').cacheable(),
 67 
 68   height: function() {
 69     return this.get('containerWidth') / this.get('aspectRatio');
 70   }.property('containerWidth').cacheable(),
 71 
 72   didInsertElement: function() {
 73     var self = this,
 74         elem = this.$();
 75 
 76     Ember.run.next(function() {
 77       self.set('containerWidth', elem.width());
 78 
 79       $(window).resize(function(){
 80         self.set('containerWidth', elem.width());
 81       });
 82     });
 83 
 84     this._super();
 85   }
 86 
 87 });
 88