|
| 1 | +// Copyright 2012-2026 OpenBroadcaster, Inc. |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | + |
| 4 | +OBModules.SampleData = new Object(); |
| 5 | + |
| 6 | +OBModules.SampleData.init = function () { |
| 7 | + OB.Callbacks.add('ready', 0, OBModules.SampleData.initMenu); |
| 8 | +}; |
| 9 | + |
| 10 | +OBModules.SampleData.initMenu = function () { |
| 11 | + OB.UI.addSubMenuItem( |
| 12 | + 'admin', |
| 13 | + 'Import Sample Data', |
| 14 | + 'import_sample_data', |
| 15 | + OBModules.SampleData.importPage, |
| 16 | + 110, |
| 17 | + 'import_sample_data' |
| 18 | + ); |
| 19 | +}; |
| 20 | + |
| 21 | +OBModules.SampleData.importPage = async function () { |
| 22 | + // Cache key preserves the module directory's actual case (see core/controllers/UI.php |
| 23 | + // find_module_html_files), so this must be 'SampleData', not 'sampledata'. |
| 24 | + OB.UI.replaceMain('modules/SampleData/sampledata.html'); |
| 25 | + |
| 26 | + OBModules.SampleData.profiles = {}; |
| 27 | + $('#sampledata_module-profile').html('<option value="">Loading…</option>'); |
| 28 | + $('#sampledata_module-import').prop('disabled', true); |
| 29 | + $('#sampledata_module-log').hide().text(''); |
| 30 | + |
| 31 | + // v2 module endpoint: /api/v2/module/SampleData/profiles |
| 32 | + const profiles = await OB.API.request({ |
| 33 | + endpoint: 'module/SampleData/profiles', |
| 34 | + method: 'GET', |
| 35 | + }); |
| 36 | + |
| 37 | + var $select = $('#sampledata_module-profile'); |
| 38 | + $select.empty(); |
| 39 | + |
| 40 | + if (!profiles || profiles.length === 0) { |
| 41 | + $select.append('<option value="">No profiles found</option>'); |
| 42 | + $('#sampledata_module-info').text('No sample data profiles found.'); |
| 43 | + $('#sampledata_module-import').prop('disabled', true); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $.each(profiles, function (_, profile) { |
| 48 | + OBModules.SampleData.profiles[profile.directory] = profile; |
| 49 | + $select.append($('<option></option>').val(profile.directory).text(profile.name)); |
| 50 | + }); |
| 51 | + |
| 52 | + OBModules.SampleData.updateDescription(); |
| 53 | + $('#sampledata_module-import').prop('disabled', false); |
| 54 | + |
| 55 | + $('#sampledata_module-profile').off('change').on('change', OBModules.SampleData.updateDescription); |
| 56 | + $('#sampledata_module-import').off('click').on('click', function () { |
| 57 | + OBModules.SampleData.runImport(false); |
| 58 | + }); |
| 59 | +}; |
| 60 | + |
| 61 | +OBModules.SampleData.updateDescription = function () { |
| 62 | + var dir = $('#sampledata_module-profile').val(); |
| 63 | + var profile = OBModules.SampleData.profiles[dir]; |
| 64 | + $('#sampledata_module-description').text(profile ? (profile.description || '') : ''); |
| 65 | +}; |
| 66 | + |
| 67 | +OBModules.SampleData.runImport = function (confirmed) { |
| 68 | + var dir = $('#sampledata_module-profile').val(); |
| 69 | + if (!dir) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (!confirmed) { |
| 74 | + OB.UI.confirm( |
| 75 | + 'Import sample data profile "' + dir + '"?\n\nThis will create permission groups, users, playlists, a sample player and schedule, and apply settings. The action is idempotent — existing items are skipped — but it cannot be undone.', |
| 76 | + function () { OBModules.SampleData.runImport(true); }, |
| 77 | + 'Yes, Import', |
| 78 | + 'No, Cancel', |
| 79 | + 'delete' |
| 80 | + ); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + $('#sampledata_module-info').text('Importing… (first run downloads ~73MB of media; this can take a minute)'); |
| 85 | + $('#sampledata_module-import').prop('disabled', true); |
| 86 | + $('#sampledata_module-log').show().text(''); |
| 87 | + |
| 88 | + // v2 module endpoint: /api/v2/module/SampleData/run |
| 89 | + OB.API.request({ |
| 90 | + endpoint: 'module/SampleData/run', |
| 91 | + method: 'POST', |
| 92 | + data: { profile: dir }, |
| 93 | + }).then(function (result) { |
| 94 | + if (!result) { |
| 95 | + $('#sampledata_module-info').text('Import failed: no response from server.'); |
| 96 | + $('#sampledata_module-import').prop('disabled', false); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + $('#sampledata_module-log').text((result.log || []).join('\n')); |
| 101 | + |
| 102 | + if (result.success) { |
| 103 | + $('#sampledata_module-info').text('Sample data imported.'); |
| 104 | + } else { |
| 105 | + $('#sampledata_module-info').text('Import failed: ' + (result.error || 'unknown error')); |
| 106 | + } |
| 107 | + |
| 108 | + $('#sampledata_module-import').prop('disabled', false); |
| 109 | + }); |
| 110 | +}; |
0 commit comments