source: trunk/phpgwapi/inc/class.javascript.inc.php @ 2169

Revision 2169, 6.3 KB checked in by amuller, 14 years ago (diff)

Ticket #911 - Unificação da parte ajax do expresso

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare API - JavaScript                                              *
4  * Written by Dave Hall skwashd at phpgroupware.org                         *
5  * Copyright (C) 2003 Free Software Foundation Inc                          *         
6  * -------------------------------------------------------------------------*
7  * This library is part of the eGroupWare API                               *
8  * http://www.egroupware.org/api                                            *
9  * ------------------------------------------------------------------------ *
10  *  This program is Free Software; you can redistribute it and/or modify it *
11  *  under the terms of the GNU General Public License as published by the   *
12  *  Free Software Foundation; either version 2 of the License, or (at your  *
13  *  option) any later version.                                              *
14  \**************************************************************************/
15
16       /**
17       * eGroupWare javascript support class
18       *
19       * Only instanstiate this class using:
20       * <code>
21       *  if(!@is_object($GLOBALS['phpgw']->js))
22       *  {
23       *    $GLOBALS['phpgw']->js = CreateObject('phpgwapi.javascript');
24       *  }
25       * </code>
26       *
27       * This way a theme can see if this is a defined object and include the data,
28       * while the is_object() wrapper prevents whiping out existing data held in
29       * this instance variables, primarily the $files variable.
30       *
31       * Note: The package arguement is the subdirectory of js - all js should live in subdirectories
32       *
33       * @package phpgwapi
34       * @subpackage sessions
35       * @abstract
36       * @author Dave Hall
37       * @copyright &copy; 2003 Free Software Foundation
38       * @license GPL
39       * @uses template
40       */
41        class javascript
42        {
43                /**
44                * @var array elements to be used for the on(Un)Load attributes of the body tag
45                */
46                var $body;
47
48                /**
49                * @var array list of validated files to be included in the head section of a page
50                */
51                var $files;
52
53                /**
54                * @var object used for holding an instance of the Template class
55                */
56                var $t;
57               
58                /**
59                * Constructor
60                *
61                * Initialize the instance variables
62                */
63                function javascript()
64                {
65                        //$this->t = CreateObject('phpgwapi.Template', ExecMethod('phpgwapi.phpgw.common.get_tpl_dir','phpgwapi'));
66                        //not currently used, but will be soon - I hope :)
67                }
68
69               
70                /**
71                * Returns the javascript required for displaying a popup message box
72                *
73                * @param string $msg the message to be displayed to user
74                * @returns string the javascript to be used for displaying the message
75                */
76                function get_alert($msg)
77                {
78                  return 'return alert("'.lang($msg).'");';
79                }
80
81                /**
82                * Adds on(Un)Load= attributes to the body tag of a page
83                *
84                * @returns string the attributes to be used
85                */
86                function get_body_attribs()
87                {
88                        $js = '';
89                        foreach(array('onLoad','onUnload','onResize') as $what)
90                        {
91                                if (!empty($this->body[$what]))
92                                {
93                                        $js .= ' '.$what.'="' . str_replace(array('"','\\'),array('\\"','\\\\'),$this->body[$what]) . '"';
94                                }
95                        }
96                        return $js;
97                }
98
99                /**
100                * Returns the javascript required for displaying a confirmation message box
101                *
102                * @param string $msg the message to be displayed to user
103                * @returns string the javascript to be used for displaying the message
104                */
105                function get_confirm($msg)
106                {
107                        return 'return confirm("'.lang($msg).'");';
108                }
109               
110                /**
111                * Used for generating the list of external js files to be included in the head of a page
112                *
113                * NOTE: This method should only be called by the template class.
114                * The validation is done when the file is added so we don't have to worry now
115                *
116                * @returns string the html needed for importing the js into a page
117                */
118                function get_script_links()
119                {
120                        $links = '';
121                        if(!empty($this->files) && is_array($this->files))
122                        {
123                                $links = "<!--JS Imports from phpGW javascript class -->\n";
124                                foreach($this->files as $app => $packages)
125                                {
126                                        if(!empty($packages) && is_array($packages))
127                                        {
128                                                foreach($packages as $pkg => $files)
129                                                {
130                                                        if(!empty($files) && is_array($files))
131                                                        {
132                                                                foreach($files as $file => $ignored)
133                                                                {
134                                                                        $links .= '<script type="text/javascript" src="'
135                                                                        . $GLOBALS['phpgw_info']['server']['webserver_url']
136                                                                        . "/$app/js/$pkg/$file" . '.js">'
137                                                                        . "</script>\n";
138                                                                }
139                                                        }
140                                                }
141                                        }
142                                }
143                        }
144                        return $links;
145                }
146
147                /**
148                * Sets an onLoad action for a page
149                *
150                * @param string javascript to be used
151                */
152                function set_onload($code)
153                {
154                        $this->body['onLoad'] .= $code;
155                }
156
157                /**
158                * Sets an onUnload action for a page
159                *
160                * @param string javascript to be used
161                */
162                function set_onunload($code)
163                {
164                        $this->body['onUnload'] .= $code;
165                }
166
167                /**
168                * Sets an onResize action for a page
169                *
170                * @param string javascript to be used
171                */
172                function set_onresize($code)
173                {
174                        $this->body['onResize'] .= $code;
175                }
176
177                /**
178                * DO NOT USE - NOT SURE IF I AM GOING TO USE IT - ALSO IT NEEDS SOME CHANGES!!!!
179                * Used for removing a file or package of files to be included in the head section of a page
180                *
181                * @param string $app application to use
182                * @param string $package the name of the package to be removed
183                * @param string $file the name of a file in the package to be removed - if ommitted package is removed
184                */
185                function unset_script_link($app, $package, $file=False)
186                {
187                        if($file !== False)
188                        {
189                                unset($this->files[$app][$package][$file]);
190                        }
191                        else
192                        {
193                                unset($this->files[$app][$package]);
194                        }
195                }
196
197                /**
198                * Checks to make sure a valid package and file name is provided
199                *
200                * @param string $package package to be included
201                * @param string $file file to be included - no ".js" on the end
202                * @param string $app application directory to search - default = phpgwapi
203                * @returns bool was the file found?
204                */
205                function validate_file($package, $file, $app='phpgwapi')
206                {
207                        if(is_readable(PHPGW_INCLUDE_ROOT .SEP .$app .SEP .'js' .SEP . $package .SEP. $file . '.js'))
208                        {
209                                $this->files[$app][$package][$file] = $file;
210                                return True;
211                        }
212                        elseif($app != 'phpgwapi')
213                        {
214                                if(is_readable(PHPGW_INCLUDE_ROOT .SEP .'phpgwapi' .SEP .'js' .SEP . $package .SEP . $file . '.js'))
215                                {
216                                        $this->files['phpgwapi'][$package][$file] = $file;
217                                        return True;
218                                }
219                                return False;
220                        }
221                }
222        }
223?>
Note: See TracBrowser for help on using the repository browser.