mirror of
https://github.com/sourcey/spectacle.git
synced 2024-09-11 05:25:36 +03:00
55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
import $ from 'jquery';
|
|
import { GetYoDigits } from './foundation.util.core';
|
|
|
|
// Abstract class for providing lifecycle hooks. Expect plugins to define AT LEAST
|
|
// {function} _setup (replaces previous constructor),
|
|
// {function} _destroy (replaces previous destroy)
|
|
class Plugin {
|
|
|
|
constructor(element, options) {
|
|
this._setup(element, options);
|
|
var pluginName = getPluginName(this);
|
|
this.uuid = GetYoDigits(6, pluginName);
|
|
|
|
if(!this.$element.attr(`data-${pluginName}`)){ this.$element.attr(`data-${pluginName}`, this.uuid); }
|
|
if(!this.$element.data('zfPlugin')){ this.$element.data('zfPlugin', this); }
|
|
/**
|
|
* Fires when the plugin has initialized.
|
|
* @event Plugin#init
|
|
*/
|
|
this.$element.trigger(`init.zf.${pluginName}`);
|
|
}
|
|
|
|
destroy() {
|
|
this._destroy();
|
|
var pluginName = getPluginName(this);
|
|
this.$element.removeAttr(`data-${pluginName}`).removeData('zfPlugin')
|
|
/**
|
|
* Fires when the plugin has been destroyed.
|
|
* @event Plugin#destroyed
|
|
*/
|
|
.trigger(`destroyed.zf.${pluginName}`);
|
|
for(var prop in this){
|
|
this[prop] = null;//clean up script to prep for garbage collection.
|
|
}
|
|
}
|
|
}
|
|
|
|
// Convert PascalCase to kebab-case
|
|
// Thank you: http://stackoverflow.com/a/8955580
|
|
function hyphenate(str) {
|
|
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
}
|
|
|
|
function getPluginName(obj) {
|
|
if(typeof(obj.constructor.name) !== 'undefined') {
|
|
return hyphenate(obj.constructor.name);
|
|
} else {
|
|
return hyphenate(obj.className);
|
|
}
|
|
}
|
|
|
|
export {Plugin};
|