Skip to content
Snippets Groups Projects
Commit 858aadf2 authored by William Ranvaud's avatar William Ranvaud
Browse files

Refactoring js to remove jquery dependency

parent 24f04553
Branches 1.x 8.x-1.x
Tags 1.0.0-rc1 2.0.0-rc1
No related merge requests found
conditional-message: conditional-message:
version: 1.x
css: css:
theme: theme:
css/conditional_message.css: {} css/conditional_message.css: {}
js: js:
js/conditional_message.js: {} js/conditional_message.js: {}
js/conditional_message_edit_form.js: {}
dependencies: dependencies:
- core/jquery
- core/drupal - core/drupal
- core/drupalSettings - core/drupalSettings
conditional-message-settings:
js:
js/conditional_message_edit_form.js: {}
...@@ -79,9 +79,13 @@ function conditional_message_page_attachments(array &$page) { ...@@ -79,9 +79,13 @@ function conditional_message_page_attachments(array &$page) {
} }
/** /**
* Implements hook_form_FORM_ID_alter(). * Implements hook_page_attachments_alter().
*/ */
function conditional_message_form_conditional_message_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) { function conditional_message_page_attachments_alter(array &$page) {
// Get the current path and compare.
$path = $current_path = \Drupal::service('path.current')->getPath();
if (substr($path, 0, 34) == '/admin/content/conditional-message') {
// Add JS to show or hide fields according to selected conditions. // Add JS to show or hide fields according to selected conditions.
$form['#attached']['library'][] = 'conditional_message/conditional-message-edit-form'; $page['#attached']['library'][] = 'conditional_message/conditional-message-settings';
}
} }
...@@ -2,39 +2,38 @@ ...@@ -2,39 +2,38 @@
* @file * @file
*/ */
(function ($, Drupal, drupalSettings) { (function (Drupal, drupalSettings) {
"use strict"; "use strict";
Drupal.behaviors.conditional_message = { Drupal.behaviors.conditional_message = {
attach: function (context, settings) { attach: function (context, settings) {
// Run only once (not on ajax calls). // Run only once (not on ajax calls).
if (context != document) { if (context != document || drupalSettings.conditional_message === undefined) {
return; return;
} }
// Shorthand for the configurations. // Shorthand for the configurations.
let config = drupalSettings.conditional_message; let config = drupalSettings.conditional_message;
let htmls = []; let htmls = [];
// TODO rewrite this in pure JS to avoid jQuery dependency.
for (let key in config) { for (let key in config) {
// Bail if user already closed the message before. // Bail if user already closed the message before.
let cmuc = 'conditionalMessageUserClosed' + key; let cmuc = 'conditionalMessageUserClosed' + key;
if (localStorage.getItem(cmuc) == config[key].hash) { if (localStorage.getItem(cmuc) == config[key].hash) {
continue; continue;
} }
(function ($, config, key, htmls) { (function (config, key, htmls) {
queryEndpoint($, config, key, htmls); this.queryEndpoint(config, key, htmls);
}).call(this, $, config, key, htmls); }).call(this, config, key, htmls);
} }
} },
}; queryEndpoint: function (config, key, htmls) {
})(jQuery, Drupal, drupalSettings); const request = new XMLHttpRequest();
request.open('GET', Drupal.url("conditional_message_data_output"), true);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
function queryEndpoint($, config, key, htmls) { request.onload = function () {
$.ajax({ if (this.status>= 200 && this.status < 400) {
url: Drupal.url("conditional_message_data_output"), success: function (result) {
// Do not proceed if key not defined. Node deleted? // Do not proceed if key not defined. Node deleted?
const result = JSON.parse(this.responseText);
if (result[key] == undefined) { if (result[key] == undefined) {
return; return;
} }
...@@ -46,9 +45,10 @@ function queryEndpoint($, config, key, htmls) { ...@@ -46,9 +45,10 @@ function queryEndpoint($, config, key, htmls) {
let cmrs = 'conditionalMessageReadStatus' + key; let cmrs = 'conditionalMessageReadStatus' + key;
let readStatus = localStorage.getItem(cmrs); let readStatus = localStorage.getItem(cmrs);
if (readStatus !== config[key].hash) { if (readStatus !== config[key].hash) {
check['session'] = false; check['session'] = true;
} else {
localStorage.setItem(cmrs, config[key].hash); localStorage.setItem(cmrs, config[key].hash);
} else {
check['session'] = false;
} }
} }
// Check close button with localStorage. // Check close button with localStorage.
...@@ -73,7 +73,7 @@ function queryEndpoint($, config, key, htmls) { ...@@ -73,7 +73,7 @@ function queryEndpoint($, config, key, htmls) {
check['type'] = false; check['type'] = false;
result[key].types.forEach(function (type) { result[key].types.forEach(function (type) {
let typeClass = 'page-node-type-' + type; let typeClass = 'page-node-type-' + type;
if ($('body').hasClass(typeClass)) { if (document.querySelector('body').classList.contains(typeClass)) {
check['type'] = true; check['type'] = true;
} }
}); });
...@@ -86,12 +86,6 @@ function queryEndpoint($, config, key, htmls) { ...@@ -86,12 +86,6 @@ function queryEndpoint($, config, key, htmls) {
&& check['type'] && check['type']
&& check['close']) { && check['close']) {
// Display the close button only if option to close enabled.
let closeButton = '';
if (result[key].close) {
closeButton = '<span>&times;</span>';
}
// Prepare color, either color name or HEX. If HEX color, add hash. // Prepare color, either color name or HEX. If HEX color, add hash.
if (/^([0-9A-F]{3}){1,2}$/i.test(config[key].bg_color)) { if (/^([0-9A-F]{3}){1,2}$/i.test(config[key].bg_color)) {
config[key].bg_color = '#' + config[key].bg_color; config[key].bg_color = '#' + config[key].bg_color;
...@@ -100,26 +94,31 @@ function queryEndpoint($, config, key, htmls) { ...@@ -100,26 +94,31 @@ function queryEndpoint($, config, key, htmls) {
config[key].color = '#' + config[key].color; config[key].color = '#' + config[key].color;
} }
// Build the message HTML. // Build the message div element.
let html = '<div class="conditional-message" data-cm-key="' htmls[key] = document.createElement('div');
+ key +'" style="background-color:' htmls[key].setAttribute('class', 'conditional-message');
+ config[key].bg_color + '; color:' htmls[key].setAttribute('data-cm-key', key);
+ config[key].color + ';">' + closeButton htmls[key].setAttribute('style', 'background-color:' + config[key].bg_color + '; color:' + config[key].color);
+ config[key].message + '</div>'; if (result[key].close) {
htmls[key] = html; config[key].message += '<span>&times;</span>';
}
htmls[key].innerHTML = config[key].message;
// Place the message in the page top or bottom. // Place the message in the page top or bottom.
switch (config[key].position) { switch (config[key].position) {
case 'bottom': case 'bottom':
$(config[key].target).append($(htmls[key]).addClass('conditional-message-bottom')); htmls[key].classList.add('conditional-message-bottom');
document.querySelector(config[key].target).append(htmls[key]);
break; break;
default: default:
$(config[key].target).prepend($(htmls[key]).addClass('conditional-message-top')); htmls[key].classList.add('conditional-message-top');
document.querySelector(config[key].target).prepend(htmls[key]);
} }
// Close button. // Close button.
$('.conditional-message span').on('click', function () { document.querySelector('.conditional-message span').addEventListener('click', function () {
let cmKey = $(this).parent().data('cm-key'); let cmKey = this.parentElement.getAttribute('data-cm-key');
$(this).parent().remove(); this.parentElement.remove();
let cmuc = 'conditionalMessageUserClosed' + cmKey; let cmuc = 'conditionalMessageUserClosed' + cmKey;
if (result[key].close) { if (result[key].close) {
localStorage.setItem(cmuc, config[key].hash); localStorage.setItem(cmuc, config[key].hash);
...@@ -127,5 +126,11 @@ function queryEndpoint($, config, key, htmls) { ...@@ -127,5 +126,11 @@ function queryEndpoint($, config, key, htmls) {
}); });
} }
} }
}); };
request.onerror = function () {
console.log('Conditional Message might not be working as expected: connection error.');
} }
request.send();
},
};
})(Drupal, drupalSettings);
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @file * @file
*/ */
(function ($, Drupal) { (function (Drupal) {
"use strict"; "use strict";
...@@ -10,28 +10,28 @@ ...@@ -10,28 +10,28 @@
attach: function (context, settings) { attach: function (context, settings) {
// Show or hide options according to selected conditions. // Show or hide options according to selected conditions.
function toggleVisibility() { function toggleVisibility() {
if ($('#edit-conditions-role', context).prop('checked')) { if (document.querySelector('#edit-conditions-role').checked) {
$('#edit-role-options-wrapper', context).show(); document.querySelector('#edit-role-options-wrapper').style.display = 'block';
} }
else { else {
$('#edit-role-options-wrapper', context).hide(); document.querySelector('#edit-role-options-wrapper').style.display = 'none';
} }
if ($('#edit-conditions-path', context).prop('checked')) { if (document.querySelector('#edit-conditions-path').checked) {
$('#edit-path-options-wrapper', context).show(); document.querySelector('#edit-path-options-wrapper').style.display = 'block';
} }
else { else {
$('#edit-path-options-wrapper', context).hide(); document.querySelector('#edit-path-options-wrapper').style.display = 'none';
} }
if ($('#edit-conditions-content-type', context).prop('checked')) { if (document.querySelector('#edit-conditions-content-type').checked) {
$('#edit-content-type-options-wrapper', context).show(); document.querySelector('#edit-content-type-options-wrapper').style.display = 'block';
} }
else { else {
$('#edit-content-type-options-wrapper', context).hide(); document.querySelector('#edit-content-type-options-wrapper').style.display = 'none';
} }
} }
toggleVisibility(); toggleVisibility();
$(document, context).on('click', toggleVisibility); document.addEventListener('click', toggleVisibility);
} }
} }
})(jQuery, Drupal); })(Drupal);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment