PageRenderTime 408ms CodeModel.GetById 322ms app.highlight 19ms RepoModel.GetById 62ms app.codeStats 0ms

/system/libraries/Javascript.php

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