QuickUiTabs.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Renders the content using the jQuery UI Tabs widget.
  4. */
  5. class QuickUiTabs extends QuickRenderer {
  6. public static function optionsForm($qt) {
  7. $form = array();
  8. $form['history'] = array(
  9. '#type' => 'checkbox',
  10. '#title' => 'History',
  11. '#description' => t('Store tab state in the URL allowing for browser back / forward and bookmarks.'),
  12. '#default_value' => (isset($qt->renderer) && $qt->renderer == 'ui_tabs' && isset($qt->options['history']) && $qt->options['history']),
  13. );
  14. return $form;
  15. }
  16. public function render() {
  17. $quickset = $this->quickset;
  18. $active_tab = $quickset->getActiveTab();
  19. $tabs = $this->build_tablinks($active_tab);
  20. $qt_name = $quickset->getName();
  21. $render_array = array(
  22. '#attached' => $this->add_attached(),
  23. 'content' => array(
  24. '#theme' => 'qt_ui_tabs',
  25. '#options' => array('attributes' => array(
  26. 'id' => 'quicktabs-' . $qt_name,
  27. 'class' => 'quicktabs-ui-wrapper',
  28. )),
  29. 'tabs' => array('#theme' => 'qt_ui_tabs_tabset', '#options' => array('active' => $active_tab), 'tablinks' => $tabs),
  30. 'divs' => array(),
  31. ),
  32. );
  33. foreach ($quickset->getContents() as $key => $tab) {
  34. if (!empty($tab)) {
  35. $attribs = array(
  36. 'id' => 'qt-'. $qt_name .'-ui-tabs' . ($key+1),
  37. );
  38. $render_array['content']['divs'][] = array(
  39. '#prefix' => '<div '. drupal_attributes($attribs) .'>',
  40. '#suffix' => '</div>',
  41. 'content' => $tab->render(),
  42. );
  43. }
  44. }
  45. return $render_array;
  46. }
  47. /**
  48. * Build the actual tab links, with appropriate href, title and attributes.
  49. *
  50. * @param $active_tab The index of the active tab.
  51. */
  52. protected function build_tablinks($active_tab) {
  53. $tabs = array();
  54. $qt_name = $this->quickset->getName();
  55. foreach ($this->quickset->getContents() as $i => $tab) {
  56. if (!empty($tab)) {
  57. // If we use l() here or a render array of type 'link', the '#' symbol will
  58. // be escaped. Sad panda is sad.
  59. $href = '#qt-'. $qt_name .'-ui-tabs' . ($i+1);
  60. $tablink = array(
  61. '#markup' => '<a href="'. $href .'">'. check_plain($this->quickset->translateString($tab->getTitle(), 'tab', $i)) .'</a>',
  62. );
  63. $tabs[$i] = $tablink;
  64. }
  65. }
  66. return $tabs;
  67. }
  68. /**
  69. * Add any necessary js, css and libraries for the render array.
  70. */
  71. protected function add_attached() {
  72. $active_tab = $this->quickset->getActiveTab();
  73. $settings = $this->quickset->getSettings();
  74. $options = $settings['options'];
  75. $attached = array(
  76. 'library' => array(
  77. array('system', 'ui.tabs'),
  78. array('system', 'jquery.bbq'),
  79. ),
  80. 'js' => array(
  81. array('data' => drupal_get_path('module', 'quicktabs') . '/js/qt_ui_tabs.js', 'weight' => JS_DEFAULT + 1),
  82. ),
  83. );
  84. $javascript = drupal_add_js();
  85. foreach ($javascript['settings']['data'] as $key => $settings) {
  86. if (key($settings) == 'quicktabs') {
  87. $qtkey = $key;
  88. break;
  89. }
  90. }
  91. if ($options['history']) {
  92. $attached['library'][] = array('system', 'jquery.bbq');
  93. $attached['js'][] = array('data' => drupal_get_path('module', 'quicktabs') . '/js/quicktabs_bbq.js', 'weight' => JS_DEFAULT);
  94. }
  95. $name = $this->quickset->getName();
  96. if (!isset($qtkey) || !array_key_exists('qt_' . $name, $javascript['settings']['data'][$qtkey]['quicktabs'])) {
  97. $quicktabs_array = array('name' => $name, 'active_tab' => $this->quickset->getActiveTab(), 'history' => $options['history']);
  98. $attached['js'][] = array('data' => array('quicktabs' => array('qt_'. $name => $quicktabs_array)), 'type' => 'setting');
  99. }
  100. return $attached;
  101. }
  102. }