Skip to content

Commit 2620538

Browse files
committed
added some web-experience code
1 parent 63acef5 commit 2620538

File tree

3 files changed

+125
-21
lines changed

3 files changed

+125
-21
lines changed

dist/breinify-web-experiences.js

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
const $ = Breinify.UTL._jquery();
1313
const _private = {
14-
setup: function(configuration, module) {
14+
setup: function (configuration, module) {
1515
console.log('using configuration', configuration);
1616

1717
// ensure that we utilize the onChange method over the ready method
@@ -20,14 +20,60 @@
2020
module.ready = null;
2121
}
2222

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+
2432
let currentIsValidPage = null;
2533
if ($.isFunction(module.isValidPage)) {
2634
currentIsValidPage = module.isValidPage;
2735
}
2836

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;
3177
}
3278
};
3379

@@ -40,18 +86,24 @@
4086
return;
4187
}
4288

43-
// check if we have the trigger plugin
44-
if (Breinify.plugins._isAdded('trigger') === true) {
89+
if (Breinify.plugins._isAdded('trigger') !== true) {
4590

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
5092
console.error('the trigger plugin is not available, ' +
5193
'skipping setup of the web-experiences with id "' + id + '"');
5294
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();
53103
}
54104

105+
// set up the module and add it as such
106+
_private.setup(configuration, module);
55107
Breinify.plugins.api.addModule(id, module);
56108
}
57109
};

dist/breinify-web-experiences.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/WebExperiences.js

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
const $ = Breinify.UTL._jquery();
1313
const _private = {
14-
setup: function(configuration, module) {
14+
setup: function (configuration, module) {
1515
console.log('using configuration', configuration);
1616

1717
// ensure that we utilize the onChange method over the ready method
@@ -20,14 +20,60 @@
2020
module.ready = null;
2121
}
2222

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+
2432
let currentIsValidPage = null;
2533
if ($.isFunction(module.isValidPage)) {
2634
currentIsValidPage = module.isValidPage;
2735
}
2836

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;
3177
}
3278
};
3379

@@ -40,18 +86,24 @@
4086
return;
4187
}
4288

43-
// check if we have the trigger plugin
44-
if (Breinify.plugins._isAdded('trigger') === true) {
89+
if (Breinify.plugins._isAdded('trigger') !== true) {
4590

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
5092
console.error('the trigger plugin is not available, ' +
5193
'skipping setup of the web-experiences with id "' + id + '"');
5294
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();
53103
}
54104

105+
// set up the module and add it as such
106+
_private.setup(configuration, module);
55107
Breinify.plugins.api.addModule(id, module);
56108
}
57109
};

0 commit comments

Comments
 (0)