layui/src/modules/rate.js

218 lines
6.0 KiB
JavaScript
Raw Normal View History

2018-04-28 10:54:27 +08:00
/**
2018-04-24 14:56:49 +08:00
2021-04-05 23:41:30 +08:00
@Title: rate 评分评星组件
2018-04-28 10:54:27 +08:00
@LicenseMIT
*/
2018-04-24 14:56:49 +08:00
2018-04-24 17:57:31 +08:00
layui.define('jquery',function(exports){
2018-04-28 11:04:52 +08:00
"use strict";
var $ = layui.jquery
2018-04-24 17:57:31 +08:00
2018-04-28 11:04:52 +08:00
//外部接口
,rate = {
config: {}
,index: layui.rate ? (layui.rate.index + 10000) : 0
2018-04-24 17:57:31 +08:00
2018-04-28 11:04:52 +08:00
//设置全局项
2018-04-24 17:57:31 +08:00
,set: function(options){
var that = this;
that.config = $.extend({}, that.config, options);
return that;
}
2021-03-31 14:07:23 +08:00
//事件
2018-04-24 17:57:31 +08:00
,on: function(events, callback){
return layui.onevent.call(this, MOD_NAME, events, callback);
}
2018-04-28 11:04:52 +08:00
}
2018-04-24 17:57:31 +08:00
2018-04-25 19:21:27 +08:00
//操作当前实例
,thisRate = function(){
var that = this
,options = that.config;
return {
2018-04-26 00:03:58 +08:00
setvalue: function(value){
that.setvalue.call(that, value);
2018-04-25 19:21:27 +08:00
}
,config: options
}
}
2018-04-24 17:57:31 +08:00
//字符常量
2018-04-26 14:00:57 +08:00
,MOD_NAME = 'rate',ELEM_VIEW = 'layui-rate', ICON_RATE = 'layui-icon-rate', ICON_RATE_SOLID = 'layui-icon-rate-solid', ICON_RATE_HALF = 'layui-icon-rate-half'
,ICON_SOLID_HALF = 'layui-icon-rate-solid layui-icon-rate-half', ICON_SOLID_RATE = 'layui-icon-rate-solid layui-icon-rate', ICON_HALF_RATE = 'layui-icon-rate layui-icon-rate-half'
2018-04-24 17:57:31 +08:00
2018-04-28 11:04:52 +08:00
//构造器
2018-04-24 17:57:31 +08:00
,Class = function(options){
var that = this;
that.index = ++rate.index;
that.config = $.extend({}, that.config, rate.config, options);
that.render();
};
//默认配置
Class.prototype.config = {
2018-04-26 14:00:57 +08:00
length: 5 //初始长度
,text: false //是否显示评分等级
2018-05-08 15:51:37 +08:00
,readonly: false //是否只读
2018-04-26 14:00:57 +08:00
,half: false //是否可以半星
,value: 0 //星星选中个数
,theme: ''
2018-04-24 17:57:31 +08:00
};
//评分渲染
Class.prototype.render = function(){
var that = this
2018-04-26 14:00:57 +08:00
,options = that.config
2018-04-28 10:54:27 +08:00
,style = options.theme ? ('style="color: '+ options.theme + ';"') : '';
2018-04-26 14:00:57 +08:00
options.elem = $(options.elem);
2020-11-26 22:12:46 +08:00
//最大值不能大于总长度
if(options.value > options.length){
options.value = options.length;
}
2018-04-26 14:00:57 +08:00
//如果没有选择半星的属性,却给了小数的数值,统一向上或向下取整
2018-04-25 22:48:22 +08:00
if(parseInt(options.value) !== options.value){
if(!options.half){
options.value = (Math.ceil(options.value) - options.value) < 0.5 ? Math.ceil(options.value): Math.floor(options.value)
}
}
2018-04-24 17:57:31 +08:00
2018-04-26 14:00:57 +08:00
//组件模板
2018-05-08 15:51:37 +08:00
var temp = '<ul class="layui-rate" '+ (options.readonly ? 'readonly' : '') +'>';
2018-04-25 22:48:22 +08:00
for(var i = 1;i <= options.length;i++){
2018-04-26 14:00:57 +08:00
var item = '<li class="layui-inline"><i class="layui-icon '
+ (i>Math.floor(options.value)?ICON_RATE:ICON_RATE_SOLID)
+ '" '+ style +'></i></li>';
2018-04-25 19:21:27 +08:00
if(options.half){
2018-04-25 22:48:22 +08:00
if(parseInt(options.value) !== options.value){
if(i == Math.ceil(options.value)){
2018-04-28 10:54:27 +08:00
temp = temp + '<li><i class="layui-icon layui-icon-rate-half" '+ style +'></i></li>';
2018-04-25 19:21:27 +08:00
}else{
2018-04-25 22:48:22 +08:00
temp = temp + item
2018-04-25 19:21:27 +08:00
}
}else{
2018-04-25 22:48:22 +08:00
temp = temp + item
2018-04-25 19:21:27 +08:00
}
}else{
2018-04-26 14:00:57 +08:00
temp = temp +item;
2018-04-25 19:21:27 +08:00
}
2018-04-25 16:11:15 +08:00
}
2018-04-28 10:54:27 +08:00
temp += '</ul>' + (options.text ? ('<span class="layui-inline">'+ options.value + '星') : '') + '</span>';
2018-04-25 16:11:15 +08:00
2018-04-26 14:00:57 +08:00
//开始插入替代元素
var othis = options.elem
,hasRender = othis.next('.' + ELEM_VIEW);
//生成替代元素
hasRender[0] && hasRender.remove(); //如果已经渲染则Rerender
that.elemTemp = $(temp);
2018-04-28 10:54:27 +08:00
options.span = that.elemTemp.next('span');
options.setText && options.setText(options.value);
2018-04-26 14:00:57 +08:00
othis.html(that.elemTemp);
othis.addClass("layui-inline");
2018-04-25 16:11:15 +08:00
2018-04-25 22:48:22 +08:00
//如果不是只读,那么进行触控事件
2018-05-08 15:51:37 +08:00
if(!options.readonly) that.action();
2018-04-25 19:21:27 +08:00
2018-04-25 16:11:15 +08:00
};
2018-04-26 14:00:57 +08:00
//评分重置
2018-04-26 00:03:58 +08:00
Class.prototype.setvalue = function(value){
var that = this
,options = that.config ;
2018-04-26 14:00:57 +08:00
options.value = value ;
that.render();
};
2018-04-26 00:03:58 +08:00
2018-04-25 22:48:22 +08:00
//li触控事件
Class.prototype.action = function(){
2018-04-25 16:11:15 +08:00
var that = this
,options = that.config
2018-04-28 10:54:27 +08:00
,_ul = that.elemTemp
,wide = _ul.find("i").width();
2018-04-25 19:21:27 +08:00
2018-04-25 16:11:15 +08:00
_ul.children("li").each(function(index){
2018-04-25 22:48:22 +08:00
var ind = index + 1
,othis = $(this);
2018-04-25 16:11:15 +08:00
//点击
2018-04-25 19:21:27 +08:00
othis.on('click', function(e){
2018-04-25 22:48:22 +08:00
//将当前点击li的索引值赋给value
options.value = ind;
2018-04-25 19:21:27 +08:00
if(options.half){
2018-04-25 22:48:22 +08:00
//获取鼠标在li上的位置
var x = e.pageX - $(this).offset().left;
2018-04-28 10:54:27 +08:00
if(x <= wide / 2){
2018-04-25 22:48:22 +08:00
options.value = options.value - 0.5;
2018-04-25 19:21:27 +08:00
}
}
2018-04-28 10:54:27 +08:00
if(options.text) _ul.next("span").text(options.value + "星");
options.choose && options.choose(options.value);
options.setText && options.setText(options.value);
});
2018-04-25 16:11:15 +08:00
//移入
2018-04-25 19:21:27 +08:00
othis.on('mousemove', function(e){
2018-04-26 14:00:57 +08:00
_ul.find("i").each(function(){
$(this).addClass(ICON_RATE).removeClass(ICON_SOLID_HALF)
2018-04-25 22:48:22 +08:00
});
_ul.find("i:lt(" + ind + ")").each(function(){
2018-04-26 14:00:57 +08:00
$(this).addClass(ICON_RATE_SOLID).removeClass(ICON_HALF_RATE)
2018-04-25 22:48:22 +08:00
});
2018-04-25 19:21:27 +08:00
// 如果设置可选半星那么判断鼠标相对li的位置
2018-04-25 16:11:15 +08:00
if(options.half){
2018-04-25 22:48:22 +08:00
var x = e.pageX - $(this).offset().left;
2018-04-28 10:54:27 +08:00
if(x <= wide / 2){
2018-04-26 14:00:57 +08:00
othis.children("i").addClass(ICON_RATE_HALF).removeClass(ICON_RATE_SOLID)
2018-04-25 16:11:15 +08:00
}
2018-04-25 19:21:27 +08:00
}
2018-04-25 16:11:15 +08:00
})
//移出
2018-04-28 10:54:27 +08:00
othis.on('mouseleave', function(){
2018-04-25 16:11:15 +08:00
_ul.find("i").each(function(){
2018-04-26 14:00:57 +08:00
$(this).addClass(ICON_RATE).removeClass(ICON_SOLID_HALF)
2018-04-25 22:48:22 +08:00
});
_ul.find("i:lt(" + Math.floor(options.value) + ")").each(function(){
2018-04-26 14:00:57 +08:00
$(this).addClass(ICON_RATE_SOLID).removeClass(ICON_HALF_RATE)
2018-04-25 19:21:27 +08:00
});
2018-04-26 14:00:57 +08:00
//如果设置可选半星,根据分数判断是否有半星
2018-04-25 19:21:27 +08:00
if(options.half){
2018-04-25 22:48:22 +08:00
if(parseInt(options.value) !== options.value){
2018-04-26 14:00:57 +08:00
_ul.children("li:eq(" + Math.floor(options.value) + ")").children("i").addClass(ICON_RATE_HALF).removeClass(ICON_SOLID_RATE)
2018-04-25 19:21:27 +08:00
}
}
2018-04-25 16:11:15 +08:00
})
2018-04-25 22:48:22 +08:00
2018-04-25 16:11:15 +08:00
})
2018-04-24 17:57:31 +08:00
};
2018-04-25 16:11:15 +08:00
2018-04-24 17:57:31 +08:00
//事件处理
Class.prototype.events = function(){
2018-04-25 16:11:15 +08:00
var that = this
,options = that.config;
};
2018-04-24 17:57:31 +08:00
//核心入口
rate.render = function(options){
var inst = new Class(options);
2018-04-25 19:21:27 +08:00
return thisRate.call(inst);
2018-04-24 17:57:31 +08:00
};
2018-04-28 11:04:52 +08:00
exports(MOD_NAME, rate);
2018-04-24 14:56:49 +08:00
})