1 (function ($, rf) {
  2 
  3     rf.ui = rf.ui || {};
  4 
  5     var defaultOptions = {
  6         interval: 1000,
  7         minValue: 0,
  8         maxValue: 100
  9     };
 10 
 11     var stateSelectors = {
 12         initial: '> .rf-pb-init',
 13         progress: '> .rf-pb-rmng',
 14         finish: '> .rf-pb-fin'
 15     };
 16 
 17     // Constructor definition
 18     rf.ui.ProgressBar = function(componentId, options) {
 19         // call constructor of parent class
 20         $super.constructor.call(this, componentId);
 21         this.__elt = this.attachToDom();
 22         this.options = $.extend(this.options, defaultOptions, options || {});
 23         this.enabled = this.options.enabled;
 24         this.minValue = this.options.minValue;
 25         this.maxValue = this.options.maxValue;
 26 
 27         this.__setValue(this.options.value || this.options.minValue /* TODO - check with Ilya */);
 28 
 29         if (this.options.resource) {
 30             this.__poll();
 31         } else if (this.options.submitFunction) {
 32             this.submitFunction = new Function("beforeUpdateHandler", "afterUpdateHandler", "params", "event", this.options.submitFunction);
 33             this.__poll();
 34         }
 35 
 36         if (this.options.onfinish) {
 37             rf.Event.bind(this.__elt, "finish", new Function("event", this.options.onfinish));
 38         }
 39     };
 40 
 41     // Extend component class and add protected methods from parent class to our container
 42     rf.BaseComponent.extend(rf.ui.ProgressBar);
 43 
 44     // define super class link
 45     var $super = rf.ui.ProgressBar.$super;
 46 
 47     $.extend(rf.ui.ProgressBar.prototype, (function() {
 48         return {
 49             name: "ProgressBar",
 50 
 51             __isInitialState: function() {
 52                 return parseFloat(this.value) < parseFloat(this.getMinValue());
 53             },
 54 
 55             __isProgressState: function() {
 56                 return !this.__isInitialState() && !this.__isFinishState();
 57             },
 58 
 59             __isFinishState: function() {
 60                 return parseFloat(this.value) >= parseFloat(this.getMaxValue());
 61             },
 62 
 63             __beforeUpdate: function(event) {
 64                 if (event.componentData && typeof event.componentData[this.id] != 'undefined') {
 65                     this.setValue(event.componentData[this.id]);
 66                 }
 67             },
 68 
 69             __afterUpdate: function(event) {
 70                 this.__poll();
 71             },
 72 
 73             __onResourceDataAvailable: function(data) {
 74                 var parsedData = rf.parseJSON(data);
 75                 if (parsedData instanceof Number || typeof parsedData == 'number') {
 76                     this.setValue(parsedData);
 77                 }
 78 
 79                 this.__poll();
 80             },
 81 
 82             __submit: function() {
 83                 if (this.submitFunction) {
 84                     this.submitFunction.call(this, $.proxy(this.__beforeUpdate, this), $.proxy(this.__afterUpdate, this),
 85                         this.__params || {});
 86                 } else {
 87                     $.get(this.options.resource, this.__params || {}, $.proxy(this.__onResourceDataAvailable, this), 'text');
 88                 }
 89             },
 90 
 91             __poll: function(immediate) {
 92                 if (this.enabled) {
 93                     if (immediate) {
 94                         this.__submit();
 95                     } else {
 96                         this.__pollTimer = setTimeout($.proxy(this.__submit, this), this.options.interval);
 97                     }
 98                 }
 99             },
100 
101             __calculatePercent: function(v) {
102                 var min = parseFloat(this.getMinValue());
103                 var max = parseFloat(this.getMaxValue());
104                 var value = parseFloat(v);
105                 if (min < value && value < max) {
106                     return (100 * (value - min))