From 0af4914415be43b5320116daf11424017bed9158 Mon Sep 17 00:00:00 2001
From: oxixes <38050447+oxixes@users.noreply.github.com>
Date: Tue, 28 Nov 2023 19:41:58 +0100
Subject: [PATCH 1/2] Handle v2 widgets
---
root/src/config.json | 4 ++++
root/src/config.xml | 5 +++++
root/src/index.html | 16 ++--------------
root/src/js/main.js | 14 --------------
root/src/js/name.js | 24 ++++++++++++------------
root/src/ts/main.ts | 11 -----------
root/src/ts/name.ts | 15 +++++++++++++++
root/tests/js/nameSpec.js | 5 +++--
8 files changed, 41 insertions(+), 53 deletions(-)
delete mode 100644 root/src/js/main.js
delete mode 100644 root/src/ts/main.ts
diff --git a/root/src/config.json b/root/src/config.json
index e82e4d2..1ca2c78 100644
--- a/root/src/config.json
+++ b/root/src/config.json
@@ -5,6 +5,7 @@
"name": "{%= author_name %}"
}
],
+ "macversion": 2,
"changelog": "doc/changelog.md",
"contents": {
"cacheable": true,
@@ -33,6 +34,9 @@
}
{% } %}
],
+ "js_files": [
+ "js/{%= jsname %}.js"
+ ],
"title": "{%= project_name %}",
"translations": {},
"type": "widget",
diff --git a/root/src/config.xml b/root/src/config.xml
index 9242ca9..c053483 100644
--- a/root/src/config.xml
+++ b/root/src/config.xml
@@ -1,6 +1,8 @@
+ 2
+
{%= project_name %}
{%= homepage %}
@@ -27,6 +29,9 @@
+
+
+
diff --git a/root/src/index.html b/root/src/index.html
index 5806525..c04fdf4 100644
--- a/root/src/index.html
+++ b/root/src/index.html
@@ -1,15 +1,3 @@
-
-
-
-
-
- Empty {%= name %} widget
-
-
+
- Empty {%= name %} widget
-
-
-
-
-
+Empty {%= name %} widget
\ No newline at end of file
diff --git a/root/src/js/main.js b/root/src/js/main.js
deleted file mode 100644
index 9752fa3..0000000
--- a/root/src/js/main.js
+++ /dev/null
@@ -1,14 +0,0 @@
-/*
- * {%= name %}
- * {%= homepage %}
- *
- * Copyright (c) {%= grunt.template.today('yyyy') %} {%= vendor_title %}
- * Licensed under the {%= licenses.join(', ') %} license{%= licenses.length === 1 ? '' : 's' %}.
- */
-
-/* globals {%= jsname %} */
-
-window.onload = function () {
- "use strict";
- new {%= jsname %}();
-};
diff --git a/root/src/js/name.js b/root/src/js/name.js
index 38abf5a..72a9ae8 100644
--- a/root/src/js/name.js
+++ b/root/src/js/name.js
@@ -8,7 +8,7 @@
/* exported {%= jsname %} */
-var {%= jsname %} = (function () {
+(function () {
"use strict";
@@ -16,22 +16,22 @@ var {%= jsname %} = (function () {
// CLASS DEFINITION
// =========================================================================
- var {%= jsname %} = function {%= jsname %}() {
- MashupPlatform.prefs.registerCallback(function (new_preferences) {
+ class {%= jsname %} {
+ constructor(MashupPlatform, shadowDOM, extra) {
+ this.MashupPlatform = MashupPlatform;
+ this.shadowDOM = shadowDOM;
- }.bind(this));
- };
+ MashupPlatform.prefs.registerCallback(function (new_preferences) {
- // =========================================================================
- // PRIVATE MEMBERS
- // =========================================================================
+ }.bind(this));
+ }
+ }
+
+ // We define the class as part of the window object so that it can be instantiated by Wirecloud
+ window["{%= jsname %}"] = {%= jsname %};
/* test-code */
- {%= jsname %}.prototype = {
- };
/* end-test-code */
- return {%= jsname %};
-
})();
diff --git a/root/src/ts/main.ts b/root/src/ts/main.ts
deleted file mode 100644
index 2c52d14..0000000
--- a/root/src/ts/main.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-///
-
-"use strict";
-
-/* import-block */
-import mod = require("{%= jsname %}");
-let {%= jsname %} = mod.{%= jsname %};
-/* end-import-block */
-
-let widget = new {%= jsname %}();
-document.addEventListener("DOMContentLoaded", () => widget.init(), false);
diff --git a/root/src/ts/name.ts b/root/src/ts/name.ts
index c05779a..b695ace 100644
--- a/root/src/ts/name.ts
+++ b/root/src/ts/name.ts
@@ -10,7 +10,22 @@ import MashupPlatform = require("MashupPlatform");
/* end-import-block */
export class {%= jsname %} {
+ private MashupPlatform: MashupPlatform;
+ private shadowDOM: any;
+ {% if (ngsi) { %}private NGSI: NGSI;{% }%}
+
+ constructor(MashupPlatform: MashupPlatform, shadowDOM: any, extra: any) {
+ this.MashupPlatform = MashupPlatform;
+ this.shadowDOM = shadowDOM;
+ {% if (ngsi) { %}this.NGSI = NGSI;{% }%}
+
+ this.init();
+ }
+
init() {
console.log("Loaded!!");
}
}
+
+// We define the class as part of the window object so that it can be instantiated by Wirecloud
+(window)["{%= jsname %}"] = {%= jsname %};
diff --git a/root/tests/js/nameSpec.js b/root/tests/js/nameSpec.js
index 47f619b..6aa3345 100644
--- a/root/tests/js/nameSpec.js
+++ b/root/tests/js/nameSpec.js
@@ -15,16 +15,17 @@
describe("{%= jsname %}", function () {
var widget;
+ var MashupPlatform;
beforeAll(function () {
- window.MashupPlatform = new MockMP({
+ MashupPlatform = new MockMP({
type: 'widget'
});
});
beforeEach(function () {
MashupPlatform.reset();
- widget = new {%= jsname %}();
+ widget = new {%= jsname %}(MashupPlatform, undefined, {});
});
it("Dummy test", function () {
From 781d9d1345a39670ae3ee3ca4dcc4f6c6d412bad Mon Sep 17 00:00:00 2001
From: oxixes
Date: Sun, 10 Mar 2024 12:35:05 +0100
Subject: [PATCH 2/2] Add entrypoint and fix linting on creation
---
root/Gruntfile.js | 3 +--
root/src/config.json | 1 +
root/src/config.xml | 1 +
root/src/js/name.js | 6 +++---
root/src/ts/name.ts | 4 ++--
root/tests/js/nameSpec.js | 4 ++--
root/typings/MashupPlatform/MashupPlatform.d.ts | 2 ++
template.js | 8 ++++++++
8 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/root/Gruntfile.js b/root/Gruntfile.js
index 2f1fdfe..00f0934 100644
--- a/root/Gruntfile.js
+++ b/root/Gruntfile.js
@@ -6,7 +6,6 @@
* Licensed under the {%= licenses.join(', ') %} license{%= licenses.length === 1 ? '' : 's' %}.
*/
-const { json } = require('stream/consumers');
var ConfigParser = require('wirecloud-config-parser');
var parser = new ConfigParser('src/{% if (json) { %}config.json{% } else { %}config.xml{% } %}');
@@ -109,7 +108,7 @@ module.exports = function (grunt) {
'index.html',{% if (!js) { %}
"ts/**/*",{% }%}{% if (json) { %}
"config.json",{% } else { %}
- "config.xml", {% } %}
+ "config.xml",{% } %}
]
},
{
diff --git a/root/src/config.json b/root/src/config.json
index 1ca2c78..2d07ea3 100644
--- a/root/src/config.json
+++ b/root/src/config.json
@@ -37,6 +37,7 @@
"js_files": [
"js/{%= jsname %}.js"
],
+ "entrypoint": "js/{%= entrypoint %}.js",
"title": "{%= project_name %}",
"translations": {},
"type": "widget",
diff --git a/root/src/config.xml b/root/src/config.xml
index c053483..a41f081 100644
--- a/root/src/config.xml
+++ b/root/src/config.xml
@@ -32,6 +32,7 @@
+
diff --git a/root/src/js/name.js b/root/src/js/name.js
index 72a9ae8..a8d69ff 100644
--- a/root/src/js/name.js
+++ b/root/src/js/name.js
@@ -6,7 +6,7 @@
* Licensed under the {%= licenses.join(', ') %} license{%= licenses.length === 1 ? '' : 's' %}.
*/
-/* exported {%= jsname %} */
+/* exported {%= entrypoint %} */
(function () {
@@ -16,7 +16,7 @@
// CLASS DEFINITION
// =========================================================================
- class {%= jsname %} {
+ class {%= entrypoint %} {
constructor(MashupPlatform, shadowDOM, extra) {
this.MashupPlatform = MashupPlatform;
this.shadowDOM = shadowDOM;
@@ -28,7 +28,7 @@
}
// We define the class as part of the window object so that it can be instantiated by Wirecloud
- window["{%= jsname %}"] = {%= jsname %};
+ window.{%= entrypoint %} = {%= entrypoint %};
/* test-code */
diff --git a/root/src/ts/name.ts b/root/src/ts/name.ts
index b695ace..3997a68 100644
--- a/root/src/ts/name.ts
+++ b/root/src/ts/name.ts
@@ -9,7 +9,7 @@ import MashupPlatform = require("MashupPlatform");
{% if (ngsi) { %}import NGSI = require("NGSI");{% }%}
/* end-import-block */
-export class {%= jsname %} {
+export class {%= entrypoint %} {
private MashupPlatform: MashupPlatform;
private shadowDOM: any;
{% if (ngsi) { %}private NGSI: NGSI;{% }%}
@@ -28,4 +28,4 @@ export class {%= jsname %} {
}
// We define the class as part of the window object so that it can be instantiated by Wirecloud
-(window)["{%= jsname %}"] = {%= jsname %};
+(window)["{%= entrypoint %}"] = {%= entrypoint %};
diff --git a/root/tests/js/nameSpec.js b/root/tests/js/nameSpec.js
index 6aa3345..066adc1 100644
--- a/root/tests/js/nameSpec.js
+++ b/root/tests/js/nameSpec.js
@@ -6,7 +6,7 @@
* Licensed under the {%= licenses.join(', ') %} license{%= licenses.length === 1 ? '' : 's' %}.
*/
-/* globals $, MashupPlatform, MockMP, {%= jsname %} */
+/* globals $, MashupPlatform, MockMP, {%= entrypoint %} */
(function () {
@@ -25,7 +25,7 @@
beforeEach(function () {
MashupPlatform.reset();
- widget = new {%= jsname %}(MashupPlatform, undefined, {});
+ widget = new {%= entrypoint %}(MashupPlatform, undefined, {});
});
it("Dummy test", function () {
diff --git a/root/typings/MashupPlatform/MashupPlatform.d.ts b/root/typings/MashupPlatform/MashupPlatform.d.ts
index d98720b..90c8722 100644
--- a/root/typings/MashupPlatform/MashupPlatform.d.ts
+++ b/root/typings/MashupPlatform/MashupPlatform.d.ts
@@ -115,4 +115,6 @@ declare module "MashupPlatform" {
interface onExceptionCB {
(response: Object, exception: Error): void;
}
+
+ var location: string;
}
diff --git a/template.js b/template.js
index 2bea84b..2dcc2f8 100644
--- a/template.js
+++ b/template.js
@@ -51,6 +51,13 @@ var capitalizeAndRemoveUnderscore = function capitalizeAndRemoveUnderscore(old)
return t.charAt(0).toUpperCase() + t.slice(1);
};
+var getEntrypointName = function getEntrypointName(vendor, name) {
+ // Remove all non-alphanumeric characters. Replace spaces with underscores.
+ vendor = vendor.replace(/[^a-zA-Z0-9]/g, '').replace(/\s/g, '_');
+ name = name.replace(/[^a-zA-Z0-9]/g, '').replace(/\s/g, '_');
+ return vendor + '_' + name;
+}
+
// The actual init template.
exports.template = function(grunt, init, done) {
init.process([
@@ -123,6 +130,7 @@ exports.template = function(grunt, init, done) {
], function(err, props){
var exportsOverride = {};
props.jsname = capitalizeAndRemoveUnderscore(props.name);
+ props.entrypoint = getEntrypointName(props.vendor, props.name);
props.bower = true; // Change way to determine bower?
props.ngsi = false; // ??
var bowerdeps = {};