2018-04-24 14:56:49 +08:00
|
|
|
|
|
|
|
|
|
|
2018-04-24 17:57:31 +08:00
|
|
|
|
layui.define('jquery',function(exports){
|
2018-04-24 14:56:49 +08:00
|
|
|
|
"use strict";
|
2018-04-24 17:57:31 +08:00
|
|
|
|
var $ = layui.jquery
|
|
|
|
|
|
|
|
|
|
//外部接口
|
|
|
|
|
,rate = {
|
|
|
|
|
config: {}
|
|
|
|
|
,index: layui.rate ? (layui.rate.index + 10000) : 0
|
|
|
|
|
|
|
|
|
|
//设置全局项
|
|
|
|
|
,set: function(options){
|
|
|
|
|
var that = this;
|
|
|
|
|
that.config = $.extend({}, that.config, options);
|
|
|
|
|
return that;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//事件监听
|
|
|
|
|
,on: function(events, callback){
|
|
|
|
|
return layui.onevent.call(this, MOD_NAME, events, callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 19:21:27 +08:00
|
|
|
|
//操作当前实例
|
|
|
|
|
,thisRate = function(){
|
|
|
|
|
var that = this
|
|
|
|
|
,options = that.config;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
|
|
function(value){
|
|
|
|
|
console.log(options)
|
|
|
|
|
|
|
|
|
|
//that.setValue();
|
|
|
|
|
}
|
|
|
|
|
,config: options
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-24 17:57:31 +08:00
|
|
|
|
//字符常量
|
2018-04-25 16:11:15 +08:00
|
|
|
|
,MOD_NAME= 'rate', ICON_RATE = 'layui-icon layui-icon-rate', ICON_RATE_SOLID = 'layui-icon layui-icon-rate-solid', ICON_RATE_HALF = 'layui-icon layui-icon-rate-half'
|
|
|
|
|
|
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-25 16:11:15 +08:00
|
|
|
|
length: 5, //初始长度
|
|
|
|
|
text: false, //是否显示评分等级
|
|
|
|
|
reader: false, //是否只读
|
|
|
|
|
half: false, //是否可以半星
|
|
|
|
|
value: 5, //星星选中个数
|
2018-04-24 17:57:31 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//评分渲染
|
|
|
|
|
Class.prototype.render = function(){
|
|
|
|
|
var that = this
|
|
|
|
|
,options = that.config;
|
2018-04-25 19:21:27 +08:00
|
|
|
|
|
2018-04-24 17:57:31 +08:00
|
|
|
|
|
2018-04-25 16:11:15 +08:00
|
|
|
|
var temp='<ul class="layui-rate">';
|
|
|
|
|
for(var i=1;i<=options.length;i++){
|
2018-04-25 19:21:27 +08:00
|
|
|
|
if(options.half){
|
|
|
|
|
if(parseInt(options.value)!==options.value){
|
|
|
|
|
if(i==Math.ceil(options.value)){
|
|
|
|
|
temp=temp+'<li class="layui-inline"><i class="layui-icon layui-icon-rate-half"></i></li>';
|
|
|
|
|
}else{
|
|
|
|
|
temp=temp+'<li class="layui-inline"><i class="layui-icon '+(i>Math.floor(options.value)?'layui-icon-rate':'layui-icon-rate-solid')+'"></i></li>';
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
temp=temp+'<li class="layui-inline"><i class="layui-icon '+(i>Math.floor(options.value)?'layui-icon-rate':'layui-icon-rate-solid')+'"></i></li>';
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
temp=temp+'<li class="layui-inline"><i class="layui-icon '+(i>Math.floor(options.value)?'layui-icon-rate':'layui-icon-rate-solid')+'"></i></li>';
|
|
|
|
|
}
|
2018-04-25 16:11:15 +08:00
|
|
|
|
}
|
2018-04-25 19:21:27 +08:00
|
|
|
|
temp+='</ul><span>'+(options.text ? options.value+"分" : "")+'</span>';
|
2018-04-25 16:11:15 +08:00
|
|
|
|
|
|
|
|
|
$(options.elem).after(temp);
|
|
|
|
|
|
2018-04-25 19:21:27 +08:00
|
|
|
|
//如果不是只读,那么进行点击事件
|
|
|
|
|
if(!options.reader) that.action();
|
|
|
|
|
|
|
|
|
|
|
2018-04-25 16:11:15 +08:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-25 19:21:27 +08:00
|
|
|
|
|
2018-04-25 16:11:15 +08:00
|
|
|
|
//li点击事件
|
2018-04-25 19:21:27 +08:00
|
|
|
|
Class.prototype.action=function(){
|
2018-04-25 16:11:15 +08:00
|
|
|
|
var that = this
|
|
|
|
|
,options = that.config
|
|
|
|
|
,_ul=$(options.elem).next("ul");
|
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 19:21:27 +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 16:11:15 +08:00
|
|
|
|
options.value=ind;
|
2018-04-25 19:21:27 +08:00
|
|
|
|
|
|
|
|
|
if(options.half){
|
|
|
|
|
var x=e.pageX-$(this).offset().left;
|
|
|
|
|
if(x<=13){
|
|
|
|
|
options.value=options.value-0.5;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-25 16:11:15 +08:00
|
|
|
|
if(options.text) _ul.next("span").text(options.value+"分");
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//移入
|
2018-04-25 19:21:27 +08:00
|
|
|
|
othis.on('mousemove', function(e){
|
2018-04-25 16:11:15 +08:00
|
|
|
|
_ul.find("i").each(function(){
|
2018-04-25 19:21:27 +08:00
|
|
|
|
this.className = ICON_RATE;
|
|
|
|
|
})
|
|
|
|
|
_ul.find("i:lt("+ind+")").each(function(){
|
|
|
|
|
this.className = ICON_RATE_SOLID;
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 如果设置可选半星,那么判断鼠标相对li的位置
|
2018-04-25 16:11:15 +08:00
|
|
|
|
if(options.half){
|
2018-04-25 19:21:27 +08:00
|
|
|
|
var x=e.pageX-$(this).offset().left;
|
|
|
|
|
if(x<=13){
|
|
|
|
|
$(this).children("i")[0].className=ICON_RATE_HALF
|
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-25 19:21:27 +08:00
|
|
|
|
othis.on('mouseout', function(){
|
2018-04-25 16:11:15 +08:00
|
|
|
|
_ul.find("i").each(function(){
|
2018-04-25 19:21:27 +08:00
|
|
|
|
this.className=ICON_RATE;
|
|
|
|
|
});
|
|
|
|
|
_ul.find("i:lt("+ Math.floor(options.value) +")").each(function(){
|
|
|
|
|
this.className=ICON_RATE_SOLID;
|
2018-04-24 17:57:31 +08:00
|
|
|
|
})
|
2018-04-25 19:21:27 +08:00
|
|
|
|
if(options.half){
|
|
|
|
|
if(parseInt(options.value)!== options.value){
|
|
|
|
|
_ul.children("li:eq("+Math.floor(options.value) +")").children("i")[0].className=ICON_RATE_HALF;
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
|
2018-04-25 16:11:15 +08:00
|
|
|
|
};
|
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-25 16:11:15 +08:00
|
|
|
|
exports(MOD_NAME, rate);
|
2018-04-24 14:56:49 +08:00
|
|
|
|
})
|