diff --git a/Engines/Wine/Shortcuts/Reader/script.js b/Engines/Wine/Shortcuts/Reader/script.js index ccdb1a3606..4746d81d05 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() - }; + } - this.uninstall = function () { - var shortcutContent = JSON.parse(this.shortcut.script); - var _winePrefix = shortcutContent.winePrefix; + uninstall() { + const shortcutContent = JSON.parse(this.shortcut.script); - var _found = false; - this._libraryManager.fetchShortcuts().forEach(function (shortcutCategory) { + const winePrefix = shortcutContent.winePrefix; + + 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(); + } + + /** + * runs shortcut + * @param {array} userArguments arguments + * @returns {void} + */ + run(userArguments) { + this.runner.run(userArguments); + } + + /** + * stops running shortcut + * @returns {void} + */ + stop() { + this.runner.stop(); + } -/** -* returns container of shortcut -* @returns {string} container -*/ -ShortcutReader.prototype.container = function () { - return this._runner.container(); -} \ No newline at end of file + /** + * uninstalls shortcut + * @returns {void} + */ + uninstall() { + this.runner.uninstall(); + } +} 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..d44982b38a --- /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`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/class) 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(); + } +} +``` +