Commit fcad5cfc authored by Thomas SECHER's avatar Thomas SECHER
Browse files

Fix js events and add window.triggerApiEnableLog() method

parent 7b56f1f9
import {TriggerApi} from "./utils/trigger-api";
(function (Drupal, DrupalSettings) { // closure
(function (Drupal) { // closure
'use strict';
// AJax command
......@@ -10,4 +10,4 @@ import {TriggerApi} from "./utils/trigger-api";
// Behaviros.
Drupal.behaviors.trigger_api = TriggerApi;
}(Drupal, drupalSettings));
}(Drupal));
class TriggerApiClass {
constructor() {
window.triggerApiEnableLog = this.enableLog
window.triggerApiDisableLog = this.disableLog
}
/**
* Drupal attach.
*
* @param context
* @param settings
*/
attach(context, settings) {
// Trigger from settings.
if (context === document) {
......@@ -70,6 +83,19 @@ class TriggerApiClass {
dispatch(type, data) {
const event = this.getEvent(type, data)
document.dispatchEvent(event)
if( window.localStorage.getItem('trigger_api_log') === 'true' ){
console.log(type, data)
}
}
enableLog(){
window.localStorage.setItem('trigger_api_log', JSON.stringify(true));
console.log("=> TRIGGER API : Logs are enabled");
}
disableLog(){
window.localStorage.setItem('trigger_api_log', JSON.stringify(false));
console.log("=> TRIGGER API : Logs are disabled");
}
}
......
......@@ -16,7 +16,7 @@
\************************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _utils_trigger_api__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/trigger-api */ \"./src/trigger_api/js/utils/trigger-api.js\");\n\n\n(function (Drupal, DrupalSettings) {\n // closure\n 'use strict'; // AJax command\n\n Drupal.AjaxCommands.prototype.TriggerApiCommand = function (ajax, response, status) {\n _utils_trigger_api__WEBPACK_IMPORTED_MODULE_0__.TriggerApi.dispatch(response.type, response.data);\n }; // Behaviros.\n\n\n Drupal.behaviors.trigger_api = _utils_trigger_api__WEBPACK_IMPORTED_MODULE_0__.TriggerApi;\n})(Drupal, drupalSettings);\n\n//# sourceURL=webpack://assets/./src/trigger_api/js/main.js?");
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _utils_trigger_api__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/trigger-api */ \"./src/trigger_api/js/utils/trigger-api.js\");\n\n\n(function (Drupal) {\n // closure\n 'use strict'; // AJax command\n\n Drupal.AjaxCommands.prototype.TriggerApiCommand = function (ajax, response, status) {\n _utils_trigger_api__WEBPACK_IMPORTED_MODULE_0__.TriggerApi.dispatch(response.type, response.data);\n }; // Behaviros.\n\n\n Drupal.behaviors.trigger_api = _utils_trigger_api__WEBPACK_IMPORTED_MODULE_0__.TriggerApi;\n})(Drupal);\n\n//# sourceURL=webpack://assets/./src/trigger_api/js/main.js?");
/***/ }),
......@@ -26,7 +26,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _uti
\*************************************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"TriggerApi\": () => (/* binding */ TriggerApi)\n/* harmony export */ });\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }\n\nfunction _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }\n\nvar TriggerApiClass = /*#__PURE__*/function () {\n function TriggerApiClass() {\n _classCallCheck(this, TriggerApiClass);\n }\n\n _createClass(TriggerApiClass, [{\n key: \"attach\",\n value: function attach(context, settings) {\n // Trigger from settings.\n if (context === document) {\n this.triggerFromSettings(settings);\n } // Trigger from context\n\n\n this.triggerFromContext(context);\n }\n /**\n * Return the event.\n *\n * @param type\n * @param data\n * @return {Event}\n */\n\n }, {\n key: \"getEvent\",\n value: function getEvent(type, data) {\n var event = new Event(type);\n event.data = data;\n return event;\n }\n /**\n * Trigger events from settings.\n *\n * @param settings\n */\n\n }, {\n key: \"triggerFromSettings\",\n value: function triggerFromSettings(settings) {\n var _this = this;\n\n if (settings.trigger_api) {\n settings.trigger_api.forEach(function (item) {\n _this.dispatch(item.type, item.data);\n });\n }\n }\n /**\n * Trigger from context.\n */\n\n }, {\n key: \"triggerFromContext\",\n value: function triggerFromContext(context) {\n var _this2 = this;\n\n context.querySelectorAll('[data-trigger-api-on]').forEach(function (item) {\n try {\n var type = item.getAttribute('data-trigger-api-type'),\n eventType = item.getAttribute('data-trigger-api-on'),\n data = JSON.parse(item.getAttribute('data-trigger-api-data'));\n\n switch (eventType) {\n case 'appear':\n _this2.dispatch(type, data);\n\n break;\n\n default:\n item.addEventListener(eventType, function () {\n _this2.dispatch(type, data);\n });\n break;\n }\n } catch (e) {}\n });\n }\n /**\n * Dispatch event.\n *\n * @param type\n * @param data\n */\n\n }, {\n key: \"dispatch\",\n value: function dispatch(type, data) {\n var event = this.getEvent(type, data);\n document.dispatchEvent(event);\n }\n }]);\n\n return TriggerApiClass;\n}();\n\nvar TriggerApi = new TriggerApiClass();\n\n//# sourceURL=webpack://assets/./src/trigger_api/js/utils/trigger-api.js?");
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"TriggerApi\": () => (/* binding */ TriggerApi)\n/* harmony export */ });\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nfunction _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }\n\nfunction _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }\n\nvar TriggerApiClass = /*#__PURE__*/function () {\n function TriggerApiClass() {\n _classCallCheck(this, TriggerApiClass);\n\n window.triggerApiEnableLog = this.enableLog;\n window.triggerApiDisableLog = this.disableLog;\n }\n /**\n * Drupal attach.\n *\n * @param context\n * @param settings\n */\n\n\n _createClass(TriggerApiClass, [{\n key: \"attach\",\n value: function attach(context, settings) {\n // Trigger from settings.\n if (context === document) {\n this.triggerFromSettings(settings);\n } // Trigger from context\n\n\n this.triggerFromContext(context);\n }\n /**\n * Return the event.\n *\n * @param type\n * @param data\n * @return {Event}\n */\n\n }, {\n key: \"getEvent\",\n value: function getEvent(type, data) {\n var event = new Event(type);\n event.data = data;\n return event;\n }\n /**\n * Trigger events from settings.\n *\n * @param settings\n */\n\n }, {\n key: \"triggerFromSettings\",\n value: function triggerFromSettings(settings) {\n var _this = this;\n\n if (settings.trigger_api) {\n settings.trigger_api.forEach(function (item) {\n _this.dispatch(item.type, item.data);\n });\n }\n }\n /**\n * Trigger from context.\n */\n\n }, {\n key: \"triggerFromContext\",\n value: function triggerFromContext(context) {\n var _this2 = this;\n\n context.querySelectorAll('[data-trigger-api-on]').forEach(function (item) {\n try {\n var type = item.getAttribute('data-trigger-api-type'),\n eventType = item.getAttribute('data-trigger-api-on'),\n data = JSON.parse(item.getAttribute('data-trigger-api-data'));\n\n switch (eventType) {\n case 'appear':\n _this2.dispatch(type, data);\n\n break;\n\n default:\n item.addEventListener(eventType, function () {\n _this2.dispatch(type, data);\n });\n break;\n }\n } catch (e) {}\n });\n }\n /**\n * Dispatch event.\n *\n * @param type\n * @param data\n */\n\n }, {\n key: \"dispatch\",\n value: function dispatch(type, data) {\n var event = this.getEvent(type, data);\n document.dispatchEvent(event);\n\n if (window.localStorage.getItem('trigger_api_log') === 'true') {\n console.log(type, data);\n }\n }\n }, {\n key: \"enableLog\",\n value: function enableLog() {\n window.localStorage.setItem('trigger_api_log', JSON.stringify(true));\n console.log(\"=> TRIGGER API : Logs are enabled\");\n }\n }, {\n key: \"disableLog\",\n value: function disableLog() {\n window.localStorage.setItem('trigger_api_log', JSON.stringify(false));\n console.log(\"=> TRIGGER API : Logs are disabled\");\n }\n }]);\n\n return TriggerApiClass;\n}();\n\nvar TriggerApi = new TriggerApiClass();\n\n//# sourceURL=webpack://assets/./src/trigger_api/js/utils/trigger-api.js?");
/***/ })
......
(()=>{"use strict";function t(t,e){for(var a=0;a<e.length;a++){var r=e[a];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}var e=new(function(){function e(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e)}var a,r;return a=e,(r=[{key:"attach",value:function(t,e){t===document&&this.triggerFromSettings(e),this.triggerFromContext(t)}},{key:"getEvent",value:function(t,e){var a=new Event(t);return a.data=e,a}},{key:"triggerFromSettings",value:function(t){var e=this;t.trigger_api&&t.trigger_api.forEach((function(t){e.dispatch(t.type,t.data)}))}},{key:"triggerFromContext",value:function(t){var e=this;t.querySelectorAll("[data-trigger-api-on]").forEach((function(t){try{var a=t.getAttribute("data-trigger-api-type"),r=t.getAttribute("data-trigger-api-on"),i=JSON.parse(t.getAttribute("data-trigger-api-data"));"appear"===r?e.dispatch(a,i):t.addEventListener(r,(function(){e.dispatch(a,i)}))}catch(t){}}))}},{key:"dispatch",value:function(t,e){var a=this.getEvent(t,e);document.dispatchEvent(a)}}])&&t(a.prototype,r),e}());!function(t,a){t.AjaxCommands.prototype.TriggerApiCommand=function(t,a,r){e.dispatch(a.type,a.data)},t.behaviors.trigger_api=e}(Drupal,drupalSettings)})();
\ No newline at end of file
(()=>{"use strict";function t(t,e){for(var a=0;a<e.length;a++){var i=e[a];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}var e=new(function(){function e(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),window.triggerApiEnableLog=this.enableLog,window.triggerApiDisableLog=this.disableLog}var a,i;return a=e,(i=[{key:"attach",value:function(t,e){t===document&&this.triggerFromSettings(e),this.triggerFromContext(t)}},{key:"getEvent",value:function(t,e){var a=new Event(t);return a.data=e,a}},{key:"triggerFromSettings",value:function(t){var e=this;t.trigger_api&&t.trigger_api.forEach((function(t){e.dispatch(t.type,t.data)}))}},{key:"triggerFromContext",value:function(t){var e=this;t.querySelectorAll("[data-trigger-api-on]").forEach((function(t){try{var a=t.getAttribute("data-trigger-api-type"),i=t.getAttribute("data-trigger-api-on"),r=JSON.parse(t.getAttribute("data-trigger-api-data"));"appear"===i?e.dispatch(a,r):t.addEventListener(i,(function(){e.dispatch(a,r)}))}catch(t){}}))}},{key:"dispatch",value:function(t,e){var a=this.getEvent(t,e);document.dispatchEvent(a),"true"===window.localStorage.getItem("trigger_api_log")&&console.log(t,e)}},{key:"enableLog",value:function(){window.localStorage.setItem("trigger_api_log",JSON.stringify(!0)),console.log("=> TRIGGER API : Logs are enabled")}},{key:"disableLog",value:function(){window.localStorage.setItem("trigger_api_log",JSON.stringify(!1)),console.log("=> TRIGGER API : Logs are disabled")}}])&&t(a.prototype,i),e}());!function(t){t.AjaxCommands.prototype.TriggerApiCommand=function(t,a,i){e.dispatch(a.type,a.data)},t.behaviors.trigger_api=e}(Drupal)})();
\ No newline at end of file
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