javascript.class.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Javascript utility functions.
  4. *
  5. * @license see /license.txt
  6. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  7. */
  8. class Javascript
  9. {
  10. /**
  11. * Minimify Javascript.
  12. *
  13. * If called with a path
  14. *
  15. * input: /dir/dir/file.js
  16. * returns: /dir/dir/file.min.js
  17. *
  18. * Otherwise returns the actual code.
  19. *
  20. * @param string $arg either a path or actual code
  21. * @return string either a path to minified content (ends with min.js) or actual code
  22. */
  23. public static function minify($arg)
  24. {
  25. if (is_readable($arg))
  26. {
  27. $path = $arg;
  28. $code = file_get_contents($path);
  29. $code = ClosureCompiler::post($code);
  30. $min_path = $path;
  31. $min_path = str_replace('.js', '', $min_path);
  32. $min_path .= '.min.js';
  33. file_put_contents($min_path, $code);
  34. return $min_path;
  35. }
  36. else
  37. {
  38. return ClosureCompiler::post($code);
  39. }
  40. }
  41. /**
  42. * Returns lang object that contains the translation.
  43. *
  44. * Javascript::get_lang('string1', 'string2', 'string3');
  45. *
  46. * returns
  47. *
  48. * if(typeof(lang) == "undefined")
  49. * {
  50. * var lang = {};
  51. * }
  52. * lang.string1 = "...";
  53. * lang.string2 = "...";
  54. * lang.string3 = "...
  55. *
  56. * @param type $_
  57. * @return type
  58. */
  59. public static function get_lang($_)
  60. {
  61. $result = array();
  62. $result[] = 'if(typeof(lang) == "undefined")';
  63. $result[] = '{';
  64. $result[] = ' var lang = {};';
  65. $result[] = '}';
  66. $keys = func_get_args();
  67. foreach ($keys as $key)
  68. {
  69. $value = get_lang($key);
  70. $result[] = 'lang.' . $key . ' = "' . $value . '";';
  71. }
  72. return implode("\n", $result);
  73. }
  74. public static function tag($src)
  75. {
  76. return '<script type="text/javascript" src="' . $src . '"></script>';
  77. }
  78. public static function tag_code($code)
  79. {
  80. $new_line = "\n";
  81. return '<script type="text/javascript">' . $new_line . $code . $new_line . '</script>';
  82. }
  83. }