You are here

function context_reaction_masonry::execute in Masonry API 7

Execution callback.

File

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

Class

context_reaction_masonry
Adds Masonry javascript to the page.

Code

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',
    ));
  }
}