Commit 5d711965 authored by osman's avatar osman

* Issue #2960268 by osman: JavaScript must not be attached unless the filter is enabled.

* Issue #2960269 by osman: Integer and boolean values saved as strings.
* Issue #2960270 by osman: Uninstall leaves leftover entries in {filter} table.
* Issue #2960271 by osman: Implement hook_help() to add help.
* Cleaned up the code.
* Grouped advanced settings in a collapsed fieldset.
parent ce557bdd
# Lazy-load
This is a simple Drupal module to lazy-load all inline images and/or iframes defined by content authors in entity content, usually the text-format-enabled version of textarea fields. i.e. Node and Block body content.
This is a simple Drupal module to lazy-load all inline images and/or iframes
defined by content authors in entity content, usually the text-format-enabled
version of textarea fields. i.e. Node and Block body content.
The module currently depends on the [bLazy][1] image script.
There is another contributed module utilizing its namesake, [Blazy][2]. Make sure to check it out, especially if you need more advanced features and support for many features out of the box.
There is another contributed module utilizing its namesake, [Blazy][2].
Make sure to check it out, especially if you need more advanced features and
support for many features out of the box.
This module focuses on the only area Blazy module lacks of; **inline-images** and **inline-iframes**.
This module focuses on the only area Blazy module lacks of; **inline-images**
and **inline-iframes**.
You can still use this module tandem with it, though that is not a requirement.
......@@ -15,30 +20,46 @@ You can still use this module tandem with it, though that is not a requirement.
* **[Libraries API][4]** module
* **bLazy v1.8.2** script as a library item:
[Download bLazy][3] from https://github.com/dinbror/blazy
1) Extract the downloaded file, 2) rename *blazy-master* directory to *blazy*, 3) copy the folder into one of the following places that *Libraries API* module supports, `sites/all/libraries` (or site-specific libraries folder):
1) Extract the downloaded file,
2) rename *blazy-master* directory to *blazy*,
3) copy the folder into one of the following places that *Libraries API*
module supports, `sites/all/libraries` (or site-specific libraries folder):
i.e.: `sites/all/libraries/blazy/blazy.min.js`
## Installation
Install the module as usual. More information can be found at https://www.drupal.org/docs/7/extend/installing-modules
Install the module as usual. More information can be found at
https://www.drupal.org/docs/7/extend/installing-modules
## Usage
This modules makes a new text filter available for the text-formats: *Lazy-load*
Enable the *Lazy-load* filter for the desired text-formats. i.e. *Full HTML* or *Filtered HTML*
Enable the *Lazy-load* filter for the desired text-formats. i.e. *Full HTML* or
*Filtered HTML*
Check out the module configuration at `admin/config/content/lazy`. The default settings should work for most developers. Incase they are not, change the settings to suit your needs and submit the form.
Check out the module configuration at `admin/config/content/lazy`. The default
settings should work for most developers. Incase they are not, change the
settings to suit your needs and submit the form.
This configuration is used globally for all the text-formats having *Lazy-load* filter enabled.
This configuration is used globally for all the text-formats having *Lazy-load*
filter enabled.
## Use Case
If you have numerous images and/or iframes in your content, it could become a challenge to update that content to make compatible for lazy-loading. In most cases those updates needs to be handled manually, because most of the time if not all, the body content (HTML) doesn't follow a pattern to update them programmatically.
If you have numerous images and/or iframes in your content, it could become
a challenge to update that content to make compatible for lazy-loading. In
most cases those updates needs to be handled manually, because most of the time
if not all, the body content (HTML) doesn't follow a pattern to update
them programmatically.
This is the main reason I created this module, to avoid a need for altering body content manually while making them easy to lazy-load.
This is the main reason I created this module, to avoid a need for altering body
content manually while making them easy to lazy-load.
**The *Lazy-load* filter doesn't make any changes to existing content.** It only rewrites the `<img>` and/or `<iframe>` tags in already rendered output to have them compatible for bLazy script to lazy-load. Since the filtered output is cached, there should not be any changes in performance.
**The *Lazy-load* filter doesn't make any changes to existing content.** It only
rewrites the `<img>` and/or `<iframe>` tags in already rendered output to have
them compatible for bLazy script to lazy-load. Since the filtered output is
cached, there should not be any changes in performance.
[1]: http://dinbror.dk/blazy/
[2]: https://www.drupal.org/project/blazy
......
......@@ -6,7 +6,7 @@
*/
/**
* Form callback for the Lazy-load configuration form.
* Lazy-load configuration form.
*/
function lazy_configuration_form($form, $form_state) {
$defaults = variable_get('lazy_filter_defaults');
......@@ -32,7 +32,14 @@ function lazy_configuration_form($form, $form_state) {
'#description' => t('Select the elements to be lazy-loaded.'),
);
$form['lazy_filter_loadInvisible'] = array(
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['advanced']['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')),
......@@ -40,68 +47,100 @@ function lazy_configuration_form($form, $form_state) {
'#return_value' => TRUE,
);
$form['lazy_filter_offset'] = array(
$form['advanced']['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['lazy_filter_saveViewportOffsetDelay'] = array(
$form['advanced']['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['lazy_filter_validateDelay'] = array(
$form['advanced']['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['lazy_filter_selector'] = array(
$form['advanced']['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['lazy_filter_skipClass'] = array(
$form['advanced']['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['lazy_filter_errorClass'] = array(
$form['advanced']['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['lazy_filter_successClass'] = array(
$form['advanced']['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['lazy_filter_src'] = array(
$form['advanced']['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['lazy_filter_placeholderSrc'] = array(
$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']),
);
return system_settings_form($form);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Submit handler for lazy_configuration_form().
*/
function lazy_configuration_form_submit($form, &$form_state) {
$int_value = array(
'lazy_filter_offset',
'lazy_filter_saveViewportOffsetDelay',
'lazy_filter_validateDelay',
);
$bool_value = array(
'lazy_filter_loadInvisible',
);
// Exclude unnecessary elements.
form_state_values_clean($form_state);
foreach ($form_state['values'] as $key => $value) {
if (in_array($key, $int_value)) {
$value = intval($value);
}
if (in_array($key, $bool_value)) {
$value = boolval($value);
}
variable_set($key, $value);
}
drupal_set_message(t('The configuration options have been saved.'));
}
......@@ -30,16 +30,15 @@ function lazy_install() {
}
/**
* Implement hook_enable()
*/
* Implements hook_enable().
*/
function lazy_enable() {
$options = array(
'!config' => url('admin/config/content/lazy'),
'%filter' => 'Lazy-load',
'!path' => url('admin/config/content/formats'),
);
$message = t('<a href="!config">Lazy module</a> is installed. Make sure to enable %filter filter in desired <a href="!path">text-formats</a>.', $options);
$message = t('The <a href="!config">Lazy</a> module was installed. The new %filter filter should be enabled in desired <a href="!path">text-formats</a>.', $options);
drupal_set_message($message, 'warning');
}
......@@ -59,6 +58,13 @@ function lazy_uninstall() {
variable_del('lazy_filter_successClass');
variable_del('lazy_filter_validateDelay');
variable_del('lazy_filter_placeholderSrc');
$module = 'lazy';
$name = 'lazy_filter';
db_delete('filter')
->condition('module', $module)
->condition('name', $name)
->execute();
}
/**
......
(function ($) {
"use strict";
Drupal.behaviors.lazy = {
attach: function (context, settings) {
// @see http://dinbror.dk/blog/blazy/?ref=example-page#Options
var options = settings.lazy.bLazy ? settings.lazy.bLazy : {};
var bLazy = new Blazy(options);
}
};
})(jQuery);
<?php
/**
* @file
* Module file for Lazy-load.
......@@ -20,6 +21,41 @@ function lazy_menu() {
);
}
/**
* Implements hook_help().
*/
function lazy_help($path, $arg) {
switch ($path) {
case 'admin/help#lazy':
$filepath = dirname(__FILE__) . '/README.md';
if (file_exists($filepath)) {
$readme = file_get_contents($filepath);
}
if (!isset($readme)) {
return NULL;
}
if (module_exists('markdown')) {
$filters = module_invoke('markdown', 'filter_info');
$info = $filters['filter_markdown'];
if (function_exists($info['process callback'])) {
$output = $info['process callback']($readme, NULL);
}
else {
$output = '<pre>' . $readme . '</pre>';
}
}
else {
$output = '<pre>' . $readme . '</pre>';
}
return $output;
}
}
/**
* Implements hook_filter_info().
*/
......@@ -37,6 +73,9 @@ function lazy_filter_info() {
return $filters;
}
/**
* Callback to return configuration defaults.
*/
function _filter_lazy_defaults() {
return array(
'lazy_filter_errorClass' => variable_get('lazy_filter_errorClass'),
......@@ -63,12 +102,6 @@ function _filter_lazy_process($text, $filter) {
$opt_src = (variable_get('lazy_filter_src') !== 'src') ? variable_get('lazy_filter_src') : 'data-filterlazy-src';
$opt_placeholderSrc = variable_get('lazy_filter_placeholderSrc');
// dpm($opt_skipClass, '$opt_skipClass');
// dpm($opt_selector, '$opt_selector');
// dpm($opt_tags, '$opt_tags');
// dpm($opt_src, '$opt_src');
// dpm($opt_placeholderSrc, '$opt_placeholderSrc');
$html_dom = filter_dom_load($text);
foreach ($opt_tags as $tag) {
......@@ -102,15 +135,13 @@ function _filter_lazy_tips($filter, $format, $long = FALSE) {
switch (variable_get('lazy_filter_alter_tag', 'img iframe')) {
case 'img iframe':
return t('Both %img and %iframe elements are lazy-loaded.', array('%img' => '<img>', '%iframe' => '<iframe>'));
break;
case 'img':
return t('%img elements are lazy-loaded.', array('%img' => '<img>', '%iframe' => '<iframe>'));
break;
case 'iframe':
return t('%iframe elements are lazy-loaded.', array('%img' => '<img>', '%iframe' => '<iframe>'));
break;
}
}
......@@ -118,31 +149,38 @@ function _filter_lazy_tips($filter, $format, $long = FALSE) {
* Implements hook_page_build().
*/
function lazy_page_build(&$page) {
$path = drupal_get_path('module', 'lazy');
$page['page_bottom']['lazy'] = array(
'#attached' => array(),
);
$attached = &$page['page_bottom']['lazy']['#attached'];
// Default settings.
// @see http://dinbror.dk/blog/blazy/?ref=example-page#Options
$settings = array(
'errorClass' => variable_get('lazy_filter_errorClass'),
'loadInvisible' => variable_get('lazy_filter_loadInvisible'),
'offset' => intval(variable_get('lazy_filter_offset')),
'saveViewportOffsetDelay' => intval(variable_get('lazy_filter_saveViewportOffsetDelay')),
'selector' => '.' . variable_get('lazy_filter_selector'),
'src' => variable_get('lazy_filter_src'),
'successClass' => variable_get('lazy_filter_successClass'),
'validateDelay' => intval(variable_get('lazy_filter_validateDelay')),
$options = array(
':module' => 'lazy',
':name' => 'lazy_filter',
':status' => 1,
);
$attached['js'][] = array(
'data' => array('lazy' => array('bLazy' => $settings)),
'type' => 'setting',
);
$attached['libraries_load'][] = array('blazy');
$attached['js'][$path . '/lazy.js'] = array('every_page' => TRUE);
$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)) {
$path = drupal_get_path('module', 'lazy');
$page['page_bottom']['lazy'] = array(
'#attached' => array(),
);
$attached = &$page['page_bottom']['lazy']['#attached'];
$settings = array(
'errorClass' => variable_get('lazy_filter_errorClass'),
'loadInvisible' => variable_get('lazy_filter_loadInvisible'),
'offset' => intval(variable_get('lazy_filter_offset')),
'saveViewportOffsetDelay' => intval(variable_get('lazy_filter_saveViewportOffsetDelay')),
'selector' => '.' . variable_get('lazy_filter_selector'),
'src' => variable_get('lazy_filter_src'),
'successClass' => variable_get('lazy_filter_successClass'),
'validateDelay' => intval(variable_get('lazy_filter_validateDelay')),
);
$attached['js'][] = array(
'data' => array('lazy' => array('bLazy' => $settings)),
'type' => 'setting',
);
$attached['libraries_load'][] = array('blazy');
$attached['js'][$path . '/lazy.js'] = array('every_page' => TRUE);
}
}
/**
......@@ -191,7 +229,7 @@ function lazy_library_get_version($library, $options) {
return;
}
$file_content = file_get_contents($file);
$json = json_decode($file_content, true);
$json = json_decode($file_content, TRUE);
return $json['version'];
}
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