Skip to content

Commit 7663a21

Browse files
author
Graham Butler
committed
Add ability to configure custom events
1 parent 0f75c38 commit 7663a21

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/element.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -560,21 +560,6 @@ const AuthorBaseElement = superClass => class extends superClass {
560560
}
561561
},
562562

563-
/**
564-
* @method createEvent
565-
* Returns a new CustomEvent object.
566-
* @param {[type]} name
567-
* Name of the event
568-
* @param {object} detail
569-
* Properties to add to event.detail
570-
* @return {CustomEvent}
571-
*/
572-
createEvent: {
573-
value: (name, detail) => {
574-
return new CustomEvent(name, { detail })
575-
}
576-
},
577-
578563
/**
579564
* @method generateGuid
580565
* @param {string} [prefix=null]
@@ -1058,16 +1043,37 @@ const AuthorBaseElement = superClass => class extends superClass {
10581043
* @param {HTMLElement} [target=null]
10591044
* DOM node to fire the event at.
10601045
*/
1061-
emit (name, detail, target = null) {
1062-
let event = this.UTIL.createEvent(name, detail)
1063-
1064-
if (target) {
1065-
return target.dispatchEvent(event)
1066-
}
1067-
1068-
this.dispatchEvent(event)
1069-
return event
1070-
}
1046+
emit ({
1047+
name = null,
1048+
detail = null,
1049+
cfg = {
1050+
bubbles: false,
1051+
cancelable: false,
1052+
composed: false
1053+
},
1054+
target = null
1055+
}) {
1056+
if (typeof arguments[0] === 'string') {
1057+
name = arguments[0]
1058+
detail = arguments[1] || null
1059+
target = arguments[2] || null
1060+
}
1061+
1062+
if (!name) {
1063+
return this.UTIL.throwError({
1064+
message: 'Event name is required'
1065+
})
1066+
}
1067+
1068+
let event = new CustomEvent(name, Object.assign({}, cfg, { detail }))
1069+
1070+
if (target) {
1071+
return target.dispatchEvent(event)
1072+
}
1073+
1074+
this.dispatchEvent(event)
1075+
return event
1076+
}
10711077

10721078
/**
10731079
* @method off

0 commit comments

Comments
 (0)