You are here

class context_reaction_masonry in Masonry API 7

Adds Masonry javascript to the page.

Hierarchy

Expanded class hierarchy of context_reaction_masonry

2 string references to 'context_reaction_masonry'
masonry_context_context_plugins in masonry_context/masonry_context.module
Implements hook_context_plugins().
masonry_context_context_registry in masonry_context/masonry_context.module
Implements hook_context_registry().

File

masonry_context/context_reaction_masonry.inc, line 10
Masonry reaction for Context module.

View source
class context_reaction_masonry extends context_reaction {

  /**
   * Masonry settings form.
   */
  function options_form($context) {
    $form = array();
    $settings = $this
      ->fetch_from_context($context);

    // Get default Masonry options
    $settings += masonry_default_options();
    $form['container'] = array(
      '#type' => 'textfield',
      '#title' => t('Container'),
      '#default_value' => isset($settings['container']) ? $settings['container'] : NULL,
      '#description' => t('The jQuery selector of the Masonry container (ie, .region-content-inner).'),
    );
    $form['item'] = array(
      '#type' => 'textfield',
      '#title' => t('Item selector'),
      '#default_value' => isset($settings['item']) ? $settings['item'] : NULL,
      '#description' => t('The jQuery selector of the Masonry item container (ie, section).'),
    );

    // Add Masonry options to an existing form
    masonry_options_form($form, $settings);
    $form['masonry_animated_duration']['#states'] = array(
      'visible' => array(
        'input.form-checkbox[name$="[masonry_animated]"]' => array(
          'checked' => TRUE,
        ),
      ),
    );
    return $form;
  }

  /**
   * Execution callback.
   */
  function execute() {
    $script = '';
    $styles = '';
    foreach ($this
      ->get_contexts() as $delta => $context) {
      if (!empty($context->reactions['masonry'])) {
        $settings = $context->reactions['masonry'];

        // Add default styling to make grids display properly out-of-the-box
        $css_margin = $settings['masonry_width_unit'] == 'px' ? '10px' : '2%';
        $css_width = $settings['masonry_width_unit'] == 'px' ? $settings['masonry_width'] - 20 . 'px' : $settings['masonry_width'] - 5 . '%';
        $styles .= '
          ' . $settings['container'] . ' ' . $settings['item'] . ' {
            float: left;
            margin: ' . $css_margin . ';
            width: ' . $css_width . ';
          }
        ';
        if ($settings['masonry_center']) {
          $styles .= '
            ' . $settings['container'] . ' ' . $settings['item'] . ' {
              margin: 0 auto;
            }
          ';
        }

        // Get column width
        if ($settings['masonry_width_unit'] == 'px') {
          $column_width = (int) $settings['masonry_width'];
        }
        else {
          $percentage = $settings['masonry_width'] / 100;
          $column_width = 'function (containerWidth) {
            return containerWidth * ' . $percentage . ';
          }';
        }
        $script .= '
          $("' . $settings['container'] . '").imagesLoaded(function () {
            $("' . $settings['container'] . '").masonry({
              itemSelector: "' . $settings['item'] . '",
              columnWidth: ' . $column_width . ',
              isAnimated: ' . (int) $settings['masonry_animated'] . ',
              animationOptions: {
                duration: ' . (int) $settings['masonry_animated_duration'] . '
              },
              isResizable: ' . (int) $settings['masonry_resizable'] . ',
              isFitWidth: ' . (int) $settings['masonry_center'] . ',
              gutterWidth: ' . (int) $settings['masonry_gutter'] . ',
              isRTL: ' . (int) $settings['masonry_rtl'] . '
            });
          });
        ';
      }
    }

    // Initialize Masonry
    if (!empty($script) && ($library = libraries_load('masonry')) && !empty($library['loaded'])) {
      $script = '(function ($) { ' . $script . ' })(jQuery);';
      drupal_add_css($styles, 'inline');
      drupal_add_js($script, array(
        'type' => 'inline',
        'scope' => 'footer',
      ));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
context_reaction::$description property
context_reaction::$plugin property
context_reaction::$title property
context_reaction::fetch_from_context function Retrieve options from the context provided. 1
context_reaction::get_contexts function Retrieve active contexts that have values for this reaction.
context_reaction::options_form_submit function Options form submit handler. 3
context_reaction::settings_form function Settings form. Provide variable settings for your reaction. 2
context_reaction::__clone function Clone our references when we're being cloned.
context_reaction::__construct function Constructor. Do not override.
context_reaction_masonry::execute function Execution callback.
context_reaction_masonry::options_form function Masonry settings form. Overrides context_reaction::options_form