PageSourceSearch

https://keystroke.net/media/com_hikashop/js/vote.js

js keystroke.net collected 2026-10-09 23:29:32 UTC 13,324 bytes, 440 lines download raw bytes

1/**
2 * @package    HikaShop for Joomla!
3 * @version    5.1.2
4 * @author     hikashop.com
5 * @copyright  (C) 2010-2024 HIKARI SOFTWARE. All rights reserved.
6 * @license    GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
7 */
8var hikaVote = function(el,opt) {
9	this.init(el,opt);
10};
11hikaVote.options = {};
12hikaVote.setOptions = function(opts) {
13	for(var opt in opts) {
14		if(!opts.hasOwnProperty(opt))
15			continue;
16		hikaVote.options[opt] = opts[opt];
17	}
18};
19hikaVote.updateVote = function(type, ref_id, value, tooltip) {
20	for(var i = window.hikaVotes.length - 1; i >= 0; i--) {
21		if(window.hikaVotes[i].type != type || window.hikaVotes[i].ref_id != ref_id)
22			continue;
23		window.hikaVotes[i].setRating(value, tooltip);
24	}
25};
26hikaVote.vote = function(val, from){
27	var d = document,
28		re = new RegExp('id_(.*?)_hikashop'),
29		m = re.exec(from),
30		ref_id = null,
31		infos = false;
32
33	if(m != null) {
34		ref_id = '';
35		for (i = 1; i < m.length; i++) {
36			ref_id = ref_id + m[i] + "\n";
37		}
38	} else {
39		infos = d.querySelector("[data-ref][id=\"hikashop_vote_rating_id\"]");
40		ref_id = infos.getAttribute("data-ref");
41		if(!val && infos.value) {
42			val = infos.value;
43		}
44	}
45
46	if(!infos) {
47		infos = d.querySelectorAll("input[data-ref=\""+parseInt(ref_id)+"\"]:not([id=\"hikashop_vote_rating_id\"])");
48		infos = infos[0];
49	}
50
51	var el = null;
52	if(from == "hikashop_vote_rating_id") {
53		el = d.getElementById("hikashop_vote_status_form");
54	} else {
55		el = d.getElementById("hikashop_vote_status_"+parseInt(ref_id));
56	}
57
58	var hikashop_vote_comment = "", pseudo_comment = 0, email_comment = 0, recaptcha_comment = '';
59	if(d.getElementById("hikashop_vote_comment")) {
60		hikashop_vote_comment = d.getElementById("hikashop_vote_comment").value;
61
62		// don't submit the votes alone when the vote and comment are connected
63		if(hikaVote.options.both == '1' && hikashop_vote_comment == '' && val != 0)
64			return;
65		pseudo_comment = d.getElementById("pseudo_comment").value;
66		email_comment = d.getElementById("email_comment").value;
67		if(d.getElementById("g-recaptcha-response")){
68			recaptcha_comment = d.getElementById("g-recaptcha-response").value;
69		}
70	}
71
72	var type = infos.getAttribute("data-votetype"), comment_task = 0;
73	if(hikaVote.options.both == '1' || hikashop_vote_comment != '' || (val == 0 && infos.id == "hikashop_vote_rating_id")) {
74		comment_task = 1;
75	}
76
77	data = "vote_type=" + encodeURIComponent(type) + "&hikashop_vote_type=vote" +
78		"&hikashop_vote_ref_id=" + parseInt(ref_id) +
79		"&hikashop_vote=" + parseInt(val) +
80		"&hikashop_vote_comment=" + encodeURIComponent(hikashop_vote_comment) +
81		"&email_comment=" + encodeURIComponent(email_comment) +
82		"&pseudo_comment=" + encodeURIComponent(pseudo_comment) +
83		"&recaptcha_comment=" + encodeURIComponent(recaptcha_comment);
84	window.Oby.xRequest(hikaVote.options.urls.save, {mode: "POST", data: data}, function(xhr) {
85		response = window.Oby.evalJSON(xhr.response);
86		if(response.error) {
87			el.innerHTML = response.error.message;
88			return;
89		}
90
91		if(!response.success)
92			return;
93
94		el.innerHTML = response.success.message;
95		setTimeout(function(){ el.innerHTML = ''; }, 3500);
96		if(comment_task) {
97			// Clear the comment textarea
98			d.getElementById('hikashop_vote_comment').value = '';
99
100			// Call a function to refresh the "vote / listing" only
101			var section = d.getElementById("hikashop_vote_listing");
102			if(!section)
103				section = d.getElementById("hikashop_product_vote_listing");
104			if(!section)
105				return;
106
107			data = "data_id="+parseInt(ref_id);
108			data += "&main_ctrl="+String(type);
109			window.Oby.xRequest(hikaVote.options.urls.show, {mode: "POST", data: data}, function(xhr) {
110				section.innerHTML = xhr.response;
111				if(hkjQuery().chosen && hkjQuery('#' + section.id + ' select')) {
112					hkjQuery('#' + section.id + ' select').chosen();
113				}
114			});
115		}
116		if(response.values) {
117			// type / ref_id / value / tooltip
118			window.hikaVote.updateVote(String(type), parseInt(ref_id), parseInt(response.values.rounded), String(response.tooltip));
119		}
120	});
121};
122hikaVote.useful = function(vote_id, val) {
123	var section = document.getElementById("hikashop_vote_listing"),
124		type = 'product';
125	if(section) {
126		type = section.getAttribute('data-votetype');
127	} else {
128		section = document.getElementById("hikashop_product_vote_listing");
129	}
130	if(!section)
131		return;
132	var el = document.getElementById(vote_id);
133	data = "hikashop_vote_type=useful&value=" + parseInt(val) + "&hikashop_vote_id=" + parseInt(vote_id) + "&vote_type=" + encodeURIComponent(type);
134	window.Oby.xRequest(hikaVote.options.urls.save, {mode: "POST", data: data}, function(xhr) {
135		response = window.Oby.evalJSON(xhr.response);
136		if(response.error)
137			el.innerHTML = response.error.message;
138		else if(response.success)
139			el.innerHTML = response.success.message;
140	});
141
142	data = "data_id=" + parseInt(hikaVote.options.itemId) + "&main_ctrl=" + encodeURIComponent(type) + "&content_type=listing";
143	window.Oby.xRequest(hikaVote.options.urls.show, {mode: "POST", data: data}, function(xhr) {
144		setTimeout(function(){
145			section.innerHTML = xhr.response;
146			if(hkjQuery().chosen && hkjQuery('#' + section.id + ' select')) {
147				hkjQuery('#' + section.id + ' select').chosen();
148			}
149		}, 5000);
150	});
151};
152
153hikaVote.prototype = {
154	options : {},
155	selectBox: null,
156	container: null,
157	max: null,
158	cb: null,
159	type: null,
160	ref_id: null,
161	/**
162	 *
163	 */
164	init: function(el, opt, cb) {
165		var t = this, d= document;
166		t.setOptions(opt);
167
168		this.options.style = 'star';
169		if(el.getAttribute('data-votestyle'))
170			this.options.style = el.getAttribute('data-votestyle');
171
172		if(typeof(el) == 'string')
173			t.selectBox = d.getElementById(el);
174		else
175			t.selectBox = el;
176
177		if(el.voteInit)
178			return;
179
180		if(!t.options.showSelectBox && t.selectBox && t.selectBox.nodeName.toLowerCase() == 'select' && typeof(jQuery) != 'undefined' && jQuery().chosen) {
181			setTimeout(function(){
182				var id = selectBox.getAttribute('id') + '_chzn';
183				if(d.getElementById(id) != null) {
184					jQuery(id).detach();
185					try{ jQuery(id+'-chzn').remove(); }catch(e){}
186				}
187			}, 50);
188		}
189
190		// set the container
191		t.setContainer();
192		// add stars
193		var max = t.selectBox.getAttribute('data-max');
194		if(max) {
195			try{ parseInt(max); } catch(e) { t.max = max; }
196			for(var i = 1; i <= max; i++)
197				t.createStar(null, i);
198		} else {
199			var elems = t.selectBox.getElementsByTagName('option');
200			t.max = 0;
201			if(elems && elems.length) {
202				for(var i = 0; i <= elems.length; i++) {
203					t.createStar(elems[i]);
204					if(elems[i].value > t.max)
205						t.max = elems[i].value;
206				}
207			}
208		}
209
210		t.cb = cb || null;
211
212		t.addEvent(t.container, 'mouseover', function(e) { t.mouseOver(e); });
213		t.addEvent(t.container, 'mouseout', function(e) { t.mouseOut(e); });
214		t.addEvent(t.container, 'click', function(e) { t.click(e); });
215
216		// bind change event for selectbox if shown
217		if (t.options.showSelectBox)
218			t.addEvent(t.selectBox, 'change', t.change);
219
220		// set the initial rating
221		t.setRating(t.options.defaultRating);
222
223		el.voteInit = true;
224	},
225	setOptions: function(opt) {
226		var t = this;
227		t.options.showSelectBox = opt.showSelectBox || false;
228		t.options.container = opt.container || null;
229		t.options.defaultRating = opt.defaultRating || null;
230		t.options.id = opt.id || 'hikashop_vote_';
231		t.type = opt.type;
232		t.ref_id = opt.ref_id;
233	},
234	setContainer: function() {
235		var t = this, d = document;
236		if(d.getElementById(t.options.container)) {
237			t.container = d.getElementById(t.options.container);
238			return;
239		}
240		t.createContainer();
241	},
242	createContainer: function() {
243		var t = this, d = document;
244		t.container = d.createElement('div');
245		t.container.className = 'hk-rating';
246		t.container.setAttribute('data-toggle', 'hk-tooltip');
247		t.container.setAttribute('data-original-title', t.selectBox.getAttribute('data-original-title'));
248		if(t.selectBox.nextSibling)
249			t.selectBox.parentNode.insertBefore(t.container, t.selectBox.nextSibling);
250		else
251			t.selectBox.parentNode.appendChild(t.container);
252	},
253	reset: function() {
254		if(t.container)
255			t.container.parentNode.removeChild(t.container);
256		t.createContainer();
257	},
258	createStar: function(el, value) {
259		var t = this, d = document;
260		if(el) value = el.getAttribute('value');
261		var e = d.createElement('a');
262		e.id = t.options.id + '_' + value;
263		e.className = 'hk-rate-' + this.options.style + ' state-empty';
264		e.title = '' + value;
265		e.value = value;
266
267		t.container.appendChild(e);
268	},
269	mouseOver: function(e) {
270		var t = this, d = document;
271		if(!e.target)
272			e.target = e.srcElement;
273		if(!e.target)
274			return;
275		el = e.target;
276		if(typeof(el) == 'string')
277			el = d.getElementById(el);
278		if(!el)
279			return;
280		t.addClass(el, 'state-hover');
281		var c = el.previousSibling;
282		while(c) {
283			t.addClass(c, 'state-hover');
284			c = c.previousSibling;
285		}
286	},
287	mouseOut: function(e) {
288		var t = this, d = document, el = null;
289		if(!e.target)
290			e.target = e.srcElement;
291
292		if(!e.target)
293			return;
294		el = e.target;
295		if(typeof(el) == 'string')
296			el = d.getElementById(el);
297		if(!el)
298			return;
299		t.removeClass(el, 'state-hover');
300
301		var c = el.previousSibling;
302		while(c) {
303			t.removeClass(c, 'state-hover');
304			c = c.previousSibling;
305		}
306	},
307	click: function(e) {
308		var t = this, d = document;
309		if (!e.target)
310			e.target = e.srcElement;
311		var rating = e.target.getAttribute('title').replace('', ''),
312			from = t.selectBox.getAttribute('id');
313		t.setRating(rating);
314		t.selectBox.value = rating;
315		// Send the id of the view which send the vote ( mini / form )
316		if(hikashop_send_vote){
317			var el = d.getElementById('hikashop_vote_rating_id');
318			if(el) el.value = rating;
319			hikashop_send_vote(rating, from);
320		}
321		if(t.cb)
322			t.cb(rating, from);
323	},
324	change: function(e) {
325		var t = this, d = document, rating = null;
326			el = d.getElementById(e.target);
327		if(!el) return;
328		t.setRating(el.value);
329	},
330	setRating: function(rating, tooltip) {
331		var t = this, d = document;
332		// use selected rating if none supplied
333		if (!rating) {
334			rating = t.selectBox.getAttribute('value');
335			// use first rating option if none selected
336			if(!rating)
337				rating = 0;
338		}
339		// get the current selected rating star
340//		var current = t.container.getElement('a[title=' + rating + ']');
341		var e = null, current = null, elements = t.container.getElementsByTagName('a');
342		for(var i = elements.length - 1; i >= 0; i--) {
343			e = elements[i];
344			if(e && e.title && e.title == rating) {
345				current = e;
346				break;
347			}
348		}
349
350		// highlight current and previous stars in yellow
351		if(current && rating != 0) {
352			current.className = 'hk-rate-'+this.options.style+' state-full';
353			var c = current.previousSibling;
354			while(c) {
355				c.className = 'hk-rate-'+this.options.style+' state-full';
356				c = c.previousSibling;
357			}
358
359			// remove highlight from higher ratings
360			var c = current.nextSibling;
361			while(c) {
362				c.className = 'hk-rate-'+this.options.style+' state-empty';
363				c = c.nextSibling;
364			}
365		}
366		// synchronize the rate with the selectbox
367		t.selectBox.value = rating;
368
369		if(!tooltip)
370			return;
371
372		// update the tooltip
373		t.container.setAttribute('data-original-title', tooltip);
374	},
375	addEvent : function(d,e,f) {
376		if( d.attachEvent )
377			d.attachEvent('on' + e, f);
378		else if (d.addEventListener)
379			d.addEventListener(e, f, false);
380		else
381			d['on' + e] = f;
382		return f;
383	},
384	hasClass : function(o,n) {
385		if(o.className == '' ) return false;
386		var reg = new RegExp("(^|\\s+)"+n+"(\\s+|$)");
387		return reg.test(o.className);
388	},
389	addClass : function(o,n) {
390		if(o.className == '')
391			o.className = n;
392		else if(!this.hasClass(o,n))
393			o.className += ' '+n;
394	},
395	trim : function(s) {
396		return (s ? '' + s : '').replace(/^\s*|\s*$/g, '');
397	},
398	removeClass : function(e, c) {
399		var t = this;
400		if( e.className != '' && t.hasClass(e,c) ) {
401			var cn = ' ' + e.className + ' ';
402			e.className = t.trim(cn.replace(' '+c+' ',' '));
403		}
404	}
405};
406
407var initVote = function(mainDiv){
408	var d = document, el = null, r = null, voteContainers = null;
409	if(mainDiv)
410		voteContainers = mainDiv.querySelectorAll('input[name=hikashop_vote_rating]');
411	else
412		voteContainers = d.getElementsByName('hikashop_vote_rating');
413	if(voteContainers.length == 0)
414		return;
415	for(var i=0; i < voteContainers.length; i++) {
416		el = d.getElementById(voteContainers[i].id);
417		if(!el || !el.getAttribute("data-votetype"))
418			continue;
419		r = new hikaVote(el, {
420			id : 'hikashop_vote_rating_'+el.getAttribute("data-votetype")+'_'+el.getAttribute("data-ref"),
421			showSelectBox : false,
422			container : null,
423			defaultRating :  el.getAttribute("data-rate"),
424			type : el.getAttribute("data-votetype"),
425			ref_id : el.getAttribute("data-ref"),
426		});
427		if(r.container)
428			window.hikaVotes.push(r);
429	}
430	el = d.getElementById('hikashop_vote_rating_id');
431	if(el) el.value = '0';
432};
433
434if(!window.hikaVotes)
435	window.hikaVotes = [];
436
437/* Vote initialization */
438window.hikashop.ready(function(){
439	initVote();
440});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.