Skip to content

Instantly share code, notes, and snippets.

@apipkin
Created February 25, 2010 03:56
Show Gist options
  • Save apipkin/314218 to your computer and use it in GitHub Desktop.
Save apipkin/314218 to your computer and use it in GitHub Desktop.
YUI.add('echofin-modal',function(Y){
var _modal = null
;
Y.Echofin.Modal = {
BUTTON_CANCEL : 'Cancel',
BUTTON_CLOSE : 'Close',
BUTTON_OK : 'Ok',
BUTTON_NO : 'No',
BUTTON_YES : 'Yes',
_buttons : [],
_title : null,
_content : null,
_callback : null,
_callback_scope : null,
_build_modal : function(type) {
if(this._content === null) {
return;
}
if(_modal === null || _modal === undefined) {
_modal = new Y.Overlay({
centered : true,
headerContent : this._title,
bodyContent : this._content,
footerContent : this._make_buttons(),
plugins : [{ fn: Y.Plugin.OverlayModal }],
zIndex : 14000
});
}else{
_modal.set('headerContent', this._title)
.set('bodyContent', this._content)
.set('footerContent', this._make_buttons())
;
}
_modal.get('boundingBox').addClass('echofin-modal-' + type);
var top = parseFloat(Y.one('window').get('scrollTop'),10);
_modal.show().render().centered();
_modal.get('boundingBox').setStyle('top', (top + 20) + 'px');
window.scrollTo(0,top);
},
_load_content : function(type) {
var io = Y.clone(BASE_IO);
io.uri = this._content;
io.on.success = Y.bind(this._display_content,this);
io.arguments = {'type' : type};
Y.io(io.uri,io);
},
_display_content : function(id, o, args) {
this._content = o.responseText;
this._build_modal(args.type);
if(this._callback !== null) {
this._callback.call(this._callback_scope, _modal);
}
},
_make_buttons : function() {
var btns = Y.Node.create('<div></div>');
for(var i = 0; i < this._buttons.length; i++) {
var btn = Y.Node.create('<button></button>');
btn.addClass('echofin-modal-button');
btn.set('innerHTML',this._buttons[i].title);
btn.on('click',this._close);
if(this._buttons[i].callback) {
btn.on('click', this._buttons[i].callback);
}
btns.append(btn);
}
return btns;
},
_close : function(e) {
_modal.hide();
},
_getDefaultTitle : function() {
return 'The page ' + location.protocol + '//' + location.host + '/ says:';
},
simple : function(content,title,buttons) {
this._content = content;
this._title = title || this._getDefaultTitle();
this._buttons = buttons || [{title : this.BUTTON_OK}];
this._build_modal('simple');
},
alert : function(content,title,buttons) {
this._content = content;
this._title = title || this._getDefaultTitle();
this._buttons = buttons || [{title : this.BUTTON_OK}];
this._build_modal('alert');
},
warning : function(content,title,buttons) {
this._content = content;
this._title = title || this._getDefaultTitle();
this._buttons = buttons || [{title : this.BUTTON_OK}];
this._build_modal('warning');
},
confirm : function(content,title,buttons) {
this._content = content;
this._title = title || this._getDefaultTitle();
this._buttons = buttons || [{title : this.BUTTON_OK},{title : this.BUTTON_CANCEL}];
this._build_modal('confirm');
},
custom : function(content,title,buttons) {
this._content = content;
this._title = title || this._getDefaultTitle();
this._buttons = buttons || [{title : this.BUTTON_OK}];
this._build_modal('custom');
},
async : function(content, callback, callback_scope, title, buttons) {
this._content = content;
this._title = title || '';
this._buttons = buttons || '';
this._callback = callback;
this._callback_scope = callback_scope;
this._load_content('async');
}
};
}, '0.1', {requires: ['echofin-base','gallery-overlay-modal', 'io']});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment