You are here

function masonry_apply in Masonry API 7.3

Same name and namespace in other branches
  1. 7.2 masonry.module \masonry_apply()

Apply Masonry to a container.

Parameters

$container: The CSS selector of the container element to apply Masonry to.

$options: An associative array of Masonry options. Contains:

  • masonry_item_selector: The CSS selector of the items within the container.
  • masonry_column_width: The width of each column (in pixels or as a percentage).
  • masonry_column_width_units: The units to use for the column width ('px' or '%').
  • masonry_gutter_width: The spacing between each column (in pixels).
  • masonry_resizable: Automatically rearrange items when the container is resized.
  • masonry_animated: Animate item rearrangements.
  • masonry_animation_duration: The duration of animations (in milliseconds).
  • masonry_fit_width: Sets the width of the container to the nearest column. Ideal for centering Masonry layouts.
  • masonry_rtl: Display items from right-to-left.
  • masonry_images_first: Load all images first before triggering Masonry.

File

./masonry.module, line 250
Provides an API for integrating the jQuery Masonry plugin with Drupal.

Code

function masonry_apply($container, $options = array()) {
  if (masonry_loaded() && !empty($container)) {

    // For any options not specified, use default options
    $options += masonry_default_options();
    if (!isset($options['masonry_item_selector'])) {
      $options['masonry_item_selector'] = '';
    }

    // Setup Masonry script
    $masonry = array(
      'masonry' => array(
        $container => array(
          'item_selector' => $options['masonry_item_selector'],
          'column_width' => $options['masonry_column_width'],
          'column_width_units' => $options['masonry_column_width_units'],
          'gutter_width' => (int) $options['masonry_gutter_width'],
          'resizable' => (bool) $options['masonry_resizable'],
          'animated' => (bool) $options['masonry_animated'],
          'animation_duration' => (int) $options['masonry_animation_duration'],
          'fit_width' => (bool) $options['masonry_fit_width'],
          'rtl' => (bool) $options['masonry_rtl'],
          'images_first' => (bool) $options['masonry_images_first'],
          'stamp' => $options['masonry_stamp_selector'],
          'percent_position' => (bool) $options['masonry_percent_position'],
        ),
      ),
    );
    $script_file = drupal_get_path('module', 'masonry') . '/masonry.js';

    // Allow other modules to alter the Masonry script
    $context = array(
      'container' => $container,
      'options' => $options,
    );
    drupal_alter('masonry_script', $masonry, $script_file, $context);

    // Apply the script
    drupal_add_js($masonry, 'setting');
    drupal_add_js($script_file);
  }
}