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