Adds a semi-transparent overlay over a dom element with a spinnin ajax icon.
var Spinner = new Class({
Extends: Mask,
options: {
message: false,
'class':'spinner',
containerPosition: {},
content: {
'class':'spinner-content'
},
messageContainer: {
'class':'spinner-msg'
},
img: {
'class':'spinner-img'
},
fxOptions: {
link: 'chain'
}
},
initialize: function(){
this.parent.apply(this, arguments);
this.target.store('spinner', this);
add this to events for when noFx is true; parent methods handle hide/show
var deactivate = function(){ this.active = false; }.bind(this);
this.addEvents({
hide: deactivate,
show: deactivate
});
},
render: function(){
this.parent();
this.element.set('id', this.options.id || 'spinner-'+Date.now());
this.content = document.id(this.options.content) || new Element('div', this.options.content);
this.content.inject(this.element);
if (this.options.message){
this.msg = document.id(this.options.message) || new Element('p', this.options.messageContainer).appendText(this.options.message);
this.msg.inject(this.content);
}
if (this.options.img){
this.img = document.id(this.options.img) || new Element('div', this.options.img);
this.img.inject(this.content);
}
this.element.set('tween', this.options.fxOptions);
},
show: function(noFx){
if (this.active) return this.chain(this.show.bind(this));
if (!this.hidden){
this.callChain.delay(20, this);
return this;
}
this.active = true;
return this.parent(noFx);
},
showMask: function(noFx){
var pos = function(){
this.content.position(Object.merge({
relativeTo: this.element
}, this.options.containerPosition));
}.bind(this);
if (noFx){
this.parent();
pos();
} else {
this.element.setStyles({
display: 'block',
opacity: 0
}).tween('opacity', this.options.style.opacity || 0.9);
pos();
this.hidden = false;
this.fireEvent('show');
this.callChain();
}
},
hide: function(noFx){
if (this.active) return this.chain(this.hide.bind(this));
if (this.hidden){
this.callChain.delay(20, this);
return this;
}
this.active = true;
return this.parent(noFx);
},
hideMask: function(noFx){
if (noFx) return this.parent();
this.element.tween('opacity', 0).get('tween').chain(function(){
this.element.setStyle('display', 'none');
this.hidden = true;
this.fireEvent('hide');
this.callChain();
}.bind(this));
},
destroy: function(){
this.content.destroy();
this.parent();
this.target.eliminate('spinner');
}
});
Spinner.implement(new Chain);
Request = Class.refactor(Request, {
options: {
useSpinner: false,
spinnerOptions: {},
spinnerTarget: false
},
initialize: function(options){
this._send = this.send;
this.send = function(options){
var spinner = this.getSpinner();
if (spinner) spinner.chain(this._send.bind(this, options)).show();
else this._send(options);
return this;
};
this.previous(options);
},
getSpinner: function(){
if (!this.spinner) {
var update = document.id(this.options.spinnerTarget) || document.id(this.options.update);
if (this.options.useSpinner && update) {
this.spinner = update.get('spinner', this.options.spinnerOptions);
['onComplete', 'onException', 'onCancel'].each(function(event){
this.addEvent(event, this.spinner.hide.bind(this.spinner));
}, this);
}
}
return this.spinner;
}
});
Element.Properties.spinner = {
set: function(options){
var spinner = this.retrieve('spinner');
spinner.setOptions(options);
return this;
},
get: function(){
var spinner = this.retrieve('spinner')
if (!spinner){
spinner = new Spinner(this);
this.store('spinner', spinner);
}
return spinner;
}
};
Element.implement({
spin: function(options){
this.get('spinner', options).show();
return this;
},
unspin: function(){
var opt = Array.link(arguments, {options: Type.isObject, callback: Type.isFunction});
this.get('spinner', opt.options).hide(opt.callback);
return this;
}
});