summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/ratings/ratings.js
blob: 9299d8d87180c46d2da49d3654b2b3c67bae643f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Prado.WebUI.TRatingList = Base.extend(
{
	selectedIndex : -1,
	enabled : true,
	readOnly : false,

	constructor : function(options)
	{
		var cap = $(options.CaptionID);
		this.options = Object.extend(
		{
			caption : cap ? cap.innerHTML : ''
		}, options || {});

		Prado.WebUI.TRatingList.register(this);
		this._init();
		this.selectedIndex = options.SelectedIndex;
		this.setRating(this.selectedIndex);
	},

	_init: function(options)
	{
		Element.addClassName($(this.options.ListID),this.options.Style);
		this.radios = new Array();
		var index=0;
		for(var i = 0; i<this.options.ItemCount; i++)
		{
			var radio = $(this.options.ListID+'_c'+i);
			var td = radio.parentNode;
			if(radio && td.tagName.toLowerCase()=='td')
			{
				this.radios.push(radio);
				Event.observe(td, "mouseover", this.hover.bindEvent(this,index));
				Event.observe(td, "mouseout", this.recover.bindEvent(this,index));
				Event.observe(td, "click", this.click.bindEvent(this, index));
				index++;
				Element.addClassName(td,"rating");
			}
		}
	},

	hover : function(ev,index)
	{
		if(this.enabled==false) return;
		for(var i = 0; i<this.radios.length; i++)
		{
			var action = i <= index ? 'addClassName' : 'removeClassName'
			Element[action](this.radios[i].parentNode,"rating_selected");
		}
		this.setCaption(index);
	},

	recover : function(ev,index)
	{
		if(this.enabled==false) return;
		for(var i = 0; i<=index; i++)
			Element.removeClassName(this.radios[i].parentNode, "rating_selected");
		this.setRating(this.selectedIndex);
	},

	click : function(ev, index)
	{
		if(this.enabled==false) return;
		for(var i = 0; i<this.radios.length; i++)
			this.radios[i].checked = (i == index);
		this.selectedIndex = index;
		this.setRating(index);
		var requestOptions = Object.extend(
		{
			ID : this.options.ListID+"_c"+index,
			EventTarget : this.options.ListID+"$c"+index
		},this.options);
		var request = new Prado.CallbackRequest(requestOptions.EventTarget, requestOptions);
		if(request.dispatch()==false)
			Event.stop(ev);
	},

	setRating: function(index)
	{

		for(var i = 0; i<this.radios.length; i++)
		{
			var node = this.radios[i].parentNode;
			var action = i > index ? 'removeClassName' : 'addClassName'
			Element[action](this.radios[i].parentNode, "rating_selected");
		}
		this.setCaption(index);
	},

	setCaption : function(index)
	{
		var value = index > -1 ? this.radios[index].value : this.options.caption;
		var caption = $(this.options.CaptionID);
		if(caption) caption.innerHTML = value;
		$(this.options.ListID).title = value;
	},

	setEnabled : function(value)
	{
		this.enabled = value;
		for(var i = 0; i<this.radios.length; i++)
		{
			var action = value ? 'removeClassName' : 'addClassName'
			Element[action](this.radios[i].parentNode, "rating_disabled");
		}
	}
},
{
ratings : {},
register : function(rating)
{
	Prado.WebUI.TRatingList.ratings[rating.options.ListID] = rating;
},

setEnabled : function(id,value)
{
	Prado.WebUI.TRatingList.ratings[id].setEnabled(value);
},

setRating : function(id,value)
{
	Prado.WebUI.TRatingList.ratings[id].setRating(value);
	Prado.WebUI.TRatingList.ratings[id].selectedIndex = value;
}
});