/* global RSNRatings, wp, jQuery */ (function ($) { function el(tag, attrs = {}, children = []) { const node = document.createElement(tag); Object.keys(attrs).forEach(k => { if (k === 'class') node.className = attrs[k]; else if (k === 'html') node.innerHTML = attrs[k]; else node.setAttribute(k, attrs[k]); }); (Array.isArray(children) ? children : [children]).forEach(c => { if (c === null || c === undefined) return; if (typeof c === 'string') node.appendChild(document.createTextNode(c)); else node.appendChild(c); }); return node; } function mount() { const root = document.getElementById('rsn-ratings-import-root'); if (!root || root.dataset.mounted) return; root.dataset.mounted = '1'; const shows = RSNRatings.shows || []; const networks = RSNRatings.networks || []; // State let state = { show_id: shows[0]?.id || '', network_id: networks[0]?.id || '', episode_date: '', viewers: '', demo: '', source_name: '', source_url: '', results_post_id: 0, results_post_title: '', results_post_url: '', youtube_url: '', // ✅ NEW // RAW fields global_views: '', global_rank: '', us_rank: '', hours_viewed: '', search_q: '', search_results: [], searching: false, mode: (shows[0]?.mode || 'linear') }; function getShowMode(showId) { const s = shows.find(x => x.id === showId); return s ? s.mode : 'linear'; } function setState(patch) { state = Object.assign({}, state, patch); render(); } function setEditorContent(title, content) { const { dispatch } = wp.data; dispatch('core/editor').editPost({ title, content }); } function ajax(action, data) { return $.ajax({ url: RSNRatings.ajaxUrl, method: 'POST', dataType: 'json', data: Object.assign({ action, nonce: RSNRatings.nonce }, data || {}) }); } function ajaxGet(action, params) { return $.ajax({ url: RSNRatings.ajaxUrl, method: 'GET', dataType: 'json', data: Object.assign({ action, nonce: RSNRatings.nonce }, params || {}) }); } function searchPosts(q) { setState({ searching: true }); ajaxGet('rsn_ratings_search_posts', { q }) .done(resp => { setState({ search_results: resp?.data?.items || [], searching: false }); }) .fail(() => setState({ search_results: [], searching: false })); } function generate() { const payload = { show_id: state.show_id, network_id: state.network_id, episode_date: state.episode_date, viewers: state.viewers, demo: state.demo, source_name: state.source_name, source_url: state.source_url, results_post_id: state.results_post_id, youtube_url: state.youtube_url, // ✅ NEW // RAW: global_views: state.global_views, global_rank: state.global_rank, us_rank: state.us_rank, hours_viewed: state.hours_viewed }; ajax('rsn_ratings_generate', { payload: JSON.stringify(payload) }) .done(resp => { if (!resp || !resp.success) return alert(resp?.data?.message || 'Generate failed'); setEditorContent(resp.data.title, resp.data.content); }) .fail(() => alert('Generate failed')); } function render() { root.innerHTML = ''; const mode = getShowMode(state.show_id); state.mode = mode; const showSelect = el('select', { class: 'widefat' }, shows.map(s => { const opt = el('option', { value: s.id }, s.name + (s.mode === 'netflix' ? ' (Netflix)' : '')); if (s.id === state.show_id) opt.selected = true; return opt; }) ); showSelect.addEventListener('change', e => { const show_id = e.target.value; const mode2 = getShowMode(show_id); // reset fields that don't apply setState({ show_id, mode: mode2, viewers: '', demo: '', source_name: '', source_url: '', global_views: '', global_rank: '', us_rank: '', hours_viewed: '', youtube_url: '' // ✅ reset too }); }); const networkSelect = el('select', { class: 'widefat' }, networks.map(n => { const opt = el('option', { value: n.id }, n.name); if (n.id === state.network_id) opt.selected = true; return opt; }) ); networkSelect.addEventListener('change', e => setState({ network_id: e.target.value })); const dateInput = el('input', { type: 'date', class: 'widefat', value: state.episode_date || '' }); dateInput.addEventListener('input', e => setState({ episode_date: e.target.value })); // Results search const resultsSearch = el('input', { type: 'text', class: 'widefat', placeholder: 'Search results post…', value: state.search_q || '' }); resultsSearch.addEventListener('input', e => { const q = e.target.value; setState({ search_q: q }); if (q.length >= 2) searchPosts(q); else setState({ search_results: [] }); }); const resultsList = el('div', { style: 'margin-top:6px; max-height:140px; overflow:auto; border:1px solid #ddd; padding:6px; background:#fff;' }, []); if (state.searching) { resultsList.appendChild(el('div', {}, 'Searching…')); } else if ((state.search_results || []).length) { state.search_results.forEach(item => { const row = el('div', { style: 'padding:6px; border-bottom:1px solid #eee; cursor:pointer;' }, [ el('div', { style: 'font-weight:600;' }, item.title), el('div', { style: 'font-size:12px; color:#666;' }, item.date) ]); row.addEventListener('click', () => { setState({ results_post_id: item.id, results_post_title: item.title, results_post_url: item.url, search_results: [], search_q: item.title }); }); resultsList.appendChild(row); }); } else { resultsList.appendChild(el('div', { style: 'color:#777; font-size:12px;' }, 'Type to search and select the matching results post.')); } // ✅ NEW: YouTube (optional) const youtubeInput = el('input', { type: 'url', class: 'widefat', placeholder: 'YouTube URL (optional)', value: state.youtube_url || '' }); youtubeInput.addEventListener('input', e => setState({ youtube_url: e.target.value })); // Linear fields const viewersInput = el('input', { type: 'text', class: 'widefat', placeholder: 'Viewers (e.g. 604000)', value: state.viewers || '' }); viewersInput.addEventListener('input', e => setState({ viewers: e.target.value })); const demoInput = el('input', { type: 'text', class: 'widefat', placeholder: 'P18–49 Demo (e.g. 0.12)', value: state.demo || '' }); demoInput.addEventListener('input', e => setState({ demo: e.target.value })); const sourceName = el('input', { type: 'text', class: 'widefat', placeholder: 'Source Name (e.g. Programming Insider)', value: state.source_name || '' }); sourceName.addEventListener('input', e => setState({ source_name: e.target.value })); const sourceUrl = el('input', { type: 'url', class: 'widefat', placeholder: 'Source URL', value: state.source_url || '' }); sourceUrl.addEventListener('input', e => setState({ source_url: e.target.value })); // RAW fields const rawGlobalViews = el('input', { type: 'text', class: 'widefat', placeholder: 'Global Views (e.g. 2.5 million)', value: state.global_views || '' }); rawGlobalViews.addEventListener('input', e => setState({ global_views: e.target.value })); const rawGlobalRank = el('input', { type: 'text', class: 'widefat', placeholder: 'Global Rank (e.g. 9 or Not Ranked)', value: state.global_rank || '' }); rawGlobalRank.addEventListener('input', e => setState({ global_rank: e.target.value })); const rawUSRank = el('input', { type: 'text', class: 'widefat', placeholder: 'U.S. Rank (e.g. 6 or Not Ranked)', value: state.us_rank || '' }); rawUSRank.addEventListener('input', e => setState({ us_rank: e.target.value })); const rawHours = el('input', { type: 'text', class: 'widefat', placeholder: 'Hours Viewed (optional)', value: state.hours_viewed || '' }); rawHours.addEventListener('input', e => setState({ hours_viewed: e.target.value })); const genBtn = el('button', { type: 'button', class: 'button button-primary widefat', style: 'margin-top:10px;' }, 'Generate Ratings Content'); genBtn.addEventListener('click', generate); // Build UI root.appendChild(el('div', {}, [ el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Show'), showSelect, // Network only meaningful for linear, but you wanted dropdown in general — we still show it; RAW can ignore. el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Network'), networkSelect, el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Episode Date'), dateInput, el('label', { style: 'display:block; margin:10px 0 4px; font-weight:600;' }, 'Results Post (Search + Select)'), resultsSearch, resultsList, // ✅ YouTube field goes under body info, above graph in output el('label', { style: 'display:block; margin:10px 0 4px; font-weight:600;' }, 'YouTube Video (Optional)'), youtubeInput, ])); if (mode === 'netflix') { // RAW UI root.appendChild(el('div', {}, [ el('hr'), el('div', { style: 'font-size:12px; color:#666; margin-bottom:6px;' }, [ 'RAW uses Netflix/Tudum. Link is fixed: ', el('a', { href: RSNRatings.tudumUrl, target: '_blank', rel: 'noopener' }, 'Tudum Top 10 TV') ]), el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Global Views'), rawGlobalViews, el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Global Rank'), rawGlobalRank, el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'U.S. Rank'), rawUSRank, el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Hours Viewed (Optional)'), rawHours, genBtn ])); } else { // Linear UI root.appendChild(el('div', {}, [ el('hr'), el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Viewers'), viewersInput, el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'P18–49 Demo'), demoInput, el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Source Name'), sourceName, el('label', { style: 'display:block; margin:8px 0 4px; font-weight:600;' }, 'Source URL'), sourceUrl, genBtn ])); } } render(); } // Gutenberg loads metaboxes async sometimes, poll briefly const interval = setInterval(() => { mount(); if (document.getElementById('rsn-ratings-import-root')?.dataset?.mounted) clearInterval(interval); }, 500); })(jQuery); ko{nI6㊻~+DټDI%qwH(Zșp?~ϟv*g1LL?y.. g1YLsJTF\$4|4pyPEP#BR91E@6\s,eF8W,{8T}$HQ!-{H{dÕIk"I4{ӻq5K*gTy!,G d"B*<{F f6#j y:,fj_y(vk'`(k{œVtaqqfl?7`4`ԩ>K0:i|k+HrHjuy?[$u+x81( ST!^kJ %k*z]*IyJ_W2^YmL~I̷7iH, M'V7IZX>Θ12R-WyBMO#JPv,Kv8њ9BccGF*H]T.zIMkO/{-֯+043IA KqU0Ly /Օ#W9rɠTt-GZoAꛏCWi:KԚivܘ6O#&$d}F'E- u^07O(RʏIgd1Ut4 U{'vi-Y3u YAһ(hR&9+\zSwus3yi#XJ &z؟k̮*5<6zC!rQ u<)`F Gj[^ta/sy6Ě@|x0[p> Ie6&΅6tcmaxFyW7xC_h'-e,Zy쬻idwr Z(hZQ6ozU(9qJn+%g/a^:a2`@-W(Ci)ɰd;y+v:I4z j#' ?UAqpƘBC 0ne hu锦x rq8wHc:`5TU2鍮Er®=(O6o'r"lݻuj?RGRs.!$ݣKSɎE;2=<:$ޯ,Tե(C.u&m!A5%FRzR*~P?,rZ}m} =0/WV~_.3qbo2N=b."!QAXQزoo1 5qx(88/Wc݁[;"$GHU5\?:̫(ԉ6'KE[bVqg̺UHo8!#moux!GtgNl-oh=!'JK3KItRJ+x߻ۥjY쑥-,g.C[NнRg˔fQ쫣SBrU`}r!]8ug߽:D#a.Nz&3ιtyp]*o~]N7;I ;\:RD rؕ IIW,h 0 esWS5C%Fۗek&JoKO>e/&S}drK֘̋֨r_I m7íѥ՜Ȟ'XEcY(O嵘r\HUp;#zd}}u@wsBlެyԚyi'5P4r+Ik?w\r~X\s'Yn.ggA:E