Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | Javascript Class |
| 3 | ################ |
| 4 | |
| 5 | CodeIgniter provides a library to help you with certain common functions |
| 6 | that you may want to use with Javascript. Please note that CodeIgniter |
| 7 | does not require the jQuery library to run, and that any scripting |
| 8 | library will work equally well. The jQuery library is simply presented |
| 9 | as a convenience if you choose to use it. |
| 10 | |
Andrey Andreev | 21c3c22 | 2014-12-04 12:10:00 +0200 | [diff] [blame] | 11 | .. important:: This library is DEPRECATED and should not be used. It has always |
| 12 | been with an 'experimental' status and is now no longer supported. |
| 13 | Currently only kept for backwards compatibility. |
| 14 | |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 15 | .. contents:: |
| 16 | :local: |
| 17 | |
| 18 | .. raw:: html |
| 19 | |
| 20 | <div class="custom-index container"></div> |
| 21 | |
| 22 | ************************** |
| 23 | Using the Javascript Class |
| 24 | ************************** |
| 25 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 26 | Initializing the Class |
| 27 | ====================== |
| 28 | |
| 29 | To initialize the Javascript class manually in your controller |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 30 | constructor, use the ``$this->load->library()`` method. Currently, |
| 31 | the only available library is jQuery, which will automatically be |
| 32 | loaded like this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | |
| 34 | $this->load->library('javascript'); |
| 35 | |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 36 | The Javascript class also accepts parameters: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 37 | |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 38 | - js_library_driver (string) *default: 'jquery'* |
| 39 | - autoload (bool) *default: TRUE* |
| 40 | |
| 41 | You may override the defaults by sending an associative array:: |
| 42 | |
| 43 | $this->load->library( |
| 44 | 'javascript', |
| 45 | array( |
| 46 | 'js_library_driver' => 'scripto', |
| 47 | 'autoload' => FALSE |
| 48 | ) |
| 49 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | |
| 51 | Again, presently only 'jquery' is available. You may wish to set |
| 52 | autoload to FALSE, though, if you do not want the jQuery library to |
| 53 | automatically include a script tag for the main jQuery script file. This |
| 54 | is useful if you are loading it from a location outside of CodeIgniter, |
| 55 | or already have the script tag in your markup. |
| 56 | |
| 57 | Once loaded, the jQuery library object will be available using: |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 58 | |
| 59 | $this->javascript |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | |
| 61 | Setup and Configuration |
| 62 | ======================= |
| 63 | |
| 64 | Set these variables in your view |
| 65 | -------------------------------- |
| 66 | |
| 67 | As a Javascript library, your files must be available to your |
| 68 | application. |
| 69 | |
| 70 | As Javascript is a client side language, the library must be able to |
| 71 | write content into your final output. This generally means a view. |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 72 | You'll need to include the following variables in the ``<head>`` |
| 73 | sections of your output. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
| 75 | :: |
| 76 | |
Derek Jones | d095adf | 2011-10-05 15:55:20 -0500 | [diff] [blame] | 77 | <?php echo $library_src;?> |
| 78 | <?php echo $script_head;?> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | |
| 80 | |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 81 | ``$library_src``, is where the actual library file will be loaded, as |
| 82 | well as any subsequent plugin script calls; $script_head is where |
| 83 | specific events, functions and other commands will be rendered. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
| 85 | Set the path to the librarys with config items |
| 86 | ---------------------------------------------- |
| 87 | |
| 88 | There are some configuration items in Javascript library. These can |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 89 | either be set in *application/config.php*, within its own |
| 90 | *config/javascript.php* file, or within any controller usings the |
| 91 | ``set_item()`` function. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
| 93 | An image to be used as an "ajax loader", or progress indicator. Without |
| 94 | one, the simple text message of "loading" will appear when Ajax calls |
| 95 | need to be made. |
| 96 | |
| 97 | :: |
| 98 | |
Derek Jones | d095adf | 2011-10-05 15:55:20 -0500 | [diff] [blame] | 99 | $config['javascript_location'] = 'http://localhost/codeigniter/themes/js/jquery/'; |
| 100 | $config['javascript_ajax_img'] = 'images/ajax-loader.gif'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | |
| 102 | If you keep your files in the same directories they were downloaded |
| 103 | from, then you need not set this configuration items. |
| 104 | |
| 105 | The jQuery Class |
| 106 | ================ |
| 107 | |
| 108 | To initialize the jQuery class manually in your controller constructor, |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 109 | use the ``$this->load->library()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Chad Hedgcock | a28812a | 2012-04-25 23:29:52 -0300 | [diff] [blame] | 111 | $this->load->library('javascript/jquery'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 112 | |
| 113 | You may send an optional parameter to determine whether or not a script |
| 114 | tag for the main jQuery file will be automatically included when loading |
| 115 | the library. It will be created by default. To prevent this, load the |
| 116 | library as follows:: |
| 117 | |
Chad Hedgcock | a28812a | 2012-04-25 23:29:52 -0300 | [diff] [blame] | 118 | $this->load->library('javascript/jquery', FALSE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
| 120 | Once loaded, the jQuery library object will be available using: |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 121 | |
| 122 | $this->jquery |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | |
| 124 | jQuery Events |
| 125 | ============= |
| 126 | |
| 127 | Events are set using the following syntax. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | :: |
| 129 | |
| 130 | $this->jquery->event('element_path', code_to_run()); |
| 131 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | In the above example: |
| 133 | |
| 134 | - "event" is any of blur, change, click, dblclick, error, focus, hover, |
| 135 | keydown, keyup, load, mousedown, mouseup, mouseover, mouseup, resize, |
| 136 | scroll, or unload. |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 137 | - "element_path" is any valid `jQuery selector |
Master Yoda | bd2a7e4 | 2015-03-25 02:36:31 -0700 | [diff] [blame] | 138 | <http://api.jquery.com/category/selectors/>`_. Due to jQuery's unique |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | selector syntax, this is usually an element id, or CSS selector. For |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 140 | example "#notice_area" would effect ``<div id="notice_area">``, and |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | "#content a.notice" would effect all anchors with a class of "notice" |
| 142 | in the div with id "content". |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 143 | - "``code_to_run()``" is script your write yourself, or an action such as |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | an effect from the jQuery library below. |
| 145 | |
| 146 | Effects |
| 147 | ======= |
| 148 | |
| 149 | The query library supports a powerful |
Master Yoda | bd2a7e4 | 2015-03-25 02:36:31 -0700 | [diff] [blame] | 150 | `Effects <http://api.jquery.com/category/effects/>`_ repertoire. Before an effect |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 151 | can be used, it must be loaded:: |
| 152 | |
| 153 | $this->jquery->effect([optional path] plugin name); // for example $this->jquery->effect('bounce'); |
| 154 | |
| 155 | |
| 156 | hide() / show() |
| 157 | --------------- |
| 158 | |
| 159 | Each of this functions will affect the visibility of an item on your |
| 160 | page. hide() will set an item invisible, show() will reveal it. |
| 161 | |
| 162 | :: |
| 163 | |
Derek Jones | d095adf | 2011-10-05 15:55:20 -0500 | [diff] [blame] | 164 | $this->jquery->hide(target, optional speed, optional extra information); |
| 165 | $this->jquery->show(target, optional speed, optional extra information); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 166 | |
| 167 | |
| 168 | - "target" will be any valid jQuery selector or selectors. |
| 169 | - "speed" is optional, and is set to either slow, normal, fast, or |
| 170 | alternatively a number of milliseconds. |
| 171 | - "extra information" is optional, and could include a callback, or |
| 172 | other additional information. |
| 173 | |
| 174 | toggle() |
| 175 | -------- |
| 176 | |
| 177 | toggle() will change the visibility of an item to the opposite of its |
| 178 | current state, hiding visible elements, and revealing hidden ones. |
| 179 | |
| 180 | :: |
| 181 | |
| 182 | $this->jquery->toggle(target); |
| 183 | |
| 184 | |
| 185 | - "target" will be any valid jQuery selector or selectors. |
| 186 | |
| 187 | animate() |
| 188 | --------- |
| 189 | |
| 190 | :: |
| 191 | |
| 192 | $this->jquery->animate(target, parameters, optional speed, optional extra information); |
| 193 | |
| 194 | |
| 195 | - "target" will be any valid jQuery selector or selectors. |
| 196 | - "parameters" in jQuery would generally include a series of CSS |
| 197 | properties that you wish to change. |
| 198 | - "speed" is optional, and is set to either slow, normal, fast, or |
| 199 | alternatively a number of milliseconds. |
| 200 | - "extra information" is optional, and could include a callback, or |
| 201 | other additional information. |
| 202 | |
| 203 | For a full summary, see |
Master Yoda | bd2a7e4 | 2015-03-25 02:36:31 -0700 | [diff] [blame] | 204 | `http://api.jquery.com/animate/ <http://api.jquery.com/animate/>`_ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 205 | |
| 206 | Here is an example of an animate() called on a div with an id of "note", |
| 207 | and triggered by a click using the jQuery library's click() event. |
| 208 | |
| 209 | :: |
| 210 | |
Derek Jones | d095adf | 2011-10-05 15:55:20 -0500 | [diff] [blame] | 211 | $params = array( |
| 212 | 'height' => 80, |
| 213 | 'width' => '50%', |
| 214 | 'marginLeft' => 125 |
| 215 | ); |
János Rusiczki | 126c09b | 2012-10-16 01:50:31 +0300 | [diff] [blame] | 216 | $this->jquery->click('#trigger', $this->jquery->animate('#note', $params, 'normal')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 217 | |
| 218 | fadeIn() / fadeOut() |
| 219 | -------------------- |
| 220 | |
| 221 | :: |
| 222 | |
Derek Jones | d095adf | 2011-10-05 15:55:20 -0500 | [diff] [blame] | 223 | $this->jquery->fadeIn(target, optional speed, optional extra information); |
| 224 | $this->jquery->fadeOut(target, optional speed, optional extra information); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 225 | |
| 226 | |
| 227 | - "target" will be any valid jQuery selector or selectors. |
| 228 | - "speed" is optional, and is set to either slow, normal, fast, or |
| 229 | alternatively a number of milliseconds. |
| 230 | - "extra information" is optional, and could include a callback, or |
| 231 | other additional information. |
| 232 | |
| 233 | toggleClass() |
| 234 | ------------- |
| 235 | |
| 236 | This function will add or remove a CSS class to its target. |
| 237 | |
| 238 | :: |
| 239 | |
| 240 | $this->jquery->toggleClass(target, class) |
| 241 | |
| 242 | |
| 243 | - "target" will be any valid jQuery selector or selectors. |
| 244 | - "class" is any CSS classname. Note that this class must be defined |
| 245 | and available in a CSS that is already loaded. |
| 246 | |
| 247 | fadeIn() / fadeOut() |
| 248 | -------------------- |
| 249 | |
| 250 | These effects cause an element(s) to disappear or reappear over time. |
| 251 | |
| 252 | :: |
| 253 | |
Derek Jones | d095adf | 2011-10-05 15:55:20 -0500 | [diff] [blame] | 254 | $this->jquery->fadeIn(target, optional speed, optional extra information); |
| 255 | $this->jquery->fadeOut(target, optional speed, optional extra information); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 256 | |
| 257 | |
| 258 | - "target" will be any valid jQuery selector or selectors. |
| 259 | - "speed" is optional, and is set to either slow, normal, fast, or |
| 260 | alternatively a number of milliseconds. |
| 261 | - "extra information" is optional, and could include a callback, or |
| 262 | other additional information. |
| 263 | |
| 264 | slideUp() / slideDown() / slideToggle() |
| 265 | --------------------------------------- |
| 266 | |
| 267 | These effects cause an element(s) to slide. |
| 268 | |
| 269 | :: |
| 270 | |
Derek Jones | d095adf | 2011-10-05 15:55:20 -0500 | [diff] [blame] | 271 | $this->jquery->slideUp(target, optional speed, optional extra information); |
| 272 | $this->jquery->slideDown(target, optional speed, optional extra information); |
| 273 | $this->jquery->slideToggle(target, optional speed, optional extra information); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 274 | |
| 275 | |
| 276 | - "target" will be any valid jQuery selector or selectors. |
| 277 | - "speed" is optional, and is set to either slow, normal, fast, or |
| 278 | alternatively a number of milliseconds. |
| 279 | - "extra information" is optional, and could include a callback, or |
| 280 | other additional information. |
| 281 | |
| 282 | Plugins |
| 283 | ======= |
| 284 | |
| 285 | Some select jQuery plugins are made available using this library. |
| 286 | |
| 287 | corner() |
| 288 | -------- |
| 289 | |
| 290 | Used to add distinct corners to page elements. For full details see |
Master Yoda | bd2a7e4 | 2015-03-25 02:36:31 -0700 | [diff] [blame] | 291 | `http://malsup.com/jquery/corner/ <http://malsup.com/jquery/corner/>`_ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 292 | |
| 293 | :: |
| 294 | |
| 295 | $this->jquery->corner(target, corner_style); |
| 296 | |
| 297 | |
| 298 | - "target" will be any valid jQuery selector or selectors. |
| 299 | - "corner_style" is optional, and can be set to any valid style such |
| 300 | as round, sharp, bevel, bite, dog, etc. Individual corners can be set |
| 301 | by following the style with a space and using "tl" (top left), "tr" |
| 302 | (top right), "bl" (bottom left), or "br" (bottom right). |
| 303 | |
| 304 | :: |
| 305 | |
| 306 | $this->jquery->corner("#note", "cool tl br"); |
| 307 | |
| 308 | |
| 309 | tablesorter() |
| 310 | ------------- |
| 311 | |
| 312 | description to come |
| 313 | |
| 314 | modal() |
| 315 | ------- |
| 316 | |
| 317 | description to come |
| 318 | |
| 319 | calendar() |
| 320 | ---------- |
| 321 | |
Andrey Andreev | 46145fa | 2014-02-07 15:49:06 +0200 | [diff] [blame] | 322 | description to come |