client/js/podium.js
changeset 2 5533075b5f08
child 3 82b4715202d1
equal deleted inserted replaced
1:e0dbcf98c13e 2:5533075b5f08
       
     1 /* Author: Raphaƫl Velt, IRI
       
     2  * 
       
     3  * Licence: CeCILL-B - http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html
       
     4  * 
       
     5  * */
       
     6 
       
     7 Btv_Podium = function(data, opts) {
       
     8     this.options = opts || {};
       
     9     this.options.container = this.options.container || 'podium';
       
    10     this.options.spacing = this.options.spacing || 20;
       
    11     this.options.background = '#ffffff';
       
    12     this.options.transitionDuration = this.options.transitionDuration || 200;
       
    13     this.$ = this.options.jquery || jQuery;
       
    14     this._$ = this.$('#' + this.options.container);
       
    15     if (!this._$.length) {
       
    16         var _el = document.createElement("div");
       
    17         _el.id = this.options.container;
       
    18         document.body.appendChild(_el);
       
    19         this._$ = this.$(_el);
       
    20     }
       
    21     this.options.width = this.options.width || this._$.width();
       
    22     this.options.height = this.options.height || this._$.height();
       
    23     this.update(data, true);
       
    24 }
       
    25 
       
    26 Btv_Podium.prototype.update = function(data, noAnimate) {
       
    27     var _data = data || [];
       
    28     var i = 0;
       
    29     while (_data.length > this._$.children().length) {
       
    30         var _newCol = document.createElement("div");
       
    31         this.$(_newCol).css({
       
    32             "float": "left",
       
    33             "background": this.options.background,
       
    34             "margin-top": this.options.height,
       
    35             "width": 0,
       
    36             "height": 0,
       
    37             "margin-left": 0
       
    38         });
       
    39         this._$.append(_newCol);
       
    40         i++;
       
    41         if (i > 10) {
       
    42             break;
       
    43         }
       
    44     }
       
    45     while (_data.length < this._$.children().length) {
       
    46         this._$.children().last().detach();
       
    47     }
       
    48     if (_data.length) {
       
    49         var _max = _data.reduce(function(_memo, _val) {
       
    50                 return Math.max(_memo, _val);
       
    51             }, 1),
       
    52             _scale = this.options.height / _max,
       
    53             _spacing = Math.min(this.options.spacing, Math.floor(.5*this.options.width/_data.length)),
       
    54             _width = Math.floor(( this.options.width - (_data.length - 1) * _spacing) / _data.length),
       
    55             _this = this;
       
    56         this._$.children().each(function(_i, _e) {
       
    57             var _height = _scale * _data[_i],
       
    58                 _css = {
       
    59                     "margin-top": _this.options.height - _height,
       
    60                     "height": _height,
       
    61                     "width": _width,
       
    62                     "margin-left": (_i ? _spacing : 0)
       
    63                 }
       
    64             if (noAnimate) {
       
    65                 _this.$(_e).css(_css); 
       
    66             } else {
       
    67                 _this.$(_e).animate(_css);
       
    68             }
       
    69         });
       
    70     }
       
    71 }