|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { ActivatedRoute } from '@angular/router'; |
| 3 | +import { ConferenceData } from '../../providers/conference-data'; |
| 4 | +import { LiveUpdateService } from '../../providers/live-update.service'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'page-sponsor-detail', |
| 8 | + templateUrl: 'sponsor-detail.html', |
| 9 | + styleUrls: ['./sponsor-detail.scss'], |
| 10 | +}) |
| 11 | +export class SponsorDetailPage { |
| 12 | + sponsor: any; |
| 13 | + jobListings: any[] = []; |
| 14 | + backHref = ''; |
| 15 | + |
| 16 | + constructor( |
| 17 | + private dataProvider: ConferenceData, |
| 18 | + private route: ActivatedRoute, |
| 19 | + public liveUpdateService: LiveUpdateService, |
| 20 | + ) {} |
| 21 | + |
| 22 | + ionViewDidEnter() { |
| 23 | + this.backHref = this.route.snapshot.queryParamMap.get('prevUrl') || '/app/tabs/sponsors'; |
| 24 | + } |
| 25 | + |
| 26 | + ionViewWillEnter() { |
| 27 | + const sponsorId = this.route.snapshot.paramMap.get('sponsorId'); |
| 28 | + |
| 29 | + this.dataProvider.load().subscribe((data: any) => { |
| 30 | + if (data && data.conference && data.conference.sponsors) { |
| 31 | + for (const [level, sponsors] of Object.entries(data.conference.sponsors)) { |
| 32 | + for (const [index, sponsor] of Object.entries(sponsors as any[])) { |
| 33 | + if (sponsor && String(sponsor.name).toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '') === sponsorId) { |
| 34 | + this.sponsor = sponsor; |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + if (this.sponsor) break; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Find job listings associated with this sponsor |
| 43 | + const listings = data['job-listings'] || []; |
| 44 | + if (this.sponsor) { |
| 45 | + this.jobListings = listings.filter( |
| 46 | + (listing: any) => listing.sponsor_name === this.sponsor.name |
| 47 | + ).map((listing: any) => ({ |
| 48 | + ...listing, |
| 49 | + roles: this.parseRoles(listing.description_html), |
| 50 | + })); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + parseRoles(html: string): {title: string, url: string}[] { |
| 56 | + if (!html) return []; |
| 57 | + const text = html.replace(/<[^>]+>/g, ' ').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/\s+/g, ' ').trim(); |
| 58 | + const urlRegex = /https?:\/\/[^\s<>"]+/g; |
| 59 | + const urls = html.match(urlRegex) || []; |
| 60 | + if (urls.length === 0) { |
| 61 | + return text.length > 0 ? [{title: text, url: ''}] : []; |
| 62 | + } |
| 63 | + const roles: {title: string, url: string}[] = []; |
| 64 | + let remaining = text; |
| 65 | + for (const url of urls) { |
| 66 | + const idx = remaining.indexOf(url); |
| 67 | + if (idx >= 0) { |
| 68 | + const before = remaining.substring(0, idx).replace(/[-|–,]\s*$/, '').trim(); |
| 69 | + if (before.length > 0) { |
| 70 | + const lines = before.split(/\n/).filter(l => l.trim()); |
| 71 | + const title = lines[lines.length - 1].replace(/^[-|–]\s*/, '').trim(); |
| 72 | + if (title.length > 0 && title.length < 200) { |
| 73 | + roles.push({title, url}); |
| 74 | + } else { |
| 75 | + roles.push({title: this.shortenUrl(url), url}); |
| 76 | + } |
| 77 | + } else { |
| 78 | + roles.push({title: this.shortenUrl(url), url}); |
| 79 | + } |
| 80 | + remaining = remaining.substring(idx + url.length).replace(/^\s*[-|–,]\s*/, '').trim(); |
| 81 | + } |
| 82 | + } |
| 83 | + if (roles.length === 0 && text.length > 0) { |
| 84 | + return [{title: text, url: ''}]; |
| 85 | + } |
| 86 | + return roles; |
| 87 | + } |
| 88 | + |
| 89 | + shortenUrl(url: string): string { |
| 90 | + try { |
| 91 | + const u = new URL(url); |
| 92 | + const path = u.pathname.replace(/\/$/, ''); |
| 93 | + const parts = path.split('/').filter(p => p); |
| 94 | + if (parts.length > 0) { |
| 95 | + return parts[parts.length - 1].replace(/-/g, ' ').replace(/^\w/, c => c.toUpperCase()); |
| 96 | + } |
| 97 | + return u.hostname; |
| 98 | + } catch { |
| 99 | + return url; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + openUrl(url: string) { |
| 104 | + if (url) { |
| 105 | + window.open(url, '_system', 'location=yes'); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + getSponsorSlug(name: string): string { |
| 110 | + return name.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, ''); |
| 111 | + } |
| 112 | +} |
0 commit comments