Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

83 строки
1.7 KiB

  1. import * as I from './interfaces/';
  2. import { Scrollbar } from './scrollbar'; // used as type annotations
  3. export class ScrollbarPlugin implements I.ScrollbarPlugin {
  4. static pluginName = '';
  5. static defaultOptions: any = {};
  6. readonly scrollbar: Scrollbar;
  7. readonly options: any;
  8. readonly name: string;
  9. constructor(
  10. scrollbar: Scrollbar,
  11. options?: any,
  12. ) {
  13. this.scrollbar = scrollbar;
  14. this.name = new.target.pluginName;
  15. this.options = {
  16. ...new.target.defaultOptions,
  17. ...options,
  18. };
  19. }
  20. onInit() {}
  21. onDestroy() {}
  22. onUpdate() {}
  23. onRender(_remainMomentum: I.Data2d) {}
  24. transformDelta(delta: I.Data2d, _evt: Event): I.Data2d {
  25. return { ...delta };
  26. }
  27. }
  28. export type PluginMap = {
  29. order: Set<string>,
  30. constructors: {
  31. [name: string]: typeof ScrollbarPlugin,
  32. },
  33. };
  34. export const globalPlugins: PluginMap = {
  35. order: new Set<string>(),
  36. constructors: {},
  37. };
  38. export function addPlugins(
  39. ...Plugins: (typeof ScrollbarPlugin)[]
  40. ): void {
  41. Plugins.forEach((P) => {
  42. const { pluginName } = P;
  43. if (!pluginName) {
  44. throw new TypeError(`plugin name is required`);
  45. }
  46. globalPlugins.order.add(pluginName);
  47. globalPlugins.constructors[pluginName] = P;
  48. });
  49. }
  50. export function initPlugins(
  51. scrollbar: Scrollbar,
  52. options: any,
  53. ): ScrollbarPlugin[] {
  54. return Array.from(globalPlugins.order)
  55. .filter((pluginName: string) => {
  56. return options[pluginName] !== false;
  57. })
  58. .map((pluginName: string) => {
  59. const Plugin = globalPlugins.constructors[pluginName];
  60. const instance = new Plugin(scrollbar, options[pluginName]);
  61. // bind plugin options to `scrollbar.options`
  62. options[pluginName] = instance.options;
  63. return instance;
  64. });
  65. }