""" py2jquery (Python 2 jQuery) Developed by Thadeus Burgess License: GPL v2 Inspired by Nathan Freeze's Client Tools for web2py This is a set of classes and functions for managing client events and resources from a python server. """ ####### ## IN db.py ### #Sets up a global manager class to manage all scripts from applications.init.modules.py2jquery import * manager = Manager(globals()) ####### ## IN web2py_ajax.html # Be sure to remove reference to the existing jquery, since it is # outdated. Let the google serve it anyways! ### {{ manager.google_load("jquery", "1.3.2") manager.google_load("jqueryui", "1.7.2") ### # You can also include the direct link. #manager.include("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js") #manager.include("http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js") ### # I downloaded the vader jqueryui theme and placed it in the css folder # Only necissary if you plan on using jqueryui manager.include(URL(r=request, c='static', f='css/vader/jquery-ui-1.7.2.custom.css')) # if you just want to test, you can use this # "http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-darkness/jquery-ui.css") }} ## # Generate our xml. # This can be called as many times as you want. # I suggest once inside of and then again before the # end of your page just before (just in case you need to render # any javascript from your view!! {{=manager.xml(False)}} # False means don't minify the output # default - True ###### ## Now that that is out of the way, on to the examples! ### ##################################### # ## Add an event to a dom element # ##################################### # web2py helper def index(): adiv = DIV("Click Me", _id='click_me') # Create a click event # that is bound to adiv # and executes an alert dialog box. # if is_function is True, then the event will be wrapped in a javascript function event = Event("click", adiv, Script(alert("Hello World")), is_function=True) ### # Add an event, if you specifiy optional parameter True, it will # include the script in jQuery(document).on_ready() function. manager.add(event, True) return dict(adiv=adiv) ##################################################### def index(): ## # You can pass any Script object to events, however, if an event is to be wrapped in a function # it must be added with manager.add() adiv = DIV("Click Me", _id='click_me') say_hello = Script(alert("Hello World!", is_function=True)) event = Event("click", adiv, say_hello) manager.add(say_hello) # Just add the function, don't call it manager.add(event, True) ##################################################### ##################################### # ## Lets add in some AJAX to get the text from the server! # ##################################### def index(): adiv = DIV("Click Me", _id='click_me') ## To generate the URL you must pass the request object. # the handle can be either a url string or a function reference. # By default the response from the server is eval'd ajax = Call(handle_it, uuid='my_callback', is_function=True, request=request) # Create our confirmation script. # Parameters: message, if_ok, if_cancel # if_ok and if_cancel must be a javascript string or Script object. confirm = Confirm("Are you sure you want to contact the server?", ajax) # Bind our event and call confirm. # Any jquery event can be used. event = Event("click", adiv, confirm) ## It is a good idea to specify a uuid for any Script object. # This will allow each script to be unique when wrapped with a function. # This also allows for multiple subscriptions, such as two click events # on the same DOM element. manager.add(ajax) manager.add(event, True) # Notice, you don't need to add the Script if it is not a function. return dict(adiv=adiv) def handle_it(): # Using a JavaScript generator, return the alert. return alert("Hello World From web2py!") ##################################### # ## What about a timer that bugs you until you click the text!? # ##################################### def index(): adiv = DIV("Click Me to stop the nagging!", _id='click_me') nag = Script(alert("Hey Bud, How ya doin?")) # Timers are really simple. # Call a Script object at each timout # timeout is in ms. nag_timer = Interval(nag, 10000) # Delay timers work the same! # nag_timer = Delay(nag, 10000) # They only run once. # You can even access the timers javascript variable id # if you need to js_variable_name = nag_timer.var.timer_id # You can also set the timeout later!! nag_timer.var.timer_id.value = 15000 # So now lets stop the timer! stop_it = StopTimer(nag_timer) # default event is click # I am currently working on a way to # wrap two scripts so the event would call them both. # So you could call stop_it and display a message saying I'll stop event = Event(event_obj=adiv, call=stop_it, rebind=True) # if you specify rebind=True, event will use jQuery.live instead of jQuery.bind # So now we add our scripts to the page manager.add(nag_timer, True) # Start when page loads manager.add(event, True) return dict(adiv=adiv) ##################################### # ## What about star ratings!???? ## Easy! With no other libraries! ## Make sure you have these in your static folder 'images/star_full.gif' # full colored star 'images/star_half.gif' # half colored star 'images/star_void.gif' # no colored star 'images/star_vote.gif' # hover colored star # You can download these from: # http://static.thadeusb.com/surrenderthebooty.thadeusb.com/images/star_full.gif # http://static.thadeusb.com/surrenderthebooty.thadeusb.com/images/star_half.gif # http://static.thadeusb.com/surrenderthebooty.thadeusb.com/images/star_void.gif # http://static.thadeusb.com/surrenderthebooty.thadeusb.com/images/star_vote.gif # No, surrenderthebooty is not a porn site!!! # It is a gaiaonline aquarium aggregator written in web2py ##################################### def index(): ajax_rate = Call(rate_stars, request=request) # In case you need to pass args along with the call ###########Call(rate_stars, args=['hi'], request=request) # Create blank set of stars # The first attribute is the value of the star. # -1 means no colored stars. # 1, 2, 3, ... means that many colored stars # Callback is what to do when star is clicked! # uuid is required for stars! stars = Stars(-1, call=ajax_rate, num=5, uuid='mystars') # Generate the stars, you may need to call XML() from your view on this. return dict(stars=stars.dom(manager)) def rate_stars(): # Passed from POST uuid = request.vars.star_uuid val = request.vars.star_val #### You could do some database manipulation here #### to insert the rating, and return an average rating. # Recreate our stars object. # Notice we are using the same uuid, and no callback. # Set the value to the selected star. stars = Stars(int(val), uuid=uuid) # Renders JavaScript that will get eval'd return stars.js(request) ##################################### # ## What about jqueryui! ## This is in progress to pythonify jqueryui! # ##################################### def index(): adiv = DIV("CLICK ME", _id='click_me') call = Call(handle_it, request=request, is_function=True) event = Event("click", a, call, is_function=True) manager.add(call) manager.add(event, True) return dict(adiv=adiv) def handle_it(): return 'jQuery("#click_me").append("
");' + \ 'jQuery("#dialog").html("%s");' % str(request.now) + \ 'jQuery("#dialog").dialog({ title: "Server Time"});' ##################################### # ## MORE TO COME! ## I am working on this every day, and am open ## to suggestions and patches! # #####################################