|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @module examples/index |
| 5 | + * @description Initialize all factories |
| 6 | + * @author OlegDutchenko <[email protected]> |
| 7 | + * @version 0.0.1 |
| 8 | + */ |
| 9 | + |
| 10 | +// ---------------------------------------- |
| 11 | +// Imports |
| 12 | +// ---------------------------------------- |
| 13 | + |
| 14 | +import $ from 'jquery'; |
| 15 | +import 'custom-jquery-methods/fn/has-inited-key'; // optional |
| 16 | +import { SomeJqueryPluginAbstract } from './abstract-class'; |
| 17 | +import * as factory from './factory'; |
| 18 | + |
| 19 | +// ---------------------------------------- |
| 20 | +// Private |
| 21 | +// ---------------------------------------- |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {jQuery} $container |
| 25 | + * @param {string} key |
| 26 | + * @return {SomeJqueryPluginAbstract} |
| 27 | + * @private |
| 28 | + */ |
| 29 | +function _create ($container, key) { |
| 30 | + const { |
| 31 | + Factory, |
| 32 | + settings: clientSettings = {}, |
| 33 | + props: clientProps = {} |
| 34 | + } = $container.data(key) || {}; |
| 35 | + |
| 36 | + if (Factory in factory) { |
| 37 | + return new factory[Factory]($container, clientSettings, clientProps); |
| 38 | + } |
| 39 | + return new SomeJqueryPluginAbstract($container, clientSettings, clientProps); |
| 40 | +} |
| 41 | + |
| 42 | +// ---------------------------------------- |
| 43 | +// Exports |
| 44 | +// ---------------------------------------- |
| 45 | + |
| 46 | +/** |
| 47 | + * @param {jQuery} $containers |
| 48 | + */ |
| 49 | +export function initialize ($containers) { |
| 50 | + $containers.each((i, container) => { |
| 51 | + const $container = $(container); |
| 52 | + |
| 53 | + // optional action |
| 54 | + // avoid duplicated initializing |
| 55 | + if ($container.hasInitedKey('someJqueryPluginIsInitialized')) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + const instance = _create($container, 'initialize-some-jquery-plugin'); |
| 60 | + instance.initialize(); |
| 61 | + }); |
| 62 | +} |
0 commit comments