Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
    <meta charset="UTF-8">
    <title>Map DownLoader - V1.6.2</title>
    <script src="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/Kendo/jquery.1.9.1.min.js"></script>
    <script src="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/Kendo/kendo.all.min.js"></script>
    <script src="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/XHR/xhr2lib.js"></script>
          <script src="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/PouchDB/FastMD5.js"></script>
    <script src="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/PouchDB/pouchdb-src.js"></script>
    <link href="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/Kendo/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/Kendo/styles/kendo.dataviz.metroblack.min.css" rel="stylesheet" />
    <link href="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/Kendo/styles/kendo.dataviz.min.css" rel="stylesheet" />
    <link href="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/Kendo/styles/kendo.metroblack.min.css" rel="stylesheet" />
    <link href="https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/Styles/ButtonStyles.css" rel="stylesheet" />
</head>

<body>
    <script type="text/x-kendo-template" id="tripleTemplate">
    <tr>
        <td class="statName"><span data-bind="text: Name" /></td>
        <td><span data-bind="text: Value" /></td>
    </tr>
    </script>
    <h2>Info & Stats</h2>
    <div id="StatsBlock" class="Block InfoTable ">
        <table>
            <tbody data-bind="source: info" data-template="tripleTemplate">
            </tbody>
        </table>
    </div>
    <div id="CommandBlock" class="Block">
        <div id="commands">
            <button onclick="download();" class="k-button">Download</button>
            <button onclick="DBDelete();" class="k-button">Delete DB</button>
        </div>
    </div>
    <div id="RunBlock" class="Block">

        <div class="Part Line">
            <progress
                id="ProgressBar"
                class="Stretch"
                data-bind="attr: { max: TotalFiles, value: FileCount }"></progress>&nbsp;
            <span data-bind="text: PercentDone"></span>%
        </div>
    </div>
    <div id="ResultsBlock" class="Block">
        <h2>Sample Image Fetched From PouchDB</h2>
        <div id="imgBox">
            <img id="imgTile" alt="MAP not loaded YET">
        </div>
    </div>
</body>
</html>

              
            
!

