@lemonadejs/rating 2.3.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -14,15 +14,29 @@ declare namespace Rating {
14
14
  name?: string;
15
15
  /** Current value */
16
16
  value?: number;
17
+ /** Tooltips displayed on hover */
18
+ tooltip?: [] | string;
19
+ /** Size */
20
+ size?: 'default' | 'small';
17
21
  /** Onchange Event */
18
- onchange?: (newValue: number) => void;
22
+ onchange?: (instance: Instance, newValue: number) => void;
19
23
  }
20
24
 
21
25
  interface Instance {
22
26
  /** Number of stars */
23
27
  number: number;
28
+ /** Form element name */
29
+ name?: string;
24
30
  /** Current value */
25
31
  value: number;
32
+ /** Tooltips displayed on hover */
33
+ tooltip?: [] | string;
34
+ /** Size */
35
+ size?: 'default' | 'small';
36
+ /** Gets the current value */
37
+ getValue: () => number;
38
+ /** Sets the value */
39
+ setValue: (index: number) => void;
26
40
  }
27
41
  }
28
42
 
package/dist/index.js CHANGED
@@ -8,16 +8,48 @@ if (!lemonade && typeof (require) === 'function') {
8
8
  global.Rating = factory();
9
9
  }(this, (function () {
10
10
 
11
- const Rating = function() {
11
+ class CustomEvents extends Event {
12
+ constructor(type, props, options) {
13
+ super(type, {
14
+ bubbles: true,
15
+ composed: true,
16
+ ...options,
17
+ });
18
+
19
+ if (props) {
20
+ for (const key in props) {
21
+ // Avoid assigning if property already exists anywhere on `this`
22
+ if (! (key in this)) {
23
+ this[key] = props[key];
24
+ }
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ // Dispatcher
31
+ const Dispatch = function(method, type, options) {
32
+ // Try calling the method directly if provided
33
+ if (typeof method === 'function') {
34
+ let a = Object.values(options);
35
+ return method(...a);
36
+ } else if (this.tagName) {
37
+ this.dispatchEvent(new CustomEvents(type, options));
38
+ }
39
+ }
40
+
41
+ const Rating = function(children, { onchange, onload }) {
12
42
  let self = this;
13
43
 
44
+ // Event
45
+ let change = self.onchange;
46
+ self.onchange = null;
47
+
14
48
  if (! self.number) {
15
49
  self.number = 5;
16
50
  }
17
- self.stars = [];
18
51
 
19
- // Event
20
- let change = self.onchange;
52
+ self.stars = [];
21
53
 
22
54
  // Current self star
23
55
  let current = null;
@@ -25,7 +57,7 @@ if (!lemonade && typeof (require) === 'function') {
25
57
  /**
26
58
  * Update the number of stars
27
59
  */
28
- const len = function() {
60
+ const len = function () {
29
61
  // Remove stars
30
62
  if (self.number < self.stars.length) {
31
63
  self.stars.splice(self.number, self.stars.length);
@@ -37,66 +69,105 @@ if (!lemonade && typeof (require) === 'function') {
37
69
  for (let i = 0; i < self.number; i++) {
38
70
  if (! self.stars[i]) {
39
71
  self.stars[i] = {
40
- icon: 'star_outline',
72
+ icon: 'star',
41
73
  };
74
+ if (self.tooltip[i]) {
75
+ self.stars[i].title = self.tooltip[i];
76
+ }
42
77
  }
43
78
  }
44
79
  // Refresh
45
80
  self.refresh('stars');
46
81
  }
47
82
 
48
- const val = function() {
49
- // Update value
50
- if (self.value > 0) {
51
- let t = self.stars[self.value-1];
52
- if (t) {
53
- self.click(null, t);
83
+ const val = function (index, events) {
84
+ if (typeof(index) === 'string') {
85
+ index = Number(index);
86
+ }
87
+ // Toggle value
88
+ if (index === current) {
89
+ index = 0;
90
+ }
91
+ // Apply value to the selected property in each star
92
+ for (let i = 0; i < self.number; i++) {
93
+ self.stars[i].selected = i <= index - 1 ? 1 : 0;
94
+ }
95
+ // Keep current value
96
+ current = index;
97
+ // Dispatch methdo
98
+ if (events !== false) {
99
+ Dispatch.call(self, change, 'change', {
100
+ instance: self,
101
+ value: index,
102
+ });
103
+ }
104
+ }
105
+
106
+ const click = function(e, s) {
107
+ let index = self.stars.indexOf(s) + 1;
108
+ val(index);
109
+ }
110
+
111
+ const mouseover = function(e, s) {
112
+ let index = self.stars.indexOf(s);
113
+ for (let i = 0; i < self.number; i++) {
114
+ if (i <= index) {
115
+ self.stars[i].hover = 1;
116
+ } else {
117
+ self.stars[i].hover = 0;
54
118
  }
55
119
  }
56
120
  }
57
121
 
58
- self.onchange = function(prop) {
122
+ const mouseout = function(e, s) {
123
+ for (let i = 0; i < self.number; i++) {
124
+ self.stars[i].hover = 0;
125
+ }
126
+ }
127
+
128
+ onchange((prop) => {
59
129
  if (prop === 'number') {
60
130
  len();
61
131
  } else if (prop === 'value') {
62
- val();
132
+ val(self.value);
133
+ } else if (prop === 'tooltip') {
134
+ if (typeof(self.tooltip) === 'string') {
135
+ self.tooltip = self.tooltip.split(',')
136
+ }
137
+ len();
63
138
  }
64
- }
139
+ })
65
140
 
66
- self.onload = function() {
141
+ onload(() => {
67
142
  // Bind global method to be compatible with LemonadeJS forms
68
- self.el.val = function(v) {
69
- if (typeof(v) === 'undefined') {
143
+ self.el.val = function (v) {
144
+ if (typeof (v) === 'undefined') {
70
145
  return self.value;
71
146
  } else {
72
147
  self.value = v;
73
148
  }
74
149
  }
75
150
 
151
+ if (self.tooltip && typeof(self.tooltip) === 'string') {
152
+ self.tooltip = self.tooltip.split(',')
153
+ } else {
154
+ self.tooltip = '';
155
+ }
76
156
  len();
77
- val();
78
- }
157
+ // Ignore events
158
+ val(self.value, false);
159
+ });
79
160
 
80
- self.click = function(e, s) {
81
- if (! s.selected) {
82
- current = null;
83
- }
84
- let index = self.stars.indexOf(s);
85
- for (let i = 0; i < self.number; i++) {
86
- let selected = i <= index && s !== current ? 1 : 0;
87
- self.stars[i].selected = selected;
88
- self.stars[i].el.style.color = selected ? 'red' : '';
89
- self.stars[i].icon = selected ? 'star' : 'star_outline';
90
- }
91
- current = s;
161
+ self.getValue = function () {
162
+ return Number(self.value);
163
+ }
92
164
 
93
- if (typeof(change) === 'function') {
94
- change(index+1, s);
95
- }
165
+ self.setValue = function (index) {
166
+ self.value = index;
96
167
  }
97
168
 
98
- return `<div value="{{self.value}}" number="{{self.number}}" name="{{self.name}}" :loop="self.stars" :ref="self.component" style="cursor: pointer">
99
- <i class="material-icons" onclick="self.parent.click">{{self.icon}}</i>
169
+ return render => render`<div class="lm-rating" value="{{self.value}}" number="{{self.number}}" name="{{self.name}}" data-size="{{self.size}}" :loop="self.stars" :ref="self.component" onmouseout="${mouseout}">
170
+ <i class="material-symbols-outlined material-icons" data-selected="{{self.selected}}" data-hover="{{self.hover}}" title="{{self.title}}" onclick="${click}" onmouseover="${mouseover}">star</i>
100
171
  </div>`;
101
172
  }
102
173
 
package/dist/style.css ADDED
@@ -0,0 +1,18 @@
1
+ .lm-rating i {
2
+ color: lightgray;
3
+ cursor: pointer;
4
+ user-select: none;
5
+ -webkit-user-drag: none;
6
+ }
7
+
8
+ .lm-rating[data-size="small"] i {
9
+ font-size: 18px;
10
+ }
11
+
12
+ .lm-rating i[data-hover="1"] {
13
+ color: gray;
14
+ }
15
+
16
+ .lm-rating i[data-selected="1"] {
17
+ color: red;
18
+ }
package/package.json CHANGED
@@ -15,9 +15,9 @@
15
15
  "javascript plugins"
16
16
  ],
17
17
  "dependencies": {
18
- "lemonadejs": "^4.3.3"
18
+ "lemonadejs": "^5.2.0"
19
19
  },
20
20
  "main": "dist/index.js",
21
21
  "types": "dist/index.d.ts",
22
- "version": "2.3.0"
22
+ "version": "5.2.0"
23
23
  }