Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VRating.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Styles
  2. import '../../../src/stylus/components/_rating.styl';
  3. // Components
  4. import VIcon from '../VIcon';
  5. // Mixins
  6. import Colorable from '../../mixins/colorable';
  7. import Delayable from '../../mixins/delayable';
  8. import Sizeable from '../../mixins/sizeable';
  9. import Rippleable from '../../mixins/rippleable';
  10. import Themeable from '../../mixins/themeable';
  11. // Utilities
  12. import { createRange } from '../../util/helpers';
  13. import mixins from '../../util/mixins';
  14. /* @vue/component */
  15. export default mixins(Colorable, Delayable, Rippleable, Sizeable, Themeable).extend({
  16. name: 'v-rating',
  17. props: {
  18. backgroundColor: {
  19. type: String,
  20. default: 'accent'
  21. },
  22. color: {
  23. type: String,
  24. default: 'primary'
  25. },
  26. dense: Boolean,
  27. emptyIcon: {
  28. type: String,
  29. default: '$vuetify.icons.ratingEmpty'
  30. },
  31. fullIcon: {
  32. type: String,
  33. default: '$vuetify.icons.ratingFull'
  34. },
  35. halfIcon: {
  36. type: String,
  37. default: '$vuetify.icons.ratingHalf'
  38. },
  39. halfIncrements: Boolean,
  40. length: {
  41. type: [Number, String],
  42. default: 5
  43. },
  44. clearable: Boolean,
  45. readonly: Boolean,
  46. hover: Boolean,
  47. value: {
  48. type: Number,
  49. default: 0
  50. }
  51. },
  52. data: function data() {
  53. return {
  54. hoverIndex: -1,
  55. internalValue: this.value
  56. };
  57. },
  58. computed: {
  59. directives: function directives() {
  60. if (this.readonly || !this.ripple) return [];
  61. return [{
  62. name: 'ripple',
  63. value: { circle: true }
  64. }];
  65. },
  66. iconProps: function iconProps() {
  67. var _$props = this.$props,
  68. dark = _$props.dark,
  69. medium = _$props.medium,
  70. large = _$props.large,
  71. light = _$props.light,
  72. small = _$props.small,
  73. size = _$props.size,
  74. xLarge = _$props.xLarge;
  75. return {
  76. dark: dark,
  77. medium: medium,
  78. large: large,
  79. light: light,
  80. size: size,
  81. small: small,
  82. xLarge: xLarge
  83. };
  84. },
  85. isHovering: function isHovering() {
  86. return this.hover && this.hoverIndex >= 0;
  87. }
  88. },
  89. watch: {
  90. internalValue: function internalValue(val) {
  91. val !== this.value && this.$emit('input', val);
  92. },
  93. value: function value(val) {
  94. this.internalValue = val;
  95. }
  96. },
  97. methods: {
  98. createClickFn: function createClickFn(i) {
  99. var _this = this;
  100. return function (e) {
  101. if (_this.readonly) return;
  102. var newValue = _this.genHoverIndex(e, i);
  103. if (_this.clearable && _this.internalValue === newValue) {
  104. _this.internalValue = 0;
  105. } else {
  106. _this.internalValue = newValue;
  107. }
  108. };
  109. },
  110. createProps: function createProps(i) {
  111. var props = {
  112. index: i,
  113. value: this.internalValue,
  114. click: this.createClickFn(i),
  115. isFilled: Math.floor(this.internalValue) > i,
  116. isHovered: Math.floor(this.hoverIndex) > i
  117. };
  118. if (this.halfIncrements) {
  119. props.isHalfHovered = !props.isHovered && (this.hoverIndex - i) % 1 > 0;
  120. props.isHalfFilled = !props.isFilled && (this.internalValue - i) % 1 > 0;
  121. }
  122. return props;
  123. },
  124. genHoverIndex: function genHoverIndex(e, i) {
  125. return i + (this.isHalfEvent(e) ? 0.5 : 1);
  126. },
  127. getIconName: function getIconName(props) {
  128. var isFull = this.isHovering ? props.isHovered : props.isFilled;
  129. var isHalf = this.isHovering ? props.isHalfHovered : props.isHalfFilled;
  130. return isFull ? this.fullIcon : isHalf ? this.halfIcon : this.emptyIcon;
  131. },
  132. getColor: function getColor(props) {
  133. if (this.isHovering) {
  134. if (props.isHovered || props.isHalfHovered) return this.color;
  135. } else {
  136. if (props.isFilled || props.isHalfFilled) return this.color;
  137. }
  138. return this.backgroundColor;
  139. },
  140. isHalfEvent: function isHalfEvent(e) {
  141. if (this.halfIncrements) {
  142. var rect = e.target && e.target.getBoundingClientRect();
  143. if (rect && e.pageX - rect.left < rect.width / 2) return true;
  144. }
  145. return false;
  146. },
  147. onMouseEnter: function onMouseEnter(e, i) {
  148. var _this2 = this;
  149. this.runDelay('open', function () {
  150. _this2.hoverIndex = _this2.genHoverIndex(e, i);
  151. });
  152. },
  153. onMouseLeave: function onMouseLeave() {
  154. var _this3 = this;
  155. this.runDelay('close', function () {
  156. return _this3.hoverIndex = -1;
  157. });
  158. },
  159. genItem: function genItem(i) {
  160. var _this4 = this;
  161. var props = this.createProps(i);
  162. if (this.$scopedSlots.item) return this.$scopedSlots.item(props);
  163. var listeners = {
  164. click: props.click
  165. };
  166. if (this.hover) {
  167. listeners.mouseenter = function (e) {
  168. return _this4.onMouseEnter(e, i);
  169. };
  170. listeners.mouseleave = this.onMouseLeave;
  171. if (this.halfIncrements) {
  172. listeners.mousemove = function (e) {
  173. return _this4.onMouseEnter(e, i);
  174. };
  175. }
  176. }
  177. return this.$createElement(VIcon, this.setTextColor(this.getColor(props), {
  178. directives: this.directives,
  179. props: this.iconProps,
  180. on: listeners
  181. }), [this.getIconName(props)]);
  182. }
  183. },
  184. render: function render(h) {
  185. var _this5 = this;
  186. var children = createRange(Number(this.length)).map(function (i) {
  187. return _this5.genItem(i);
  188. });
  189. return h('div', {
  190. staticClass: 'v-rating',
  191. class: {
  192. 'v-rating--readonly': this.readonly,
  193. 'v-rating--dense': this.dense
  194. }
  195. }, children);
  196. }
  197. });
  198. //# sourceMappingURL=VRating.js.map