Browse Source

First commit (actually 2015/01/10).

Frédéric G. MARAND 7 years ago
commit
3471257283
5 changed files with 251 additions and 0 deletions
  1. 68 0
      lib/OSInet/Splash/SettingsForm.php
  2. 67 0
      lib/OSInet/Splash/SplashBlock.php
  3. 38 0
      scripts/splash.js
  4. 9 0
      splash.info
  5. 69 0
      splash.module

+ 68 - 0
lib/OSInet/Splash/SettingsForm.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * @file
+ * SettingsForm.php
+ *
+ * @author: Frédéric G. MARAND <fgm@osinet.fr>
+ *
+ * @copyright (c) 2015 Ouest Systèmes Informatiques (OSInet).
+ *
+ * @license General Public License version 2 or later
+ */
+
+namespace OSInet\Splash;
+
+
+class SettingsForm {
+  /**
+   * @var array
+   */
+  public $form;
+
+  /**
+   * @var array
+   */
+  public $state;
+
+  public function __construct(array $form, array &$form_state) {
+    $this->form = $form;
+    $this->state = $form_state;
+  }
+
+  public function build() {
+    $form = $this->form;
+    $form['splash-source'] = [
+      '#title' => t('Source selector'),
+      '#description' => t('The jQuery selector for the source container'),
+      '#type' => 'textfield',
+      '#default_value' => variable_get('splash-source', 'body'),
+    ];
+
+    $form['splash-destination'] = [
+      '#title' => t('Destination selector'),
+      '#description' => t('The jQuery selector for the destination container. If empty, the splash will just disappear after the timeout.'),
+      '#type' => 'textfield',
+      '#default_value' => variable_get('splash-destination', ''),
+    ];
+
+    $form['splash-timeout'] = [
+      '#title' => t('Timeout'),
+      '#description' => t('The number of milliseconds during which to display the splash.'),
+      '#type' => 'textfield',
+      '#size' => 5,
+      '#default_value' => variable_get('splash-timeout', 3),
+    ];
+
+    $form['splash-body'] = [
+      '#title' => t('Splash content'),
+      '#description' => t('The content inserted as the splash. This is raw text, so be cautious. It will typically be something like &lt;img src="somepath.png" /&gt;'),
+      '#type' => 'textarea',
+      '#rows' => 5,
+      '#cols' => 80,
+      '#default_value' => variable_get('splash-body', ''),
+    ];
+
+    $form = system_settings_form($form);
+    return $form;
+  }
+}

+ 67 - 0
lib/OSInet/Splash/SplashBlock.php

@@ -0,0 +1,67 @@
+<?php
+/**
+ * @file
+ * SplashBlock.php
+ *
+ * @author: Frédéric G. MARAND <fgm@osinet.fr>
+ *
+ * @copyright (c) 2015 Ouest Systèmes Informatiques (OSInet).
+ *
+ * @license General Public License version 2 or later
+ */
+
+namespace OSInet\Splash;
+
+
+class SplashBlock {
+  const MODULE = 'splash';
+  const DELTA = 'splash';
+
+  protected static $defaults = [
+    'source' => '#block-system-main',
+    'destination' => '#block-splash-splash',
+    'timeout' => 3,
+    'body' => '',
+  ];
+
+
+  public static function info() {
+    $ret = [
+      'info' => t('Splash image'),
+      'cache' => DRUPAL_CACHE_GLOBAL,
+    ];
+
+    return $ret;
+  }
+
+  public static function variableGet($name) {
+    return variable_get(static::MODULE . "-$name", static::$defaults[$name]);
+  }
+
+  public function build() {
+    $subject = NULL;
+    $content = '<span />';
+    $settings = [
+      'splash' => [
+        'source' => static::variableGet('source'),
+        'destination' => static::variableGet('destination'),
+        'timeout' => static::variableGet('timeout'),
+        'body' => static::variableGet('body'),
+      ],
+    ];
+    drupal_add_js($settings, 'setting');
+
+    $file = drupal_get_path('module', 'splash') . '/scripts/splash.js';
+    $options = [
+//      'defer' => TRUE,
+//      'every_page' => TRUE,
+    ];
+    drupal_add_js($file, 'file', $options);
+
+    $ret = [
+      'subject' => $subject,
+      'content' => $content,
+    ];
+    return $ret;
+  }
+}

