WE Javascript example page
WE Javascript is a JS ressources register. It index all javascript (and some specificaly named css) files and offer to load it by AJAX when function is called. It optionnaly can compress JS files in the same time using the Dean Edwards JavaScript's Packer.
This page is used to test several behaviors of WE Javascript (beta version) for Drupal 6.x. First, sorry if everything is not clear in this documentation, or if my english is not pretty good.Hope this module will be usefull. To understand the interest of these tests, you can process like that :
- Click on "Show source"
- Click on "Execute"
- Click on "Show source" again
If test succeed, you will see a javascript alert box, then, the source of the tested object will change, replaced by the requested function definition. See into your debug console the AJAX content loaded by WE Javascript.
Javascript functions
Functions loaded with WE uses global namespace for functions. After first loading, if you are sure function exists, you can load it directly with my_function();
- Javascript : $we.load_function("function_name"); to load function.
- Javascript : $we.call_function("function_name",["arg1","arg2"]); to execute it in the same time.
- PHP : we_javascript_add_function("function_name"); to add function to the rendered page.
Javascript class objects
The button below will execute this test code :$we.load_class('we_javascript_example_class',{
async:true,
complete:function(){
var obj = new we_javascript_example_class();
obj.test();
}});
- Javascript : $we.load_class("class_name"); to load class.
- PHP : we_javascript_add_class("class_name"); to add class to the rendered page.
jQuery plugin
We have created a standard jquery plugin called example (jquery.example.js), we can use it like that : $(document).example();.
- Javascript : $("selector").plugin_name("args"); to load plugin and execute it.
- PHP : we_javascript_add_jquery("plugin_name"); to add plugin to the rendered page.
Inline javascript
You can add inline javascript by several ways in PHP.
- we_javascript_add_inline("script_name"); with "script_name" the name of the inline.script_name.js
- we_javascript_add_inline("script/local/path.js"); width path of the inline.script_name.js
- we_javascript_add_inline("name",alert(\"My script !\");"); with your script directly passed to the function.
- $we.load_inline("name"); to load named javascript file. (removed)
- $we.call_inline("name"); to execute it in the same time.
If you want to load remote JS files like <script src="yourfile.js"></script> see add_js_link jQuery plugin into WE Javascript tools module.
CSS files
CSS files are parsed by WE Javascript and loaded as a json object. Image files are loaded by javascript and styles declaration are append to a specific <style> tag at the top of the document. CSS content is append only when all contained images are completly loaded.
Images
$we.load_image($we.url_module('we_javascript_example') + '/images/big_one.jpg',{async:true,complete:function() {
alert('Image loaded,\nyou can use it now,\nin CSS or in a img tag.');
}});Cascading calls / Code parsing
Most of the time, javascript functions are not called alone. Scripts can be a long imbrication of different calls. For this case, javascripts files are parsed before sending to AJAX, and all functions calls are included into response.
Add related Javascripts
If you want to trigger a script loading and add other script to the package (from an other module for example) you can use the hook_we_javascript_add_script($function_type,$function_name,$script)
Startup package
You can still use the API into Drupal to preload content at page loadin without AJAX call.
Javascript compression
WE Javascript can compress automatically all javascripts before send it in AJAX. This option can be disabled into WE configuration.
Javascript API
Inheritance plugin was used for define $we object, in order to keep a similar approach of PHP class constructors. I choose to create a separate $we object instead using functions as a jQuery plugin. First to have a separate namespace, secondly beceause any of theses object's method can be used alone, without the main object, and without Drupal too. Yet we can imagine to make this object a part of Drupal javascript object. $we object uses jQuery but has no relation (D'ont be confused by the $ in $we) Here is the list of the most usefull functions of this object :
- call_function
- call_inline
- load_function
- load_class
- load_jquery
- load_css
- load_image
Drupal API
Here is the list of hooks created by the we_javascript Drupal module.
- hook_we_javascript_init_page() : Launched to init normal pages (not AJAX calls)
- hook_we_javascript_init_ajax() : Launched to init AJAX pages
- hook_we_javascript_scripts() : Launched to get all available scripts
- hook_we_javascript_add_script() : Launched when a new script is added to output array
- we_javascript_add_function
- we_javascript_add_class
- we_javascript_add_jquery
- we_javascript_add_inline
- we_javascript_add_css
- we_javascript_add_image