rate.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(options.value > options.length){
  62. options.value = options.length;
  63. }
  64. //如果没有选择半星的属性,却给了小数的数值,统一向上或向下取整
  65. if(parseInt(options.value) !== options.value){
  66. if(!options.half){
  67. options.value = (Math.ceil(options.value) - options.value) < 0.5 ? Math.ceil(options.value): Math.floor(options.value)
  68. }
  69. }
  70. //组件模板
  71. var temp = '<ul class="layui-rate" '+ (options.readonly ? 'readonly' : '') +'>';
  72. for(var i = 1;i <= options.length;i++){
  73. var item = '<li class="layui-inline"><i class="layui-icon '
  74. + (i>Math.floor(options.value)?ICON_RATE:ICON_RATE_SOLID)
  75. + '" '+ style +'></i></li>';
  76. if(options.half){
  77. if(parseInt(options.value) !== options.value){
  78. if(i == Math.ceil(options.value)){
  79. temp = temp + '<li><i class="layui-icon layui-icon-rate-half" '+ style +'></i></li>';
  80. }else{
  81. temp = temp + item
  82. }
  83. }else{
  84. temp = temp + item
  85. }
  86. }else{
  87. temp = temp +item;
  88. }
  89. }
  90. temp += '</ul>' + (options.text ? ('<span class="layui-inline">'+ options.value + '星') : '') + '</span>';
  91. //开始插入替代元素
  92. var othis = options.elem
  93. ,hasRender = othis.next('.' + ELEM_VIEW);
  94. //生成替代元素
  95. hasRender[0] && hasRender.remove(); //如果已经渲染,则Rerender
  96. that.elemTemp = $(temp);
  97. options.span = that.elemTemp.next('span');
  98. options.setText && options.setText(options.value);
  99. othis.html(that.elemTemp);
  100. othis.addClass("layui-inline");
  101. //如果不是只读,那么进行触控事件
  102. if(!options.readonly) that.action();
  103. };
  104. //评分重置
  105. Class.prototype.setvalue = function(value){
  106. var that = this
  107. ,options = that.config ;
  108. options.value = value ;
  109. that.render();
  110. };
  111. //li触控事件
  112. Class.prototype.action = function(){
  113. var that = this
  114. ,options = that.config
  115. ,_ul = that.elemTemp
  116. ,wide = _ul.find("i").width();
  117. _ul.children("li").each(function(index){
  118. var ind = index + 1
  119. ,othis = $(this);
  120. //点击
  121. othis.on('click', function(e){
  122. //将当前点击li的索引值赋给value
  123. options.value = ind;
  124. if(options.half){
  125. //获取鼠标在li上的位置
  126. var x = e.pageX - $(this).offset().left;
  127. if(x <= wide / 2){
  128. options.value = options.value - 0.5;
  129. }
  130. }
  131. if(options.text) _ul.next("span").text(options.value + "星");
  132. options.choose && options.choose(options.value);
  133. options.setText && options.setText(options.value);
  134. });
  135. //移入
  136. othis.on('mousemove', function(e){
  137. _ul.find("i").each(function(){
  138. $(this).addClass(ICON_RATE).removeClass(ICON_SOLID_HALF)
  139. });
  140. _ul.find("i:lt(" + ind + ")").each(function(){
  141. $(this).addClass(ICON_RATE_SOLID).removeClass(ICON_HALF_RATE)
  142. });
  143. // 如果设置可选半星,那么判断鼠标相对li的位置
  144. if(options.half){
  145. var x = e.pageX - $(this).offset().left;
  146. if(x <= wide / 2){
  147. othis.children("i").addClass(ICON_RATE_HALF).removeClass(ICON_RATE_SOLID)
  148. }
  149. }
  150. })
  151. //移出
  152. othis.on('mouseleave', function(){
  153. _ul.find("i").each(function(){
  154. $(this).addClass(ICON_RATE).removeClass(ICON_SOLID_HALF)
  155. });
  156. _ul.find("i:lt(" + Math.floor(options.value) + ")").each(function(){
  157. $(this).addClass(ICON_RATE_SOLID).removeClass(ICON_HALF_RATE)
  158. });
  159. //如果设置可选半星,根据分数判断是否有半星
  160. if(options.half){
  161. if(parseInt(options.value) !== options.value){
  162. _ul.children("li:eq(" + Math.floor(options.value) + ")").children("i").addClass(ICON_RATE_HALF).removeClass(ICON_SOLID_RATE)
  163. }
  164. }
  165. })
  166. })
  167. };
  168. //事件处理
  169. Class.prototype.events = function(){
  170. var that = this
  171. ,options = that.config;
  172. };
  173. //核心入口
  174. rate.render = function(options){
  175. var inst = new Class(options);
  176. return thisRate.call(inst);
  177. };
  178. exports(MOD_NAME, rate);
  179. })