forked from surebert/surebert-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS.php
72 lines (64 loc) · 1.61 KB
/
JS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Used to respond to javascript requests
* @author visco
* @package sb_JS
*/
class sb_JS{
/**
* The current on response headers
* @var integer
*/
protected static $sb_on_response_headers = 0;
/**
* Uses jsexec_header to set the innerHTML of an element by id
* @param integer $id the HTML element id
* @param string $html The innerHTML to set
* @return string
*/
public static function set_html($id, $html){
$js = '$("'.$id.'").html('.json_encode($html).');';
return self::exec_header($js);
}
/**
* Executes a script at a certain path
* @param string $script_path full path
*/
public static function exec_script($script_path, $context=null){
header('Content-type: text/javascript');
if(!is_null($context)){
if(is_object($context)){
$context = get_object_vars($context);
}
if(is_array($context)){
extract($context);
}
}
require($script_path);
}
/**
*
* @param string $message The message to notify
* @param string $class The class to use for the notification 'error', 'success', etc
* @return string
*/
public static function notify($message, $class='success'){
return self::exec_header("sb.notify(".json_encode($message).", ".json_encode($class).");");
}
/**
* Executes the full response text, can be called more than once
* @param type $js
*/
public static function exec_response($js){
header('Content-type: text/javascript');
echo $js;
}
/**
* Executes a response header
* @param type $js
*/
public static function exec_header($js){
header("sb_on_response".(self::$sb_on_response_headers++).": ".$js);
}
}
?>