|
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. |
| 19 | + |
| 20 | +- `constructor()` |
| 21 | +- `_setup()` |
| 22 | +- `_beforeInitialize()` |
| 23 | +- `_afterInitialize()` |
| 24 | +- `initialize()` |
| 25 | +- `defaults` |
| 26 | + |
| 27 | + |
| 28 | +### `constructor()` |
| 29 | + |
| 30 | +Here we describe the future instances.: |
| 31 | +- get data from arguments |
| 32 | +- declare instance properties |
| 33 | + |
| 34 | + |
| 35 | +### `_setup()` |
| 36 | + |
| 37 | +`protected` |
| 38 | + |
| 39 | +The method is designed to prepare data for initialization. |
| 40 | +For example, setting up future plugin options, etc. |
| 41 | + |
| 42 | +### `_beforeInitialize()` |
| 43 | + |
| 44 | +`protected` |
| 45 | + |
| 46 | +It describes the actions that must be performed before initializing the plug-in. |
| 47 | +For example, add the CSS class `.is-ready` to the HTML element to which your plugin will be applied. |
| 48 | + |
| 49 | +### `_afterInitialize()` |
| 50 | + |
| 51 | +`protected` |
| 52 | + |
| 53 | +Here we can describe the actions that should be performed after the initialization of the plugin. |
| 54 | +For example, add an additional handler to scroll or resize window events, which will update the parameters of your current plugin. |
| 55 | + |
| 56 | +### `initialize()` |
| 57 | + |
| 58 | +`public` |
| 59 | + |
| 60 | +Directly launch your plugin. |
| 61 | + |
| 62 | +### `defaults` |
| 63 | + |
| 64 | +`public` |
| 65 | + |
| 66 | +A getter that returns an object with default options, settings, or configuration for your plugin. |
| 67 | + |
| 68 | +## Usage example |
| 69 | + |
| 70 | +```js |
| 71 | +import $ from 'jquery'; // for example |
| 72 | +import 'some-jquery-plugin'; // for example |
| 73 | +import { WebPluginInterface } from 'web-plugin-interface'; |
| 74 | + |
| 75 | +/** |
| 76 | +* @implements WebPluginInterface |
| 77 | +*/ |
| 78 | +export class SomeJqueryPluginAbstract extends WebPluginInterface { |
| 79 | + /** |
| 80 | + * @param {jQuery} $container |
| 81 | + * @param {Object} [customSettings={}] |
| 82 | + */ |
| 83 | + constructor ($container, customSettings = {}) { |
| 84 | + super(); |
| 85 | + this.$container = $container; |
| 86 | + this.customSettings = customSettings; |
| 87 | + this.settings = {}; |
| 88 | + this.readyCssClass = 'is-ready'; |
| 89 | + this.initializedCssClass = 'is-initialized'; |
| 90 | + } |
| 91 | + |
| 92 | + /** @protected */ |
| 93 | + _setup () { |
| 94 | + this.settings = $.extends({}, this.defaults, this.customSettings); |
| 95 | + } |
| 96 | + |
| 97 | + /** @protected */ |
| 98 | + _beforeInitialize(){ |
| 99 | + this.$container.addClass(this.readyCssClass); |
| 100 | + } |
| 101 | + |
| 102 | + /** @protected */ |
| 103 | + _afterInitialize(){ |
| 104 | + this.$container.addClass(this.initializedCssClass) |
| 105 | + } |
| 106 | + |
| 107 | + /** @public */ |
| 108 | + initialize(){ |
| 109 | + this.$container.someJqueryPlugin(this.settings); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @public |
| 114 | + * @returns Object |
| 115 | + */ |
| 116 | + get defaults (){ |
| 117 | + return { |
| 118 | + // an example of some options of your plugin |
| 119 | + autoplay: false, |
| 120 | + speed: 500 |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
0 commit comments