/includes/skip/elements/animated/accordion.php

  1. <?php 
  2. /** 
  3. * Accordion Class 
  4. *  
  5. * Inserts a jQuery UI Accordion and all needed JS. 
  6. *  
  7. * @package Skip\Animated 
  8. * @since 1.0 
  9. * @ignore 
  10. */ 
  11.  
  12. namespace skip\v1_0_0; 
  13.  
  14. class Accordion extends HTML_Element{ 
  15.  
  16. var $title_tag; 
  17. var $content_tag; 
  18.  
  19. /** 
  20. * Accordion constructor 
  21. * @param array $args Optional 
  22. * @package Skip 
  23. * @since 1.0 
  24. */ 
  25. function __construct( $args = array() ) { 
  26.  
  27. /** 
  28. * Additional parent args: 
  29. * 'id' 
  30. * 'classes' 
  31. * 'before_element' 
  32. * 'after_element' 
  33. * 'params' 
  34. */ 
  35. $defaults = array( 
  36. 'tag' => 'div',  
  37. 'title_tag' => 'h3',  
  38. 'content_tag' => 'div' 
  39. ); 
  40.  
  41. $args = wp_parse_args( $args, $defaults ); 
  42. extract( $args , EXTR_SKIP ); 
  43.  
  44. parent::__construct( $tag, $args ); 
  45.  
  46. $this->title_tag = $title_tag; 
  47. $this->content_tag = $content_tag; 
  48.  
  49. /** 
  50. * Adding section to accordion 
  51. * 
  52. * @package Skip 
  53. * @since 1.0 
  54. *  
  55. * @param string $title Title of the tab 
  56. * @param string $content Content in the tab 
  57. * @param array $args Array of [ 'id' ], [ 'classes' ], [ 'params_title' ] and [ 'params_content' ] 
  58. */ 
  59. public function add_element( $title, $content, $args = array() ) { 
  60. $defaults = array( 
  61. 'id' => id(),  
  62. 'classes' => '',  
  63. 'params_title' => array(),  
  64. 'params_content' => array() 
  65. ); 
  66.  
  67. $args = wp_parse_args( $args, $defaults ); 
  68. extract( $args , EXTR_SKIP ); 
  69.  
  70. $element = array( 
  71. 'title' => $title,  
  72. 'content' => $content,  
  73. 'id' => $id,  
  74. 'classes'=> $classes,  
  75. 'params_title' => $params_title,  
  76. 'params_content' => $params_content 
  77. ); 
  78.  
  79. parent::add_element( $element ); 
  80.  
  81. /** 
  82. * Getting the accordion html 
  83. * 
  84. * @package Skip 
  85. * @since 1.0 
  86. *  
  87. * @return string $html The accordion as html 
  88. */ 
  89. public function render( $hide = FALSE ) { 
  90. global $skip_javascripts; 
  91.  
  92. $skip_javascripts[] = ' 
  93. var cookieName_' . $this->params[ 'id' ] . ' = "stickyAccordion_' . $this->params[ 'id' ] . '"; 
  94.  
  95. $( "#' . $this->params[ 'id' ] . '" ).accordion({ 
  96. header: "' . $this->title_tag . '",  
  97. autoHeight: false,  
  98. collapsible:true,  
  99. active: ( $.cookies.get( cookieName_' . $this->params[ 'id' ] . ' ) || 0 ),  
  100. change: function( e, ui ) 
  101. $.cookies.set( cookieName_' . $this->params[ 'id' ] . ', $( this ).find( "' . $this->title_tag . '" ).index ( ui.newHeader[0] ) ); 
  102. });'; 
  103.  
  104. $html = '<' . $this->tag . $this->params() . '>'; 
  105.  
  106. foreach( $this->elements AS $element ): 
  107.  
  108. /** 
  109. * Title 
  110. */ 
  111. $html.= '<' . $this->title_tag . $this->params( $element[ 'params_title' ] ) . '>'; 
  112. $html.= '<a href="#">'; 
  113. $html.= display( $element['title'] ); 
  114. $html.= '</a>'; 
  115. $html.= '</' . $this->title_tag . '>'; 
  116.  
  117. /** 
  118. * Content 
  119. */ 
  120. $html.= '<' . $this->content_tag . $this->params( $element[ 'params_content' ] ) . '>'; 
  121. $html.= display( $element['content'] ); 
  122. $html.= '</' . $this->content_tag . '>'; 
  123.  
  124. endforeach; 
  125.  
  126. $html.= '</' . $this->tag . '>'; 
  127.  
  128. return $html; 
  129.  
  130. /** 
  131. * Accordion getter Function 
  132. * @see skip_accordion() 
  133. * @ignore 
  134. */ 
  135. function get_accordion( $elements, $args = array(), $return = 'html' ) { 
  136. $accordion = new Accordion( $args ); 
  137.  
  138. if( count( $elements ) > 0 ) 
  139. foreach ( $elements AS $element ) 
  140. $accordion->add_element( $element['title'], $element['content'], array_key_exists( 'args', $element ) ? $element['args'] : array() ); 
  141. else 
  142. return FALSE; 
  143.  
  144. return element_return( $accordion, $return );  
  145.  
  146. /** 
  147. * Accordion 
  148. *  
  149. * Creating a jQuery UI Accordion. 
  150. * 
  151. * @package Skip\Animated 
  152. * @since 1.0 
  153. * @param array $elements Elements array structured [ 'title' ], [ 'content' ] and [ 'args' ] 
  154. * @param array $args Args array of [ 'id' ], [ 'classes' ], [ 'params' ], [ 'tag' ], [ 'title_tag' ] and [ 'content_tag' ] 
  155. * @param string $return How to return 'echo', 'object' or 'html' 
  156. */ 
  157. function accordion( $elements, $args = array(), $return = 'html' ) { 
  158. get_accordion( $elements, $args = array(), 'echo' ); 
.