rate.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. @Title: layui.rate 评分评星
  3. @Author: star1029
  4. @License:MIT
  5. */
  6. layui.define('jquery',function(exports){
  7. "use strict";
  8. var $ = layui.jquery
  9. //外部接口
  10. ,rate = {
  11. config: {}
  12. ,index: layui.rate ? (layui.rate.index + 10000) : 0
  13. //设置全局项
  14. ,set: function(options){
  15. var that = this;
  16. that.config = $.extend({}, that.config, options);
  17. return that;
  18. }
  19. //事件监听
  20. ,on: function(events, callback){
  21. return layui.onevent.call(this, MOD_NAME, events, callback);
  22. }
  23. }
  24. //操作当前实例
  25. ,thisRate = function(){
  26. var that = this
  27. ,options = that.config;
  28. return {
  29. setvalue: function(value){
  30. that.setvalue.call(that, value);
  31. }
  32. ,config: options
  33. }
  34. }
  35. //字符常量
  36. ,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'
  37. ,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'
  38. //构造器
  39. ,Class = function(options){
  40. var that = this;
  41. that.index = ++rate.index;
  42. that.config = $.extend({}, that.config, rate.config, options);
  43. that.render();
  44. };
  45. //默认配置
  46. Class.prototype.config = {
  47. length: 5 //初始长度
  48. ,text: false //是否显示评分等级
  49. ,readonly: false //是否只读
  50. ,half: false //是否可以半星
  51. ,value: 0 //星星选中个数
  52. ,theme: ''
  53. };
  54. //评分渲染
  55. Class.prototype.render = function(){
  56. var that = this
  57. ,options = that.config
  58. ,style = options.theme ? ('style="color: '+ options.theme + ';"') : '';
  59. options.elem = $(options.elem);
  60. //如果没有选择半星的属性,却给了小数的数值,统一向上或向下取整
  61. if(parseInt(options.value) !== options.value){
  62. if(!options.half){
  63. options.value = (Math.ceil(options.value) - options.value) < 0.5 ? Math.ceil(options.value): Math.floor(options.value)
  64. }
  65. }
  66. //组件模板
  67. var temp = '<ul class="layui-rate" '+ (options.readonly ? 'readonly' : '') +'>';
  68. for(var i = 1;i <= options.length;i++){
  69. var item = '<li class="layui-inline"><i class="layui-icon '
  70. + (i>Math.floor(options.value)?ICON_RATE:ICON_RATE_SOLID)
  71. + '" '+ style +'></i></li>';
  72. if(options.half){
  73. if(parseInt(options.value) !== options.value){
  74. if(i == Math.ceil(options.value)){
  75. temp = temp + '<li><i class="layui-icon layui-icon-rate-half" '+ style +'></i></li>';
  76. }else{
  77. temp = temp + item
  78. }
  79. }else{
  80. temp = temp + item
  81. }
  82. }else{
  83. temp = temp +item;
  84. }
  85. }
  86. temp += '</ul>' + (options.text ? ('<span class="layui-inline">'+ options.value + '星') : '') + '</span>';
  87. //开始插入替代元素
  88. var othis = options.elem
  89. ,hasRender = othis.next('.' + ELEM_VIEW);
  90. //生成替代元素
  91. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  92. that.elemTemp = $(temp);
  93. options.span = that.elemTemp.next('span');
  94. options.setText && options.setText(options.value);
  95. othis.html(that.elemTemp);
  96. othis.addClass("layui-inline");
  97. //如果不是只读,那么进行触控事件
  98. if(!options.readonly) that.action();
  99. };
  100. //评分重置
  101. Class.prototype.setvalue = function(value){
  102. var that = this
  103. ,options = that.config ;
  104. options.value = value ;
  105. that.render();
  106. };
  107. //li触控事件
  108. Class.prototype.action = function(){
  109. var that = this
  110. ,options = that.config
  111. ,_ul = that.elemTemp
  112. ,wide = _ul.find("i").width();
  113. _ul.children("li").each(function(index){
  114. var ind = index + 1
  115. ,othis = $(this);
  116. //点击
  117. othis.on('click', function(e){
  118. //将当前点击li的索引值赋给value
  119. options.value = ind;
  120. if(options.half){
  121. //获取鼠标在li上的位置
  122. var x = e.pageX - $(this).offset().left;
  123. if(x <= wide / 2){
  124. options.value = options.value - 0.5;
  125. }
  126. }
  127. if(options.text) _ul.next("span").text(options.value + "星");
  128. options.choose && options.choose(options.value);
  129. options.setText && options.setText(options.value);
  130. });
  131. //移入
  132. othis.on('mousemove', function(e){
  133. _ul.find("i").each(function(){
  134. $(this).addClass(ICON_RATE).removeClass(ICON_SOLID_HALF)
  135. });
  136. _ul.find("i:lt(" + ind + ")").each(function(){
  137. $(this).addClass(ICON_RATE_SOLID).removeClass(ICON_HALF_RATE)
  138. });
  139. // 如果设置可选半星,那么判断鼠标相对li的位置
  140. if(options.half){
  141. var x = e.pageX - $(this).offset().left;
  142. if(x <= wide / 2){
  143. othis.children("i").addClass(ICON_RATE_HALF).removeClass(ICON_RATE_SOLID)
  144. }
  145. }
  146. })
  147. //移出
  148. othis.on('mouseleave', function(){
  149. _ul.find("i").each(function(){
  150. $(this).addClass(ICON_RATE).removeClass(ICON_SOLID_HALF)
  151. });
  152. _ul.find("i:lt(" + Math.floor(options.value) + ")").each(function(){
  153. $(this).addClass(ICON_RATE_SOLID).removeClass(ICON_HALF_RATE)
  154. });
  155. //如果设置可选半星,根据分数判断是否有半星
  156. if(options.half){
  157. if(parseInt(options.value) !== options.value){
  158. _ul.children("li:eq(" + Math.floor(options.value) + ")").children("i").addClass(ICON_RATE_HALF).removeClass(ICON_SOLID_RATE)
  159. }
  160. }
  161. })
  162. })
  163. };
  164. //事件处理
  165. Class.prototype.events = function(){
  166. var that = this
  167. ,options = that.config;
  168. };
  169. //核心入口
  170. rate.render = function(options){
  171. var inst = new Class(options);
  172. return thisRate.call(inst);
  173. };
  174. exports(MOD_NAME, rate);
  175. })