|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @module examples/abstract-class |
| 5 | + * @description Abstract class example with WebPluginInterface implementation |
| 6 | + * @author OlegDutchenko <[email protected]> |
| 7 | + * @version 0.0.1 |
| 8 | + */ |
| 9 | + |
| 10 | +import $ from 'jquery'; // for example |
| 11 | +import 'some-jquery-plugin'; // for example |
| 12 | +import { WebPluginInterface } from 'web-plugin-interface'; |
| 13 | + |
| 14 | +/** |
| 15 | + * @implements WebPluginInterface |
| 16 | + */ |
| 17 | +export class SomeJqueryPluginAbstract extends WebPluginInterface { |
| 18 | + /** |
| 19 | + * @param {jQuery} $container |
| 20 | + * @param {Object} [customSettings={}] |
| 21 | + * @param {Object} [customProps={}] |
| 22 | + */ |
| 23 | + constructor ($container, customSettings = {}, customProps = {}) { |
| 24 | + super(); |
| 25 | + this.$container = $container; |
| 26 | + this.customSettings = customSettings; |
| 27 | + this.settings = {}; |
| 28 | + this.props = {}; |
| 29 | + this.readyCssClass = 'is-ready'; |
| 30 | + this.initializedCssClass = 'is-initialized'; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @type {Object} |
| 35 | + */ |
| 36 | + get defaultSettings () { |
| 37 | + return { |
| 38 | + // an example of some options of your plugin |
| 39 | + autoplay: true, |
| 40 | + speed: 500 |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @type {Object} |
| 46 | + */ |
| 47 | + get defaultProps () { |
| 48 | + return { |
| 49 | + // an example of options that is native for your plugin |
| 50 | + stopAutoPlayIfOutView: true |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @protected |
| 56 | + */ |
| 57 | + _setup () { |
| 58 | + this.props = $.extends({}, this.defaultProps, this.customProps); |
| 59 | + this.settings = $.extends({}, this.defaultSettings, this.customSettings); |
| 60 | + |
| 61 | + // props example |
| 62 | + if (this.props.stopAutoPlayIfOutView) { |
| 63 | + this.settings.autoplay = this.detectIfInView; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @protected |
| 69 | + */ |
| 70 | + _beforeInitialize () { |
| 71 | + this.$container.addClass(this.readyCssClass); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @protected |
| 76 | + */ |
| 77 | + _afterInitialize () { |
| 78 | + this.$container.addClass(this.initializedCssClass); |
| 79 | + |
| 80 | + // props example |
| 81 | + if (this.props.stopAutoPlayIfOutView) { |
| 82 | + this._autoplayInViewObserve(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + initialize () { |
| 87 | + this._setup(); |
| 88 | + this._beforeInitialize(); |
| 89 | + |
| 90 | + // fire up |
| 91 | + this.$container.someJqueryPlugin(this.settings); |
| 92 | + |
| 93 | + this._afterInitialize(); |
| 94 | + } |
| 95 | + |
| 96 | + // ------------------------------ |
| 97 | + // Custom extend implemented interface |
| 98 | + // ------------------------------ |
| 99 | + |
| 100 | + /** |
| 101 | + * @type {boolean} |
| 102 | + */ |
| 103 | + get detectIfInView () { |
| 104 | + // your code |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @protected |
| 109 | + */ |
| 110 | + _autoplayInViewObserve () { |
| 111 | + // your code |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | + |
0 commit comments