Commit 39c2e79c authored by osman's avatar osman

Must run Drupal database update

* NEW: Added support for field images.
* UPDATED: Module configuration to reflect added feature.
* More cleaning-up.
parent 5d711965
......@@ -10,36 +10,56 @@
*/
function lazy_configuration_form($form, $form_state) {
$defaults = variable_get('lazy_filter_defaults');
$filter_enabled = lazy_is_filter_enabled();
$description = t('The %filter filter must be enabled for at least one <a href="!path">text-format</a>.', array(
'!path' => url('admin/config/content/formats'),
'%filter' => 'Lazy-load',
));
$form['info'] = array(
'#type' => 'item',
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Global settings'),
'#description' => t('The configuration options available on this form applies to all <a href="!path">text-formats</a> that have %filter filter enabled.', array(
'!path' => url('admin/config/content/formats'),
'%filter' => 'Lazy-load',
)),
'#collapsible' => FALSE,
);
$form['lazy_filter_alter_tag'] = array(
'#type' => 'select',
'#title' => t('Apply to'),
$form['settings']['lazy_filter_alter_tag'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the inline elements to be lazy-loaded via filter.'),
'#options' => array(
'img iframe' => t('Images & Iframes'),
'img' => t('Images only'),
'iframe' => t('IFrames only'),
'img' => t('Images (%img tags)', array('%img' => '<img>')),
'iframe' => t('Iframes (%iframe tags)', array('%iframe' => '<iframe>')),
),
'#default_value' => variable_get('lazy_filter_alter_tag', $defaults['lazy_filter_alter_tag']),
'#description' => t('Select the elements to be lazy-loaded.'),
'#description' => $filter_enabled ? '' : $description,
'#disabled' => !$filter_enabled,
);
$form['settings']['lazy_filter_image_fields'] = array(
'#type' => 'checkbox',
'#title' => t('Enable on image fields attached to fieldable entities. For example, content-types, blocks.'),
'#description' => t('Default is %val', array('%val' => ($defaults['lazy_filter_loadInvisible']) ? 'checked' : 'unchecked')),
'#default_value' => variable_get('lazy_filter_image_fields', $defaults['lazy_filter_image_fields']),
'#return_value' => TRUE,
);
$form['advanced'] = array(
$form['settings']['lazy_filter_placeholderSrc'] = array(
'#type' => 'textfield',
'#title' => t('Placeholder image URL'),
'#description' => t('Default is %val', array('%val' => $defaults['lazy_filter_placeholderSrc'])),
'#default_value' => variable_get('lazy_filter_placeholderSrc', $defaults['lazy_filter_placeholderSrc']),
);
$form['blazy'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced'),
'#title' => t('bLazy configuration'),
'#description' => t('<p><a href="!url">bLazy</a> is a lightweight lazy loading and multi-serving image script created by Bjoern Klinggaard. See its website for usage details and demos.</p>', array(
'!url' => 'http://dinbror.dk/blog/blazy/',
)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['advanced']['lazy_filter_loadInvisible'] = array(
$form['blazy']['lazy_filter_loadInvisible'] = array(
'#type' => 'checkbox',
'#title' => t('loadInvisible'),
'#description' => t('If checked loads invisible (hidden) elements. Default is %val', array('%val' => ($defaults['lazy_filter_loadInvisible']) ? 'checked' : 'unchecked')),
......@@ -47,69 +67,62 @@ function lazy_configuration_form($form, $form_state) {
'#return_value' => TRUE,
);
$form['advanced']['lazy_filter_offset'] = array(
$form['blazy']['lazy_filter_offset'] = array(
'#type' => module_exists('elements') ? 'numberfield' : 'textfield',
'#title' => t('offset'),
'#description' => t('The offset controls how early you want the elements to be loaded before they’re visible. Default is %val, so %val pixel before an element is visible it’ll start loading.', array('%val' => $defaults['lazy_filter_offset'])),
'#default_value' => variable_get('lazy_filter_offset', $defaults['lazy_filter_offset']),
);
$form['advanced']['lazy_filter_saveViewportOffsetDelay'] = array(
$form['blazy']['lazy_filter_saveViewportOffsetDelay'] = array(
'#type' => module_exists('elements') ? 'numberfield' : 'textfield',
'#title' => t('saveViewportOffsetDelay'),
'#description' => t('Delay for how often it should call the saveViewportOffset function on resize. Default is %val', array('%val' => $defaults['lazy_filter_saveViewportOffsetDelay'])),
'#default_value' => variable_get('lazy_filter_saveViewportOffsetDelay', $defaults['lazy_filter_saveViewportOffsetDelay']),
);
$form['advanced']['lazy_filter_validateDelay'] = array(
$form['blazy']['lazy_filter_validateDelay'] = array(
'#type' => module_exists('elements') ? 'numberfield' : 'textfield',
'#title' => t('validateDelay'),
'#description' => t('Delay for how often it should call the validate function on scroll/resize. Default is %val', array('%val' => $defaults['lazy_filter_validateDelay'])),
'#default_value' => variable_get('lazy_filter_validateDelay', $defaults['lazy_filter_validateDelay']),
);
$form['advanced']['lazy_filter_selector'] = array(
$form['blazy']['lazy_filter_selector'] = array(
'#type' => 'textfield',
'#title' => t('Selector class'),
'#description' => t('Element selector for elements that should lazy load. Do not include a leading period. Default is %val', array('%val' => $defaults['lazy_filter_selector'])),
'#default_value' => variable_get('lazy_filter_selector', $defaults['lazy_filter_selector']),
);
$form['advanced']['lazy_filter_skipClass'] = array(
$form['blazy']['lazy_filter_skipClass'] = array(
'#type' => 'textfield',
'#title' => t('skipClass'),
'#description' => t('Elements having this class name will be ignored. Default is %val', array('%val' => $defaults['lazy_filter_skipClass'])),
'#default_value' => variable_get('lazy_filter_skipClass', $defaults['lazy_filter_skipClass']),
);
$form['advanced']['lazy_filter_errorClass'] = array(
$form['blazy']['lazy_filter_errorClass'] = array(
'#type' => 'textfield',
'#title' => t('errorClass'),
'#description' => t('The classname an element will get if something goes wrong. Default is %val', array('%val' => $defaults['lazy_filter_errorClass'])),
'#default_value' => variable_get('lazy_filter_errorClass', $defaults['lazy_filter_errorClass']),
);
$form['advanced']['lazy_filter_successClass'] = array(
$form['blazy']['lazy_filter_successClass'] = array(
'#type' => 'textfield',
'#title' => t('successClass'),
'#description' => t('The classname an element will get when loaded. Default is %val', array('%val' => $defaults['lazy_filter_successClass'])),
'#default_value' => variable_get('lazy_filter_successClass', $defaults['lazy_filter_successClass']),
);
$form['advanced']['lazy_filter_src'] = array(
$form['blazy']['lazy_filter_src'] = array(
'#type' => 'textfield',
'#title' => t('src'),
'#description' => t('Attribute where the original element source will be assigned. Do not change this unless attribute is used for other purposes. Default is %val', array('%val' => $defaults['lazy_filter_src'])),
'#default_value' => variable_get('lazy_filter_src', $defaults['lazy_filter_src']),
);
$form['advanced']['lazy_filter_placeholderSrc'] = array(
'#type' => 'textfield',
'#title' => t('Placeholder image URL'),
'#description' => t('Default is %val', array('%val' => $defaults['lazy_filter_placeholderSrc'])),
'#default_value' => variable_get('lazy_filter_placeholderSrc', $defaults['lazy_filter_placeholderSrc']),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
......
......@@ -15,12 +15,13 @@ function lazy_install() {
'lazy_filter_offset' => 100,
'lazy_filter_saveViewportOffsetDelay' => 50,
'lazy_filter_selector' => 'b-lazy',
'lazy_filter_alter_tag' => 'img iframe',
'lazy_filter_alter_tag' => array('img' => 'img', 'iframe' => 'iframe'),
'lazy_filter_skipClass' => 'no-b-lazy',
'lazy_filter_src' => 'data-src',
'lazy_filter_successClass' => 'b-loaded',
'lazy_filter_validateDelay' => 25,
'lazy_filter_placeholderSrc' => 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
'lazy_filter_image_fields' => FALSE,
);
variable_set('lazy_filter_defaults', $defaults);
......@@ -58,6 +59,7 @@ function lazy_uninstall() {
variable_del('lazy_filter_successClass');
variable_del('lazy_filter_validateDelay');
variable_del('lazy_filter_placeholderSrc');
variable_del('lazy_filter_image_fields');
$module = 'lazy';
$name = 'lazy_filter';
......@@ -92,3 +94,36 @@ function lazy_requirements($phase) {
return $requirements;
}
/**
* Update module default values.
*/
function lazy_update_7100() {
// Get current default values.
$defaults = variable_get('lazy_filter_defaults');
// Update it from old string value to new array value.
$defaults['lazy_filter_alter_tag'] = array('img' => 'img', 'iframe' => 'iframe');
// Fix already existing value with new format.
$lazy_filter_alter_tag = variable_get('lazy_filter_alter_tag');
switch ($lazy_filter_alter_tag) {
case 'img':
variable_set('lazy_filter_alter_tag', array('img' => 'img', 'iframe' => 0));
break;
case 'iframe':
variable_set('lazy_filter_alter_tag', array('img' => 0, 'iframe' => 'iframe'));
break;
default:
variable_set('lazy_filter_alter_tag', $defaults['lazy_filter_alter_tag']);
break;
}
// Add new setting to support image fields.
$defaults['lazy_filter_image_fields'] = FALSE;
// Update the defaults with new values.
variable_set('lazy_filter_defaults', $defaults);
variable_set('lazy_filter_image_fields', $defaults['lazy_filter_image_fields']);
}
(function ($) {
"use strict";
'use strict';
Drupal.behaviors.lazy = {
attach: function (context, settings) {
var options = settings.lazy.bLazy ? settings.lazy.bLazy : {};
var bLazy = new Blazy(options);
new Blazy(options);
}
};
......
......@@ -89,6 +89,7 @@ function _filter_lazy_defaults() {
'lazy_filter_successClass' => variable_get('lazy_filter_successClass'),
'lazy_filter_validateDelay' => variable_get('lazy_filter_validateDelay'),
'lazy_filter_placeholderSrc' => variable_get('lazy_filter_placeholderSrc'),
'lazy_filter_image_fields' => variable_get('lazy_filter_image_fields'),
);
}
......@@ -98,7 +99,7 @@ function _filter_lazy_defaults() {
function _filter_lazy_process($text, $filter) {
$opt_skipClass = variable_get('lazy_filter_skipClass');
$opt_selector = ltrim(variable_get('lazy_filter_selector'), '.');
$opt_tags = explode(' ', variable_get('lazy_filter_alter_tag'));
$opt_tags = variable_get('lazy_filter_alter_tag');
$opt_src = (variable_get('lazy_filter_src') !== 'src') ? variable_get('lazy_filter_src') : 'data-filterlazy-src';
$opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
......@@ -132,7 +133,10 @@ function _filter_lazy_process($text, $filter) {
* Implements callback_filter_tips().
*/
function _filter_lazy_tips($filter, $format, $long = FALSE) {
switch (variable_get('lazy_filter_alter_tag', 'img iframe')) {
$tags = variable_get('lazy_filter_alter_tag');
$tags = implode(' ', $tags);
$tags = trim(str_replace(array('0'), '', $tags));
switch ($tags) {
case 'img iframe':
return t('Both %img and %iframe elements are lazy-loaded.', array('%img' => '<img>', '%iframe' => '<iframe>'));
......@@ -146,9 +150,9 @@ function _filter_lazy_tips($filter, $format, $long = FALSE) {
}
/**
* Implements hook_page_build().
* Is filter enabled for any of text-formats.
*/
function lazy_page_build(&$page) {
function lazy_is_filter_enabled() {
$options = array(
':module' => 'lazy',
':name' => 'lazy_filter',
......@@ -156,7 +160,17 @@ function lazy_page_build(&$page) {
);
$enabled_formats = db_query('SELECT f.format, f.settings FROM {filter} f WHERE f.module = :module AND f.name = :name AND f.status = :status', $options)->fetchAllKeyed();
if (count($enabled_formats)) {
return count($enabled_formats) ? TRUE : FALSE;
}
/**
* Implements hook_page_build().
*/
function lazy_page_build(&$page) {
$filter_enabled = lazy_is_filter_enabled();
$field_enabled = variable_get('lazy_filter_image_fields');
if ($filter_enabled || $field_enabled) {
$path = drupal_get_path('module', 'lazy');
$page['page_bottom']['lazy'] = array(
......@@ -233,3 +247,45 @@ function lazy_library_get_version($library, $options) {
return $json['version'];
}
/**
* Implements hook_theme_registry_alter().
*/
function lazy_theme_registry_alter(&$theme_registry) {
if (variable_get('lazy_filter_image_fields') && isset($theme_registry['image'])) {
$theme_registry['image']['function'] = 'theme_lazy_image';
}
}
/**
* Overrides theme_image().
*/
function theme_lazy_image($variables) {
$opt_skipClass = variable_get('lazy_filter_skipClass');
$opt_selector = ltrim(variable_get('lazy_filter_selector'), '.');
$opt_src = (variable_get('lazy_filter_src') !== 'src') ? variable_get('lazy_filter_src') : 'data-filterlazy-src';
$opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
// Skip Blazy rendering if variables contain `.no-b-lazy` CSS class name.
if (!empty($variables['attributes']['class']) && in_array($opt_skipClass, $variables['attributes']['class'])) {
return theme('image', $variables);
}
else {
// $defaults = _filter_lazy_defaults();
$attributes = $variables['attributes'];
$attributes['src'] = $opt_placeholderSrc;
$attributes[$opt_src] = file_create_url($variables['path']);
$attributes['class'][] = $opt_selector;
foreach (array(
'width',
'height',
'alt',
'title',
) as $key) {
if (isset($variables[$key])) {
$attributes[$key] = $variables[$key];
}
}
return '<img' . drupal_attributes($attributes) . ' />';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment