blob: 3b7dcc6ff2dac124b6a2f81142aea84b4205382b [file] [log] [blame]
Derek Jones811f4752010-03-02 18:13:59 -06001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3
4
5class CI_Javascript {
6
7 var $_javascript_location = 'js';
8
9 function CI_Javascript($params = array())
10 {
11 $defaults = array('js_library_driver' => 'jquery', 'autoload' => TRUE);
12
13 foreach ($defaults as $key => $val)
14 {
15 if (isset($params[$key]) && $params[$key] !== "")
16 {
17 $defaults[$key] = $params[$key];
18 }
19 }
20
21 extract($defaults);
22
23 $this->CI =& get_instance();
24
25 // load the requested js library
26 $this->CI->load->library($js_library_driver, array('autoload' => $autoload));
27 // make js to refer to current library
28 $this->js =& $this->CI->$js_library_driver;
29
30 log_message('debug', "Javascript Class Initialized and loaded. Driver used: $js_library_driver");
31 }
32
33 // --------------------------------------------------------------------
34 // Event Code
35 // --------------------------------------------------------------------
36
37 /**
38 * Blur
39 *
40 * Outputs a javascript library blur event
41 *
42 * @access public
43 * @param string The element to attach the event to
44 * @param string The code to execute
45 * @return string
46 */
47 function blur($element = 'this', $js = '')
48 {
49 return $this->js->_blur($element, $js);
50 }
51
52 // --------------------------------------------------------------------
53
54 /**
55 * Change
56 *
57 * Outputs a javascript library change event
58 *
59 * @access public
60 * @param string The element to attach the event to
61 * @param string The code to execute
62 * @return string
63 */
64 function change($element = 'this', $js = '')
65 {
66 return $this->js->_change($element, $js);
67 }
68
69 // --------------------------------------------------------------------
70
71 /**
72 * Click
73 *
74 * Outputs a javascript library click event
75 *
76 * @access public
77 * @param string The element to attach the event to
78 * @param string The code to execute
79 * @param boolean whether or not to return false
80 * @return string
81 */
82 function click($element = 'this', $js = '', $ret_false = TRUE)
83 {
84 return $this->js->_click($element, $js, $ret_false);
85 }
86
87 // --------------------------------------------------------------------
88
89 /**
90 * Double Click
91 *
92 * Outputs a javascript library dblclick event
93 *
94 * @access public
95 * @param string The element to attach the event to
96 * @param string The code to execute
97 * @return string
98 */
99 function dblclick($element = 'this', $js = '')
100 {
101 return $this->js->_dblclick($element, $js);
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Error
108 *
109 * Outputs a javascript library error event
110 *
111 * @access public
112 * @param string The element to attach the event to
113 * @param string The code to execute
114 * @return string
115 */
116 function error($element = 'this', $js = '')
117 {
118 return $this->js->_error($element, $js);
119 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Focus
125 *
126 * Outputs a javascript library focus event
127 *
128 * @access public
129 * @param string The element to attach the event to
130 * @param string The code to execute
131 * @return string
132 */
133 function focus($element = 'this', $js = '')
134 {
135 return $this->js->__add_event($focus, $js);
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Hover
142 *
143 * Outputs a javascript library hover event
144 *
145 * @access public
146 * @param string - element
147 * @param string - Javascript code for mouse over
148 * @param string - Javascript code for mouse out
149 * @return string
150 */
151 function hover($element = 'this', $over, $out)
152 {
153 return $this->js->__hover($element, $over, $out);
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Keydown
160 *
161 * Outputs a javascript library keydown event
162 *
163 * @access public
164 * @param string The element to attach the event to
165 * @param string The code to execute
166 * @return string
167 */
168 function keydown($element = 'this', $js = '')
169 {
170 return $this->js->_keydown($element, $js);
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * Keyup
177 *
178 * Outputs a javascript library keydown event
179 *
180 * @access public
181 * @param string The element to attach the event to
182 * @param string The code to execute
183 * @return string
184 */
185 function keyup($element = 'this', $js = '')
186 {
187 return $this->js->_keyup($element, $js);
188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Load
194 *
195 * Outputs a javascript library load event
196 *
197 * @access public
198 * @param string The element to attach the event to
199 * @param string The code to execute
200 * @return string
201 */
202 function load($element = 'this', $js = '')
203 {
204 return $this->js->_load($element, $js);
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Mousedown
211 *
212 * Outputs a javascript library mousedown event
213 *
214 * @access public
215 * @param string The element to attach the event to
216 * @param string The code to execute
217 * @return string
218 */
219 function mousedown($element = 'this', $js = '')
220 {
221 return $this->js->_mousedown($element, $js);
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Mouse Out
228 *
229 * Outputs a javascript library mouseout event
230 *
231 * @access public
232 * @param string The element to attach the event to
233 * @param string The code to execute
234 * @return string
235 */
236 function mouseout($element = 'this', $js = '')
237 {
238 return $this->js->_mouseout($element, $js);
239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Mouse Over
245 *
246 * Outputs a javascript library mouseover event
247 *
248 * @access public
249 * @param string The element to attach the event to
250 * @param string The code to execute
251 * @return string
252 */
253 function mouseover($element = 'this', $js = '')
254 {
255 return $this->js->_mouseover($element, $js);
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Mouseup
262 *
263 * Outputs a javascript library mouseup event
264 *
265 * @access public
266 * @param string The element to attach the event to
267 * @param string The code to execute
268 * @return string
269 */
270 function mouseup($element = 'this', $js = '')
271 {
272 return $this->js->_mouseup($element, $js);
273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * Output
279 *
280 * Outputs the called javascript to the screen
281 *
282 * @access public
283 * @param string The code to output
284 * @return string
285 */
286 function output($js)
287 {
288 return $this->js->_output($js);
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * Ready
295 *
296 * Outputs a javascript library mouseup event
297 *
298 * @access public
299 * @param string The element to attach the event to
300 * @param string The code to execute
301 * @return string
302 */
303 function ready($js)
304 {
305 return $this->js->_document_ready($js);
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * Resize
312 *
313 * Outputs a javascript library resize event
314 *
315 * @access public
316 * @param string The element to attach the event to
317 * @param string The code to execute
318 * @return string
319 */
320 function resize($element = 'this', $js = '')
321 {
322 return $this->js->_resize($element, $js);
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Scroll
329 *
330 * Outputs a javascript library scroll event
331 *
332 * @access public
333 * @param string The element to attach the event to
334 * @param string The code to execute
335 * @return string
336 */
337 function scroll($element = 'this', $js = '')
338 {
339 return $this->js->_scroll($element, $js);
340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Unload
346 *
347 * Outputs a javascript library unload event
348 *
349 * @access public
350 * @param string The element to attach the event to
351 * @param string The code to execute
352 * @return string
353 */
354 function unload($element = 'this', $js = '')
355 {
356 return $this->js->_unload($element, $js);
357 }
358
359 // --------------------------------------------------------------------
360 // Effects
361 // --------------------------------------------------------------------
362
363
364 /**
365 * Add Class
366 *
367 * Outputs a javascript library addClass event
368 *
369 * @access public
370 * @param string - element
371 * @param string - Class to add
372 * @return string
373 */
374 function addClass($element = 'this', $class = '')
375 {
376 return $this->js->_addClass($element, $class);
377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Animate
383 *
384 * Outputs a javascript library animate event
385 *
386 * @access public
387 * @param string - element
388 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
389 * @param string - Javascript callback function
390 * @return string
391 */
392 function animate($element = 'this', $params = array(), $speed = '', $extra = '')
393 {
394 return $this->js->_animate($element, $params, $speed, $extra);
395 }
396
397 // --------------------------------------------------------------------
398
399 /**
400 * Fade In
401 *
402 * Outputs a javascript library hide event
403 *
404 * @access public
405 * @param string - element
406 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
407 * @param string - Javascript callback function
408 * @return string
409 */
410 function fadeIn($element = 'this', $speed = '', $callback = '')
411 {
412 return $this->js->_fadeIn($element, $speed, $callback);
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Fade Out
419 *
420 * Outputs a javascript library hide event
421 *
422 * @access public
423 * @param string - element
424 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
425 * @param string - Javascript callback function
426 * @return string
427 */
428 function fadeOut($element = 'this', $speed = '', $callback = '')
429 {
430 return $this->js->_fadeOut($element, $speed, $callback);
431 }
432 // --------------------------------------------------------------------
433
434 /**
435 * Slide Up
436 *
437 * Outputs a javascript library slideUp event
438 *
439 * @access public
440 * @param string - element
441 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
442 * @param string - Javascript callback function
443 * @return string
444 */
445 function slideUp($element = 'this', $speed = '', $callback = '')
446 {
447 return $this->js->_slideUp($element, $speed, $callback);
448
449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Remove Class
455 *
456 * Outputs a javascript library removeClass event
457 *
458 * @access public
459 * @param string - element
460 * @param string - Class to add
461 * @return string
462 */
463 function removeClass($element = 'this', $class = '')
464 {
465 return $this->js->_removeClass($element, $class);
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Slide Down
472 *
473 * Outputs a javascript library slideDown event
474 *
475 * @access public
476 * @param string - element
477 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
478 * @param string - Javascript callback function
479 * @return string
480 */
481 function slideDown($element = 'this', $speed = '', $callback = '')
482 {
483 return $this->js->_slideDown($element, $speed, $callback);
484 }
485
486 // --------------------------------------------------------------------
487
488 /**
489 * Slide Toggle
490 *
491 * Outputs a javascript library slideToggle event
492 *
493 * @access public
494 * @param string - element
495 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
496 * @param string - Javascript callback function
497 * @return string
498 */
499 function slideToggle($element = 'this', $speed = '', $callback = '')
500 {
501 return $this->js->_slideToggle($element, $speed, $callback);
502
503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Hide
509 *
510 * Outputs a javascript library hide action
511 *
512 * @access public
513 * @param string - element
514 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
515 * @param string - Javascript callback function
516 * @return string
517 */
518 function hide($element = 'this', $speed = '', $callback = '')
519 {
520 return $this->js->_hide($element, $speed, $callback);
521 }
522
523 // --------------------------------------------------------------------
524
525 /**
526 * Toggle
527 *
528 * Outputs a javascript library toggle event
529 *
530 * @access public
531 * @param string - element
532 * @return string
533 */
534 function toggle($element = 'this')
535 {
536 return $this->js->_toggle($element);
537
538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * Toggle Class
544 *
545 * Outputs a javascript library toggle class event
546 *
547 * @access public
548 * @param string - element
549 * @return string
550 */
551 function toggleClass($element = 'this', $class='')
552 {
553 return $this->js->_toggleClass($element, $class);
554 }
555
556 // --------------------------------------------------------------------
557
558 /**
559 * Show
560 *
561 * Outputs a javascript library show event
562 *
563 * @access public
564 * @param string - element
565 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
566 * @param string - Javascript callback function
567 * @return string
568 */
569 function show($element = 'this', $speed = '', $callback = '')
570 {
571 return $this->js->_show($element, $speed, $callback);
572 }
573
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Compile
579 *
580 * gather together all script needing to be output
581 *
582 * @access public
583 * @param string The element to attach the event to
584 * @return string
585 */
586 function compile($view_var = 'script_foot', $script_tags = TRUE)
587 {
588 $this->js->_compile($view_var, $script_tags);
589 }
590
591 /**
592 * Clear Compile
593 *
594 * Clears any previous javascript collected for output
595 *
596 * @access public
597 * @return void
598 */
599 function clear_compile()
600 {
601 $this->js->_clear_compile();
602 }
603
604 // --------------------------------------------------------------------
605
606 /**
607 * External
608 *
609 * Outputs a <script> tag with the source as an external js file
610 *
611 * @access public
612 * @param string The element to attach the event to
613 * @return string
614 */
615 function external($external_file = '', $relative = FALSE)
616 {
617 if ($external_file !== '')
618 {
619 $this->_javascript_location = $external_file;
620 }
621 else
622 {
623 if ($this->CI->config->item('javascript_location') != '')
624 {
625 $this->_javascript_location = $this->CI->config->item('javascript_location');
626 }
627 }
628
629 if ($relative === TRUE OR strncmp($external_file, 'http://', 7) == 0 OR strncmp($external_file, 'https://', 8) == 0)
630 {
631 $str = $this->_open_script($external_file);
632 }
633 elseif (strpos($this->_javascript_location, 'http://') !== FALSE)
634 {
635 $str = $this->_open_script($this->_javascript_location.$external_file);
636 }
637 else
638 {
639 $str = $this->_open_script($this->CI->config->slash_item('base_url').$this->_javascript_location.$external_file);
640 }
641
642 $str .= $this->_close_script();
643 return $str;
644 }
645
646 // --------------------------------------------------------------------
647
648 /**
649 * Inline
650 *
651 * Outputs a <script> tag
652 *
653 * @access public
654 * @param string The element to attach the event to
655 * @param boolean If a CDATA section should be added
656 * @return string
657 */
658 function inline($script, $cdata = TRUE)
659 {
660 $str = $this->_open_script();
661 $str .= ($cdata) ? "\n// <![CDATA[\n{$script}\n// ]]>\n" : "\n{$script}\n";
662 $str .= $this->_close_script();
663
664 return $str;
665 }
666
667 // --------------------------------------------------------------------
668
669 /**
670 * Open Script
671 *
672 * Outputs an opening <script>
673 *
674 * @access private
675 * @param string
676 * @return string
677 */
678 function _open_script($src = '')
679 {
680 $str = '<script type="text/javascript" charset="'.strtolower($this->CI->config->item('charset')).'"';
681 $str .= ($src == '') ? '>' : ' src="'.$src.'">';
682 return $str;
683 }
684
685 // --------------------------------------------------------------------
686
687 /**
688 * Close Script
689 *
690 * Outputs an closing </script>
691 *
692 * @access private
693 * @param string
694 * @return string
695 */
696 function _close_script($extra = "\n")
697 {
698 return "</script>$extra";
699 }
700
701
702 // --------------------------------------------------------------------
703 // --------------------------------------------------------------------
704 // AJAX-Y STUFF - still a testbed
705 // --------------------------------------------------------------------
706 // --------------------------------------------------------------------
707
708 /**
709 * Update
710 *
711 * Outputs a javascript library slideDown event
712 *
713 * @access public
714 * @param string - element
715 * @param string - One of 'slow', 'normal', 'fast', or time in milliseconds
716 * @param string - Javascript callback function
717 * @return string
718 */
719 function update($element = 'this', $speed = '', $callback = '')
720 {
721 return $this->js->_updater($element, $speed, $callback);
722 }
723
724 // --------------------------------------------------------------------
725
726 /**
727 * Generate JSON
728 *
729 * Can be passed a database result or associative array and returns a JSON formatted string
730 *
731 * @param mixed result set or array
732 * @param bool match array types (defaults to objects)
733 * @return string a json formatted string
734 */
735 function generate_json($result = NULL, $match_array_type = FALSE)
736 {
737 // JSON data can optionally be passed to this function
738 // either as a database result object or an array, or a user supplied array
739 if ( ! is_null($result))
740 {
741 if (is_object($result))
742 {
743 $json_result = $result->result_array();
744 }
745 elseif (is_array($result))
746 {
747 $json_result = $result;
748 }
749 else
750 {
751 return $this->_prep_args($result);
752 }
753 }
754 else
755 {
756 return 'null';
757 }
758
759 $json = array();
760 $_is_assoc = TRUE;
761
762 if ( ! is_array($json_result) AND empty($json_result))
763 {
764 show_error("Generate JSON Failed - Illegal key, value pair.");
765 }
766 elseif ($match_array_type)
767 {
768 $_is_assoc = $this->_is_associative_array($json_result);
769 }
770
771 foreach ($json_result as $k => $v)
772 {
773 if ($_is_assoc)
774 {
775 $json[] = $this->_prep_args($k, TRUE).':'.$this->generate_json($v, $match_array_type);
776 }
777 else
778 {
779 $json[] = $this->generate_json($v, $match_array_type);
780 }
781 }
782
783 $json = implode(',', $json);
784
785 return $_is_assoc ? "{".$json."}" : "[".$json."]";
786
787 }
788
789 // --------------------------------------------------------------------
790
791 /**
792 * Is associative array
793 *
794 * Checks for an associative array
795 *
796 * @access public
797 * @param type
798 * @return type
799 */
800 function _is_associative_array($arr)
801 {
802 foreach (array_keys($arr) as $key => $val)
803 {
804 if ($key !== $val)
805 {
806 return TRUE;
807 }
808 }
809
810 return FALSE;
811 }
812
813 // --------------------------------------------------------------------
814
815 /**
816 * Prep Args
817 *
818 * Ensures a standard json value and escapes values
819 *
820 * @access public
821 * @param type
822 * @return type
823 */
824 function _prep_args($result, $is_key = FALSE)
825 {
826 if (is_null($result))
827 {
828 return 'null';
829 }
830 elseif (is_bool($result))
831 {
832 return ($result === TRUE) ? 'true' : 'false';
833 }
834 elseif (is_string($result) OR $is_key)
835 {
836 return '"'.str_replace(array('\\', "\t", "\n", "\r", '"'), array('\\\\', '\\t', '\\n', "\\r", '\"'), $result).'"';
837 }
838 elseif (is_scalar($result))
839 {
840 return $result;
841 }
842 }
843}
844
845
846/* End of file Javascript.php */
847/* Location: ./system/libraries/Javascript.php */