@ -1,3 +1,20 @@
var jsde _creation _hook = function ( win )
{
/ * T h i s f u n c t i o n i s a h o o k t h a t i s c a l l e d a f t e r e a c h c r e a t i o n o f
* a JSDE window . * /
}
var jsde _contents _hook = function ( win )
{
/ * T h i s f u n c t i o n i s a h o o k t h a t i s c a l l e d a f t e r e a c h t i m e
* content is set in a JSDE window . * /
placeHooksForWindow ( win ) ;
}
notification _popups = [ ] ;
error _popups = [ ] ;
function hookSubmitEvent ( form , callback , error )
{
/ * H o o k s a f o r m t o b e s u b m i t t e d v i a A J A X , e x e c u t i n g t h e g i v e n
@ -11,18 +28,48 @@ function hookSubmitEvent(form, callback, error)
form . each ( function ( index ) {
var element = $ ( this ) ;
var method = element . attr ( "method" ) .toUpperCase ( ) ;
var method = element . attr ( "method" ) ;
var target = element . attr ( "action" ) ;
if ( typeof method !== "undefined" )
{
method = method . toUpperCase ( ) ;
}
else
{
method = "GET" ;
}
element . submit ( function ( ) {
var submit _button = element . find ( "button[type=submit]" ) ;
var submit _icon = submit _button . data ( "submit-icon" ) ;
if ( typeof submit _icon !== "undefined" )
{
/ * F i r s t w e w i l l t r y t o r e p l a c e a n e x i s t i n g i c o n
* in the button . If there is no icon yet , the
* entire contents of the button will be replaced
* with the submission icon . * /
var current _icon = submit _button . find ( "i" ) ;
if ( current _icon . length == 0 )
{
submit _button . html ( "<span style='text-align: center;'><i></i></span>" ) ;
current _icon = element . find ( "i" ) ;
}
current _icon . removeClass ( ) . addClass ( submit _icon ) ;
}
var formdata = element . serialize ( ) ;
console . log ( formdata ) ;
$ . ajax ( {
type : method ,
url : target ,
data : formdata ,
success : callback ,
error : error
dataType : "json" ,
success : function ( data , xhr ) { console . log ( data ) ; callback ( element , data , xhr ) ; } ,
error : function ( data , xhr , err ) { error ( element , data , xhr , err ) ; }
} ) ;
return false ;
@ -30,6 +77,69 @@ function hookSubmitEvent(form, callback, error)
} ) ;
}
function placeHooksForWindow ( win )
{
console . log ( ) ;
$ ( win . _outer ) . find ( "form" ) . each ( function ( ) {
var callback = $ ( this ) . data ( "hook-callback" ) ;
if ( typeof callback !== "undefined" )
{
console . log ( "Hooking" , this , "using callback" , callback ) ;
hookSubmitEvent ( $ ( this ) , window [ callback ] ) ;
}
} ) ;
}
function callbackNodeCreated ( form , data )
{
if ( data . result == "success" )
{
spawnNotification ( data . message ) ;
var node _id = data . node _id ;
form . getWindow ( ) . Close ( ) ;
new JsdeWindow ( {
width : 480 ,
height : 300 ,
x : 100 ,
y : 100 ,
title : "Node lookup" ,
contents : "Loading..." ,
source _url : "/nodes/" + node _id
} ) ;
}
else if ( data . result == "error" )
{
spawnError ( data . message ) ;
}
}
function spawnNotification ( message )
{
var popup = spawnPopup ( message , "notification" ) ;
notification _popups . push ( popup ) ;
}
function spawnError ( message )
{
var popup = spawnPopup ( message , "error" ) ;
error _popups . push ( popup ) ;
}
function spawnPopup ( message , template )
{
var popup = $ ( "#jsde_templates" ) . find ( ".template_" + template ) . clone ( ) . removeClass ( "template_notification template_error" ) ;
popup . find ( ".message" ) . html ( message . replace ( "\n" , "<br>" ) ) ;
popup . hide ( ) ;
popup . prependTo ( "#notification_area" ) ;
popup . fadeIn ( 300 ) . wait ( 5000 ) . fadeOut ( 400 , $ ) . remove ( ) ;
return popup
}
$ ( function ( ) {
hookSubmitEvent ( $ ( "#form_search" ) ) ;
@ -56,4 +166,7 @@ $(function(){
contents : "Loading..." ,
source _url : "/intro"
} ) ;
spawnNotification ( "Test notification" ) ;
spawnError ( "Test error" ) ;
} ) ;