1/** 2 * This class will be used to define different events that we can trigger from the can.js 3 * code. This allows us to move logic out of canjs by listening for events instead of 4 * having to hook into the existing callbacks. 5 * 6 * This needs to be included before can.js 7 * 8 * The idea is also to keep the event emission here so that there is a central location 9 * for events that are emitted from canjs. 10 * 11 * The idea being it will make it faster to find and refactor code as move it out of can.js. 12 * Add a new method for the event type, this prevents typos and allow us to redefine behavior here 13 */ 14class CanJsEventBridge { 15 static EVENT_PREFIX = 'canjs'; 16 static EVENT_NAMES = { 17 /** 18 * Triggered after we load the options for an email list on 19 * targeting / query pages. and can.js does it's thing with the dom. 20 * Contains the raw data sent by the list_select ajax endpoint. 21 * If you need to do something with filters this is probably where you should 22 * attach your event. 23 */ 24 TARGETING_AJAX_LOADED: 'TARGETING_AJAX_LOADED', 25 VALIDATION_DONE: 'VALIDATION_DONE', 26 27 /** 28 * This also occurs when the data is loaded, but is more specfically narrowed 29 * to use cases where the developer wants to know that a user has switched "lists". 30 * This was split out becuase in the future, this functionality might not be as tightly 31 * coupled to the endpoint that returns the filter data. 32 * This event is useful to update certain things like filter visibility based upon the new list. 33 * The payload here is the new "__meta_data" object on the filters/list_select endpoint. 34 * This payload currently contains: 35 * { 36 * permalink: params[:list], 37 * is_personal_list: (params[:list] == "subscribers"), 38 * user_has_group_settings_permission: @can_user_manage_group || false, 39 * is_partner: @group&.campaigns_enabled? || false, 40 * cross_data_enabled: @cross_data_enabled || false, 41 * cross_data_opted_out: @cross_data_opted_out || false 42 * } 43 * 44 * Ideally 45 */ 46 LIST_META_DATA_CHANGED: 'LIST_META_DATA_CHANGED', 47 } 48 49 static log() { 50 const DEBUG = window.location.href.includes('localhost'); // Set to true when localhost 51 if (DEBUG) { 52 console.log(...arguments); 53 } 54 } 55 56 57 constructor() { 58 } 59 60 static EmitValidationDone(payload) { 61 this.dispatchEvent(this.EVENT_NAMES.VALIDATION_DONE, payload); 62 } 63 64 /** 65 * Emits the entire Ajax response for TargetingAjaxLoaded. * 66 * @param {*} payload Entire AJAX response from the list_select endpoint. 67 */ 68 static EmitTargetingAjaxLoaded(payload) { 69 this.dispatchEvent(this.EVENT_NAMES.TARGETING_AJAX_LOADED, payload); 70 } 71 72 // Specifically this comes from list_select endpoint, from the "__meta_data" object field. 73 // currently: `{"permalink":"dsfdsfds","is_partner":false,"cross_data_enabled":false,"cross_data_opted_out":false}` 74 static EmitListMetaDataChanged(payload) { 75 if (!payload?.permalink) { 76 console.error(`Invalid payload passed please ensure that you are passing the ['__meta_data']['data'] object from list_select as the payload`, payload) 77 throw new Error(`Invalid payload passed please ensure that you are passing the ['__meta_data']['data'] object from list_select as the payload`, payload) 78 } 79 this.dispatchEvent(this.EVENT_NAMES.LIST_META_DATA_CHANGED, payload); 80 } 81 82 /** Use one of the options from EVENT_NAMES */ 83 static dispatchEvent(name, data) { 84 if (!name) { 85 console.warn('CanJsEventBridge: dispatchEvent called without name', name, data); 86 return; 87 } 88 const event = new CustomEvent(`${this.EVENT_PREFIX}:${name}`, { 89 bubbles: true, 90 cancelable: true, // not really 91 detail: data 92 }); 93 this.log(`CanJsEventBridge Firing: ${event.name}`, event); 94 window.dispatchEvent(event); 95 } 96 97 static addEventListener(name, callback) { 98 window.addEventListener(`${this.EVENT_PREFIX}:${name}`, callback); 99 } 100 101 static removeEventListener(name, callback) { 102 window.removeEventListener(`${this.EVENT_PREFIX}:${name}`, callback); 103 } 104} 105 106// Export the class so it can be used in other modules 107if (typeof module !== 'undefined' && module.exports) { 108 module.exports = CanJsEventBridge; 109} 110 111window.CanJsEventBridge = CanJsEventBridge;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.