javaeye3/public/javascripts/rating_src.js

221 lines
7.1 KiB
JavaScript

// Modified by Quake base on http://livepipe.net/control/rating
Object.Event = {
extend: function(object){
object._objectEventSetup = function(event_name){
this._observers = this._observers || {};
this._observers[event_name] = this._observers[event_name] || [];
};
object.observe = function(event_name,observer){
if(typeof(event_name) == 'string' && typeof(observer) != 'undefined'){
this._objectEventSetup(event_name);
if(!this._observers[event_name].include(observer))
this._observers[event_name].push(observer);
}else
for(var e in event_name)
this.observe(e,event_name[e]);
};
object.stopObserving = function(event_name,observer){
this._objectEventSetup(event_name);
if(event_name && observer)
this._observers[event_name] = this._observers[event_name].without(observer);
else if(event_name)
this._observers[event_name] = [];
else
this._observers = {};
};
object.observeOnce = function(event_name,outer_observer){
var inner_observer = function(){
outer_observer.apply(this,arguments);
this.stopObserving(event_name,inner_observer);
}.bind(this);
this._objectEventSetup(event_name);
this._observers[event_name].push(inner_observer);
};
object.notify = function(event_name){
this._objectEventSetup(event_name);
var collected_return_values = [];
var args = $A(arguments).slice(1);
try{
for(var i = 0; i < this._observers[event_name].length; ++i)
collected_return_values.push(this._observers[event_name][i].apply(this._observers[event_name][i],args) || null);
}catch(e){
if(e == $break)
return false;
else
throw e;
}
return collected_return_values;
};
if(object.prototype){
object.prototype._objectEventSetup = object._objectEventSetup;
object.prototype.observe = object.observe;
object.prototype.stopObserving = object.stopObserving;
object.prototype.observeOnce = object.observeOnce;
object.prototype.notify = function(event_name){
if(object.notify){
var args = $A(arguments).slice(1);
args.unshift(this);
args.unshift(event_name);
object.notify.apply(object,args);
}
this._objectEventSetup(event_name);
var args = $A(arguments).slice(1);
var collected_return_values = [];
try{
if(this.options && this.options[event_name] && typeof(this.options[event_name]) == 'function')
collected_return_values.push(this.options[event_name].apply(this,args) || null);
for(var i = 0; i < this._observers[event_name].length; ++i)
collected_return_values.push(this._observers[event_name][i].apply(this._observers[event_name][i],args) || null);
}catch(e){
if(e == $break)
return false;
else
throw e;
}
return collected_return_values;
};
}
}
};
if(typeof(Control) == 'undefined')
Control = {};
Control.Rating = Class.create({
initialize: function(container,options){
Control.Rating.instances.push(this);
this.value = false;
this.links = [];
this.container = $(container);
this.container.update('');
this.options = {
min: 1,
max: 5,
rated: false,
input: false,
reverse: false,
capture: true,
multiple: false,
classNames: {
off: 'rating_off',
half: 'rating_half',
on: 'rating_on',
selected: 'rating_selected'
},
updateUrl: false,
updateParameterName: 'value',
afterChange: Prototype.emptyFunction
};
Object.extend(this.options,options || {});
if(this.options.value){
this.value = this.options.value;
delete this.options.value;
}
if(this.options.input){
this.options.input = $(this.options.input);
this.options.input.observe('change',function(input){
this.setValueFromInput(input);
}.bind(this,this.options.input));
this.setValueFromInput(this.options.input,true);
}
var range = $R(this.options.min,this.options.max);
(this.options.reverse ? $A(range).reverse() : range).each(function(i){
var link = this.buildLink(i);
this.container.appendChild(link);
this.links.push(link);
}.bind(this));
this.setValue(this.value || this.options.min - 1,false,true);
},
buildLink: function(rating){
var link = $(document.createElement('a'));
link.value = rating;
if(this.options.multiple || (!this.options.rated && !this.options.multiple)){
link.href = '';
link.onmouseover = this.mouseOver.bind(this,link);
link.onmouseout = this.mouseOut.bind(this,link);
link.onclick = this.click.bindAsEventListener(this,link);
}else{
link.style.cursor = 'default';
link.observe('click',function(event){
Event.stop(event);
return false;
}.bindAsEventListener(this));
}
link.addClassName(this.options.classNames.off);
return link;
},
disable: function(){
this.links.each(function(link){
link.onmouseover = Prototype.emptyFunction;
link.onmouseout = Prototype.emptyFunction;
link.onclick = Prototype.emptyFunction;
link.observe('click',function(event){
Event.stop(event);
return false;
}.bindAsEventListener(this));
link.style.cursor = 'default';
}.bind(this));
},
setValueFromInput: function(input,prevent_callbacks){
this.setValue((input.options ? input.options[input.options.selectedIndex].value : input.value),true,prevent_callbacks);
},
setValue: function(value,force_selected,prevent_callbacks){
this.value = value;
if(this.options.input){
if(this.options.input.options){
$A(this.options.input.options).each(function(option,i){
if(option.value == this.value){
this.options.input.options.selectedIndex = i;
throw $break;
}
}.bind(this));
}else
this.options.input.value = this.value;
}
this.render(this.value,force_selected);
if(!prevent_callbacks){
if(this.options.updateUrl){
var params = {};
params[this.options.updateParameterName] = this.value;
new Ajax.Request(this.options.updateUrl,{
parameters: params
});
}
this.notify('afterChange',this.value);
}
},
render: function(rating,force_selected){
(this.options.reverse ? this.links.reverse() : this.links).each(function(link,i){
if(link.value <= Math.ceil(rating)){
link.className = this.options.classNames[link.value <= rating ? 'on' : 'half'];
if(this.options.rated || force_selected)
link.addClassName(this.options.classNames.selected);
}else
link.className = this.options.classNames.off;
}.bind(this));
},
mouseOver: function(link){
this.render(link.value,true);
},
mouseOut: function(link){
this.render(this.value);
},
click: function(event,link){
this.options.rated = true;
this.setValue((link.value ? link.value : link),true);
if(!this.options.multiple)
this.disable();
if(this.options.capture){
Event.stop(event);
return false;
}
}
});
Object.extend(Control.Rating,{
instances: [],
findByElementId: function(id){
return Control.Rating.instances.find(function(instance){
return (instance.container.id && instance.container.id == id);
});
}
});
Object.Event.extend(Control.Rating);