Source for file StyleCollection.class.php

Documentation is available at StyleCollection.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."GUIManager/StyleCollection.interface.php");
  4.  
  5. /**
  6. * A StyleCollection is one of the tree building pieces of CSS styles. As the name
  7. * suggests it handles a collection of StyleProperties.
  8. *
  9. * The other two CSS styles building pieces are <code>StylePropertiy</code> and
  10. * <code>StyleComponent</code>. To clarify the relationship between these three
  11. * building pieces, consider the following example:
  12. * <pre>
  13. * div {
  14. * margin: 20px;
  15. * border: 1px solid #000;
  16. * }
  17. * </pre>
  18. * <code>div</code> is a <code>StyleCollection</code> consisting of 2
  19. * <code>StyleProperties</code>: <code>margin</code> and <code>border</code>. Each
  20. * of the latter consists of one or more <code>StyleComponents</code>. In
  21. * specific, <code>margin</code> consists of one <code>StyleComponent</code>
  22. * with the value <code>20px</code>, and <code>border</code> has three
  23. * <code>StyleComponents</code> with values <code>1px</code>, <code>solid</code>,
  24. * and <code>#000</code> correspondingly.
  25. *
  26. * @package harmoni.gui
  27. *
  28. * @copyright Copyright &copy; 2005, Middlebury College
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  30. *
  31. * @version $Id: StyleCollection.class.php,v 1.15 2007/09/04 20:25:21 adamfranco Exp $
  32. */
  33. class StyleCollection extends StyleCollectionInterface {
  34.  
  35. var $_id;
  36.  
  37. /**
  38. * The display name of this StyleCollection.
  39. * @var string _displayName
  40. * @access private
  41. */
  42. var $_displayName;
  43. /**
  44. * The description of this StyleCollection.
  45. * @var string _description
  46. * @access private
  47. */
  48. var $_description;
  49. /**
  50. * The selector of this StyleCollection.
  51. * @var string _selector
  52. * @access private
  53. */
  54. var $_selector;
  55. /**
  56. * The class selector of this style collection. A class selector is the string
  57. * that would be included in the 'class' attribute of HTML tags.
  58. * @var string _classSelector
  59. * @access private
  60. */
  61. var $_classSelector;
  62. /**
  63. * An array of the StyleProperties contained by this StyleCollection.
  64. * @var array _SPs
  65. * @access private
  66. */
  67. var $_SPs;
  68. /**
  69. * HTML to place around the the component's content.
  70. *
  71. * @var string $_preHTML;
  72. * @access private
  73. * @since 11/22/05
  74. */
  75. var $_preHTML = "";
  76. /**
  77. * HTML to place around the the component's content.
  78. *
  79. * @var string $_preHTML;
  80. * @access private
  81. * @since 11/22/05
  82. */
  83. var $_postHTML = "";
  84.  
  85. /**
  86. * The constructor.
  87. * @access public
  88. * @param string selector The selector of this StyleCollection.
  89. * @param string classSelector The class selector of this style collection. If <code>null</code>,
  90. * it will be ignored, but the collection will not be able to be applied
  91. * to components.
  92. * @param string displayName The display name of this StyleCollection.
  93. * @param string description The description of this StyleCollection.
  94. ***/
  95. function StyleCollection($selector, $classSelector, $displayName, $description) {
  96. if(func_num_args()<=3){
  97. throwError(new Error("Too few arguments--only ".func_num_args()." of 4","GUIManager",true));
  98. }
  99. $this->_selector = $selector;
  100. $this->_classSelector = $classSelector;
  101. $this->_SPs = array();
  102. $this->_displayName = $displayName;
  103. $this->_description = $description;
  104. }
  105. /**
  106. * Sets the id
  107. *
  108. * @param object HarmoniId $id
  109. * @return void
  110. * @access public
  111. * @since 4/26/06
  112. */
  113. function setId ($id) {
  114. if (!is_object($id))
  115. throwError(new Error("String Id Passed","GUIManager",true));
  116. $this->_id =$id;
  117. }
  118. /**
  119. * Answers the id
  120. *
  121. * @return object HarmoniId
  122. * @access public
  123. * @since 4/26/06
  124. */
  125. function getId () {
  126. if (isset($this->_id)){
  127. return $this->_id;
  128. }else{
  129. $im = Services::getService("Id");
  130. $this->_id = $im->createId();
  131. return $this->_id;
  132. }
  133. }
  134.  
  135. /**
  136. * Sets the index
  137. *
  138. * @param string $component
  139. * @return void
  140. * @access public
  141. * @since 4/26/06
  142. */
  143. function setComponent ($component) {
  144. $this->_component = $component;
  145. }
  146. /**
  147. * Answers the component this style collection acts on ie BLANK, BLOCK, etc.
  148. *
  149. * @return string
  150. * @access public
  151. * @since 4/26/06
  152. */
  153. function getComponent () {
  154. if (isset($this->_component))
  155. return $this->_component;
  156. }
  157.  
  158. /**
  159. * Sets the Index
  160. *
  161. * @param string $index
  162. * @return void
  163. * @access public
  164. * @since 4/26/06
  165. */
  166. function setIndex ($index) {
  167. $this->_index = $index;
  168. }
  169. /**
  170. * Answers the component this style collection acts on ie BLANK, BLOCK, etc.
  171. *
  172. * @return string
  173. * @access public
  174. * @since 4/26/06
  175. */
  176. function getIndex () {
  177. if (isset($this->_index))
  178. return $this->_index;
  179. }
  180.  
  181. /**
  182. * Returns the CSS code for this StyleCollection.
  183. * @access public
  184. * @param string tabs This is a string (normally a bunch of tabs) that will be
  185. * prepended to each text line. This argument is optional but its usage is highly
  186. * recommended in order to produce a nicely formatted HTML output.
  187. * @return string The CSS code for this StyleCollection.
  188. ***/
  189. function getCSS($tabs = "") {
  190. // nothing to return
  191. if (count($this->_SPs) == 0)
  192. return "";
  193.  
  194. $css = $tabs.$this->_selector." {\n\t".$tabs;
  195.  
  196. $values = array();
  197. foreach (array_keys($this->_SPs) as $key)
  198. $values[] = $this->_SPs[$key]->getCSS();
  199.  
  200. $css .= implode("\n\t".$tabs, $values);
  201.  
  202. $css .= "\n".$tabs."}\n";
  203.  
  204. return $css;
  205. }
  206. /**
  207. * Returns the class selector of this style collection. A class selector is the string
  208. * that would be included in the 'class' attribute of HTML tags. One can use
  209. * this method in order to apply the style collection to an arbitrary component.
  210. * @access public
  211. * @return string The class name of this style collection.
  212. ***/
  213. function getClassSelector() {
  214. return $this->_classSelector;
  215. }
  216.  
  217. /**
  218. * Determines whether this <code>StyleCollection</code> can be applied to <code>Components</code>.
  219. * @access public
  220. * @return boolean <code>TRUE</code> if this <code>StyleCollection</code> can be applied to <code>Components</code>.
  221. ***/
  222. function canBeApplied() {
  223. return isset($this->_classSelector);
  224. }
  225.  
  226. /**
  227. * Returns the selector of this StyleCollection.
  228. * @access public
  229. * @return string The selector of this StyleCollection.
  230. ***/
  231. function getSelector() {
  232. return $this->_selector;
  233. }
  234.  
  235. /**
  236. * Returns the display name of this StyleCollection.
  237. * @access public
  238. * @return string The display name of this StyleCollection.
  239. ***/
  240. function getDisplayName() {
  241. return $this->_displayName;
  242. }
  243. /**
  244. * Returns the description of this StlyeProperty.
  245. * @access public
  246. * @return string The description of this StlyeProperty.
  247. ***/
  248. function getDescription() {
  249. return $this->_description;
  250. }
  251. /**
  252. * Adds one StyleProperty to this StyleCollection.
  253. * @access public
  254. * @param ref object sc A StyleProperty object.
  255. * @return ref object The style property that was just added.
  256. ***/
  257. function addSP($sp) {
  258. ArgumentValidator::validate($sp, ExtendsValidatorRule::getRule("StylePropertyInterface"), true);
  259. $this->_SPs[$sp->getName()] =$sp;
  260. return $sp;
  261. }
  262.  
  263. /**
  264. * Returns the StyleProperties of this StyleCollection in a suitable
  265. * for CSS generation order.
  266. * @access public
  267. * @return ref array An array of the StyleProperties of this StyleCollection.
  268. ***/
  269. function getSPs() {
  270. return $this->_SPs;
  271. }
  272. /**
  273. * Returns the StyleProperty with the given name
  274. *
  275. * @param string $name the name of the Styleproperty
  276. * @access public
  277. * @return ref object StyleProperty
  278. ***/
  279. function getStyleProperty($name) {
  280. if(isset($this->_SPs[$name])){
  281. return $this->_SPs[$name];
  282. }else{
  283. $null=null;
  284. return $null;
  285. }
  286. }
  287. /**
  288. * Remove the given StyleProperty from this Style Collection.
  289. * @access public
  290. * @param ref object The style property to remove.
  291. * @return ref object The style property that was removed. <code>NULL</code>
  292. * if it could not be found.
  293. ***/
  294. function removeSP($sp) {
  295. ArgumentValidator::validate($sp, ExtendsValidatorRule::getRule("StylePropertyInterface"), true);
  296.  
  297. $result =$this->_SPs[$sp->getName()];
  298. unset($this->_SPs[$sp->getName()]);
  299. return $result;
  300. }
  301. /**
  302. * Return HTML to nested inside of the component's block. This includes
  303. * things such as corner images.
  304. *
  305. * See the example below:
  306. * <pre>
  307. * <div class='block3'>
  308. *
  309. * <!-- preHTML start -->
  310. * <div class="content">
  311. * <img class="borderTL" src="images/block3_TL.gif" width="14" height="14" />
  312. * <img class="borderTR" src="images/block3_TR.gif" width="14" height="14" />
  313. * <!-- preHTML end -->
  314. *
  315. * <h1>Hello world! (this is when my component renders itself)</h1>
  316. *
  317. * <!-- postHTML start -->
  318. * <div class="roundedCornerSpacer">&nbsp;</div>
  319. * </div>
  320. * <div class="bottomCorners">
  321. * <img class="borderBL" src="images/block3_BL.gif" width="14" height="14" />
  322. * <img class="borderBR" src="images/block3_BR.gif" width="14" height="14" />
  323. * </div>
  324. * <!-- postHTML end -->
  325. *
  326. * </div>
  327. * </pre>
  328. *
  329. * @param string $tabs
  330. * @return string
  331. * @access public
  332. * @since 11/22/05
  333. */
  334. function getPreHTML ($tabs) {
  335. $html = "";
  336. foreach (array_keys($this->_SPs) as $key)
  337. $html .= $this->_SPs[$key]->getPreHTML($tabs);
  338.  
  339. return $html;
  340. }
  341. /**
  342. * Return HTML to nested inside of the component's block. This includes
  343. * things such as corner images.
  344. *
  345. * See the example below:
  346. * <pre>
  347. * <div class='block3'>
  348. *
  349. * <!-- preHTML start -->
  350. * <div class="content">
  351. * <img class="borderTL" src="images/block3_TL.gif" width="14" height="14" />
  352. * <img class="borderTR" src="images/block3_TR.gif" width="14" height="14" />
  353. * <!-- preHTML end -->
  354. *
  355. * <h1>Hello world! (this is when my component renders itself)</h1>
  356. *
  357. * <!-- postHTML start -->
  358. * <div class="roundedCornerSpacer">&nbsp;</div>
  359. * </div>
  360. * <div class="bottomCorners">
  361. * <img class="borderBL" src="images/block3_BL.gif" width="14" height="14" />
  362. * <img class="borderBR" src="images/block3_BR.gif" width="14" height="14" />
  363. * </div>
  364. * <!-- postHTML end -->
  365. *
  366. * </div>
  367. * </pre>
  368. *
  369. * @param string $tabs
  370. * @return string
  371. * @access public
  372. * @since 11/22/05
  373. */
  374. function getPostHTML ($tabs) {
  375. $html = "";
  376. foreach (array_reverse(array_keys($this->_SPs), true) as $key)
  377. $html .= $this->_SPs[$key]->getPostHTML($tabs);
  378.  
  379. return $html;
  380. }
  381. }
  382.  
  383. ?>

Documentation generated on Wed, 19 Sep 2007 10:26:56 -0400 by phpDocumentor 1.3.0RC3