<commit_before>"use strict"

define([
    "dojo/query",
    "dojo/request",
    "esri/geometry",
    "tiles/base64utils",
    "tiles/dbStore",
    "tiles/tilingScheme",
    "tiles/FileSaver"
    ], function(query, request, geometry,Base64Utils,DbStore,TilingScheme,FileSaver)
    {
        return {
            /**
             * Utility method to get the basemap layer reference
             * @param map
             * @returns {Number} layerId
             */
            getBasemapLayer: function(map)
            {
                var layerId = map.layerIds[0];
                return map.getLayer(layerId);
            },

            /**
             * Method that extends a layer object with the offline capability.
             * After extending one layer, you can call layer.goOffline() or layer.goOnline()
             * @param layer
             * @param callback
             * @returns {callback} callback(boolean, string)
             */
            extend: function(layer,callback)
            {
                console.log("extending layer", layer.url);

                layer._lastTileUrl = "";                

                /* we add some methods to the layer object */
                /* we don't want to extend the tiled layer class, as it is a capability that we want to add only to one instance */
                /* we also add some additional attributes inside an "offline" object */

                layer._getTileUrl = layer.getTileUrl;
                layer.offline = {
                    online: true,
                    store: new DbStore(),
                    proxyPath: "../../lib/proxy.php"
                };

                if( /*false &&*/ layer.offline.store.isSupported() )
                    layer.offline.store.init(callback);
                else
                    return callback(false, "indexedDB not supported");

                /**
                 * Internal method that overrides the getTileUrl() method.
                 * If application is offline then tiles are written to local storage.
                 * Retrieves tiles as requested by the ArcGIS API for JavaScript.
                 * If a tile is in cache it is returned.
                 * If it is not in cache then one is retrieved over the internet.
                 * @param level
                 * @param row
                 * @param col
                 * @returns {String} URL
                 */
                layer.getTileUrl = function(level,row,col)
                {
                    if(isNaN(level) == false && isNaN(row) == false && isNaN(col) == false){
                        console.log("looking for tile",level,row,col);
                        var url = this._getTileUrl(level,row,col);

                        if( this.offline.online )
                        {
                            console.log("fetching url online: ", url);
                            layer._lastTileUrl = url;
                            return url;
                        }

                        url = url.split('?')[0];

                        /* temporary URL returned immediately, as we haven't retrieved the image from the indexeddb yet */
                        var tileid = "void:"+level+"-"+row+"-"+col;

                        this.offline.store.get(url, function(success, offlineTile)
                        {
                            /* when the .get() callback is called we replace the temporary URL originally returned by the data:image url */
                            var img = query("img[src="+tileid+"]")[0];

                            if( success )
                            {
                                console.log("found tile offline", url);
                                var imgURL = "data:image;base64," + offlineTile.img;

                                // search for the img with src="void:"+level+"-"+row+"-"+col and replace with actual url
                                img.style.borderColor = "blue";
                                // when we return a nonexistent url to the image, the TiledMapServiceLayer::_tileErrorHandler() method
                                // sets img visibility to 'hidden', so we need to show the image back once we have put the data:image
                                img.style.visibility = "visible";
                                img.src = imgURL;

                                return "";	/* this result goes nowhere, seriously */
                            }
                            else
                            {
                                img.style.borderColor = "green";
                                console.log("tile is not in the offline store", url);
                                return "";	/* this result goes nowhere, seriously */
                            }
                        });

                        return tileid;
                    }
                    else{
                        return null;
                    }

                };

                /**
                 * Returns an object that contains the number of tiles that would need to be downloaded
                 * for the specified extent and zoom level, and the estimated byte size of such tiles.
                 * This method is useful to give the user an indication of the required time and space
                 * before launching the actual download operation. The byte size estimation is very rough.
                 * @param extent<commit_msg>two simple changes (although diff seems more complex) - layer.resampling = false - temp url is now void:/level/row/col, as tiledlayer code seems to split the returned url by '/' to get level row and col values<commit_after>"use strict"

define([
    "dojo/query",
    "dojo/request",
    "esri/geometry",
    "tiles/base64utils",
    "tiles/dbStore",
    "tiles/tilingScheme",
    "tiles/FileSaver"
    ], function(query, request, geometry,Base64Utils,DbStore,TilingScheme,FileSaver)
    {
        return {
            /**
             * Utility method to get the basemap layer reference
             * @param map
             * @returns {Number} layerId
             */
            getBasemapLayer: function(map)
            {
                var layerId = map.layerIds[0];
                return map.getLayer(layerId);
            },

            /**
             * Method that extends a layer object with the offline capability.
             * After extending one layer, you can call layer.goOffline() or layer.goOnline()
             * @param layer
             * @param callback
             * @returns {callback} callback(boolean, string)
             */
            extend: function(layer,callback)
            {
                console.log("extending layer", layer.url);

                layer._lastTileUrl = "";                

                /* we add some methods to the layer object */
                /* we don't want to extend the tiled layer class, as it is a capability that we want to add only to one instance */
                /* we also add some additional attributes inside an "offline" object */

                layer._getTileUrl = layer.getTileUrl;
                layer.offline = {
                    online: true,
                    store: new DbStore(),
                    proxyPath: "../../lib/proxy.php"
                };

                if( /*false &&*/ layer.offline.store.isSupported() )
                    layer.offline.store.init(callback);
                else
                    return callback(false, "indexedDB not supported");

                layer.resampling = false;

                /**
                 * Internal method that overrides the getTileUrl() method.
                 * If application is offline then tiles are written to local storage.
                 * Retrieves tiles as requested by the ArcGIS API for JavaScript.
                 * If a tile is in cache it is returned.
                 * If it is not in cache then one is retrieved over the internet.
                 * @param level
                 * @param row
                 * @param col
                 * @returns {String} URL
                 */
                layer.getTileUrl = function(level,row,col)
                {
                    if(isNaN(level) || isNaN(row) || isNaN(col) )
                    {
                        console.log("level,row,col:",level,row,col);
                        return null;
                    }

                    console.log("looking for tile",level,row,col);
                    var url = this._getTileUrl(level,row,col);

                    if( this.offline.online )
                    {
                        console.log("fetching url online: ", url);
                        layer._lastTileUrl = url;
                        return url;
                    }

                    url = url.split('?')[0];

                    /* temporary URL returned immediately, as we haven't retrieved the image from the indexeddb yet */
                    var tileid = "void:/"+level+"/"+row+"/"+col;

                    this.offline.store.get(url, function(success, offlineTile)
                    {
                        /* when the .get() callback is called we replace the temporary URL originally returned by the data:image url */
                        var img = query("img[src="+tileid+"]")[0];

                        if( success )
                        {
                            console.log("found tile offline", url);
                            var imgURL = "data:image;base64," + offlineTile.img;

                            // search for the img with src="void:"+level+"-"+row+"-"+col and replace with actual url
                            img.style.borderColor = "blue";
                            // when we return a nonexistent url to the image, the TiledMapServiceLayer::_tileErrorHandler() method
                            // sets img visibility to 'hidden', so we need to show the image back once we have put the data:image
                            img.style.visibility = "visible";
                            img.src = imgURL;
                        }
                        else
                        {
                            img.style.borderColor = "green";
                            console.log("tile is not in the offline store", url);
                        }
                        return "";  /* this result goes nowhere, seriously */
                    });

                    return tileid;
                };

                /**
                 * Returns an object that contains the number of tiles that would need to be downloaded
                 * for the specified extent and zoom level, and the estimated byte size of such tiles.
                 * This method is useful to give the user an indication of the required time and space
                 * before launching the actual download operation. The byte size estimation is very rough.
                 * @param extent
{'fraction_non_alphanumeric': 0.08381056267612036, 'fraction_numerical': 0.001636214889555495, 'mean_word_length': 1.171733122779313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6416551', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2373, 'n_words': 825}