|
11 | 11 |
|
12 | 12 | const $ = Breinify.UTL._jquery();
|
13 | 13 | const _private = {
|
14 |
| - setup: function(configuration, module) { |
| 14 | + setup: function (configuration, module) { |
15 | 15 | console.log('using configuration', configuration);
|
16 | 16 |
|
17 | 17 | // ensure that we utilize the onChange method over the ready method
|
|
20 | 20 | module.ready = null;
|
21 | 21 | }
|
22 | 22 |
|
23 |
| - // next check if we have limitations on the path and set up the isValidPage |
| 23 | + // set up the logic if it's defined |
| 24 | + if ($.isPlainObject(configuration.activationLogic)) { |
| 25 | + this.setupActivityLogic(configuration.activationLogic, module); |
| 26 | + } |
| 27 | + }, |
| 28 | + |
| 29 | + setupActivityLogic: function (logic, module) { |
| 30 | + const _self = this; |
| 31 | + |
24 | 32 | let currentIsValidPage = null;
|
25 | 33 | if ($.isFunction(module.isValidPage)) {
|
26 | 34 | currentIsValidPage = module.isValidPage;
|
27 | 35 | }
|
28 | 36 |
|
29 |
| - module.isValidPage = null; |
30 |
| - module.findRequirements = null; |
| 37 | + /** |
| 38 | + * Checks if the current page (pathname) is valid for this module. If the method returns `true`, the |
| 39 | + * `findRequirements` and `onChange` method may follow up. |
| 40 | + * |
| 41 | + * @return {boolean} returns `true` if the page is valid for this module, or `false` if no further execution should be handled |
| 42 | + */ |
| 43 | + module.isValidPage = function () { |
| 44 | + return _self.checkActivityLogic(logic) === true && |
| 45 | + (currentIsValidPage === null || currentIsValidPage.call(module) === true); |
| 46 | + }; |
| 47 | + }, |
| 48 | + |
| 49 | + checkActivityLogic: function (logic) { |
| 50 | + const paths = $.isArray(logic.paths) ? logic.paths : []; |
| 51 | + for (let i = 0; i < paths.length; i++) { |
| 52 | + const path = $.isPlainObject(paths[i]) ? paths[i] : {}; |
| 53 | + const type = Breinify.UTL.isNonEmptyString(path.type); |
| 54 | + |
| 55 | + if (type === 'ALL_PATHS') { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + const value = Breinify.UTL.isNonEmptyString(path.value); |
| 60 | + if (value === null) { |
| 61 | + console.warn('found invalid value that was not or an empty string'); |
| 62 | + } else if (type === 'STATIC_PATHS') { |
| 63 | + if (value === window.location.pathname) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + } else if (type === 'REGEX') { |
| 67 | + if (new RegExp(type.value).test(window.location.pathname) === true) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + } else { |
| 71 | + console.warn('found undefined path type "' + path + '" in the activation logic, skipping'); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // add a snippet usage here |
| 76 | + return false; |
31 | 77 | }
|
32 | 78 | };
|
33 | 79 |
|
|
40 | 86 | return;
|
41 | 87 | }
|
42 | 88 |
|
43 |
| - // check if we have the trigger plugin |
44 |
| - if (Breinify.plugins._isAdded('trigger') === true) { |
| 89 | + if (Breinify.plugins._isAdded('trigger') !== true) { |
45 | 90 |
|
46 |
| - // if we have the trigger plugin ensure that we initialize it before we set up the module |
47 |
| - Breinify.plugins.trigger.init(); |
48 |
| - _private.setup(configuration, module); |
49 |
| - } else { |
| 91 | + // if we don't have the trigger plugin stop now, it's a required dependency for web-experiences |
50 | 92 | console.error('the trigger plugin is not available, ' +
|
51 | 93 | 'skipping setup of the web-experiences with id "' + id + '"');
|
52 | 94 | return;
|
| 95 | + } else if (Breinify.plugins.api.isModule(id) === true) { |
| 96 | + |
| 97 | + // if we have this module already there is nothing more to do |
| 98 | + return; |
| 99 | + } else { |
| 100 | + |
| 101 | + // initialize the plugin (can be called doubled) |
| 102 | + Breinify.plugins.trigger.init(); |
53 | 103 | }
|
54 | 104 |
|
| 105 | + // set up the module and add it as such |
| 106 | + _private.setup(configuration, module); |
55 | 107 | Breinify.plugins.api.addModule(id, module);
|
56 | 108 | }
|
57 | 109 | };
|
|
0 commit comments