Commit 9eff9957 authored by bdlangton's avatar bdlangton
Browse files

Initial commit

parents
CONTENTS OF THIS FILE
---------------------
* Introduction
* Requirements
* Installation
* Configuration
* Maintainers
INTRODUCTION
------------
The Browser Storage Commands module provides AJAX commands to add and delete
data from browser storage (either local or session).
* For a full description of the module, visit the project page:
https://www.drupal.org/project/browser_storage_commands
* To submit bug reports and feature suggestions, or track changes:
https://www.drupal.org/project/issues/browser_storage_commands
REQUIREMENTS
------------
This module requires no modules outside of Drupal core.
INSTALLATION
------------
* Install as you would normally install a contributed Drupal module. Visit
https://www.drupal.org/node/1897420 for further information.
CONFIGURATION
-------------
The module has no menu or modifiable settings. There is no configuration.
MAINTAINERS
-----------
Current maintainers:
* Barrett Langton (bdlangton) - https://www.drupal.org/u/bdlangton
name: Browser Storage AJAX Commands
description: 'Provides AJAX commands for adding/removing from browser storage.'
package: ajax
type: module
core: 8.x
ajax:
js:
js/ajax.js: {}
dependencies:
- core/drupal.ajax
(function (Drupal) {
Drupal.AjaxCommands.prototype.storageAdd = function (ajax, response, status) {
// Get the correct browser storage.
let storage = localStorage;
if (response.storage === 'session') {
storage = sessionStorage;
}
// Get the browser storage object.
let data = storage.getItem(response.key) || {};
if (data != null && Object.keys(data).length > 0) {
data = JSON.parse(data);
}
if (data == null) {
data = {};
}
// Add all provided values to the object.
if (response.data != null) {
for (var entry in response.data) {
data[entry] = response.data[entry];
}
}
// Save the new object.
storage.setItem(response.key, JSON.stringify(data));
}
Drupal.AjaxCommands.prototype.storageDelete = function (ajax, response, status) {
// Get the correct browser storage.
let storage = localStorage;
if (response.storage === 'session') {
storage = sessionStorage;
}
// Get the browser storage object. If it's empty, there is nothing to do.
let data = storage.getItem(response.key) || {};
if (data != null && Object.keys(data).length == 0) {
return;
}
if (data == null) {
data = {};
}
data = JSON.parse(data);
// If there are data elements to delete, then delete those individually and
// resave the object. If no data elements are provided, then delete the
// entire object from browser storage.
if (response.data != null && Object.keys(response.data).length > 0) {
for (var entry in response.data) {
delete data[entry];
}
storage.setItem(response.key, JSON.stringify(data));
}
else {
storage.removeItem(response.key);
}
}
})(Drupal);
<?php
namespace Drupal\browser_storage_commands\Ajax;
use Drupal\Core\Ajax\CommandInterface;
/**
* Generic AJAX command for adding info to browser storage.
*
* This command instructs the client to add data provided to the browser storage
* key also provided.
*
* This command is implemented by Drupal.AjaxCommands.prototype.storageAdd()
* defined in js/ajax.js.
*
* @ingroup ajax
*/
class StorageAddCommand extends StorageBase implements CommandInterface {
/**
* {@inheritdoc}
*/
public function render() {
return [
'command' => 'storageAdd',
'key' => $this->key,
'data' => $this->data,
'storage' => $this->storage,
];
}
}
<?php
namespace Drupal\browser_storage_commands\Ajax;
/**
* Base class for storage commands.
*
* @ingroup ajax
*/
class StorageBase {
/**
* The key for storing the data in browser storage.
*
* @var string
*/
protected $key;
/**
* Data to add or remove from browser storage.
*
* @var array
*/
protected $data;
/**
* Which storage to use (local or session).
*
* @var string
*/
protected $storage;
/**
* Constructs a storage command object.
*
* @param string $key
* The key for storing data in browser storage.
* @param array $data
* Data to add to local storage.
* @param string $storage
* Which storage to use.
*/
public function __construct($key = 'Drupal.localstorage', array $data = [], $storage = 'local') {
$this->key = $key;
$this->data = $data;
$this->storage = $storage;
}
}
<?php
namespace Drupal\browser_storage_commands\Ajax;
use Drupal\Core\Ajax\CommandInterface;
/**
* Generic AJAX command for deleting info to browser storage.
*
* This command instructs the client to delete data keys provided from the
* browser storage key also provided. If no data keys are provided, it deletes
* the entire storage for the key.
*
* This command is implemented by Drupal.AjaxCommands.prototype.storageDelete()
* defined in js/ajax.js.
*
* @ingroup ajax
*/
class StorageDeleteCommand extends StorageBase implements CommandInterface {
/**
* {@inheritdoc}
*/
public function render() {
return [
'command' => 'storageDelete',
'key' => $this->key,
'data' => $this->data,
'storage' => $this->storage,
];
}
}
Supports Markdown
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