Loading elbow_room.js +38 −0 Original line number Diff line number Diff line /** * @file * * Applies Elbow room preference. */ (function($, Drupal, drupalSettings) { const elbowRoomStorage = new ElbowRoomState(); Drupal.behaviors.ElbowRoom = { attach: function() { // Load persistent state if any. if (elbowRoomStorage.exists()) { drupalSettings.elbow_room.default = elbowRoomStorage.get(); } // Get the form... add a link above var link = this.createLink(); link.addEventListener('click', this.clickLink); link.addEventListener('click', this.updateElbowRoomState); // Add the link to the form. var wrapper = document.createElement('div'); wrapper.setAttribute('class', 'elbow-room-link-wrapper'); Loading Loading @@ -40,6 +54,30 @@ $('form.node-form div.layout-node-form').addClass('elbow-room'); } e.preventDefault(); }, /** * "Click" event handler... * * ...for the "Show/Hide sidebar" link. Saves Elbow room's state after each * click. * * @param {object} event * Click event object. */ updateElbowRoomState: function(e) { const isNotElbowRoomLink = !e.target.classList.contains( "elbow-room-action" ); if (isNotElbowRoomLink) { return; } const elbowRoomState = e.target.classList.contains("has-elbow-room") ? 1 : 0; elbowRoomStorage.set(elbowRoomState); } } })(jQuery, Drupal, drupalSettings); elbow_room.libraries.yml +5 −0 Original line number Diff line number Diff line Loading @@ -7,3 +7,8 @@ base: dependencies: - core/jquery - core/jquery.once - elbow_room/state state: js: elbow_room_state.js: {} elbow_room_state.js 0 → 100644 +58 −0 Original line number Diff line number Diff line /** * @file * Manages Elbow room state persistence. */ class ElbowRoomState { constructor() { this.elbowRoomStorageKey = "Drupal.elbow_room.state"; this.hasLocalStorage = typeof localStorage !== "undefined"; this.hasElbowRoomState = this.hasLocalStorage ? localStorage.getItem(this.elbowRoomStorageKey) !== null : false; } /** * Have we got a persistent state? * * @return {bool} * Do we have localStorage? */ exists() { if (this.hasLocalStorage && this.hasElbowRoomState) { return true; } return false; } /** * Get the persistent state if it exists. * * @return {(int|null)} * Get Elbow room state. */ get() { if (!this.exists()) { return; } const elbowRoomStateString = localStorage.getItem(this.elbowRoomStorageKey); const elbowRoomState = parseInt(elbowRoomStateString, 10); return elbowRoomState; } /** * Set the persistent state. * * @param {int} currentState * Current Elbow room state. */ set(currentState) { localStorage.setItem(this.elbowRoomStorageKey, currentState); this.hasElbowRoomState = true; } } Loading
elbow_room.js +38 −0 Original line number Diff line number Diff line /** * @file * * Applies Elbow room preference. */ (function($, Drupal, drupalSettings) { const elbowRoomStorage = new ElbowRoomState(); Drupal.behaviors.ElbowRoom = { attach: function() { // Load persistent state if any. if (elbowRoomStorage.exists()) { drupalSettings.elbow_room.default = elbowRoomStorage.get(); } // Get the form... add a link above var link = this.createLink(); link.addEventListener('click', this.clickLink); link.addEventListener('click', this.updateElbowRoomState); // Add the link to the form. var wrapper = document.createElement('div'); wrapper.setAttribute('class', 'elbow-room-link-wrapper'); Loading Loading @@ -40,6 +54,30 @@ $('form.node-form div.layout-node-form').addClass('elbow-room'); } e.preventDefault(); }, /** * "Click" event handler... * * ...for the "Show/Hide sidebar" link. Saves Elbow room's state after each * click. * * @param {object} event * Click event object. */ updateElbowRoomState: function(e) { const isNotElbowRoomLink = !e.target.classList.contains( "elbow-room-action" ); if (isNotElbowRoomLink) { return; } const elbowRoomState = e.target.classList.contains("has-elbow-room") ? 1 : 0; elbowRoomStorage.set(elbowRoomState); } } })(jQuery, Drupal, drupalSettings);
elbow_room.libraries.yml +5 −0 Original line number Diff line number Diff line Loading @@ -7,3 +7,8 @@ base: dependencies: - core/jquery - core/jquery.once - elbow_room/state state: js: elbow_room_state.js: {}
elbow_room_state.js 0 → 100644 +58 −0 Original line number Diff line number Diff line /** * @file * Manages Elbow room state persistence. */ class ElbowRoomState { constructor() { this.elbowRoomStorageKey = "Drupal.elbow_room.state"; this.hasLocalStorage = typeof localStorage !== "undefined"; this.hasElbowRoomState = this.hasLocalStorage ? localStorage.getItem(this.elbowRoomStorageKey) !== null : false; } /** * Have we got a persistent state? * * @return {bool} * Do we have localStorage? */ exists() { if (this.hasLocalStorage && this.hasElbowRoomState) { return true; } return false; } /** * Get the persistent state if it exists. * * @return {(int|null)} * Get Elbow room state. */ get() { if (!this.exists()) { return; } const elbowRoomStateString = localStorage.getItem(this.elbowRoomStorageKey); const elbowRoomState = parseInt(elbowRoomStateString, 10); return elbowRoomState; } /** * Set the persistent state. * * @param {int} currentState * Current Elbow room state. */ set(currentState) { localStorage.setItem(this.elbowRoomStorageKey, currentState); this.hasElbowRoomState = true; } }