Skip to content

Commit 04adbcd

Browse files
patmmccannmuuki88
andauthored
Codex: Add config upload and zip download (prebid#6065)
* Add config upload and zip download * Remove JSZip usage from download page --------- Co-authored-by: Muki Seiler <muuki88@users.noreply.github.com>
1 parent 19ee99b commit 04adbcd

2 files changed

Lines changed: 63 additions & 10 deletions

File tree

assets/js/download.js

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99
$(".adapters .col-md-4").show();
1010
setPrepickedModules();
1111

12+
var configInput = document.getElementById('configFileInput');
13+
if (configInput) {
14+
configInput.addEventListener('change', function(event) {
15+
var file = event.target.files[0];
16+
if (!file) return;
17+
var reader = new FileReader();
18+
reader.onload = function(e) {
19+
try {
20+
var cfg = JSON.parse(e.target.result);
21+
applyConfig(cfg);
22+
} catch (err) {
23+
alert('Invalid configuration file');
24+
}
25+
};
26+
reader.readAsText(file);
27+
});
28+
}
29+
1230
document.getElementById('download-button').addEventListener('click', function (event) {
1331
event.preventDefault();
1432
submit_download();
@@ -79,24 +97,21 @@
7997
setTimeout(function () {
8098
$('#download-button').html('<i class="glyphicon glyphicon-download-alt"></i> Download Prebid.js').removeClass('disabled');
8199
}, 5000);
82-
// Try to find out the filename from the content disposition `filename` value
83100
var filename = "prebid" + form_data["version"] + ".js";
84-
// this doens't work in our current jquery version.
85101
var disposition = jqXHR.getResponseHeader("Content-Disposition");
86102
if (disposition && disposition.indexOf("attachment") !== -1) {
87103
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
88104
var matches = filenameRegex.exec(disposition);
89105
if (matches != null && matches[1])
90106
filename = matches[1].replace(/['"]/g, "");
91107
}
92-
// The actual download
93-
var blob = new Blob([data], { type: "text/javascript" });
94-
var link = document.createElement("a");
95-
link.href = window.URL.createObjectURL(blob);
96-
link.download = filename;
97-
document.body.appendChild(link);
98-
link.click();
99-
document.body.removeChild(link);
108+
109+
var jsBlob = new Blob([data], { type: "text/javascript" });
110+
var configData = JSON.stringify({ version: form_data.version, modules: form_data.modules }, null, 2);
111+
112+
triggerDownload(jsBlob, filename);
113+
triggerDownload(new Blob([configData], { type: "application/json" }), "prebid-config.json");
114+
100115
if (form_data["removedModules"].length > 0) {
101116
alert(
102117
"The following modules were removed from your download because they aren't present in Prebid.js version " +
@@ -112,6 +127,15 @@
112127
});
113128
}
114129

130+
function triggerDownload(blob, filename) {
131+
var link = document.createElement("a");
132+
link.href = window.URL.createObjectURL(blob);
133+
link.download = filename;
134+
document.body.appendChild(link);
135+
link.click();
136+
document.body.removeChild(link);
137+
}
138+
115139
const renameModules = (function() {
116140
const RENAMES = [
117141
[
@@ -206,6 +230,32 @@
206230
});
207231
}
208232

233+
function applyConfig(cfg) {
234+
if (!cfg) return;
235+
if (cfg.version) {
236+
var versionOption = document.querySelector('#version_selector option[value="' + cfg.version + '"]');
237+
if (versionOption) {
238+
versionOption.selected = true;
239+
searchParams.set("version", cfg.version);
240+
}
241+
}
242+
243+
if (Array.isArray(cfg.modules)) {
244+
var moduleCheckboxes = document.querySelectorAll('.module-check-box');
245+
moduleCheckboxes.forEach(function(cb){ cb.checked = false; });
246+
cfg.modules.forEach(function(module){
247+
var cb = document.getElementById(module);
248+
if (cb) cb.checked = true;
249+
});
250+
if (cfg.modules.length) {
251+
searchParams.set("modules", cfg.modules.join(','));
252+
} else {
253+
searchParams.delete("modules");
254+
}
255+
window.history.replaceState(null, "", window.location.pathname + "?" + searchParams.toString());
256+
}
257+
}
258+
209259
function setPrepickedVersion() {
210260
var version = searchParams.get("version");
211261
if (version) {

download.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Prebid.js is open source software that is offered for free as a convenience. Whi
6464
<h4>Select Prebid Version</h4>
6565
<select id="version_selector" class="selectpicker">
6666
</select>
67+
<br/>
68+
<label for="configFileInput"><strong>Upload Configuration</strong></label>
69+
<input type="file" id="configFileInput" accept="application/json" />
6770
<br>
6871
<h4>Select Bidder Adapters</h4>
6972
<div class="row adapters">

0 commit comments

Comments
 (0)