Source for file JavaScript.php

Documentation is available at JavaScript.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Web
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * PHP to JavaScript helper.
  29. 29:  *
  30. 30:  * <code>
  31. 31:  * $js = new JavaScript;
  32. 32:  * $js->jQuery('.prod img')
  33. 33:  *     ->css('position', 'relative')
  34. 34:  *     ->animate(array('top' => '100px'));
  35. 35:  * echo $js;
  36. 36:  * </code>
  37. 37:  *
  38. 38:  * @author     David Grudl
  39. 39:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  40. 40:  * @package    Nette\Web
  41. 41:  */
  42. 42: class JavaScript extends Object
  43. 43: {
  44. 44:     /** @var string */
  45. 45:     protected $js;
  46. 46:  
  47. 47:     /** @var string|FALSE*/
  48. 48:     private $state '';
  49. 49:  
  50. 50:  
  51. 51:     public function __construct($init ''$ref NULL)
  52. 52:     {
  53. 53:         $this->js = $ref;
  54. 54:         $this->js = $init;
  55. 55:     }
  56. 56:  
  57. 57:  
  58. 58:  
  59. 59:     /**
  60. 60:      * Sets value of a JavaScript property.
  61. 61:      * @param  string  property name
  62. 62:      * @param  mixed   property value
  63. 63:      * @return void 
  64. 64:      */
  65. 65:     public function __set($name$value)
  66. 66:     {
  67. 67:         if ($this->state === FALSE{
  68. 68:             throw new InvalidStateException('Invalid syntax.');
  69. 69:  
  70. 70:         elseif ($value instanceof self{
  71. 71:             $this->js .= $this->state $name ' = ' $value->js;
  72. 72:  
  73. 73:         else {
  74. 74:             $this->js .= $this->state $name ' = ' json_encode($value);
  75. 75:         }
  76. 76:  
  77. 77:         $this->state FALSE;
  78. 78:     }
  79. 79:  
  80. 80:  
  81. 81:  
  82. 82:     /**
  83. 83:      * Returns JavaScript property value.
  84. 84:      * @param  string  property name
  85. 85:      * @return JavaScript provides a fluent interface
  86. 86:      */
  87. 87:     public function &__get($name)
  88. 88:     {
  89. 89:         if ($this->state === FALSE{
  90. 90:             throw new InvalidStateException('Invalid syntax.');
  91. 91:  
  92. 92:         elseif ($name === 'var'{
  93. 93:             $this->js .= $this->state $name ' ';
  94. 94:  
  95. 95:         else {
  96. 96:             $this->js .= $this->state $name;
  97. 97:             $this->state '.';
  98. 98:         }
  99. 99:  
  100. 100:         return $this;
  101. 101:     }
  102. 102:  
  103. 103:  
  104. 104:  
  105. 105:     /**
  106. 106:      * Calls JavaScript function.
  107. 107:      * @param  string  method name
  108. 108:      * @param  array   arguments
  109. 109:      * @return JavaScript  provides a fluent interface
  110. 110:      */
  111. 111:     public function __call($method$args)
  112. 112:     {
  113. 113:         if ($this->state === FALSE{
  114. 114:             throw new InvalidStateException('Invalid syntax.');
  115. 115:         }
  116. 116:         foreach ($args as $key => $arg{
  117. 117:             $args[$key$arg instanceof self $arg->js json_encode($arg);
  118. 118:         }
  119. 119:         $this->js .= $this->state $method '(' implode(', '$args')';
  120. 120:         $this->state '.';
  121. 121:         return $this;
  122. 122:     }
  123. 123:  
  124. 124:  
  125. 125:  
  126. 126:     /**
  127. 127:      * Appends user expressions.
  128. 128:      * @param  mixed  one or more parameters
  129. 129:      * @return JavaScript provides a fluent interface
  130. 130:      */
  131. 131:     public function raw($arg)
  132. 132:     {
  133. 133:         $this->state '';
  134. 134:         foreach (func_get_args(as $arg{
  135. 135:             $this->js .= is_string($arg$arg json_encode($arg);
  136. 136:         }
  137. 137:         return $this;
  138. 138:     }
  139. 139:  
  140. 140:  
  141. 141:  
  142. 142:     /**
  143. 143:      * Returns string represenation of JavaScript expression.
  144. 144:      * @return string 
  145. 145:      */
  146. 146:     public function __toString()
  147. 147:     {
  148. 148:         return $this->js;
  149. 149:     }
  150. 150: