|
| 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 = function () { |
| 22 | + OB.UI.replaceMain('modules/sampledata/sampledata.html'); |
| 23 | + |
| 24 | + OBModules.SampleData.profiles = {}; |
| 25 | + $('#sampledata_module-profile').html('<option value="">Loading…</option>'); |
| 26 | + $('#sampledata_module-import').prop('disabled', true); |
| 27 | + $('#sampledata_module-log').hide().text(''); |
| 28 | + |
| 29 | + OB.API.post('sampledata', 'listProfiles', {}, function (response) { |
| 30 | + if (!response.status) { |
| 31 | + $('#sampledata_module-info').text('Error loading sample data profiles.'); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + var profiles = response.data || []; |
| 36 | + var $select = $('#sampledata_module-profile'); |
| 37 | + $select.empty(); |
| 38 | + |
| 39 | + if (profiles.length === 0) { |
| 40 | + $select.append('<option value="">No profiles found</option>'); |
| 41 | + $('#sampledata_module-import').prop('disabled', true); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + $.each(profiles, function (_, profile) { |
| 46 | + OBModules.SampleData.profiles[profile.directory] = profile; |
| 47 | + $select.append( |
| 48 | + $('<option></option>').val(profile.directory).text(profile.name) |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + OBModules.SampleData.updateDescription(); |
| 53 | + $('#sampledata_module-import').prop('disabled', false); |
| 54 | + }); |
| 55 | + |
| 56 | + $('#sampledata_module-profile').off('change').on('change', OBModules.SampleData.updateDescription); |
| 57 | + $('#sampledata_module-import').off('click').on('click', function () { |
| 58 | + OBModules.SampleData.runImport(false); |
| 59 | + }); |
| 60 | +}; |
| 61 | + |
| 62 | +OBModules.SampleData.updateDescription = function () { |
| 63 | + var dir = $('#sampledata_module-profile').val(); |
| 64 | + var profile = OBModules.SampleData.profiles[dir]; |
| 65 | + $('#sampledata_module-description').text(profile ? (profile.description || '') : ''); |
| 66 | +}; |
| 67 | + |
| 68 | +OBModules.SampleData.runImport = function (confirmed) { |
| 69 | + var dir = $('#sampledata_module-profile').val(); |
| 70 | + if (!dir) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if (!confirmed) { |
| 75 | + OB.UI.confirm( |
| 76 | + '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.', |
| 77 | + function () { OBModules.SampleData.runImport(true); }, |
| 78 | + 'Yes, Import', |
| 79 | + 'No, Cancel', |
| 80 | + 'delete' |
| 81 | + ); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $('#sampledata_module-info').text('Importing…'); |
| 86 | + $('#sampledata_module-import').prop('disabled', true); |
| 87 | + $('#sampledata_module-log').show().text(''); |
| 88 | + |
| 89 | + OB.API.post('sampledata', 'runProfile', { profile: dir }, function (response) { |
| 90 | + var log = (response.data && response.data.log) ? response.data.log.join('\n') : ''; |
| 91 | + $('#sampledata_module-log').text(log); |
| 92 | + |
| 93 | + if (response.status) { |
| 94 | + $('#sampledata_module-info').text('Sample data imported.'); |
| 95 | + } else { |
| 96 | + $('#sampledata_module-info').text('Import failed: ' + (response.msg || 'unknown error')); |
| 97 | + } |
| 98 | + |
| 99 | + $('#sampledata_module-import').prop('disabled', false); |
| 100 | + }); |
| 101 | +}; |
0 commit comments