@competitionlabs/gamification-widget-loader 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "@competitionlabs/gamification-widget-loader",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/loader.js",
7
+ "files": [
8
+ "dist"
9
+ ],
7
10
  "scripts": {
8
11
  "dev": "vite --open /example/index.html",
9
12
  "build": "vite build",
10
- "publish": "npm run build && npm publish --access restricted",
13
+ "publish:restricted": "npm run build && npm publish --access restricted",
11
14
  "test": "echo \"Error: no test specified\" && exit 1"
12
15
  },
13
16
  "keywords": [],
package/ajax.js DELETED
@@ -1,65 +0,0 @@
1
- export const createAjaxClass = function ({ sizeof }) {
2
- return class Ajax {
3
- constructor() {
4
- this.xhr = new XMLHttpRequest();
5
- }
6
-
7
- /* eslint no-undef: "off" */
8
- createCORSRequest(method, url) {
9
- if ('withCredentials' in this.xhr) {
10
- /* Most browsers. */ this.xhr.open(method, url, true);
11
- } else if (typeof XDomainRequest !== 'undefined') {
12
- /* IE8 & IE9 */ this.xhr = new XDomainRequest();
13
- url = url.indexOf('https') > -1 && location.protocol !== 'https:' ? url.replace('https', 'http') : url;
14
- this.xhr.open(method, url);
15
- } else {
16
- /* CORS not supported. */ this.xhr = null;
17
- }
18
- return this.xhr;
19
- }
20
-
21
- abort() {
22
- if (this.xhr && this.xhr.readyState !== undefined && this.xhr.readyState !== 4) {
23
- this.xhr.abort();
24
- }
25
- return this;
26
- }
27
-
28
- /**
29
- * Retrieves data from a URL without a page refresh.
30
- */
31
- getData(data) {
32
- const obj = this;
33
- try {
34
- data.type = data.type !== undefined && typeof data.type === 'string' && data.type.length > 0 ? data.type : 'POST';
35
- data.data = data.data !== undefined && typeof data.data === 'object' ? data.data : {};
36
- data.url = data.url !== undefined && typeof data.url === 'string' && data.url.length > 0 ? data.url : '';
37
- data.success = data.success !== undefined ? data.success : function () {};
38
- data.error = data.error !== undefined ? data.error : function () {};
39
- data.headers = data.headers !== undefined ? data.headers : {};
40
- data.extraCallback = data.extraCallback !== undefined ? data.extraCallback : function () {};
41
- obj.xhr = this.createCORSRequest(data.type, data.url);
42
- obj.xhr.onload = function () {
43
- data.extraCallback(data, obj.xhr);
44
- data.success(obj.xhr.responseText, data, obj.xhr);
45
- };
46
- obj.xhr.onerror = function () {
47
- data.error(obj.xhr.status);
48
- };
49
- if (typeof XDomainRequest === 'undefined') {
50
- if (sizeof(data.headers) > 0) {
51
- for (const item in data.headers) {
52
- obj.xhr.setRequestHeader(item, data.headers[item]);
53
- }
54
- } else if ((data.type === 'POST' || data.type === 'PUT' || data.type === 'DELETE') && sizeof(data.headers) === 0) {
55
- obj.xhr.setRequestHeader('Content-Type', 'application/json');
56
- } else {
57
- obj.xhr.setRequestHeader('Content-Type', 'text/plain');
58
- }
59
- }
60
- obj.xhr.send(JSON.stringify(data.data));
61
- return obj.xhr;
62
- } catch {}
63
- }
64
- };
65
- };
@@ -1,22 +0,0 @@
1
- const loader = window.clLoaderV3;
2
- loader.settings = {
3
- ...loader.settings,
4
- apiKey: '37de27fd1d6242f283e826034bd83a81',
5
- spaceName: 'client_2',
6
- memberId: 'fynniqABiZWyLxn9MTLM',
7
- uri: {
8
- ...loader.settings.uri,
9
- gatewayDomain: 'http://192.168.91.30:9998',
10
- gatewayWSDomain: 'ws://192.168.91.30:9998'
11
- },
12
- uomSymbol: '€',
13
- bindContainer: document.querySelector('#widget'),
14
- language: 'en',
15
- widgetStatus: 'Live',
16
- isWidgetMinimized: false,
17
- resources: [
18
- 'https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap'
19
- ]
20
- };
21
-
22
- loader.init();
@@ -1,13 +0,0 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1">
6
- <title>CompetitionLabs Widget Loader Example</title>
7
- </head>
8
- <body>
9
- <div id="widget"></div>
10
- <script type="module" src="../loader.js"></script>
11
- <script type="module" src="/example/example.js"></script>
12
- </body>
13
- </html>
package/loader.js DELETED
@@ -1,182 +0,0 @@
1
- /*
2
- COMPETITION LABS LTD v1.0.0
3
- (c) 2026 Nathan Galea
4
- License: www.competitionlabs.com/terms-of-service
5
-
6
- Centralised loader script that is responsible for loading/handling all your product/environment specific scripts
7
- in one place. Allows better control over your vast product range, centralising all your business related configurations
8
- in one place:
9
- - load all your scripts/styles/configurations/presets from one script
10
- - control when and how the scripts get initialised
11
- - restrict loading per product
12
- */
13
- import sizeof from './sizeof.js';
14
- import { createAjaxClass } from './ajax.js';
15
-
16
- (function () {
17
- 'use strict';
18
-
19
- const Ajax = createAjaxClass({ sizeof });
20
-
21
- var loadScript = function () {
22
- this.ajax = new Ajax();
23
- this.settings = {
24
- apiKey: '', // default API key
25
- spaceName: 'my_space_name', // default space name
26
- enforceGameLookup: true, // default enforce game lookup
27
- language: 'en', // default language setting
28
- currency: '', // default currency setting
29
- enableNotifications: false, // requires a working/valid SSE channel
30
- memberId: '',
31
- uri: {
32
- gatewayDomain: '',
33
- gatewayWSDomain: '',
34
- translationPath: '',
35
- widgetData: '/api/v1/:space/widget'
36
- },
37
- uomSymbol: '',
38
- isWidgetMinimized: true,
39
- bindContainer: null,
40
- miniBindContainer: null,
41
- loadTranslationsForceCustomPath: true,
42
- widgetStatus: 'Live',
43
- resources: [],
44
- assetsBasePath: '/build'
45
- };
46
-
47
- this.initialiseWidget = function (widgetData) {
48
- const _this = this;
49
- console.log('initialiseWidget', widgetData);
50
-
51
- const widget = {
52
- ...widgetData.data[0],
53
- widgets: {
54
- type_1: {
55
- script: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/javascript/gamification-ux-package.v3-selfinit.js",
56
- css: ["https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/css/styles.css"],
57
- translations: {
58
- ja: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/i18n/translation_ja.json",
59
- en: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/i18n/translation_en.json"
60
- }
61
- },
62
- type_2: {
63
- script: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/javascript/gamification-ux-package.v3-selfinit.js",
64
- css: ["https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/css/styles.css"],
65
- translations: {
66
- ja: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/i18n/translation_ja.json",
67
- en: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/i18n/translation_en.json"
68
- }
69
- },
70
- }
71
- };
72
-
73
- console.log('MY WIDGET DATA', widget)
74
- const widgetType = widget.layout.toLowerCase();
75
- const widgetAssets = widget.widgets?.[widgetType];
76
-
77
- if (!widgetAssets) {
78
- throw new Error(`CompetitionLabs widget configuration does not contain assets for ${widget.layout}`);
79
- }
80
-
81
- const translationPath = widgetAssets.translations?.[_this.settings.language] || widgetAssets.translations?.en || '';
82
- const stylesheets = Array.isArray(widgetAssets.css) ? widgetAssets.css : [];
83
-
84
- stylesheets.forEach(function (stylesheet) {
85
- const isAlreadyLoaded = Array.from(document.head.querySelectorAll('link[rel="stylesheet"]'))
86
- .some((link) => link.href === new URL(stylesheet, document.baseURI).href);
87
-
88
- if (!isAlreadyLoaded) {
89
- const link = document.createElement('link');
90
- link.rel = 'stylesheet';
91
- link.href = stylesheet;
92
- document.head.appendChild(link);
93
- }
94
- });
95
-
96
- const widgetInstanceData = {
97
- enableNotifications: _this.settings.enableNotifications,
98
- memberId: _this.settings.memberId,
99
- uri: {
100
- ..._this.settings.uri,
101
- translationPath
102
- },
103
- uomSymbol: _this.settings.uomSymbol,
104
- spaceName: _this.settings.spaceName,
105
- apiKey: _this.settings.apiKey,
106
- isWidgetMinimized: _this.settings.isWidgetMinimized,
107
- bindContainer: _this.settings.bindContainer,
108
- miniBindContainer: _this.settings.miniBindContainer,
109
- language: _this.settings.language,
110
- loadTranslationsForceCustomPath: _this.settings.loadTranslationsForceCustomPath,
111
- resources: [..._this.settings.resources],
112
- widgetStatus: _this.settings.widgetStatus,
113
- widgetData: Array.isArray(widgetData?.data) ? widgetData.data : [widget]
114
- };
115
- const initialise = function () {
116
- if (typeof window._clWidgetV32026SelfInit !== 'function') {
117
- throw new Error('CompetitionLabs widget script did not create window._clWidgetV32026SelfInit');
118
- }
119
-
120
- window._clWidgetV32026 = new window._clWidgetV32026SelfInit({
121
- ...widgetInstanceData
122
- });
123
- };
124
-
125
- if (!widgetAssets.script) {
126
- throw new Error(`CompetitionLabs widget configuration does not contain a script for ${widget.layout}`);
127
- }
128
-
129
- if (typeof window._clWidgetV32026SelfInit !== 'undefined') {
130
- initialise();
131
-
132
- }else {
133
- const script = document.createElement('script');
134
- script.type = 'text/javascript';
135
- script.src = widgetAssets.script;
136
- script.onload = function () {
137
- initialise();
138
- };
139
- script.onerror = function () {
140
- throw new Error(`CompetitionLabs widget script failed to load: ${widgetAssets.script}`);
141
- };
142
- document.body.appendChild(script);
143
- }
144
-
145
- };
146
-
147
- this.getWidgetData = function (callback) {
148
- const _this = this;
149
- const widgetDataUrl = `${_this.settings.uri.gatewayDomain}${_this.settings.uri.widgetData.replace(':space', _this.settings.spaceName)}?status=${_this.settings.widgetStatus}`;
150
- _this.ajax.abort().getData({
151
- type: 'GET',
152
- url: widgetDataUrl,
153
- headers: {
154
- 'X-API-KEY': _this.settings.apiKey
155
- },
156
- success: function (response, dataObj, xhr) {
157
- if (xhr.status === 200) {
158
- const json = JSON.parse(response);
159
- _this.initialiseWidget(json);
160
- if (typeof callback === 'function') {
161
- callback(json);
162
- }
163
- }
164
- },
165
- error: function () {
166
- if (typeof callback === 'function') {
167
- callback();
168
- }
169
- }
170
- });
171
- };
172
-
173
- this.init = function (callback) {
174
- const _this = this;
175
- console.log('Initting Data')
176
- _this.getWidgetData(callback);
177
- };
178
- };
179
-
180
- /* eslint new-cap: "off" */
181
- window.clLoaderV3 = new loadScript();
182
- })();
package/sizeof.js DELETED
@@ -1,27 +0,0 @@
1
- const isElement = function (value) {
2
- return typeof Element !== 'undefined' && value instanceof Element;
3
- };
4
-
5
- /**
6
- * returns the size of an Object or array
7
- *
8
- * @memberOf module:utils
9
- * @static
10
- * @param obj {Object}
11
- * @return {Number}
12
- */
13
- const sizeof = function (obj) {
14
- let size = 0;
15
- let key;
16
- for (key in obj) {
17
- if (obj.hasOwnProperty(key)) size++;
18
- }
19
-
20
- if (size === 0 && isElement(obj)) {
21
- size = 1;
22
- }
23
-
24
- return size;
25
- };
26
-
27
- export default sizeof;
package/vite.config.js DELETED
@@ -1,12 +0,0 @@
1
- import { defineConfig } from 'vite';
2
-
3
- export default defineConfig({
4
- build: {
5
- lib: {
6
- entry: 'loader.js',
7
- name: 'CompetitionLabsWidgetLoader',
8
- formats: ['iife'],
9
- fileName: () => 'loader.js'
10
- }
11
- }
12
- });