tips.js 1.65 KB
/**
 * Created by TaoHuang on 2016/7/1.
 */

var jQuery = require('yoho-jquery'),
    Handlebars = require('yoho-handlebars');

((function($) {

    var tpl = '<div class="tips hide">' +
        '<div class="triangle"></div>' +
        '<div class="rectangle"> {{content}}</div>' +
        '</div>';

    var tplFn = Handlebars.compile(tpl);

    var _that = this;
    var $tips = null;

    var methods = {
        show: function() {
            var topLeft = this.offset();

            if (_that.isInit) {
                $tips = this.find('.tips');
                this.find('.rectangle').text(arguments[0]);
            } else {
                _that.isInit = true;
                this.append(tplFn({content: arguments[0]}));
                $tips = this.find('.tips').removeClass('hide');
                $tips.css({
                    top: topLeft.top + this.height() - 2,
                    left: topLeft.left,
                    width: this.width() + 2,
                    height: this.height
                });
            }

            return this;
        },
        hide: function() {
            if (_that.isInit) {
                this.find('.tips').addClass('hide');
                _that.isInit = false;
            }

            return this;
        },
        status: function() {
            return _that.isInit;
        }
    };

    _that.isInit = false;

    $.fn.tips = function(method) {

        // 方法调用
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else {
            $.error('Method' + method + 'does not exist on jQuery.tooltip');
        }

    };

})(jQuery));