You are here

scrollreveal.module in Scroll Reveal 7.2

Same filename and directory in other branches
  1. 7 scrollreveal.module

Basic Module file.

File

scrollreveal.module
View source
<?php

/**
* @file
* Basic Module file.
*
*/

/**
 * Implements hook_init().
 */
function scrollreveal_init() {
  $module_path = drupal_get_path('module', 'scrollreveal');
  $settings = variable_get('scrollreveal_settings');
  if (scrollreveal_check_path($settings['pages']['visibility'], $settings['pages']['pages']) && scrollreveal_check_theme() && isset($settings['triggers_fieldset'])) {
    drupal_add_js(array(
      'scrollreveal' => $settings,
    ), 'setting');
    drupal_add_js($module_path . '/scrollreveal.custom.js');
    libraries_load('scrollreveal');
  }
}
function scrollreveal_check_path($visibility, $pages) {
  $pages = drupal_strtolower($pages);

  // Convert the Drupal path to lowercase
  $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));

  // Compare the lowercase internal and lowercase path alias (if any).
  $page_match = drupal_match_path($path, $pages);
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  }

  // When $block->visibility has a value of 0 (BLOCK_VISIBILITY_NOTLISTED),
  // the block is displayed on all pages except those listed in $block->pages.
  // When set to 1 (BLOCK_VISIBILITY_LISTED), it is displayed only on those
  // pages listed in $block->pages.
  $page_match = !($visibility xor $page_match);
  return $page_match;
}
function scrollreveal_check_theme() {
  global $theme_key;
  $settings = variable_get('scrollreveal_settings');
  $visibility = $settings['theme']['visibility'];
  $theme_match = in_array($theme_key, $settings['theme']['themes']);
  $theme_match = !($visibility xor $theme_match);
  return $theme_match;
}