CSS

              
                html
{
    font: 14px "Verdana", "Helvetica", sans-serif;
    width: 100%;
    height: 100%;
    padding: 0px;
    background: #50a3c4; /* Old browsers */
    background: -moz-linear-gradient(top, #50a3c4 1%, #aacbe2 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(1%,#50a3c4), color-stop(100%,#aacbe2)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #50a3c4 1%,#aacbe2 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #50a3c4 1%,#aacbe2 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #50a3c4 1%,#aacbe2 100%); /* IE10+ */
    background: linear-gradient(to bottom, #50a3c4 1%,#aacbe2 100%); /* W3C */
}

body
{
    width: 95%;
    height: 95%;
    margin: 8px 8px 8px 8px;
}

.Part
{
    width: 100%;
    margin-bottom: 8px;
}

.Block
{
    margin-bottom: 20px;
}

.Line
{
    display: flex;
    box-orient: horizontal;
    align-items: center;
    display: -ms-flexbox;
    -ms-box-orient: horizontal;
    -ms-flex-align: center;
    display: -webkit-flex;
    -webkit-box-orient: horizontal;
    -webkit-align-items: center;
    display: -moz-flex;
    -moz-box-orient: horizontal;
    -moz-align-items: center;
    font-size: large;
    font-weight: bold;
    width: 100%;
}

.Stretch
{
    flex: 2 0;
    -ms-flex: 2 0;
    -webkit-flex: 2 0;
    -moz-flex: 2 0;
    height: 2em;
}

.Fixed
{
    flex: 0 0;
    -ms-flex: 0 0;
    -webkit-flex: 0 0;
    -moz-flex: 0 0;
}

.InfoTable
{
    margin-bottom: 20px;
    padding: 0px;
    width: 100%;
}

    .InfoTable table
    {
        width: 80%;
        height: 100%;
        margin: 0px;
        padding: 0px;
        border-spacing: 0px;
    }

    .InfoTable tr:nth-child(odd)
    {
        background-color: #0E0E0E;
    }

    .InfoTable tr:nth-child(even)
    {
        background-color: #3E3E3E;
    }

    .InfoTable td
    {
        border: 1px solid;
        vertical-align: middle;
        border-width: 1px;
        border-color: beige;
        text-align: left;
        padding: 5px;
        font-size: 14px;
        font-family: Arial;
        font-weight: bold;
        color: white;
    }

.statName
{
    width: 25%;
}

              
            
!

JS

              
                //----------------------------------------
// Setup and Common functions
//----------------------------------------

var serverURL = "https://googledrive.com/host/0B2Ay-nw1QSW2SHhjR0VBZXE2bUU/TinyBlueMarble/";
var browserName = null;
var manifest = null;

// executed when all js and css is loaded (i.e. document ready event)
$(function () {
    HideBlocks();
    kendo.bind(document.body, vm);
    testXHR();
    BrowserType();
    blobType();
    DBsetup().pipe(fetchManifest).pipe(setup);
});

function testXHR() {
    vm.setInfo("xhr2", $xhr.supported().toString());
}

function blobType() {
    if (browserName == "Chrome") {
        vm.setInfo("blobs", "Base64");
    }
}

function BrowserType() {
    var n = navigator.appName;
    var ua = navigator.userAgent;
    var tem;
    var m = ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
    if (m && (tem = ua.match(/version\/([\.\d]+)/i)) != null) m[2] = tem[1];
    m = m ? [m[1], m[2]] : [n, navigator.appVersion, '-?'];
    if ((m[0] == "Netscape") && ua.match(/rv:11/g)) {
        m[0] = "MSIE";
    }
    vm.setInfo("browser", m[0] + ", V" + m[1]);
    browserName = m[0];
    return { brand: m[0], version: m[1] };
}

function fetchManifest() {
    var defer = $.Deferred();
    $xhr.defaultError(function (text, code) {
        console.error("xhr2 error: ", text, " code: ", code, " url: ", this.xhr2data.url);
    });
    var path = serverURL;
    $xhr.ajax({
        url: path + "manifest.json",
        dataType: "json",
        success: function (data) {
            manifest = data;
            vm.setInfo("count", manifest.files.length.toString());
            defer.resolve();
        }
    });
    return defer.promise();
}

function setup() {
    vm.setInfo("status", "Ready");
    $("#CommandBlock").show();
}

function HideBlocks() {
    $("#CommandBlock").hide();
    $("#RunBlock").hide();
    $("#ResultsBlock").hide();
}


function ms2Time(ms) {
    var secs = ms / 1000;
    ms = Math.floor(ms % 1000);
    var minutes = secs / 60;
    secs = Math.floor(secs % 60);
    var hours = minutes / 60;
    minutes = Math.floor(minutes % 60);
    hours = Math.floor(hours % 24);
    return hours + ":" + kendo.toString(minutes, "00") + ":" + kendo.toString(secs, "00") + "." + kendo.toString(ms, "000");
}

function clone(obj) {
    return jQuery.extend({}, obj);
}

function deepClone(obj) {
    return jQuery.extend(true, {}, obj);
}

//----------------------------------------
// Kendo MVVM Code
//----------------------------------------
var Triple = kendo.data.Model.define({
    fields: {
        "Name": {
            type: "string"
        },
        "Key": {
            type: "string"
        },
        "Value": {
            type: "string"
        }
    }
});

var vm = kendo.observable({
    info: [
        new Triple({ Name: "Status", Key: "status", Value: "Initializing" }),
        new Triple({ Name: "XHR2", Key: "xhr2", Value: "false" }),
        new Triple({ Name: "Blobs", Key: "blobs", Value: "binary" }),
        new Triple({ Name: "Browser", Key: "browser", Value: "xx" }),
        new Triple({ Name: "PouchDB Status", Key: "db", Value: "Empty" }),
        new Triple({ Name: "Remote Files for Test", Key: "count", Value: "xx" }),
        new Triple({ Name: "Total Time", Key: "time", Value: ".." }),
        new Triple({ Name: "Files/sec", Key: "rate", Value: ".." })
    ],
    TotalFiles: 100,
    FileCount: 20,
    PercentDone: "20",
    setInfo: function (key, val) {
        var self = this.info;
        $.each(self, function (idx, triple) {
            if (key === triple.Key) {
                self[idx].set("Value", val);
                return false;
            }
        });
    }
});
//----------------------------------------
// PouchDB related functions
//----------------------------------------

var DB = null;
var DBmanifest = null;
DBName = "PouchTesting";

function DBsetup() {
    var defer = $.Deferred();
    Pouch.enableAllDbs = true;
    DB = new PouchDB(DBName);
    DBInfo().done(function () {
        var info = DBManifest;
        delete info.files;
        vm.setInfo("db", JSON.stringify(info));
        vm.setInfo("blobs", PouchDB.BlobSupport);
        defer.resolve();
    });
    return defer.promise();
}

function DBInfo() {
    var defer = $.Deferred();
    DB.get("manifest", function (err, doc) {
        if (err) {
            console.info("dbInfo - manifest missing", err.error, " reason: ", err.reason, " status: ", err.status);
            DBManifest = "Empty";
            defer.resolve(null);
        } else {
            console.info("getDBInfo found a manifest");
            DBManifest = doc.data;
            defer.resolve(doc.data);
        }
    });
    return defer.promise();
}

function DBDelete() {
    var defer = $.Deferred();
    PouchDB.destroy(DBName, function (err, info) {
        if (err) {
            console.error("Could not Delete DB: " + DBName);
            defer.resolve();
        } else {
            console.info("dbDelete deleted: " + DBName);
            vm.setInfo("db", "Empty");
            DB = new Pouch(DBName);
            defer.resolve();
        }
    });
    return defer.promise();
}

function DBSaveManifest(info) {
    info = deepClone(info);
    var count = info.files.length;
    info.fileCount = count;
    DB.put({ _id: "manifest", data: info }, function (err, response) {
        if (err) {
            console.error("DBSaveManifest: Could store MANIFEST: error:" + err.error + " reason: " + err.reason + " status: " + err.status);
        } else {
            console.info("Saved Map Manifest: ", response.id + " rev: " + response.rev);
            delete info.files;
            vm.setInfo("db", JSON.stringify(info));
        }
    });
}

function DBSaveBlob(blob, req) {
    if (blob.size != req.size) {
        console.error("Blob Length found: ", blob.size, " expected: ", req.size);
    }
    blob.readSize = blob.size;
    blob.id = req.id;
    req.blob = blob;
    DB.putAttachment(req.id, "pic", blob, blob.type, function (err, response) {
        if (err) {
            console.error("Could store blob: error: " + err.error + " reason: " + err.reason + " status: " + err.status);
        } else {
            console.log("worker-" + req.workID + " saved: ", response.id + " md5: " + req.blob.md5 + " rev: " + response.rev);
            updateStats();
            req.defer.resolve(req.workID);
        }
    });
}


function DBGetBlob(id) {
    var defer = $.Deferred();
    DB.getAttachment(id, "pic", function (err, response) {
        if (err) {
            console.error("Could get blob " + id + " : error: " + err.error + " reason: " + err.reason + " status: " + err.status);
            defer.fail();
        } else {
            defer.resolve(response);
        }
    });
    return defer.promise();
}
//----------------------------------------
// Main Thread (download an store)
//----------------------------------------
var maxThreads = 12;
var Walker = null;
var info = null;
var start = null;
var fCount = 0;
var fMax = 0;
var showID = "4-9-6";

function download() {
    DBDelete().done(spawnWorkers);
    HideBlocks();
    $("#RunBlock").show();
    fMax = manifest.files.length;
    maxThreads = setThreading();
    fCount = 0;
    vm.set("TotalFiles", fMax);
    vm.set("FileCount", fCount);
    vm.set("PercentDone", fCount * 100 / fMax);
    return;
}

function MakeWalker() {
    var j = 0;
    function walker() {
        if (j == fMax) {
            return "done";
        }
        var files = manifest.files;
        var reply = {
            id: files[j].id,
            size: files[j].size,
            path: serverURL + files[j].id + ".jpg"
        };
        j++;
        return reply;
    }
    return walker;
}

function spawnWorkers() {
    DBSaveManifest(manifest);
    start = new Date();
    console.log("starting to fetch blobs");
    Walker = MakeWalker();
    for (var i = 0; i < maxThreads; i++) {
        worker(i);
    }
    return;
}

function worker(workID) {
    var req = Walker();
    if (req == "done") {
        return;
    }
    req.workID = workID;
    req.defer = $.Deferred();
    req.defer.promise().done(worker);
    getBlob(req);
}

function getBlob(req) {
    $xhr.ajax({
        url: req.path,
        dataType: "blob",
        success: function (data) {
            console.log("worker-" + req.workID + " fetched: ", req.id);
            DBSaveBlob(data, req);
        }
    });
}

function updateStats() {
    fCount++;
    vm.set("FileCount", fCount);
    vm.set("PercentDone", Math.round(fCount * 100 / fMax));
    if (fCount >= fMax) {
        downloadDone();
    }
}

function downloadDone() {
    var time = new Date() - start;
    var rate = fCount * 1000 / time;
    console.log("Done Storing Tiles");
    HideBlocks();
    $("#ResultsBlock").show();
    vm.setInfo("time", ms2Time(time));
    vm.setInfo("rate", rate.toString());
    DBGetBlob(showID).done(showResult);
}

function showResult(blob) {
    var imgURL = URL.createObjectURL(blob);
    $("#imgTile").attr("src", imgURL);
    URL.revokeObjectURL(imgURL);
}

function setThreading() {
    var threads = 50;
    switch (BrowserType().brand) {
        case "Firefox":
            threads = 1;
            break;
        case "Chrome":
            threads = 100;
            break;
        case "MSIE":
            threads = 100;
            break;
    }
    return threads;
}
              
            
!
999px

Console