Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 60 additions & 10 deletions assets/js/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
$(".adapters .col-md-4").show();
setPrepickedModules();

var configInput = document.getElementById('configFileInput');
if (configInput) {
configInput.addEventListener('change', function(event) {
var file = event.target.files[0];
if (!file) return;
var reader = new FileReader();
reader.onload = function(e) {
try {
var cfg = JSON.parse(e.target.result);
applyConfig(cfg);
} catch (err) {
alert('Invalid configuration file');
}
};
reader.readAsText(file);
});
}

document.getElementById('download-button').addEventListener('click', function (event) {
event.preventDefault();
submit_download();
Expand Down Expand Up @@ -79,24 +97,21 @@
setTimeout(function () {
$('#download-button').html('<i class="glyphicon glyphicon-download-alt"></i> Download Prebid.js').removeClass('disabled');
}, 5000);
// Try to find out the filename from the content disposition `filename` value
var filename = "prebid" + form_data["version"] + ".js";
// this doens't work in our current jquery version.
var disposition = jqXHR.getResponseHeader("Content-Disposition");
if (disposition && disposition.indexOf("attachment") !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1])
filename = matches[1].replace(/['"]/g, "");
}
// The actual download
var blob = new Blob([data], { type: "text/javascript" });
var link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);

var jsBlob = new Blob([data], { type: "text/javascript" });
var configData = JSON.stringify({ version: form_data.version, modules: form_data.modules }, null, 2);

triggerDownload(jsBlob, filename);
triggerDownload(new Blob([configData], { type: "application/json" }), "prebid-config.json");

if (form_data["removedModules"].length > 0) {
alert(
"The following modules were removed from your download because they aren't present in Prebid.js version " +
Expand All @@ -112,6 +127,15 @@
});
}

function triggerDownload(blob, filename) {
var link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

const renameModules = (function() {
const RENAMES = [
[
Expand Down Expand Up @@ -206,6 +230,32 @@
});
}

function applyConfig(cfg) {
if (!cfg) return;
if (cfg.version) {
var versionOption = document.querySelector('#version_selector option[value="' + cfg.version + '"]');
if (versionOption) {
versionOption.selected = true;
searchParams.set("version", cfg.version);
}
}

if (Array.isArray(cfg.modules)) {
var moduleCheckboxes = document.querySelectorAll('.module-check-box');
moduleCheckboxes.forEach(function(cb){ cb.checked = false; });
cfg.modules.forEach(function(module){
var cb = document.getElementById(module);
if (cb) cb.checked = true;
});
if (cfg.modules.length) {
searchParams.set("modules", cfg.modules.join(','));
} else {
searchParams.delete("modules");
}
window.history.replaceState(null, "", window.location.pathname + "?" + searchParams.toString());
}
}

function setPrepickedVersion() {
var version = searchParams.get("version");
if (version) {
Expand Down
3 changes: 3 additions & 0 deletions download.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Prebid.js is open source software that is offered for free as a convenience. Whi
<h4>Select Prebid Version</h4>
<select id="version_selector" class="selectpicker">
</select>
<br/>
<label for="configFileInput"><strong>Upload Configuration</strong></label>
<input type="file" id="configFileInput" accept="application/json" />
<br>
<h4>Select Bidder Adapters</h4>
<div class="row adapters">
Expand Down