function renderAvailableStores()

in src/Feature/Cart/code/Scripts/checkout.js [462:563]


function renderAvailableStores(data, success, sender) {
    map.entities.clear();
    sender.hide();

    var lineId = sender.selector.replace('#StoreSearchResultsContainer-', '');

    if (lineId === "#StoreSearchResultsContainer") {
        lineId = "";
    } else {
        lineId = "-" + lineId;
    }

    if (!success) {
        return;
    }

    var storeCount = 0;
    var pin;
    var pinInfoBox;
    var mapBounds = map.getBounds();
    var stores = data.Stores;

    // Display search location
    if (searchLocation != null && searchLocation != undefined && mapBounds.contains(searchLocation)) {
        // Plot the location to the map
        pin = new Microsoft.Maps.Pushpin(searchLocation, { draggable: false, text: "X" });
        map.entities.push(pin);
    }

    // If we have stores, plot them on the map
    if (stores.length > 0) {
        for (var i = 0; i < stores.length; i++) {
            var currentStoreLocation = stores[i];
            currentStoreLocation.location = { latitude: currentStoreLocation.Latitude, longitude: currentStoreLocation.Longitude };

            // Test each location to see if it is within the bounding rectangle
            if (mapBounds.contains(currentStoreLocation.location)) {
                sender.show();

                //  Increment the counter used to manage the sequential entity index
                storeCount++;
                currentStoreLocation.LocationCount = storeCount;

                // This is the html that appears when a push pin is clicked on the map
                var storeAddressText = '<div style="width:80%;height:100%;">\
                    <p style="background-color:gray;color:black;margin-bottom:5px;">\
                        <span style="padding-right:45px;">Store</span>\
                            <span style="font-weight:bold;">Distance</span>\
                                <p><p style="margin-bottom:0px;margin-top:0px;">\
                                    <span style="color:black;padding-right:35px;">'
                                        + currentStoreLocation.Name +
                '</span><span style="color:black;">'
                + currentStoreLocation.Distance +
                ' miles</span>\
                </p><p style="margin-bottom:0px;margin-top:0px;">'
                + currentStoreLocation.Address.Address1 +
                ' </p><p style="margin-bottom:0px;margin-top:0px;">'
                + currentStoreLocation.Address.City + ', '
                + currentStoreLocation.Address.State + ' '
                + currentStoreLocation.Address.ZipPostalCode +
                '</p></div>';

                // Plot the location to the map	
                pin = new Microsoft.Maps.Pushpin(currentStoreLocation.location, { draggable: false, text: "" + storeCount + "" });

                // Populating the Bing map push pin popup with store location data
                pinInfoBox = new Microsoft.Maps.Infobox(currentStoreLocation.location, { width: 225, offset: new Microsoft.Maps.Point(0, 10), showPointer: true, visible: false, description: storeAddressText });

                // Registering the event that fires when a pushpin on a Bing map is clicked
                Microsoft.Maps.Events.addHandler(pin, 'click', (function (pinInfoBox) {
                    return function () {
                        pinInfoBox.setOptions({ visible: true });
                    }
                })(pinInfoBox));

                map.entities.push(pin);
                map.entities.push(pinInfoBox);

                currentStoreLocation.Address.Name = currentStoreLocation.Name;
                lineId = lineId.replace('-', '');
                if (lineId === '') {
                    checkoutDataViewModel.stores.push(new StoreViewModel(currentStoreLocation));
                    if (storeCount === 1) {
                        checkoutDataViewModel.store(new StoreViewModel(currentStoreLocation));
                    }
                } else {
                    var selectedLine = ko.utils.arrayFirst(checkoutDataViewModel.cart().cartLines(), function (line) {
                        return line.externalCartLineId === lineId;
                    });
                    if (selectedLine != null) {
                        selectedLine.stores.push(new StoreViewModel(currentStoreLocation));
                        if (storeCount === 1) {
                            selectedLine.store(new StoreViewModel(currentStoreLocation));
                        }
                    }

                    shippingMethodsArray.push(lineId);
                }
            }
        }
    }
}