+ 38 - 0
scripts/splash.js

@@ -0,0 +1,38 @@
+(function ($) {
+  Drupal.behaviors.splash = {
+    attach: function(context, settings) {
+      var source = settings.splash.source;
+      var destination = settings.splash.destination;
+      var timeout = 0 + settings.splash.timeout;
+      var body = settings.splash.body;
+      var jBody = $('<div />').html(body);
+
+      var sourceChildren = $(source).children();
+
+      sourceChildren.detach();
+      jBody.appendTo($(source));
+
+      if (timeout) {
+        setTimeout(function() {
+          Drupal.behaviors.splash.timeoutCallback(settings, sourceChildren);
+        }, timeout);
+      }
+    },
+    timeoutCallback: function(settings, sourceChildren) {
+      var source = settings.splash.source;
+      var destination = settings.splash.destination;
+      var timeout = 0 + settings.splash.timeout;
+
+      var jSource = $(source);
+      var jSplash = jSource.children();
+
+      jSplash.detach();
+      sourceChildren.appendTo(jSource);
+
+      if (timeout) {
+        jSplash.appendTo($(destination));
+      }
+    }
+  };
+
+})(jQuery);

+ 9 - 0
splash.info

@@ -0,0 +1,9 @@
+name = Splash
+description = Splash screen
+php = 5.4
+core = 7.x
+package = OSInet - experimental
+
+configure = admin/structure/block/splash
+
+dependencies[] = xautoload

+ 69 - 0
splash.module

@@ -0,0 +1,69 @@
+<?php
+use OSInet\Splash\SettingsForm;
+use OSInet\Splash\SplashBlock;
+
+/**
+ * This module provides a splash screen and associated block.
+ *
+ * Settings:
+ * - An image file to display
+ * - An initial jQuery selector on which to display the splash: can be full
+ *   screen or not
+ * - An optional later jQuery selector on which to display the image after it
+ *   stopped being displayed on the element targeted by the initial selector
+ * - A list of pages on which to display the splash, as used by block.module
+ */
+
+function splash_menu() {
+  $ret = [];
+
+  $ret['admin/structure/block/splash'] = [
+    'title' => 'Splash',
+    'description' => 'Configure Splash block',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => ['splash_settings_form'],
+    'access arguments' => ['administer blocks'],
+    'type' => MENU_LOCAL_ACTION,
+  ];
+
+  return $ret;
+}
+
+function splash_block_info() {
+  $ret = [
+    SplashBlock::DELTA => SplashBlock::info(),
+  ];
+
+  return $ret;
+}
+
+/**
+ * Implements hook_block_view().
+ */
+function splash_block_view($delta = '') {
+  if ($delta != SplashBlock::DELTA) {
+    watchdog('splash', 'Incorrect block delta for display: @delta', ['@delta' => $delta], WATCHDOG_ERROR);
+    return;
+  }
+
+  return (new SplashBlock())->build();
+}
+
+/**
+ * Form builder for Splash settings.
+ *
+ * @param array $form
+ * @param array $form_state
+ */
+function splash_settings_form(array $form, array &$form_state) {
+  $form = new SettingsForm($form, $form_state);
+  $ret = $form->build();
+  return $ret;
+}
+
+/**
+ * Implements hook_xautoload().
+ */
+function splash_xautoload($api) {
+  $api->absolute()->addPsr4('OSInet\Splash\\', __DIR__ . '/lib/OSInet/Splash');
+}