All files / UI_Component Tooltip.js

100% Statements 101/101
95.83% Branches 46/48
100% Functions 8/8
100% Lines 101/101
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255                  1x                                 1x 1x 1x 1x           4x   4x 4x 4x                   16x                   19x   19x 19x 19x 19x 19x 19x 1x 18x 1x   17x     19x 18x 18x 18x 18x   1x 1x 1x 1x       19x 19x 19x 19x 19x 9x 6x 6x 3x 1x 1x 2x 1x 1x 1x 1x 1x   10x 3x 3x 1x 2x 1x   1x   7x 7x 7x 4x 3x 1x   2x     19x 1x   18x     19x 19x 19x 19x 19x 19x 19x 19x 19x 11x 8x 3x   5x       19x 19x 15x       19x 19x 19x 15x 15x   4x 4x     19x 19x 18x 18x   1x 1x     19x 19x 20x   20x                                   19x                       19x       1x                                   1x                                                    
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { is, Map, fromJS } from 'immutable';
import { MAIN_TEXT_FONT, TextItemFamily, AnimationEffect, KEY } from './common/CommonDefine';
import './css/Tooltip.css';
import CommonAPI from './common/CommonAPI';
import ScrollText from './common/ScrollText';
import Image from './common/Image';
 
const tailResource = {
    normal: {
        bg: 'url(./images/tooltip/c_tooltip_white_bubble_bg_9patch.png)  24 14 fill stretch',
        top: 'url(./images/tooltip/c_tooltip_white_bubble_arrow_top.png)',
        bottom: 'url(./images/tooltip/c_tooltip_white_bubble_arrow_bottom.png)',
        left: 'url(./images/tooltip/c_tooltip_white_bubble_arrow_left.png)',
        right: 'url(./images/tooltip/c_tooltip_white_bubble_arrow_right.png)',
    },
    highContrast: {
        bg: 'url(./images/tooltip/c_tooltip_highcontrast_bubble_bg_9patch.png)  24 14 fill stretch',
        top: 'url(./images/tooltip/c_tooltip_highcontrast_bubble_arrow_top.png)',
        bottom: 'url(./images/tooltip/c_tooltip_highcontrast_bubble_arrow_bottom.png)',
        left: 'url(./images/tooltip/c_tooltip_highcontrast_bubble_arrow_left.png)',
        right: 'url(./images/tooltip/c_tooltip_highcontrast_bubble_arrow_right.png)',
    },
};
 
const patchT = 24;
const patchL = 14;
const TOOLTIP_MIN_LENGTH = 132;
const TOOLTIP_MAX_LENGTH = 902;
 
 
export default class Tooltip extends Component {
 
    constructor(props) {
        super(props);
 
        this.balloonTextFont = MAIN_TEXT_FONT;
        this.balloonTextSize = TextItemFamily.TITLE2_TEXT_SIZE;
        this.bodyGap = TextItemFamily.BODY_TEXT_GAP;
    }
 
    componentWillMount() {
    }
 
    componentWillReceiveProps(nextProps) {
    }
 
    shouldComponentUpdate(nextProps, nextState) {  // return true -->render()
        return (JSON.stringify(nextProps) !== JSON.stringify(this.props));
    }
 
    componentDidUpdate(prevProps, prevState) {
    }
 
    componentWillUnmount() {
    }
 
