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

Revision 2537, 6.8 KB checked in by amuller, 14 years ago (diff)

Ticket #1036 - implementação do packer do js

  • 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                                                                        if ($GLOBALS['phpgw_info']['server']['jspacker'] == "True")
135                                                                        {
136                                                                                require_once('class.JavaScriptPacker.php');
137                                                                                $script = file_get_contents(PHPGW_INCLUDE_ROOT .SEP .$app .SEP .'js' .SEP . $pkg .SEP. $file . '.js');
138                                                                                $packer = new JavaScriptPacker($script, 'Normal', true, false);
139                                                                                $packed = $packer->pack();
140                                                                                $links .= "<script>".$packed."</script>\n";
141                                                                        }
142                                                                        else
143                                                                        {
144                                                                                $links .= '<script type="text/javascript" src="'
145                                                                                        . $GLOBALS['phpgw_info']['server']['webserver_url']
146                                                                                        . "/$app/js/$pkg/$file" . '">'
147                                                                                        . "</script>\n";
148                                                                        }
149                                                                }
150                                                        }
151                                                }
152                                        }
153                                }
154                        }
155                        return $links;
156                }
157
158                /**
159                * Sets an onLoad action for a page
160                *
161                * @param string javascript to be used
162                */
163                function set_onload($code)
164                {
165                        $this->body['onLoad'] .= $code;
166                }
167
168                /**
169                * Sets an onUnload action for a page
170                *
171                * @param string javascript to be used
172                */
173                function set_onunload($code)
174                {
175                        $this->body['onUnload'] .= $code;
176                }
177
178                /**
179                * Sets an onResize action for a page
180                *
181                * @param string javascript to be used
182                */
183                function set_onresize($code)
184                {
185                        $this->body['onResize'] .= $code;
186                }
187
188                /**
189                * DO NOT USE - NOT SURE IF I AM GOING TO USE IT - ALSO IT NEEDS SOME CHANGES!!!!
190                * Used for removing a file or package of files to be included in the head section of a page
191                *
192                * @param string $app application to use
193                * @param string $package the name of the package to be removed
194                * @param string $file the name of a file in the package to be removed - if ommitted package is removed
195                */
196                function unset_script_link($app, $package, $file=False)
197                {
198                        if($file !== False)
199                        {
200                                unset($this->files[$app][$package][$file]);
201                        }
202                        else
203                        {
204                                unset($this->files[$app][$package]);
205                        }
206                }
207
208                /**
209                * Checks to make sure a valid package and file name is provided
210                *
211                * @param string $package package to be included
212                * @param string $file file to be included - no ".js" on the end
213                * @param string $app application directory to search - default = phpgwapi
214                * @returns bool was the file found?
215                */
216                function validate_file($package, $file, $app='phpgwapi')
217                {
218                        if(is_readable(PHPGW_INCLUDE_ROOT .SEP .$app .SEP .'js' .SEP . $package .SEP. $file . '.js'))
219                        {
220                                $this->files[$app][$package][$file] = $file;
221                                return True;
222                        }
223                        elseif($app != 'phpgwapi')
224                        {
225                                if(is_readable(PHPGW_INCLUDE_ROOT .SEP .'phpgwapi' .SEP .'js' .SEP . $package .SEP . $file . '.js'))
226                                {
227                                        $this->files['phpgwapi'][$package][$file] = $file;
228                                        return True;
229                                }
230                                return False;
231                        }
232                }
233        }
234?>
Note: See TracBrowser for help on using the repository browser.