/**
* Implementation of hook_menu().
*/
function scrollreveal_menu() {

  // Admin settings.
  $items['admin/config/user-interface/scrollreveal'] = array(
    'title' => 'Scroll Reveal',
    'description' => 'Add Scroll Reveal effect to any tagged element',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'scrollreveal_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Callback function for admin setting.
 */
function scrollreveal_admin($form, &$form_state) {

  // This module will not work if used in overlay paths such as
  // admin/* , node/add etc if user has overlay access.
  // Since the form builder is called after every AJAX request, we rebuild
  // the form based on $form_state.
  $settings = variable_get('scrollreveal_settings');
  $num_elements = isset($settings['triggers_fieldset']) ? count($settings['triggers_fieldset']) + 1 : 1;
  $form_state['triggers'] = $num_elements;
  $themes = list_themes();
  $active_themes = array();
  foreach ($themes as $theme) {
    if ($theme->status) {
      $active_themes[$theme->name] = $theme->info['name'];
    }
  }
  $options['helper'] = array(
    '#markup' => t('<h3>Scroll Reveal Options</h3>
<p>A simple way to create and maintain how elements fade in, triggered when they enter the viewport. An open-source experiment from <a href="https://twitter.com/julianlloyd">@JulianLloyd</a></p>'),
  );
  $options['config'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['config']["enter"] = array(
    '#type' => 'select',
    '#title' => "Enter",
    '#default_value' => 0,
    '#description' => "Controls the vector origin of your reveal animation.",
    '#options' => array(
      'top' => t('Top'),
      'bottom' => t('Bottom'),
      'right' => t('Right'),
      'left' => t('Left'),
    ),
    '#default_value' => isset($settings['config']["enter"]) ? $settings['config']["enter"] : 'bottom',
  );
  $options['config']["move"] = array(
    '#type' => 'textfield',
    '#title' => "Move",
    '#description' => "The distance your revealing element travels in pixels.",
    '#default_value' => 0,
    '#size' => 6,
    '#maxlength' => 4,
    '#default_value' => isset($settings['config']["move"]) ? $settings['config']["move"] : 24,
  );
  $options['config']["over"] = array(
    '#type' => 'textfield',
    '#title' => "Over",
    '#description' => "The duration of your reveal animation in seconds.",
    '#default_value' => 0,
    '#size' => 6,
    '#maxlength' => 4,
    '#default_value' => isset($settings['config']["over"]) ? $settings['config']["over"] : 0.66,
  );
  $options['config']["after"] = array(
    '#type' => 'textfield',
    '#title' => "After",
    '#description' => "The duration before your reveal begins in seconds.",
    '#default_value' => 0,
    '#size' => 6,
    '#maxlength' => 4,
    '#default_value' => isset($settings['config']["after"]) ? $settings['config']["after"] : 0,
  );
  $options['config']['easing'] = array(
    '#type' => 'select',
    '#title' => t('Easing effect'),
    '#options' => array(
      'ease' => t('Ease'),
      'ease-in' => t('Ease In'),
      'ease-out' => t('Ease Out'),
      'ease-in-out' => t('Ease In Out'),
    ),
    '#default_value' => isset($settings['config']["easing"]) ? $settings['config']["easing"] : 'ease',
  );
  $options['config']['reset'] = array(
    '#type' => 'select',
    '#title' => t('Reset effect'),
    '#description' => t("Replay reveal animations every time elements enter the viewport."),
    '#options' => array(
      'false' => t('False'),
      'true' => t('True'),
    ),
    '#default_value' => isset($settings['config']["reset"]) ? $settings['config']["reset"] : 'false',
  );
  $options['config']["viewportFactor"] = array(
    '#type' => 'textfield',
    '#title' => "Viewport",
    '#description' => t("If set to 0, the element is considered in the viewport as soon as it enters.</br>If set to 1, the element is considered in the viewport when it is fully visible."),
    '#size' => 6,
    '#maxlength' => 4,
    '#default_value' => isset($settings['config']["viewportFactor"]) ? $settings['config']["viewportFactor"] : 0.33,
  );
  $options['triggers_fieldset'] = array(
    '#tree' => TRUE,
    '#title' => t("Target elements"),
    // The prefix/suffix provide the div that we're replacing, named by
    // #ajax['wrapper'] above.
    '#prefix' => '<div id="triggers-div">',
    '#suffix' => '</div>',
    '#type' => 'vertical_tabs',
    '#description' => t('Elements where Parallax Effect will be added'),
  );
  $i = 1;
  $triggers = isset($settings['triggers_fieldset']) ? $settings['triggers_fieldset'] : array();
  foreach ($triggers as $trigger) {
    $options['triggers_fieldset']["trigger{$i}"] = array(
      '#type' => 'fieldset',
      '#title' => isset($trigger["element"]) ? $trigger["element"] : t('New element'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#group' => 'triggers_fieldset',
    );
    $options['triggers_fieldset']["trigger{$i}"]["element"] = array(
      '#type' => 'textfield',
      '#default_value' => isset($trigger["element"]) ? $trigger["element"] : '',
      '#title' => "Valid jQuery selector",
      '#required' => TRUE,
      '#description' => "A jQuery selector to identify the element(s) that will appear with the Scroll Reveal. For example for a list of items in a view you could use #block-views-services-block .views-row.",
    );
    $options['triggers_fieldset']["trigger{$i}"]["enter"] = array(
      '#type' => 'select',
      '#title' => "Enter",
      '#default_value' => isset($trigger["enter"]) ? $trigger["enter"] : 'top',
      '#description' => "Controls the vector origin of your reveal animation.",
      '#options' => array(
        'top' => t('Top'),
        'bottom' => t('Bottom'),
        'right' => t('Right'),
        'left' => t('Left'),
      ),
    );
    $options['triggers_fieldset']["trigger{$i}"]["move"] = array(
      '#type' => 'textfield',
      '#title' => "Move",
      '#description' => "The distance your revealing element travels in pixels.",
      '#default_value' => isset($trigger["move"]) ? $trigger["move"] : 0,
      '#size' => 6,
      '#maxlength' => 4,
    );
    $options['triggers_fieldset']["trigger{$i}"]["over"] = array(
      '#type' => 'textfield',
      '#title' => "Over",
      '#description' => "The duration of your reveal animation in seconds.",
      '#default_value' => isset($trigger["over"]) ? $trigger["over"] : 0,
      '#size' => 6,
      '#maxlength' => 4,
    );
    $options['triggers_fieldset']["trigger{$i}"]["after"] = array(
      '#type' => 'textfield',
      '#title' => "After",
      '#description' => "The duration before your reveal begins in seconds.",
      '#default_value' => isset($trigger["after"]) ? $trigger["after"] : 0,
      '#size' => 6,
      '#maxlength' => 4,
    );
    $options['triggers_fieldset']["trigger{$i}"]["wait"] = array(
      '#type' => 'textfield',
      '#title' => "Move",
      '#description' => "The duration before your reveal begins in seconds.",
      '#default_value' => isset($trigger["wait"]) ? $trigger["wait"] : 0,
      '#size' => 6,
      '#maxlength' => 4,
    );
    $options['triggers_fieldset']["trigger{$i}"]['delete'] = array(
      '#type' => 'button',
      '#value' => t('Delete element'),
      '#name' => 'delete-' . $i,
      '#submit' => array(
        'scrollreveal_delete_submit',
      ),
    );
    $i++;
  }
  $options['triggers_fieldset']["trigger{$i}"] = array(
    '#type' => 'fieldset',
    '#title' => t('New element'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'triggers_fieldset',
  );
  $options['triggers_fieldset']["trigger{$i}"]["element"] = array(
    '#type' => 'textfield',
    '#default_value' => NULL,
    '#title' => "Valid jQuery selector",
    '#description' => "A jQuery selector to identify the element(s) that will appear with the Scroll Reveal. For example for a list of items in a view you could use #block-views-services-block .views-row.",
    '#required' => FALSE,
  );
  $options['triggers_fieldset']["trigger{$i}"]["enter"] = array(
    '#type' => 'select',
    '#title' => "Enter",
    '#default_value' => 0,
    '#description' => "Controls the vector origin of your reveal animation.",
    '#options' => array(
      'top' => t('Top'),
      'bottom' => t('Bottom'),
      'right' => t('Right'),
      'left' => t('Left'),
    ),
  );
  $options['triggers_fieldset']["trigger{$i}"]["move"] = array(
    '#type' => 'textfield',
    '#title' => "Move",
    '#description' => "The distance your revealing element travels in pixels.",
    '#default_value' => 0,
    '#size' => 6,
    '#maxlength' => 4,
  );
  $options['triggers_fieldset']["trigger{$i}"]["over"] = array(
    '#type' => 'textfield',
    '#title' => "Over",
    '#description' => "The duration of your reveal animation in seconds.",
    '#default_value' => 0,
    '#size' => 6,
    '#maxlength' => 4,
  );
  $options['triggers_fieldset']["trigger{$i}"]["after"] = array(
    '#type' => 'textfield',
    '#title' => "After",
    '#description' => "The duration before your reveal begins in seconds.",
    '#default_value' => 0,
    '#size' => 6,
    '#maxlength' => 4,
  );
  $options['triggers_fieldset']["trigger{$i}"]["wait"] = array(
    '#type' => 'textfield',
    '#title' => "Move",
    '#description' => "The duration before your reveal begins in seconds.",
    '#default_value' => 0,
    '#size' => 6,
    '#maxlength' => 4,
  );
  $options['pages'] = array(
    '#type' => 'fieldset',
    '#title' => t('Pages Visibility'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['pages']['visibility'] = array(
    '#type' => 'radios',
    '#title' => t('Activate on specific pages'),
    '#options' => array(
      0 => t('All pages except those listed'),
      1 => t('Only the listed pages'),
    ),
    '#default_value' => $settings['pages']['visibility'],
  );
  $options['pages']['pages'] = array(
    '#type' => 'textarea',
    '#title' => 'List of pages to activate',
    '#default_value' => $settings['pages']['pages'],
    '#description' => t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );

  // Per-path visibility.
  $options['theme'] = array(
    '#type' => 'fieldset',
    '#title' => t('Themes Visibility'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options['theme']['visibility'] = array(
    '#type' => 'radios',
    '#title' => t('Activate on specific themes'),
    '#options' => array(
      0 => t('All themes except those listed'),
      1 => t('Only the listed themes'),
    ),
    '#default_value' => $settings['theme']['visibility'],
  );
  $options['theme']['themes'] = array(
    '#type' => 'select',
    '#title' => 'List of themes where library will be loaded.',
    '#options' => $active_themes,
    '#multiple' => TRUE,
    '#default_value' => $settings['theme']['themes'],
    '#description' => t("Specify in which themes you wish the library to load."),
  );
  $options['#tree'] = TRUE;
  $form['scrollreveal_settings'] = $options;
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  $form['#submit'][] = 'scrollreveal_admin_form_submit';

  // Disable automatic defaults, which don't work with nested values.
  return $form;
}
function scrollreveal_delete_submit($form, &$form_state) {
}

/**
 * Now we add a handler/function to validate the data entered into the
 * "year of birth" field to make sure it's between the values of 1900
 * and 2000. If not, it displays an error. The value report is
 * $form_state['values'] (see http://drupal.org/node/144132#form-state).
 *
 * Notice the name of the function. It is simply the name of the form
 * followed by '_validate'. This is always the name of the default validation
 * function. An alternate list of validation functions could have been provided
 * in $form['#validate'].
 *
 * @see scrollreveal_admin()
 */
function scrollreveal_admin_validate($form, &$form_state) {
  if ($form_state['clicked_button']['#value'] == "Delete element") {
    $settings = variable_get('scrollreveal_settings');
    $child = $form_state['clicked_button']['#parents'][2];
    unset($settings['triggers_fieldset'][$child]);
    variable_set('scrollreveal_settings', $settings);
    $form_state['rebuild'] = TRUE;
    $form_state['flag'] = 1;
    drupal_set_message(t('The element have been deleted.'));
    drupal_goto('admin/config/user-interface/scrollreveal');
  }
}
function scrollreveal_admin_form_submit($form, &$form_state) {

  // Exclude unnecessary elements.
  form_state_values_clean($form_state);
  $i = $form_state['triggers'];
  if (!strlen($form_state['values']['scrollreveal_settings']['triggers_fieldset']["trigger{$i}"]["element"])) {
    unset($form_state['values']['scrollreveal_settings']['triggers_fieldset']["trigger{$i}"]);
  }
  unset($form_state['values']['scrollreveal_settings']['triggers_fieldset']["scrollreveal_settings__triggers_fieldset__active_tab"]);
  foreach ($form_state['values'] as $key => $value) {
    if (is_array($value) && isset($form_state['values']['array_filter'])) {
      $value = array_keys(array_filter($value));
    }
    variable_set($key, $value);
  }
  drupal_set_message(t('The configuration options have been saved.'));
}

/**
 * Libraries implementation.
 */

/**
 * Implements hook_libraries_info().
 */
function scrollreveal_libraries_info() {
  $libraries['scrollreveal'] = array(
    'name' => 'ScrollReveal',
    'vendor url' => 'https://github.com/julianlloyd/scrollReveal.js',
    'download url' => 'https://github.com/julianlloyd/scrollReveal.js',
    'version' => "2.1.0",
    'files' => array(
      'js' => array(
        'scrollReveal.js',
      ),
    ),
  );
  return $libraries;
}

/**
 * Check if the Sroll Reveal library is installed.
 *
 * @return
 *   A boolean indicating the installed status.
 */
function scrollreveal_installed() {
  if (($library = libraries_detect('scrollreveal')) && !empty($library['installed'])) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

/**
 * Check if the Scroll Reveal library has been loaded.
 *
 * @return
 *   A boolean indicating the loaded status.
 */
function scrollreveal_library_loaded() {
  if (($library = libraries_load('scrollreveal')) && !empty($library['loaded'])) {
    return TRUE;
  }
  else {
    return FALSE;
  }
}

Functions

Namesort descending Description
scrollreveal_admin Callback function for admin setting.
scrollreveal_admin_form_submit
scrollreveal_admin_validate Now we add a handler/function to validate the data entered into the "year of birth" field to make sure it's between the values of 1900 and 2000. If not, it displays an error. The value report is $form_state['values'] (see…
scrollreveal_check_path
scrollreveal_check_theme
scrollreveal_delete_submit
scrollreveal_init Implements hook_init().
scrollreveal_installed Check if the Sroll Reveal library is installed.
scrollreveal_libraries_info Implements hook_libraries_info().
scrollreveal_library_loaded Check if the Scroll Reveal library has been loaded.
scrollreveal_menu Implementation of hook_menu().