You are here

function qtip_init in qTip (Stylish jQuery Tooltips) 7.2

Same name and namespace in other branches
  1. 6.2 qtip.module \qtip_init()
  2. 6 qtip.module \qtip_init()
  3. 7 qtip.module \qtip_init()

Implements hook_init().

File

./qtip.module, line 27

Code

function qtip_init() {
  $settings = _qtip_fetch_default_settings();
  $pages = $settings['qtip_pages_list'];
  $visibility = $settings['qtip_pages_visibility'];
  $current_path = current_path();

  // Default to loading the library
  $page_match = TRUE;

  // Matches path if necessary
  // @see: This functionality has been adapted from Core's block.module
  if ($pages) {

    // Convert path to lowercase. This allows comparison of the same path with different case. Ex: /Page, /page, /PAGE.
    $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 $visibility has a value of 0 (QTIP_PAGES_VISIBILITY_NOTLISTED),
    // the library is loaded on all pages except those listed in $pages.
    // When set to 1 (QTIP_PAGES_VISIBILITY_LISTED), it is loaded only on those
    // pages listed in $pages.
    $page_match = !($visibility xor $page_match);
  }
  else {
    if ($visibility == QTIP_PAGES_VISIBILITY_LISTED) {
      $page_match = FALSE;
    }
  }

  // if user is on admin/config/user-interface/qtip always load the tooltips so that the examples work as expected
  if ($page_match || $current_path == 'admin/config/user-interface/qtip') {

    // If the admin choose to use a CDN
    if ($settings['qtip_cdn'] != 'none') {
      switch ($settings['qtip_cdn']) {
        case 'cdnjs':
          drupal_add_js('//cdnjs.cloudflare.com/ajax/libs/qtip2/' . $settings['qtip_cdn_version'] . ($settings['qtip_cdn_features'] == 'basic' ? '/basic' : '') . '/jquery.qtip' . ($settings['qtip_cdn_compression'] == 'min' ? '.min' : '') . '.js', 'external');
          drupal_add_css('//cdnjs.cloudflare.com/ajax/libs/qtip2/' . $settings['qtip_cdn_version'] . ($settings['qtip_cdn_features'] == 'basic' ? '/basic' : '') . '/jquery.qtip' . ($settings['qtip_cdn_compression'] == 'min' ? '.min' : '') . '.css', 'external');
          break;
        case 'jsdelivr':
        default:
          drupal_add_js('//cdn.jsdelivr.net/qtip2/' . $settings['qtip_cdn_version'] . ($settings['qtip_cdn_features'] == 'basic' ? '/basic' : '') . '/jquery.qtip' . ($settings['qtip_cdn_compression'] == 'min' ? '.min' : '') . '.js', 'external');
          drupal_add_css('//cdn.jsdelivr.net/qtip2/' . $settings['qtip_cdn_version'] . ($settings['qtip_cdn_features'] == 'basic' ? '/basic' : '') . '/jquery.qtip' . ($settings['qtip_cdn_compression'] == 'min' ? '.min' : '') . '.css', 'external');
          if ($settings['qtip_cdn_imagesloaded']) {
            drupal_add_js('//cdn.jsdelivr.net/imagesloaded/' . QTIP_RECOMMENDED_IMAGESLOADED_VERSION . '/imagesloaded.pkgd' . ($settings['qtip_cdn_compression'] == 'min' ? '.min' : '') . '.js', 'external');
          }
          break;
      }
    }
    else {
      if (module_exists('libraries')) {

        // Try to load the library and check if that worked.
        $library = libraries_load('qtip');

        // The library did not load properly
        if (empty($library['loaded'])) {
          drupal_set_message(t('The qTip library is not loaded. qTips will not display on the site.'), 'error', FALSE);
        }
      }
      else {
        drupal_set_message(t('You must have the !libraries module installed and enabled to use a custom build of qTip.', array(
          '!libraries' => '<a href="http://drupal.org/projects/libraries">Libraries</a>',
        )), 'error', FALSE);
      }
    }

    // Give qtip.js an explicit weight so submodules can precede it.
    drupal_add_js(drupal_get_path('module', 'qtip') . '/js/qtip.js', array(
      'weight' => 1,
    ));
    drupal_add_css(drupal_get_path('module', 'qtip') . '/css/qtip.css');
    $instances = qtip_load_multiple();

    // Send the instances settings to qtip.js
    drupal_add_js(array(
      'instances' => qtip_clean_settings_multiple($instances),
    ), 'setting');
    drupal_add_js(array(
      'qtipDebug' => drupal_json_encode(qtip_fetch_debug_settings()),
    ), 'setting');
  }
  elseif (drupal_match_path($current_path, 'admin/config/user-interface/qtip/manage*')) {
    drupal_add_css(drupal_get_path('module', 'qtip') . '/css/qtip.admin.css');
  }
}