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 @@ + + - - - +
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 @@