Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * Star Ratings (js)
  3. */
  4. 'use strict';
  5. document.addEventListener('DOMContentLoaded', function (e) {
  6. const basicRatings = document.querySelector('.basic-ratings'),
  7. readOnlyRatings = document.querySelector('.read-only-ratings'),
  8. customSvg = document.querySelector('.custom-svg-ratings'),
  9. halfStar = document.querySelector('.half-star-ratings'),
  10. onSetEvents = document.querySelector('.onset-event-ratings'),
  11. onChangeEvents = document.querySelector('.onChange-event-ratings'),
  12. ratingMethods = document.querySelector('.methods-ratings'),
  13. initializeRatings = document.querySelector('.btn-initialize'),
  14. destroyRatings = document.querySelector('.btn-destroy'),
  15. getRatings = document.querySelector('.btn-get-rating'),
  16. setRatings = document.querySelector('.btn-set-rating'),
  17. iconStar = document.querySelector('.icon-star-ratings'),
  18. iconStarSm = document.querySelector('.icon-star-sm-ratings'),
  19. iconStarMd = document.querySelector('.icon-star-md-ratings'),
  20. iconStarLg = document.querySelector('.icon-star-lg-ratings'),
  21. iconStarPrimary = document.querySelector('.icon-star-primary-ratings'),
  22. iconStarWarning = document.querySelector('.icon-star-warning-ratings'),
  23. iconStarSuccess = document.querySelector('.icon-star-success-ratings'),
  24. iconStarDanger = document.querySelector('.icon-star-danger-ratings');
  25. let r = parseInt(window.Helpers.getCssVar('gray-200', true).slice(1, 3), 16);
  26. let g = parseInt(window.Helpers.getCssVar('gray-200', true).slice(3, 5), 16);
  27. let b = parseInt(window.Helpers.getCssVar('gray-200', true).slice(5, 7), 16);
  28. const halfStarGray = window.Helpers.getCssVar('gray-200', true).replace('#', '%23');
  29. const halfStarGradient = isRtl
  30. ? "%3Cstop offset='50%25' style='stop-color:" +
  31. halfStarGray +
  32. "' /%3E%3Cstop offset='50%25' style='stop-color:%23FFD700' /%3E"
  33. : "%3Cstop offset='50%25' style='stop-color:%23FFD700' /%3E%3Cstop offset='50%25' style='stop-color:" +
  34. halfStarGray +
  35. "' /%3E";
  36. const fullStarSVG =
  37. "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='16' %3E%3Cpath fill='%23FFD700' d='M21.947 9.179a1 1 0 0 0-.868-.676l-5.701-.453l-2.467-5.461a.998.998 0 0 0-1.822-.001L8.622 8.05l-5.701.453a1 1 0 0 0-.619 1.713l4.213 4.107l-1.49 6.452a1 1 0 0 0 1.53 1.057L12 18.202l5.445 3.63a1.001 1.001 0 0 0 1.517-1.106l-1.829-6.4l4.536-4.082c.297-.268.406-.686.278-1.065'/%3E%3C/svg%3E";
  38. const halfStarSVG = `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cdefs%3E%3ClinearGradient id='halfStarGradient'%3E${halfStarGradient}%3C/linearGradient%3E%3C/defs%3E%3Cpath fill='url(%23halfStarGradient)' d='M21.947 9.179a1 1 0 0 0-.868-.676l-5.701-.453l-2.467-5.461a.998.998 0 0 0-1.822-.001L8.622 8.05l-5.701.453a1 1 0 0 0-.619 1.713l4.213 4.107l-1.49 6.452a1 1 0 0 0 1.53 1.057L12 18.202l5.445 3.63a1.001 1.001 0 0 0 1.517-1.106l-1.829-6.4l4.536-4.082c.297-.268.406-.686.278-1.065'/%3E%3C/svg%3E`;
  39. const emptyStarSVG =
  40. "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='16' %3E%3Cpath fill='rgb(" +
  41. r +
  42. ',' +
  43. g +
  44. ',' +
  45. b +
  46. ")' d='M21.947 9.179a1 1 0 0 0-.868-.676l-5.701-.453l-2.467-5.461a.998.998 0 0 0-1.822-.001L8.622 8.05l-5.701.453a1 1 0 0 0-.619 1.713l4.213 4.107l-1.49 6.452a1 1 0 0 0 1.53 1.057L12 18.202l5.445 3.63a1.001 1.001 0 0 0 1.517-1.106l-1.829-6.4l4.536-4.082c.297-.268.406-.686.278-1.065'/%3E%3C/svg%3E";
  47. // Basic Ratings
  48. // --------------------------------------------------------------------
  49. if (basicRatings) {
  50. let ratings = new Raty(basicRatings, {
  51. starOn: fullStarSVG,
  52. starHalf: halfStarSVG,
  53. starOff: emptyStarSVG
  54. });
  55. ratings.init();
  56. }
  57. // Read Only Ratings
  58. // --------------------------------------------------------------------
  59. if (readOnlyRatings) {
  60. let ratings = new Raty(readOnlyRatings, {
  61. number: 5,
  62. starOn: fullStarSVG,
  63. starHalf: halfStarSVG,
  64. starOff: emptyStarSVG
  65. });
  66. ratings.init();
  67. }
  68. // custom-svg icons
  69. const customFullStarSVG =
  70. "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23FFD700' d='m6.516 14.323l-1.49 6.452a.998.998 0 0 0 1.529 1.057L12 18.202l5.445 3.63a1.001 1.001 0 0 0 1.517-1.106l-1.829-6.4l4.536-4.082a1 1 0 0 0-.59-1.74l-5.701-.454l-2.467-5.461a.998.998 0 0 0-1.822 0L8.622 8.05l-5.701.453a1 1 0 0 0-.619 1.713zm2.853-4.326a1 1 0 0 0 .832-.586L12 5.43l1.799 3.981a1 1 0 0 0 .832.586l3.972.315l-3.271 2.944c-.284.256-.397.65-.293 1.018l1.253 4.385l-3.736-2.491a.995.995 0 0 0-1.109 0l-3.904 2.603l1.05-4.546a1 1 0 0 0-.276-.94l-3.038-2.962z'/%3E%3C/svg%3E";
  71. const customEmptyStarSVG =
  72. "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='rgb(" +
  73. r +
  74. ',' +
  75. g +
  76. ',' +
  77. b +
  78. ")' d='m6.516 14.323l-1.49 6.452a.998.998 0 0 0 1.529 1.057L12 18.202l5.445 3.63a1.001 1.001 0 0 0 1.517-1.106l-1.829-6.4l4.536-4.082a1 1 0 0 0-.59-1.74l-5.701-.454l-2.467-5.461a.998.998 0 0 0-1.822 0L8.622 8.05l-5.701.453a1 1 0 0 0-.619 1.713zm2.853-4.326a1 1 0 0 0 .832-.586L12 5.43l1.799 3.981a1 1 0 0 0 .832.586l3.972.315l-3.271 2.944c-.284.256-.397.65-.293 1.018l1.253 4.385l-3.736-2.491a.995.995 0 0 0-1.109 0l-3.904 2.603l1.05-4.546a1 1 0 0 0-.276-.94l-3.038-2.962z'/%3E%3C/svg%3E";
  79. // Custom SVG Ratings
  80. // --------------------------------------------------------------------
  81. if (customSvg) {
  82. let ratings = new Raty(customSvg, {
  83. starOn: customFullStarSVG,
  84. starOff: customEmptyStarSVG
  85. });
  86. ratings.init();
  87. }
  88. // Half Star Ratings
  89. // --------------------------------------------------------------------
  90. if (halfStar) {
  91. let ratings = new Raty(halfStar, {
  92. starOn: fullStarSVG,
  93. starHalf: halfStarSVG,
  94. starOff: emptyStarSVG,
  95. rtl: isRtl
  96. });
  97. ratings.init();
  98. }
  99. // Ratings Events
  100. // --------------------------------------------------------------------
  101. // onSet Event
  102. if (onSetEvents) {
  103. let ratings = new Raty(onSetEvents, {
  104. starOn: fullStarSVG,
  105. starHalf: halfStarSVG,
  106. starOff: emptyStarSVG,
  107. click: function (score) {
  108. alert('The rating is set to ' + score + ' !');
  109. }
  110. });
  111. ratings.init();
  112. }
  113. // onChange Event
  114. if (onChangeEvents) {
  115. let ratings = new Raty(onChangeEvents, {
  116. starOn: fullStarSVG,
  117. starHalf: halfStarSVG,
  118. starOff: emptyStarSVG,
  119. half: true,
  120. mouseover: function (score) {
  121. const counterElement = onChangeEvents.parentElement.querySelector('.counter');
  122. if (counterElement) {
  123. counterElement.textContent = (Math.round(score * 2) / 2).toFixed(1);
  124. }
  125. },
  126. mouseout: function () {
  127. const counterElement = onChangeEvents.parentElement.querySelector('.counter');
  128. if (counterElement) {
  129. const currentScore = ratings.score() || 0;
  130. counterElement.textContent = (Math.round(currentScore * 2) / 2).toFixed(1);
  131. }
  132. }
  133. });
  134. const initialCounter = onChangeEvents.parentElement.querySelector('.counter');
  135. if (initialCounter) {
  136. initialCounter.textContent = '0.0';
  137. }
  138. ratings.init();
  139. }
  140. // Ratings Methods
  141. // --------------------------------------------------------------------
  142. let currentScore = 0;
  143. let ratingInstance = null;
  144. // Initialize rating
  145. function initializeRating(score = currentScore) {
  146. if (!ratingInstance) {
  147. ratingInstance = new Raty(ratingMethods, {
  148. starOn: fullStarSVG,
  149. starHalf: halfStarSVG,
  150. starOff: emptyStarSVG,
  151. click: function (newScore) {
  152. currentScore = newScore;
  153. },
  154. score: score,
  155. readOnly: false,
  156. rtl: isRtl
  157. });
  158. ratingInstance.init();
  159. }
  160. }
  161. initializeRating(currentScore);
  162. // Destroy rating
  163. function destroyRating() {
  164. if (ratingInstance) {
  165. ratingMethods.innerHTML = '';
  166. ratingInstance = null;
  167. currentScore = 0;
  168. } else {
  169. alert('Please Initialize Ratings First');
  170. }
  171. }
  172. // Get Current Rating
  173. function getRating() {
  174. if (ratingInstance) {
  175. alert('Current Ratings are ' + currentScore);
  176. } else {
  177. alert('Please Initialize Ratings First');
  178. }
  179. }
  180. function setRating(score) {
  181. if (ratingInstance) {
  182. destroyRating();
  183. initializeRating(score);
  184. currentScore = score;
  185. } else {
  186. alert('Please Initialize Ratings First');
  187. }
  188. }
  189. if (initializeRatings) {
  190. initializeRatings.addEventListener('click', () => initializeRating(currentScore));
  191. }
  192. if (destroyRatings) {
  193. destroyRatings.addEventListener('click', destroyRating);
  194. }
  195. if (getRatings) {
  196. getRatings.addEventListener('click', getRating);
  197. }
  198. if (setRatings) {
  199. setRatings.addEventListener('click', () => setRating(1));
  200. }
  201. // icon Star Ratings
  202. // --------------------------------------------------------------------
  203. if (iconStar) {
  204. let ratings = new Raty(iconStar, {
  205. starType: 'i',
  206. starOff: 'icon-base icon-xl bx bxs-heart bg-danger-subtle',
  207. starOn: 'icon-base icon-xl bx bxs-heart text-danger'
  208. });
  209. ratings.init();
  210. }
  211. // size variant star Ratings
  212. // --------------------------------------------------------------------
  213. if (iconStarSm) {
  214. let ratings = new Raty(iconStarSm, {
  215. starType: 'i',
  216. starOff: 'icon-base bx bxs-star bg-secondary-subtle',
  217. starOn: 'icon-base bx bxs-star text-secondary'
  218. });
  219. ratings.init();
  220. }
  221. if (iconStarMd) {
  222. let ratings = new Raty(iconStarMd, {
  223. starType: 'i',
  224. starOff: 'icon-base icon-xl bx bxs-star bg-secondary-subtle',
  225. starOn: 'icon-base icon-xl bx bxs-star text-secondary'
  226. });
  227. ratings.init();
  228. }
  229. if (iconStarLg) {
  230. let ratings = new Raty(iconStarLg, {
  231. starType: 'i',
  232. starOff: 'icon-base icon-42px bx bxs-star bg-secondary-subtle',
  233. starOn: 'icon-base icon-42px bx bxs-star text-secondary'
  234. });
  235. ratings.init();
  236. }
  237. // color variant star Ratings
  238. // --------------------------------------------------------------------
  239. if (iconStarPrimary) {
  240. let ratings = new Raty(iconStarPrimary, {
  241. starType: 'i',
  242. starOff: 'icon-base icon-xl bx bxs-star bg-primary-subtle',
  243. starOn: 'icon-base icon-xl bx bxs-star text-primary'
  244. });
  245. ratings.init();
  246. }
  247. if (iconStarWarning) {
  248. let ratings = new Raty(iconStarWarning, {
  249. starType: 'i',
  250. starOff: 'icon-base icon-xl bx bxs-star bg-warning-subtle',
  251. starOn: 'icon-base icon-xl bx bxs-star text-warning'
  252. });
  253. ratings.init();
  254. }
  255. if (iconStarSuccess) {
  256. let ratings = new Raty(iconStarSuccess, {
  257. starType: 'i',
  258. starOff: 'icon-base icon-xl bx bxs-star bg-success-subtle',
  259. starOn: 'icon-base icon-xl bx bxs-star text-success'
  260. });
  261. ratings.init();
  262. }
  263. if (iconStarDanger) {
  264. let ratings = new Raty(iconStarDanger, {
  265. starType: 'i',
  266. starOff: 'icon-base icon-xl bx bxs-star bg-danger-subtle',
  267. starOn: 'icon-base icon-xl bx bxs-star text-danger'
  268. });
  269. ratings.init();
  270. }
  271. });