|
1 | | -# web-plugin-interface |
| 1 | +# web-plugin-interface |
| 2 | + |
| 3 | +[](https://github.com/WezomAgency/web-plugin-interface/blob/master/LICENSE) |
| 4 | +[](https://www.npmjs.com/package/web-plugin-interface) |
| 5 | +[](https://github.com/WezomAgency/eslint-config-wezom-relax#readme) |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +A simple interface for implementing abstract factories. |
| 10 | +These factories allow you to remove routine work, as well as to simplify the preparation and initialization of most web plug-ins: sliders, carousels, pop-up windows, etc. |
| 11 | + |
| 12 | +Although in JavaScript, interfaces are not yet implemented by means of the syntax of the language, but we can follow the basic principles `Interface ⇒ Abstract ⇒ Factory`. |
| 13 | + |
| 14 | +When creating an abstraction, we must implement all the methods and properties of the interface. Which are initially empty and have no logic and code. |
| 15 | +The basis must be laid in an abstract class and brought to a final and typical concept in the classes of factories. |
| 16 | + |
| 17 | +`WebPluginInterface` has the core methods and properties that are key to working with most web plugins. |
| 18 | +Below is a list of these methods and properties. Then you can see [an example implementation](##usage-example). |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +- [`constructor()`](#constructor) |
| 23 | +- [`_setup()`](#_setup) |
| 24 | +- [`_beforeInitialize()`](#_beforeinitialize) |
| 25 | +- [`_afterInitialize()`](#_afterinitialize) |
| 26 | +- [`initialize()`](#initialize) |
| 27 | +- [`defaults`](#defaults) |
| 28 | + |
| 29 | + |
| 30 | +### `constructor()` |
| 31 | + |
| 32 | +Here we describe the future instances.: |
| 33 | +- get data from arguments |
| 34 | +- declare instance properties |
| 35 | + |
| 36 | + |
| 37 | +### `_setup()` |
| 38 | + |
| 39 | +_`protected`_ |
| 40 | + |
| 41 | +The method is designed to prepare data for initialization. |
| 42 | +For example, setting up future plugin options, etc. |
| 43 | + |
| 44 | +### `_beforeInitialize()` |
| 45 | + |
| 46 | +_`protected`_ |
| 47 | + |
| 48 | +It describes the actions that must be performed before initializing the plug-in. |
| 49 | +For example, add the CSS class `.is-ready` to the HTML element to which your plugin will be applied. |
| 50 | + |
| 51 | +### `_afterInitialize()` |
| 52 | + |
| 53 | +_`protected`_ |
| 54 | + |
| 55 | +Here we can describe the actions that should be performed after the initialization of the plugin. |
| 56 | +For example, add an additional handler to scroll or resize window events, which will update the parameters of your current plugin. |
| 57 | + |
| 58 | +### `initialize()` |
| 59 | + |
| 60 | +_`public`_ |
| 61 | + |
| 62 | +Directly launch your plugin. |
| 63 | + |
| 64 | +### `defaults` |
| 65 | + |
| 66 | +_`public`_ |
| 67 | + |
| 68 | +A getter that returns an object with default options, settings, or configuration for your plugin. |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## Usage example |
| 73 | + |
| 74 | +Abstract class example with interface implementation |
| 75 | + |
| 76 | +```js |
| 77 | +// some-plugin-init/abstract.js |
| 78 | + |
| 79 | +import $ from 'jquery'; // for example |
| 80 | +import 'some-jquery-plugin'; // for example |
| 81 | +import { WebPluginInterface } from 'web-plugin-interface'; |
| 82 | + |
| 83 | +/** |
| 84 | +* @implements WebPluginInterface |
| 85 | +*/ |
| 86 | +export class SomeJqueryPluginAbstract extends WebPluginInterface { |
| 87 | + /** |
| 88 | + * @param {jQuery} $container |
| 89 | + * @param {Object} [customSettings={}] |
| 90 | + */ |
| 91 | + constructor ($container, customSettings = {}) { |
| 92 | + super(); |
| 93 | + this.$container = $container; |
| 94 | + this.customSettings = customSettings; |
| 95 | + this.settings = {}; |
| 96 | + this.readyCssClass = 'is-ready'; |
| 97 | + this.initializedCssClass = 'is-initialized'; |
| 98 | + } |
| 99 | + |
| 100 | + /** @protected */ |
| 101 | + _setup () { |
| 102 | + this.settings = $.extends({}, this.defaults, this.customSettings); |
| 103 | + } |
| 104 | + |
| 105 | + /** @protected */ |
| 106 | + _beforeInitialize () { |
| 107 | + this.$container.addClass(this.readyCssClass); |
| 108 | + } |
| 109 | + |
| 110 | + /** @protected */ |
| 111 | + _afterInitialize () { |
| 112 | + this.$container.addClass(this.initializedCssClass); |
| 113 | + } |
| 114 | + |
| 115 | + /** @public */ |
| 116 | + initialize () { |
| 117 | + this._setup(); |
| 118 | + this._beforeInitialize(); |
| 119 | + this.$container.someJqueryPlugin(this.settings); |
| 120 | + this._afterInitialize(); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @public |
| 125 | + * @returns Object |
| 126 | + */ |
| 127 | + get defaults () { |
| 128 | + return { |
| 129 | + // an example of some options of your plugin |
| 130 | + autoplay: false, |
| 131 | + speed: 500 |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
0 commit comments