If I override the onShow such as:
onShow: function(){
this.restack();
//DO ALTERNATIVE to default this.show();
this.holder.show();
This.center();
},
If I don't call this.show(); will I need to copy the code from q.js show method (shown below) into my onshow for everything to work correctly still?
// show holder
show: function(force) {
if (Q.active != this || force) {
this.positionHolder();
if (typeof this.setValue == "function" && this.input)
this.setValue(this.input.value);
Q.callback('onShow', this);
Q.active = this;
this.holder.appear({ duration: 0.1 });
}
},
Thanks again
Andy
andy
Splitting the default show() method in Q.js to call onBeforeShow() before the window is shown and onAfterShow() after the window is shown, these could be skeleton methods with nothing in ready to override so for example if you load up a window with some ajax containing a login form / any form you would want an onAfterShow to run any js code on the form once its show and inserted into the DOM, this way would guarantee it would run in onAfterShow etc..
I have this In my subclass now and all working well...
show:function(force){
//leave this here - copied from main q.js show
if (Q.active != this || force)
{
this.positionHolder();
if (typeof this.setValue == "function" && this.input)
this.setValue(this.input.value);
//restack from qwindow onshow
this.restack();
Q.active = this;
//show the window
this.holder.show();
//window shown - run aftershow
(function() { this.onAfterShow() }).bind(this).delay(0.1);
}
},
onAfterShow: function(){
//center window if desired
if (this.options.centered)
this.center();
//Run js code passed back via ajax json object
if (this.options.execute){
try{ eval(this.options.execute);}catch(e){alert(e);}
}
},
Thanks for all your help again!
Ill show show you what im using it on once all my framework is in place!!
I have a subclass of window that gets its content from a remote RPC url using JSON...
var win = new Q.AjaxWindow({
ajaxmethod:'get or post',
ajaxurl:'http://blahblah.com',
ajaxpostvars:'&var=val', //post vars if posting ajax request
ajaxgetvars:'&var2=val2', //get vars if normal ajax request
id: id,
style: className,
});
So the above for example is called from a link to login to my system...
Please feel free to use any of it if it is of use or let me know which bits are 'rubbish' so to speak... works really fast because of the small q.js footprint as opposed to stuff ive used before like xilinus prototype windows....very impressed with Q.
Things ill probably add are:
Update method - to pass a new url / params to the window.
Refresh method - to refresh the window via ajax with the current url/params.
Plus loads of bits to tweak...just added some css for visibility to fix being able to see the window shown in its default position before being centered.
Q.AjaxWindow = Class.create(Q.Window, {
initialize: function($super, options) {
// merge options
options = $H({
onHide : function(){this.remove();}.bind(this),
centered : (options && options.centered ? options.centered : true),
ajaxmethod : (options && options.ajaxmethod ? options.ajaxmethod : 'get')
}).merge(options || {})
.toObject();
var ajax = new Ajax.Request(options.ajaxurl,{
evalJSON:true,
parameters: options.ajaxgetvars,
method:options.ajaxmethod,
postBody:options.ajaxpostvars,
onFailure:function(request){
alert('AJAX call failed');
},
onSuccess:function(transport){
json = transport.responseJSON;
if (json){
if (json.title) options.title = json_decode(json.title);
if (json.draggable) options.draggable =json.draggable;
if (json.resizable) options.resizable =json.resizable;
if (json.width) options.width = json.width;
if (json.height) options.height = json.height;
if (json.content) options.text = json_decode(json.content);
if (json.execute) options.execute = json_decode(json.execute);
//initialise window (this will call build) as cant call ajaxwindow from here.
$super(options);
}
}
});
},
// build the ajax window
build: function($super) {
//override build - call superclass build then our subclass init stuff
$super();
this.ajaxwindow();
},
ajaxwindow:function(){
// window resize event keep the window central
this.onWindowResize = (function() {
this.center();
}).bind(this);
(document.onresize ? document : window).observe("resize", this.onWindowResize);
// create blocking cover
this.holder
.addClassName("q-window-blocking")
.setStyle({ zIndex: 99998 });
// create protective layer
this.protection = new Element("div")
.addClassName("q-protective-layer");
this.div
.insert({ top: this.protection });
this.protection
.hide();
this.protection.appear({ duration: 0, to: 0.5 });
this.show();
},
show:function(force){
//leave this here - copied from main q.js show
if (Q.active != this || force) {
this.positionHolder();
if (typeof this.setValue == "function" && this.input)
this.setValue(this.input.value);
Q.active = this;
this.holder.setStyle({visibility:'hidden'});
this.holder.show();
//show the window
//if (this.options.centered)
// this.center();
//window shown - run aftershow
(function() { this.onAfterShow() }).bind(this).delay(0.1);
}
},
onAfterShow: function(){
//restack from qwindow onshow
this.restack();
//center window if desired
if (this.options.centered)
this.center();
this.holder.setStyle({visibility:'visible'});
//Run js code passed back via ajax json object
if (this.options.execute){
try{ eval(this.options.execute);}catch(e){alert(e);}
}
},
// remove all elements and the instance itself as well
remove: function() {
this.protection.fade({ duration: 0.01 });
(function() {
try {
this.protection.remove();
//this.
// holder.
// remove();
delete this;
} catch(e) {
// well, we've tried
}
}).bind(this).delay(0.2);
},
center: function(){
var dim = document.viewport.getDimensions();
var layout = new Element.Layout(this.holder);
this.holder.setStyle({
left: (dim.width / 2) - (layout.get('width')/2) + 'px',
top: (dim.height / 2) - (layout.get('height')/2) + 'px'
});
this.holder.setOpacity(1);
}
})