Skip to content

Instantly share code, notes, and snippets.

@jshirley
Created June 8, 2009 17:06
Show Gist options
  • Save jshirley/125943 to your computer and use it in GitHub Desktop.
Save jshirley/125943 to your computer and use it in GitHub Desktop.
YUI({
combine: false,
// filter: 'debug',
base: '/static/scripts/yui3/build/'
}).use(
"widget", "overlay", "node", "io-form", "event", "event-simulate",
function(Y) {
var overlay = new Y.Overlay({
headerContent: "",
bodyContent: "This will be replaced",
zIndex: 500,
visible: false
});
overlay.render();
var RestEventHandler = {
start: function(id, args) {
},
complete: function(id, o, args) {
},
completeAndReplace: function(id, o, args) {
var tgt = args[0];
var p = tgt.get('parentNode');
if ( !p ) return;
p.set('innerHTML', o.responseText);
},
completeAndPopup: function(id, o, args) {
var tgt = args[0];
overlay.set('bodyContent', o.responseText);
overlay.show();
},
failure: function(id, o, args) {
alert("Darn: " + o.status);
},
abort: function(id, args) {
}
};
Y.on('delegate', function(e) {
var tgt = e.target;
var cfg = {};
if ( tgt.hasClass('rest-delete') )
cfg.method = 'DELETE';
else if ( tgt.hasClass('rest-post') )
cfg.method = 'POST';
else if ( tgt.hasClass('rest-put') )
cfg.method = 'PUT';
else if ( tgt.hasClass('rest-get') )
cfg.method = 'GET';
else
return;
try {
e.preventDefault();
} catch(e) {
console.log("Failed halt, darn:\n" + e);
}
else if ( tgt.hasClass('popup-response') ) {
completeHandler = RestEventHandler.completeAndPopup;
overlay.set("align", { node: tgt, points:[Y.WidgetPositionExt.TR, Y.WidgetPositionExt.TR]});
}
if ( uri.indexOf('?') > 0 )
uri += '&' + extra.join('&');
else
uri += '?' + extra.join('&');
Y.on('io:start', RestEventHandler.start, this, [ tgt ]);
Y.on('io:complete', completeHandler, this, [ tgt ]);
Y.on('io:abort', RestEventHandler.abort, this, [ tgt ]);
Y.on('io:failure', RestEventHandler.failure, this, [ tgt ]);
var request = Y.io(uri, cfg);
}, '#main_content', 'click', 'a.rest-delete,a.rest-post,a.rest-get,a.rest-put');
Y.on('delegate', function(e) {
var tgt = e.target;
/* An href element with the rest-delete class */
//if ( tgt.hasClass('rest-form') ) {
try {
e.preventDefault();
e.halt();
} catch(e) {
console.log("Bug in YUI3 :(");
}
var uri = tgt.get('action');
tgt.elements = tgt.get('elements');
var cfg = {
headers: { 'Content-Type': 'text/html; charset=utf-8' },
form: { id: document.getElementById(tgt.get('id')), useDisabled: true }
};
cfg.method = tgt.get('method') || 'GET';
cfg.method = cfg.method.toUpperCase();
/* On complete, delete the parent element */
var completeHandler = RestEventHandler.complete;
if ( tgt.hasClass('delete-parent') ) {
}
else if ( tgt.hasClass('replace-target') ) {
completeHandler = RestEventHandler.completeAndReplace;
}
else if ( tgt.hasClass('popup-response') ) {
completeHandler = RestEventHandler.completeAndPopup;
overlay.set("align", { node: tgt, points:[Y.WidgetPositionExt.TR, Y.WidgetPositionExt.TR]});
}
Y.on('io:start', RestEventHandler.start, this, [ tgt ]);
Y.on('io:complete', completeHandler, this, [ tgt ]);
Y.on('io:abort', RestEventHandler.abort, this, [ tgt ]);
Y.on('io:failure', RestEventHandler.failure, this, [ tgt ]);
var request = Y.io(uri, cfg);
//}
}, document.body, 'submit', 'form.rest-form');
Y.on('delegate', function(e) {
e.target.select();
}, 'body', 'click', 'input.select-on-focus');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment