Removes code from #constructor to separate functions

This commit is contained in:
Zeno Rocha
2015-09-29 22:14:26 -07:00
parent 3394f59691
commit a5e29bd420
4 changed files with 104 additions and 41 deletions

View File

@@ -1,12 +1,21 @@
/**
* Inner class which performs selection and copy operations.
* Inner class which performs selection from either `text` or `target`
* properties and then executes copy or cut operations.
*/
class ClipboardAction {
/**
* Initializes selection from either `text` or `target` property.
* @param {Object} options
*/
constructor(options = {}) {
constructor(options) {
this.resolveOptions(options);
this.initSelection();
}
/**
* Defines base properties passed from constructor.
* @param {Object} options
*/
resolveOptions(options = {}) {
this.action = options.action;
this.emitter = options.emitter;
this.target = options.target;
@@ -14,7 +23,13 @@ class ClipboardAction {
this.trigger = options.trigger;
this.selectedText = '';
}
/**
* Decides which selection strategy is going to be applied based
* on the existence of `text` and `target` properties.
*/
initSelection() {
if (this.text && this.target) {
throw new Error('Multiple attributes declared, use either "target" or "text"');
}
@@ -157,8 +172,8 @@ class ClipboardAction {
}
/**
* Sets the `target` property using an element that will be have its content
* copied.
* Sets the `target` property using an element
* that will be have its content copied.
* @param {Element} target
*/
set target(target) {

View File

@@ -10,7 +10,6 @@ const prefix = 'data-clipboard-';
*/
class Clipboard extends Emitter {
/**
* Delegates a click event on the passed selector.
* @param {String} selector
* @param {Object} options
*/
@@ -18,13 +17,12 @@ class Clipboard extends Emitter {
super();
this.resolveOptions(options);
Delegate.bind(document.body, selector, 'click', (e) => this.initialize(e));
this.delegateClick(selector);
}
/**
* Defines if attributes would be resolved using an internal setter function
* or a custom function that was passed in the constructor.
* Defines if attributes would be resolved using internal setter functions
* or custom functions that were passed in the constructor.
* @param {Object} options
*/
resolveOptions(options = {}) {
@@ -33,6 +31,32 @@ class Clipboard extends Emitter {
this.text = (typeof options.text === 'function') ? options.text : this.setText;
}
/**
* Delegates a click event on the passed selector.
* @param {String} selector
*/
delegateClick(selector) {
Delegate.bind(document.body, selector, 'click', (e) => this.initialize(e));
}
/**
* Defines a new `ClipboardAction` on each click event.
* @param {Event} e
*/
initialize(e) {
if (this.clipboardAction) {
this.clipboardAction = null;
}
this.clipboardAction = new ClipboardAction({
action : this.action(e.delegateTarget),
target : this.target(e.delegateTarget),
text : this.text(e.delegateTarget),
trigger : e.delegateTarget,
emitter : this
});
}
/**
* Sets the `action` lookup function.
* @param {Element} trigger
@@ -69,24 +93,6 @@ class Clipboard extends Emitter {
return trigger.getAttribute(prefix + 'text');
}
/**
* Defines a new `ClipboardAction` on each click event.
* @param {Event} e
*/
initialize(e) {
if (this.clipboardAction) {
this.clipboardAction = null;
}
this.clipboardAction = new ClipboardAction({
action : this.action(e.delegateTarget),
target : this.target(e.delegateTarget),
text : this.text(e.delegateTarget),
trigger : e.delegateTarget,
emitter : this
});
}
}
export default Clipboard;