@@ -108,7 +108,7 @@ export function createOc(oc) {
108108
109109 const reanimateScripts = ( component ) => {
110110 for ( const script of Array . from ( component . querySelectorAll ( "script" ) ) ) {
111- const newScript = document . createElement ( "script" ) ;
111+ const newScript = $ document. createElement ( "script" ) ;
112112 newScript . textContent = script . textContent ;
113113 for ( const attribute of Array . from ( script . attributes ) ) {
114114 newScript . setAttribute ( attribute . name , attribute . value ) ;
@@ -126,9 +126,9 @@ export function createOc(oc) {
126126 } ;
127127
128128 oc . addStylesToHead = ( styles ) => {
129- const style = document . createElement ( "style" ) ;
129+ const style = $ document. createElement ( "style" ) ;
130130 style . textContent = styles ;
131- document . head . appendChild ( style ) ;
131+ $ document. head . appendChild ( style ) ;
132132 } ;
133133
134134 const loadAfterReady = ( ) => {
@@ -351,14 +351,43 @@ export function createOc(oc) {
351351 initialising = false ;
352352
353353 oc . events = ( ( ) => {
354- const obj = $ ( { } ) ;
354+ let listeners = { } ;
355355
356356 return {
357- fire : obj . trigger . bind ( obj ) ,
358- on : obj . on . bind ( obj ) ,
359- off : obj . off . bind ( obj ) ,
360- reset : ( ) => {
361- obj . off ( ) ;
357+ fire ( key , data ) {
358+ if ( listeners [ key ] ) {
359+ for ( const cb of listeners [ key ] ) {
360+ cb ( data , data ) ;
361+ }
362+ }
363+ } ,
364+ on ( key , cb ) {
365+ if ( ! cb ) {
366+ throw new Error ( "Callback is required" ) ;
367+ }
368+ if ( ! listeners [ key ] ) {
369+ listeners [ key ] = [ ] ;
370+ }
371+ listeners [ key ] . push ( cb ) ;
372+ } ,
373+ off ( events , handler ) {
374+ if ( typeof events === "string" ) {
375+ events = [ events ] ;
376+ }
377+ for ( const event of events ) {
378+ if ( listeners [ event ] ) {
379+ if ( handler ) {
380+ listeners [ event ] = listeners [ event ] . filter (
381+ ( cb ) => cb !== handler ,
382+ ) ;
383+ } else {
384+ delete listeners [ event ] ;
385+ }
386+ }
387+ }
388+ } ,
389+ reset ( ) {
390+ listeners = { } ;
362391 } ,
363392 } ;
364393 } ) ( ) ;
@@ -569,7 +598,7 @@ export function createOc(oc) {
569598
570599 oc . renderUnloadedComponents = ( ) => {
571600 oc . ready ( ( ) => {
572- const unloadedComponents = document . querySelectorAll (
601+ const unloadedComponents = $ document. querySelectorAll (
573602 `${ OC_TAG } :not([data-rendered="true"]):not([data-failed="true"])` ,
574603 ) ;
575604
0 commit comments