| | 1288 | /** |
|---|
| | 1289 | * Add a JS file to the specified JavaScript context. |
|---|
| | 1290 | * |
|---|
| | 1291 | * For add JS libraries in the global space use the 'global' context |
|---|
| | 1292 | * |
|---|
| | 1293 | * @param string $context The JS library context |
|---|
| | 1294 | * @param string $path Path to the library. It must be relatative to $CFG->dirroot |
|---|
| | 1295 | * @param boolean $external Specifies if the library must to be loaded using their own <script> tag |
|---|
| | 1296 | * @param boolean $overwrite if you are overwriting a JS configuration |
|---|
| | 1297 | */ |
|---|
| | 1298 | function templates_add_js_context($context,$path,$external=true,$overwrite=false){ |
|---|
| | 1299 | global $CFG,$js_files; |
|---|
| | 1300 | |
|---|
| | 1301 | if(!isset($js_files)){ |
|---|
| | 1302 | $js_files = array(); |
|---|
| | 1303 | $js_files['global'][]=array("path"=>"lib/global.js","external"=>false); |
|---|
| | 1304 | } |
|---|
| | 1305 | |
|---|
| | 1306 | if(!empty($context)){ |
|---|
| | 1307 | if(!array_key_exists($context,$js_files)||$overwrite){ |
|---|
| | 1308 | $js_files[$context] = array(); |
|---|
| | 1309 | } |
|---|
| | 1310 | if(file_exists($CFG->dirroot.$path)){ |
|---|
| | 1311 | $js_files[$context][]=array('path'=>$path,'external'=>$external); |
|---|
| | 1312 | } |
|---|
| | 1313 | } |
|---|
| | 1314 | } |
|---|
| | 1315 | |
|---|
| | 1316 | /** |
|---|
| | 1317 | * Setup the JS libraries for the specified context |
|---|
| | 1318 | * |
|---|
| | 1319 | * @param string $context Context to be loaded (default: global) |
|---|
| | 1320 | */ |
|---|
| | 1321 | function templates_js_setup($context="global"){ |
|---|
| | 1322 | global $CFG,$PAGE,$js_files,$metatags; |
|---|
| | 1323 | |
|---|
| | 1324 | if(!$PAGE->js_setup[$context]){ |
|---|
| | 1325 | $inlinefiles = null; |
|---|
| | 1326 | foreach($js_files as $js_context => $files){ |
|---|
| | 1327 | if($context == $js_context){ |
|---|
| | 1328 | foreach($files as $file){ |
|---|
| | 1329 | if($file['external']){ |
|---|
| | 1330 | $resp.="\n<script type=\"text/JavaScript\" language=\"JavaScript\" src=\"{$CFG->wwwroot}{$file['path']}\"><!-- $context JS library--></script>\n"; |
|---|
| | 1331 | } |
|---|
| | 1332 | else{ |
|---|
| | 1333 | $inlinefiles.=file_get_contents($CFG->dirroot.$file['path']); |
|---|
| | 1334 | } |
|---|
| | 1335 | } |
|---|
| | 1336 | } |
|---|
| | 1337 | } |
|---|
| | 1338 | if($inlinefiles!=null){ |
|---|
| | 1339 | //@todo Add JS compress |
|---|
| | 1340 | $inlinefiles=str_replace("{{url}}",$CFG->wwwroot,$inlinefiles); |
|---|
| | 1341 | $resp.="\n<script type=\"text/JavaScript\" language=\"JavaScript\">$inlinefiles</script>\n"; |
|---|
| | 1342 | |
|---|
| | 1343 | } |
|---|
| | 1344 | $PAGE->js_setup[$context]=true; |
|---|
| | 1345 | $metatags.=$resp; |
|---|
| | 1346 | } |
|---|
| | 1347 | } |
|---|
| | 1348 | |
|---|
| | 1349 | /** |
|---|
| | 1350 | * This function checks if the specified JS context is available (configured) or not |
|---|
| | 1351 | * |
|---|
| | 1352 | * @param string $context Context to be checked |
|---|
| | 1353 | * @return boolean True if the context is avaible, false if not |
|---|
| | 1354 | */ |
|---|
| | 1355 | function templates_js_available($context){ |
|---|
| | 1356 | global $js_files; |
|---|
| | 1357 | return array_key_exists($context,$js_files); |
|---|
| | 1358 | } |
|---|
| | 1359 | |
|---|