优化 rate 代码书写格式

优化 rate.js 代码
pull/1345/head
贤心 2023-08-23 22:52:49 +08:00 committed by GitHub
commit f3c2d22ac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 37 deletions

View File

@ -10,42 +10,47 @@ layui.define(['jquery', 'lay'],function(exports){
// 外部接口
var rate = {
config: {}
,index: layui.rate ? (layui.rate.index + 10000) : 0
config: {},
index: layui.rate ? (layui.rate.index + 10000) : 0,
//设置全局项
,set: function(options){
set: function(options){
var that = this;
that.config = $.extend({}, that.config, options);
return that;
}
},
//事件
,on: function(events, callback){
on: function(events, callback){
return layui.onevent.call(this, MOD_NAME, events, callback);
}
}
// 操作当前实例
,thisRate = function(){
var that = this
,options = that.config;
var thisRate = function () {
var that = this;
var options = that.config;
return {
setvalue: function (value) {
that.setvalue.call(that, value);
},
config: options
}
,config: options
}
}
};
//字符常量
,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'
var MOD_NAME = 'rate';
var ELEM_VIEW = 'layui-rate';
var ICON_RATE = 'layui-icon-rate';
var ICON_RATE_SOLID = 'layui-icon-rate-solid';
var ICON_RATE_HALF = 'layui-icon-rate-half';
var ICON_SOLID_HALF = 'layui-icon-rate-solid layui-icon-rate-half';
var ICON_SOLID_RATE = 'layui-icon-rate-solid layui-icon-rate';
var ICON_HALF_RATE = 'layui-icon-rate layui-icon-rate-half';
//构造器
,Class = function(options){
var Class = function (options) {
var that = this;
that.index = ++rate.index;
that.config = $.extend({}, that.config, rate.config, options);
@ -54,12 +59,12 @@ layui.define(['jquery', 'lay'],function(exports){
//默认配置
Class.prototype.config = {
length: 5 //初始长度
,text: false //是否显示评分等级
,readonly: false //是否只读
,half: false //是否可以半星
,value: 0 //星星选中个数
,theme: ''
length: 5, //初始长度
text: false, //是否显示评分等级
readonly: false, //是否只读
half: false, //是否可以半星
value: 0, //星星选中个数
theme: ''
};
//评分渲染
@ -114,8 +119,8 @@ layui.define(['jquery', 'lay'],function(exports){
temp += '</ul>' + (options.text ? ('<span class="layui-inline">'+ options.value + '星') : '') + '</span>';
//开始插入替代元素
var othis = options.elem
,hasRender = othis.next('.' + ELEM_VIEW);
var othis = options.elem;
var hasRender = othis.next('.' + ELEM_VIEW);
//生成替代元素
hasRender[0] && hasRender.remove(); //如果已经渲染则Rerender
@ -137,8 +142,8 @@ layui.define(['jquery', 'lay'],function(exports){
//评分重置
Class.prototype.setvalue = function(value){
var that = this
,options = that.config ;
var that = this;
var options = that.config;
options.value = value ;
that.render();
@ -146,14 +151,14 @@ layui.define(['jquery', 'lay'],function(exports){
//li触控事件
Class.prototype.action = function(){
var that = this
,options = that.config
,_ul = that.elemTemp
,wide = _ul.find("i").width();
var that = this;
var options = that.config;
var _ul = that.elemTemp;
var wide = _ul.find("i").width();
_ul.children("li").each(function(index){
var ind = index + 1
,othis = $(this);
var ind = index + 1;
var othis = $(this);
//点击
othis.on('click', function(e){
@ -211,8 +216,8 @@ layui.define(['jquery', 'lay'],function(exports){
//事件处理
Class.prototype.events = function () {
var that = this
,options = that.config;
var that = this;
//var options = that.config;
};
//核心入口