    process() {
        const { OSD, showFlag, tailDirection, tailPostion, highContrast, enlarge, tailW, tailH } = this.props;
 
        const BalloonStyle = {};
        BalloonStyle.position = 'relative';
        BalloonStyle.left = OSD.layout.l;
        BalloonStyle.top = OSD.layout.t;
        BalloonStyle.height = OSD.layout.h;
        if (OSD.layout.w < TOOLTIP_MIN_LENGTH) {                // TOOLTIP_MIN_LENGTH is min width
            BalloonStyle.width = TOOLTIP_MIN_LENGTH;
        } else if (OSD.layout.w > TOOLTIP_MAX_LENGTH) {         // TOOLTIP_MAX_LENGTH is max width
            BalloonStyle.width = TOOLTIP_MAX_LENGTH;
        } else {
            BalloonStyle.width = OSD.layout.w;
        }
 
        if (showFlag) {
            BalloonStyle.animationName = 'Appear';
            BalloonStyle.animationFillMode = 'forwards';
            BalloonStyle.animationDuration = '1.10s';
            BalloonStyle.animationTimingFunction = AnimationEffect.Elastic;
        } else {
            BalloonStyle.animationName = 'Disappear';
            BalloonStyle.animationFillMode = 'forwards';
            BalloonStyle.animationDuration = '0.85s';
            BalloonStyle.animationTimingFunction = AnimationEffect.Out;
        }
 
        // calculate tail position
        const ArrowStyle = {};
        ArrowStyle.position = 'absolute';
        ArrowStyle.width = tailW;
        ArrowStyle.height = tailH;
        if (tailPostion === 'center') {
            if (tailDirection === 'top') {
                ArrowStyle.left = (BalloonStyle.width - this.props.tailW) / 2;
                ArrowStyle.top = 0;
            } else if (tailDirection === 'bottom') {
                ArrowStyle.left = (BalloonStyle.width - this.props.tailW) / 2;
                ArrowStyle.top = BalloonStyle.height - patchT;
            } else if (tailDirection === 'left') {
                ArrowStyle.left = -this.props.tailW + patchL;
                ArrowStyle.top = (BalloonStyle.height - this.props.tailH) / 2;
            } else Eif (tailDirection === 'right') {
                ArrowStyle.left = BalloonStyle.width - patchL;
                ArrowStyle.top = (BalloonStyle.height - this.props.tailH) / 2;
            }
        } else if (tailPostion === 'start') {
            ArrowStyle.left = this.props.tailOffset;
            if (tailDirection === 'top') {
                ArrowStyle.top = 0;
            } else if (tailDirection === 'bottom') {
                ArrowStyle.top = BalloonStyle.height - patchT;
            } else {
                ArrowStyle.top = 0;
            }
        } else Eif (tailPostion === 'end') {
            ArrowStyle.left = BalloonStyle.width - this.props.tailOffset - this.props.tailW;
            if (tailDirection === 'top') {
                ArrowStyle.top = 0;
            } else if (tailDirection === 'bottom') {
                ArrowStyle.top = BalloonStyle.height - patchT;
            } else {
                ArrowStyle.top = 0;
            }
        }
        if (highContrast) {
            ArrowStyle.backgroundImage = tailResource.highContrast[tailDirection];
        } else {
            ArrowStyle.backgroundImage = tailResource.normal[tailDirection];
        }
 
        const TextBGStyle = {};
        TextBGStyle.position = 'absolute';
        TextBGStyle.height = OSD.layout.h;
        TextBGStyle.width = OSD.layout.w;
        TextBGStyle.left = 0;
        TextBGStyle.borderStyle = 'solid';
        TextBGStyle.borderWidth = '24px 14px';
        TextBGStyle.borderImage = highContrast ? tailResource.highContrast.bg : tailResource.normal.bg;
        if (tailDirection === 'top') {
            TextBGStyle.top = this.props.tailH - patchT;
        } else if (tailDirection === 'bottom') {
            TextBGStyle.top = 0;
        } else {
            TextBGStyle.top = 0;
        }
 
        // icon compotent
        let iconComponentList = null;
        if (typeof OSD.icon !== 'undefined') {
            iconComponentList = (<Image key={'icon'} OSD={OSD.icon} />);
        }
 
        // text compotent
        const textComponentList = [];
        const TextStyle = {};
        if (typeof OSD.icon !== 'undefined') {
            TextStyle.left = 14 + OSD.icon.w + 6;
            TextStyle.width = BalloonStyle.width - 25 - 28 - 6 - OSD.icon.w;
        } else {
            TextStyle.left = 11;
            TextStyle.width = BalloonStyle.width - 50;
        }
 
        const len = OSD.text.length;                        // OSD.text marked required
        if (len === 1) {
            TextStyle.height = TextItemFamily.TITLE2_TEXT_HEIGHT;
            TextStyle.lineHeight = TextItemFamily.TITLE2_TEXT_HEIGHT;
        } else {
            TextStyle.height = TextItemFamily.TITLE_TEXT_HEIGHT;
            TextStyle.lineHeight = TextItemFamily.TITLE_TEXT_HEIGHT;
        }
 
        const aligntext = (len === 1 && typeof OSD.icon === 'undefined') ? 'center' : 'left';
        for (let idx = 0; idx < len; idx++) {
            TextStyle.top = ((OSD.layout.h - (2 * patchT) - (len * TextStyle.height)) / 2) + (idx * TextStyle.height);
            // console.log(`TextStyle.top: ${TextStyle.top} idx: ${idx}`);
            textComponentList.push(
              <ScrollText
                key={`text${idx}`}
                scroll={len === 1}
                fontSize={enlarge ? (this.balloonTextSize * 1.2) : this.balloonTextSize}
                fontFamily={this.balloonTextFont}
                textGap={this.bodyGap}
                width={TextStyle.width}
                height={TextStyle.height}
                top={TextStyle.top}
                left={TextStyle.left}
                lineHeight={`${TextStyle.lineHeight}px`}
                textAlign={aligntext}
              >
                {OSD.text[idx]}
              </ScrollText>);
        }
 
        return (
          <div style={BalloonStyle}>
            <div style={ArrowStyle} />
            <div style={TextBGStyle}>
              {textComponentList}
              {iconComponentList}
            </div>
          </div>
        );
    }
 
    render() {
        return this.process();
    }
}
 
Tooltip.defaultProps = {
    showFlag: true,
    tailW: 42,
    tailH: 30,
    tailOffset: 22,
    highContrast: false,
    enlarge: false,
    OSD: {
        layout: {
            l: 0,
            t: 0,
            w: 414,
            h: 116,
        },
        icon: null,
    },
};
 
Tooltip.propTypes = {
    showFlag: PropTypes.bool,
    tailDirection: PropTypes.oneOf(['top', 'bottom', 'left', 'right']).isRequired,          // tailDirection:top/bottom/left/right
    tailPostion: PropTypes.oneOf(['start', 'center', 'end']).isRequired,              // tailPostion: start/center/end
    tailW: PropTypes.number,                            // tail width
    tailH: PropTypes.number,                            // tail height
    tailOffset: PropTypes.number,                        // The tail's offset distance relative to popup balloon's edge.
    highContrast: PropTypes.bool,
    enlarge: PropTypes.bool,
    OSD: PropTypes.shape({             // UI content
        layout: PropTypes.shape({
            l: PropTypes.number,    // tooltip left value
            t: PropTypes.number,    // tooltip top value
            w: PropTypes.number,    // tooltip width value
            h: PropTypes.number,    //tooltip height value
        }),
        icon: PropTypes.shape({
            l: PropTypes.number,
            t: PropTypes.number,
            w: PropTypes.number,
            h: PropTypes.number,
            url: PropTypes.string,
        }),
        text: PropTypes.array.isRequired,
    }),
};