UNPKG

@stratusjs/idx

Version:

AngularJS idx/property Service and Components bundle to be used as an add on to StratusJS

126 lines (113 loc) 4.96 kB
/** * @file ListTrac Service @stratusjs/idx/listTrac * @example import '@stratusjs/idx/listTrac' */ // Runtime import {Stratus} from '@stratusjs/runtime/stratus' import {auto, IQService, IWindowService} from 'angular' import {DOMComplete} from '@stratusjs/core/dom' // Stratus Preload import '@stratusjs/angularjs/services/model' // Needed as $provider import '@stratusjs/angularjs/services/collection' // Needed as $provider Stratus.Services.ListTrac = [ '$provide', ($provide: auto.IProvideService) => { $provide.factory( 'ListTrac', ( $q: IQService, $window: IWindowService, ) => { const eventQueue: Array<() => void> = [] let listTracLoaded = false let listTracPending = false let listTracAccountId = '' let LT: any let eventTypes: { [key: string]: number } = {} async function loadListTrac() { if (!listTracAccountId) { console.warn('ListTrac Account Id not provided, will not load') return } // Only attempt to load ListTrac if it's not already loaded or currently attempting to load if (!listTracPending && !listTracLoaded) { try { listTracPending = true await DOMComplete() // console.log('importing ListTrac') // Inject the javascript file into the HEAD to load const jsFile: HTMLScriptElement = $window.document.createElement('script') jsFile.type = 'application/javascript' jsFile.async = true jsFile.src = `https://code.listtrac.com/monitor.ashx?nonjq=1&acct=${listTracAccountId}` jsFile.onload = () => loadedListTrac() Stratus.Select('head').append(jsFile) // TODO make a timeout to check if it still doesn't load after some to time manually check..? } catch (e) { console.error(e) } } } async function loadedListTrac() { if ( Object.prototype.hasOwnProperty.call($window, '_LT') && typeof ($window._LT) !== 'undefined' && $window._LT !== null ) { eventTypes = $window._eventType LT = $window._LT listTracLoaded = true listTracPending = false processEventQueue() } else { listTracLoaded = false console.warn('ListTrac failed to load') } listTracPending = false } function setAccountId(accountId: string): void { listTracAccountId = accountId // console.log('Set account id to', listTracAccountId) } async function track(eventName: string, mlsId: string, postalCode: number, agentId: string): Promise<void> { // console.log('setting up Event', eventName) await loadListTrac() const promiseFunction = () => { LT._trackEvent( eventTypes[eventName], mlsId, postalCode, agentId ) return } addEventToQueue(promiseFunction) } function addEventToQueue(event: () => void) { // console.log('addEventToQueue added') eventQueue.push(event) if (listTracLoaded) { processEventQueue() } } function processEventQueue() { // console.log('processEventQueue running') if (!listTracLoaded) { console.warn('Cannot processEventQueue as ListTrac not loaded') return } while (eventQueue.length > 0) { // $q(eventQueue.shift()).then(() => console.log('Promise done')) $q(eventQueue.shift()) } } return { setAccountId, track } } ) } ]