Advertisement
foolmoron

Mouse over tooltip component

Dec 13th, 2013
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var MouseOverTooltipComponent = IgeEventingClass.extend({
  2.     classId: 'MouseOverTooltipComponent',
  3.     componentId: 'mouseOverTooltip',
  4.  
  5.     DEFAULT_TEXT: "Default text!",
  6.     DEFAULT_PIXELS_PER_LINE: 14,
  7.     DEFAULT_STYLE: {
  8.         'backgroundColor': '#3a3a3a',
  9.         'borderColor': '#ffffff',
  10.         'borderWidth': 2,
  11.         'borderRadius': 10,
  12.         'color': '#ffffff',
  13.         'width': 150,
  14.         'top': 55,
  15.         'left': 30,
  16.     }, 
  17.    
  18.     /**
  19.     * Options:
  20.     * mountTaget        - object - Object that the tooltip will be mounted to.  Default target is the entity.
  21.     * text              - string - Text to display in the label.
  22.     * pixelsPerLine     - int - Number of pixels to space each line in the label using LineHeightComponent.
  23.     * style             - object - CSS-like object in accordance with the list of properties in IgeUiStyleExtension.
  24.     */
  25.     init: function (entity, options) {
  26.         var self = this;
  27.         this._entity = entity;
  28.         this._enabled = true;
  29.        
  30.         this._options = {
  31.             mountTarget: this._entity,
  32.             text: this.DEFAULT_TEXT,
  33.             pixelsPerLine: this.DEFAULT_PIXELS_PER_LINE,
  34.             style: this.DEFAULT_STYLE,
  35.         };
  36.         //apply options
  37.         if (options) {
  38.             for (i in options)
  39.                 this._options[i] = options[i];
  40.             if (options.style) { // overwrite default style with custom styles
  41.                 this._options.style = JSON.parse(JSON.stringify(this.DEFAULT_STYLE)); // clone object
  42.                 for (i in options.style)
  43.                     this._options.style[i] = options.style[i];     
  44.             }
  45.         }
  46.        
  47.         //build tooltip
  48.         this.tooltip = new IgeUiLabel()
  49.             .value(this._options.text)
  50.             .applyStyle(this._options.style)
  51.             ;
  52.         //customize font object of label
  53.         this.tooltip._fontEntity
  54.             .autoWrap(true)
  55.             ;
  56.         if (LineHeightComponent) { // can't use this if you don't have it
  57.             this.tooltip._fontEntity
  58.                 .addComponent(LineHeightComponent)
  59.                 .lineHeight.pixelsPerLine(this._options.pixelsPerLine)
  60.                 ;
  61.         }
  62.        
  63.         //enable mouse event emissions if necessary
  64.         if (!this._entity.mouseOver())
  65.             this._entity.mouseOver(function() {});
  66.         if (!this._entity.mouseOut())
  67.             this._entity.mouseOut(function() {});          
  68.         //mouse hooks on entity
  69.         this._entity.on('mouseOver', function() {
  70.             if (self.enabled())
  71.                 self.tooltip.mount(self._options.mountTarget);
  72.         });
  73.         this._entity.on('mouseOut', function() {
  74.             if (self.enabled())
  75.                 self.tooltip.unMount();
  76.         });
  77.     },
  78.    
  79.     enabled: function (val) {
  80.         if (val !== undefined) {
  81.             if (!val)
  82.                 this.tooltip.unMount();
  83.            
  84.             this._enabled = val;                   
  85.             return this._entity;
  86.         }
  87.         return this._enabled;
  88.     },
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement