Creates a mask element to cover another.
var Mask = new Class({
Implements: [Options, Events],
Binds: ['position'],
options: {/*
onShow: function(){},
onHide: function(){},
onDestroy: function(){},
onClick: function(){},
inject: {
where: 'after',
target: null,
},
hideOnClick: false,
id: null,
destroyOnHide: false,*/
style: {},
'class': 'mask',
maskMargins: false,
useIframeShim: true,
iframeShimOptions: {}
},
initialize: function(target, options){
this.target = document.id(target) || document.id(document.body);
this.target.store('Mask', this);
this.setOptions(options);
this.render();
this.inject();
},
render: function(){
this.element = new Element('div', {
'class': this.options['class'],
id: this.options.id || 'mask-' + Date.now(),
styles: Object.merge(this.options.style, {
display: 'none'
}),
events: {
click: function(){
this.fireEvent('click');
if (this.options.hideOnClick) this.hide();
}.bind(this)
}
});
this.hidden = true;
},
toElement: function(){
return this.element;
},
inject: function(target, where){
where = where || this.options.inject ? this.options.inject.where : '' || this.target == document.body ? 'inside' : 'after';
target = target || this.options.inject ? this.options.inject.target : '' || this.target;
this.element.inject(target, where);
if (this.options.useIframeShim){
this.shim = new IframeShim(this.element, this.options.iframeShimOptions);
this.addEvents({
show: this.shim.show.bind(this.shim),
hide: this.shim.hide.bind(this.shim),
destroy: this.shim.destroy.bind(this.shim)
});
}
},
position: function(){
this.resize(this.options.width, this.options.height);
this.element.position({
relativeTo: this.target,
position: 'topLeft',
ignoreMargins: !this.options.maskMargins,
ignoreScroll: this.target == document.body
});
return this;
},
resize: function(x, y){
var opt = {
styles: ['padding', 'border']
};
if (this.options.maskMargins) opt.styles.push('margin');
var dim = this.target.getComputedSize(opt);
if (this.target == document.body){
var win = window.getScrollSize();
if (dim.totalHeight < win.y) dim.totalHeight = win.y;
if (dim.totalWidth < win.x) dim.totalWidth = win.x;
}
this.element.setStyles({
width: Array.pick([x, dim.totalWidth, dim.x]),
height: Array.pick([y, dim.totalHeight, dim.y])
});
return this;
},
show: function(){
if (!this.hidden) return this;
window.addEvent('resize', this.position);
this.position();
this.showMask.apply(this, arguments);
return this;
},
showMask: function(){
this.element.setStyle('display', 'block');
this.hidden = false;
this.fireEvent('show');
},
hide: function(){
if (this.hidden) return this;
window.removeEvent('resize', this.position);
this.hideMask.apply(this, arguments);
if (this.options.destroyOnHide) return this.destroy();
return this;
},
hideMask: function(){
this.element.setStyle('display', 'none');
this.hidden = true;
this.fireEvent('hide');
},
toggle: function(){
this[this.hidden ? 'show' : 'hide']();
},
destroy: function(){
this.hide();
this.element.destroy();
this.fireEvent('destroy');
this.target.eliminate('mask');
}
});
Element.Properties.mask = {
set: function(options){
var mask = this.retrieve('mask');
return this.eliminate('mask').store('mask:options', options);
},
get: function(options){
if (options || !this.retrieve('mask')){
if (this.retrieve('mask')) this.retrieve('mask').destroy();
if (options || !this.retrieve('mask:options')) this.set('mask', options);
this.store('mask', new Mask(this, this.retrieve('mask:options')));
}
return this.retrieve('mask');
}
};
Element.implement({
mask: function(options){
this.get('mask', options).show();
return this;
},
unmask: function(){
this.get('mask').hide();
return this;
}
});