Fixed focus after ajax page reload
[fa-stable.git] / includes / ajax.inc
1 <?php
2 require_once($path_to_root. "/includes/JsHttpRequest.php");
3
4 class Ajax extends JsHttpRequest {
5
6         var $aCommands = array();
7         var $triggers = array();
8         
9     function Ajax() 
10     {
11           $this->JsHttpRequest($_SESSION['language']->encoding);
12     }
13         //
14         //      This function is used in ctrl routines to activate
15         //      update of ajaxified html element selected by given name/id.
16         //
17         function activate($trigname) {
18           if (in_ajax()) {
19                 $this->triggers[$trigname] = true;
20           }
21         }
22         //
23         //      Javascript clientside redirection.
24         //      This is the last command added to reponse (if any).
25         //
26         function redirect($url) {
27             if(in_ajax()) {
28                   $this->_addCommand(true, array('n'=>'rd'),$url);
29                   $this->run();
30             }
31         }
32         //
33         // Adds an executable Javascript code.
34         //
35         function addScript($trigger, $sJS)
36         {
37                 $this->_addCommand($trigger, array('n'=>'js'),$sJS);
38                 return $this;
39         }
40         //
41         // Assign target attribute with data.
42         //
43         function addAssign($trigger, $sTarget,$sAttribute,$sData)
44         {
45                 $this->_addCommand($trigger, array('n'=>'as','t'=>$sTarget,'p'=>$sAttribute),$sData);
46                 return $this;
47         }
48         //
49         // Updates input element or label with data.
50         //
51         function addUpdate($trigger, $sTarget, $sData)
52         {
53                 $this->_addCommand($trigger, array('n'=>'up','t'=>$sTarget),$sData);
54                 return $this;
55         }
56         //
57         // Set disable state of element.
58         //
59         function addDisable($trigger, $sTarget, $sData=true)
60         {
61                 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget),$sData);
62                 return $this;
63         }
64         //
65         // Set state of element to enabled.
66         //
67         function addEnable($trigger, $sTarget, $sData=true)
68         {
69                 $this->_addCommand($trigger, array('n'=>'di','t'=>$sTarget), !$sData);
70                 return $this;
71         }
72         //
73         // Set current focus.
74         //
75         function addFocus($trigger, $sTarget)
76         {
77                 $this->_addCommand($trigger, array('n'=>'fc'),$sTarget);
78                 return $this;
79         }
80         //
81         // Internal procedure adding command to response.
82         //
83         function _addCommand($trigger, $aAttributes, $mData)
84         {
85           if ($this->isActive() && ($trigger !== false)) {
86 //              display_error('adding '.$trigger.':'.htmlentities($mData));
87
88                 $aAttributes['why'] = $trigger;
89                 $aAttributes['data'] = $mData;
90                 $this->aCommands[] = $aAttributes;
91           }
92         }
93         /*
94         * Register binds function with ajax call parameter
95         
96         function register($trigger, $function) 
97         {
98             if (isset($_REQUEST[$trigger])) {
99                 $function(&$this);
100             }
101         }
102         */
103         function run() {
104             
105             if (!$this->isActive()) return;
106
107                 // remove not active commands
108                 foreach ($this->aCommands as $idx => $com) {
109 // If we should reload whole page content ignore all commands but the update.
110 // This is page repost equivalent, although header and footer are not reloaded.
111                   if ($com['why'] !== true && !isset($this->triggers[$com['why']])) {
112                         unset($this->aCommands[$idx]);
113 //                      display_error('unset '.$com['t']);
114                   }
115                   else
116                   if($com['n'] == 'up' && $com['t'] == '_page_body') {
117                           $cmds = array($com);
118                           foreach( $this->aCommands as $k=> $cmd) {
119                                 if ($cmd['n'] == 'fc') {        // save focus
120                                         $cmds[] = $cmd; break;
121                                 }
122                           }
123                           $this->aCommands = $cmds;
124                           break;
125                   }
126                 }
127 //              display_error('Activate:'.htmlentities(print_r($this->triggers, true)));
128 //              display_error('Commands :'.htmlentities(print_r($this->aCommands, true)));
129             $GLOBALS['_RESULT'] = $this->aCommands;
130 //          exit();
131         }
132 }
133
134 function in_ajax() {
135     global $Ajax;
136     return $Ajax->isActive();
137 }
138
139 ?>