From 023db9c771c59b2bef27f33ca9dd331780016e40 Mon Sep 17 00:00:00 2001 From: Marc Arndt Date: Fri, 14 Jun 2019 20:34:26 +0200 Subject: [PATCH 1/4] - refactor the ShortcutReader and WineShortcutReader classes --- Engines/Wine/Shortcuts/Reader/script.js | 161 ++++++++++++------------ 1 file changed, 81 insertions(+), 80 deletions(-) diff --git a/Engines/Wine/Shortcuts/Reader/script.js b/Engines/Wine/Shortcuts/Reader/script.js index ccdb1a3606..f127d5f6ce 100644 --- a/Engines/Wine/Shortcuts/Reader/script.js +++ b/Engines/Wine/Shortcuts/Reader/script.js @@ -1,122 +1,123 @@ include("engines.wine.engine.object"); -var _WineShortcutReader = function (shortcut) { - var that = this; - that._shortcutManager = Bean("shortcutManager"); - that._libraryManager = Bean("libraryManager"); - that._uiQuestionFactory = Bean("uiQuestionFactory"); - that._winePrefixesDirectory = Bean("propertyReader").getProperty("application.user.containers") + "/" + WINE_PREFIX_DIR + "/"; +class WineShortcutReader { + constructor(shortcut) { + this.shortcut = shortcut; + + this.shortcutManager = Bean("shortcutManager"); + this.libraryManager = Bean("libraryManager"); + this.uiQuestionFactory = Bean("uiQuestionFactory"); + this.winePrefixesDirectory = Bean("propertyReader").getProperty("application.user.containers") + "/" + WINE_PREFIX_DIR + "/"; + } - this.shortcut = shortcut; + get wineprefix() { + const shortcutContent = JSON.parse(this.shortcut.script); - this.wineprefix = function () { - var shortcutContent = JSON.parse(this.shortcut.script); return shortcutContent.winePrefix; - }; - - this.container = this.wineprefix; + } - this.run = function (userArguments) { - var shortcutContent = JSON.parse(this.shortcut.script); + run(userArguments) { + const shortcutContent = JSON.parse(this.shortcut.script); if (!userArguments) { userArguments = []; } - var args = (shortcutContent.arguments ? shortcutContent.arguments : []).concat(Java.from(userArguments)); + const args = (shortcutContent.arguments ? shortcutContent.arguments : []).concat(Java.from(userArguments)); + + const userData = { + wineDebug: shortcutContent.wineDebug + }; - var userData = {}; - userData["wineDebug"] = shortcutContent.wineDebug; new Wine() .prefix(shortcutContent.winePrefix) .run(shortcutContent.executable, args, shortcutContent.workingDirectory, false, false, userData) - }; + } - this.stop = function () { - var shortcutContent = JSON.parse(this.shortcut.script); + stop() { + const shortcutContent = JSON.parse(this.shortcut.script); new Wine() .prefix(shortcutContent.winePrefix) .kill() - }; + } + + uninstall() { + const shortcutContent = JSON.parse(this.shortcut.script); - this.uninstall = function () { - var shortcutContent = JSON.parse(this.shortcut.script); - var _winePrefix = shortcutContent.winePrefix; + const winePrefix = shortcutContent.winePrefix; - var _found = false; - this._libraryManager.fetchShortcuts().forEach(function (shortcutCategory) { + let found = false; + this.libraryManager.fetchShortcuts().forEach(function (shortcutCategory) { shortcutCategory.getShortcuts().forEach(function (shortcut) { - var _otherShortcutContent = JSON.parse(shortcut.script); + const otherShortcutContent = JSON.parse(shortcut.script); - if (_otherShortcutContent.winePrefix == _winePrefix && shortcut.name != that.shortcut.name) { - _found = true; + if (otherShortcutContent.winePrefix == winePrefix && shortcut.name != that.shortcut.name) { + found = true; } }); }); - this._shortcutManager.deleteShortcut(this.shortcut); + this.shortcutManager.deleteShortcut(this.shortcut); - if (!_found) { - this._uiQuestionFactory.create(tr("The container {0} is no longer used.\nDo you want to delete it?", _winePrefix), + if (!found) { + this.uiQuestionFactory.create(tr("The container {0} is no longer used.\nDo you want to delete it?", winePrefix), function () { - remove(that._winePrefixesDirectory + _winePrefix); + remove(that.winePrefixesDirectory + winePrefix); }); } } -}; - -/** -* ShortcutReader prototype -* @constructor -*/ -function ShortcutReader() { } -/** -* sets shortcut -* @param {string} shortcut shortcut -* @returns {void} -*/ -ShortcutReader.prototype.of = function (shortcut) { - this.shortcut = shortcut; - var shortcutContentParsed = JSON.parse(this.shortcut.script); - - if (shortcutContentParsed.type == "WINE") { - this._runner = new _WineShortcutReader(this.shortcut); +class ShortcutReader { + constructor() { + // do nothing } -} -/** -* runs shortcut -* @param {array} userArguments arguments -* @returns {void} -*/ -ShortcutReader.prototype.run = function (userArguments) { - this._runner.run(userArguments); -} + /** + * sets shortcut + * @param {string} shortcut shortcut + * @returns {void} + */ + of(shortcut) { + const shortcutContentParsed = JSON.parse(shortcut.script); -/** -* stops running shortcut -* @returns {void} -*/ -ShortcutReader.prototype.stop = function () { - this._runner.stop(); -} + if (shortcutContentParsed.type == "WINE") { + this.runner = new WineShortcutReader(shortcut); + } + } -/** -* uninstalls shortcut -* @returns {void} -*/ -ShortcutReader.prototype.uninstall = function () { - this._runner.uninstall(); -} + /** + * returns container of shortcut + * @returns {string} container + */ + get container() { + return this.runner.container(); + } -/** -* returns container of shortcut -* @returns {string} container -*/ -ShortcutReader.prototype.container = function () { - return this._runner.container(); + /** + * runs shortcut + * @param {array} userArguments arguments + * @returns {void} + */ + run(userArguments) { + this.runner.run(userArguments); + } + + /** + * stops running shortcut + * @returns {void} + */ + stop() { + this.runner.stop(); + } + + /** + * uninstalls shortcut + * @returns {void} + */ + uninstall() { + this.runner.uninstall(); + } } \ No newline at end of file From c98cb064248d7f431fe982b38116f83630dcce0b Mon Sep 17 00:00:00 2001 From: Marc Arndt Date: Sat, 15 Jun 2019 12:11:09 +0200 Subject: [PATCH 2/4] - add EOF --- Engines/Wine/Shortcuts/Reader/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engines/Wine/Shortcuts/Reader/script.js b/Engines/Wine/Shortcuts/Reader/script.js index f127d5f6ce..4746d81d05 100644 --- a/Engines/Wine/Shortcuts/Reader/script.js +++ b/Engines/Wine/Shortcuts/Reader/script.js @@ -120,4 +120,4 @@ class ShortcutReader { uninstall() { this.runner.uninstall(); } -} \ No newline at end of file +} From be16754019d26b6edd857b9d74dc9d309be8b8f3 Mon Sep 17 00:00:00 2001 From: Marc Arndt Date: Sun, 16 Jun 2019 12:52:39 +0200 Subject: [PATCH 3/4] - add a short documentation page for how to write classes inside scripts --- docs/_docs/General/best-practices.md | 2 +- docs/_docs/General/classes.md | 96 ++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 docs/_docs/General/classes.md diff --git a/docs/_docs/General/best-practices.md b/docs/_docs/General/best-practices.md index b57afcce11..bb1e31cd48 100644 --- a/docs/_docs/General/best-practices.md +++ b/docs/_docs/General/best-practices.md @@ -1,7 +1,7 @@ --- title: "Best practices" category: General -order: 3 +order: 4 toc: false --- diff --git a/docs/_docs/General/classes.md b/docs/_docs/General/classes.md new file mode 100644 index 0000000000..fe9e5397db --- /dev/null +++ b/docs/_docs/General/classes.md @@ -0,0 +1,96 @@ +--- +title: "Classes" +category: General +order: 3 +toc: true +--- + +Most components inside the scripts are implemented as JavaScript prototype classes. +To allow an easier understanding of the code and to be more inline with the Phoenicis application implementation classes should be implemented using the new [`ECMAScript 2015`]() syntax. + + +### Structure +A class defined with this newer syntax has the following structure: + +```javascript +class ClassName extends ParentClassName { + constructor() { + super(); + + // object initialization + } + + foo() { + // method body + + return ; + } + + bar() { + // method body + } +} +``` + +Where: +- `ClassName` is the unique name of the class +- `ParentClassName` is the name of the parent class (this is optional) +- `constructor(...)` is the constructor method of the class, which is called whenever an object of the class is instantiated via the `new` keyword +- `super(...)` calls the constructor method of the parent class +- `foo(...)` and `bar(...)` are prototype function definitions + +### Example +```javascript +class ShortcutReader { + constructor() { + // do nothing + } + + /** + * sets shortcut + * @param {string} shortcut shortcut + * @returns {void} + */ + of(shortcut) { + const shortcutContentParsed = JSON.parse(shortcut.script); + + if (shortcutContentParsed.type == "WINE") { + this.runner = new WineShortcutReader(shortcut); + } + } + + /** + * returns container of shortcut + * @returns {string} container + */ + get container() { + return this.runner.container(); + } + + /** + * runs shortcut + * @param {array} userArguments arguments + * @returns {void} + */ + run(userArguments) { + this.runner.run(userArguments); + } + + /** + * stops running shortcut + * @returns {void} + */ + stop() { + this.runner.stop(); + } + + /** + * uninstalls shortcut + * @returns {void} + */ + uninstall() { + this.runner.uninstall(); + } +} +``` + From 17d1b9c8733490df88279cb9f5da4673fe422064 Mon Sep 17 00:00:00 2001 From: Marc Arndt Date: Sun, 16 Jun 2019 12:55:09 +0200 Subject: [PATCH 4/4] - add missing link --- docs/_docs/General/classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_docs/General/classes.md b/docs/_docs/General/classes.md index fe9e5397db..d44982b38a 100644 --- a/docs/_docs/General/classes.md +++ b/docs/_docs/General/classes.md @@ -6,7 +6,7 @@ toc: true --- Most components inside the scripts are implemented as JavaScript prototype classes. -To allow an easier understanding of the code and to be more inline with the Phoenicis application implementation classes should be implemented using the new [`ECMAScript 2015`]() syntax. +To allow an easier understanding of the code and to be more inline with the Phoenicis application implementation classes should be implemented using the new [`ECMAScript 2015`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/class) syntax. ### Structure