Codebase

From Wikidocumentaries
Revision as of 18:29, 10 January 2019 by Admin (talk | contribs) (Created page with "Navigate the codebase Components * method '''wikidocumentaries''' ** $store.state.wikidocumentaries store/store.js <pre> const wikidocumentaries = { title: 'Vapaamuurar...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Navigate the codebase

Components

  • method wikidocumentaries
    • $store.state.wikidocumentaries

store/store.js

const wikidocumentaries = {
    title: 'Vapaamuurarin hauta',
    description: 'Muistomerkki Helsingissä',
    headerImageURL: 'https://upload.wikimedia.org/wikipedia/commons/b/bf/Helsinki_Kaisaniemi_Freemason%27s_grave_1908_I_K_Inha.png',
    wikipedia: {
        excerptHTML: null,
        remainingHTML: null,
        wikipediaURL: 'https://fi.wikipedia.org/wiki/Vapaamuurarin_hauta'
    },
    wikidata: {
        id: 'Q5501061',
        instance_of: {
            id: 'Q5003624',
            value: 'muistomerkki',
            url: 'https://www.wikidata.org/wiki/Q5003624'
        },
        statements: [
            {
                id: 'P361',
                label: 'osa kohdetta',
                value: 'Kaisaniemen puisto ja kasvitieteellinen puutarha',
                url: 'https://www.wikidata.org/wiki/Q30506322'
            },
        dates: [],
    },
    images: [
        {
            id: 'musketti.M012:HK19670603:7117',
            source: 'finna',
            imageURL: "https://www.finna.fi/Cover/Show?id=musketti.M012:HK19670603:7117&index=0&size=large",
            title: "Vapaamuurarin hauta Kaisaniemessä",
            authors: "Pietinen",
            institutions: "Museovirasto",
            infoURL: "https://www.finna.fi/Record/musketti.M012:HK19670603:7117",
            location: "Helsinki,",
            geoLocations: ["POLYGON((24.7828131 60.0999549, 24.8356577 60.130414, 24.8513844 60.2249765, 24.8419098 60.2212043, 24.8347825 60.2585099, 24.8677628 60.2523073, 24.9473908 60.2784652, 24.9731653 60.2643801, 25.0209862 60.2893227, 25.0882105 60.2713417, 25.0823359 60.2496391, 25.1358461 60.2372286, 25.1598757 60.2488133, 25.1425242 60.2697779, 25.2545116 60.2952274, 25.2509121 60.2734979, 25.2273451 60.2611057, 25.240926 60.246305, 25.2014099 60.2181613, 25.2204176 60.1997262, 25.1800446 60.0987408, 25.1693516 59.9434386, 24.9423061 59.922486, 24.7828131 60.0999549))"],
            year: 1932,
            license: "CC BY 4.0"
        },
    ],
    geo: {
        location: "POINT(24.943752 60.176013)",
        admin: "Helsinki"
    },
    topicStartYear: 1784,
}

export default new Vuex.Store({
    state: {
        wikidocumentariesDataState: WIKI.STATES.UNINITIALIZED,
        wikidocumentaries: wikidocumentaries,
        shownImages: [],
        timelineImages: [],
        historicalMapSearchPageMap: null,
        historicalMaps: [],
        basemaps: basemaps,
        selectedBasemaps: [],
        selectedBasemapOpacity: 0.7,
        shouldFitMapToBasemap: false,
        photoOfTheDay: "https://upload.wikimedia.org/wikipedia/commons/a/ad/Kulutusosuuskuntien_Keskusliiton_kokoelma_D1974_11333A_%2830804608561%29.jpg",
        mapOfTheDay: {
            id: "File:1900_Plan_af_Helsingfors_stad.tif",
            warperID: 148,
            server: "http://warper.wmflabs.org/",
            coordinates: [24.9351, 60.1658],
        },
        nearbyWikiItems: [],
    },
    getters: {
        topicStartYear: state => {
            
            var startYear = (new Date()).getFullYear();

            if (state.wikidocumentaries.topicStartYear != undefined) {
                startYear = state.wikidocumentaries.topicStartYear;
            }
            if (state.wikidocumentaries.images.length > 0) {
                for (var i = 0; i < state.wikidocumentaries.images.length; i++) {
                    //console.log("state.wikidocumentaries.images[i]", state.wikidocumentaries.images[i]);
                    //console.log("state.wikidocumentaries.images[i].year", state.wikidocumentaries.images[i].year);
                    if (state.wikidocumentaries.images[i].year != null &&
                        startYear > state.wikidocumentaries.images[i].year) {
                        if (!isWikidataBasedUnchangableStartYear(state.wikidocumentaries)) {
                            startYear = state.wikidocumentaries.images[i].year;
                        }
                    }
                }
            }

            //console.log(startYear);

            return startYear;
        }
    },
    mutations: {
        resetState(state) {

            state.wikidocumentariesDataState = WIKI.STATES.UNINITIALIZED;

            state.wikidocumentaries = {
                title: null,
                description: null,
                headerImageURL: null,
                wikipedia: {
                    excerptHTML: null,
                    remainingHTML: null,
                    wikipediaURL: null,
                },
                wikidata: {
                    id: null,
                    instance_of: {
                        id: null,
                        value: null,
                        url: null
                    },
                    statements: [],
                    dates: [],
                },
                images: [],
                geo: {
                    location: null,
                    scale: null,
                    admin: null
                },
                topicStartYear: null
            };
            state.shownImages = [];
            state.timelineImages = [];
            state.historicalMapSearchPageMap = null;
            state.historicalMaps = [];
            state.basemaps = basemaps;
            state.selectedBasemaps = [];
            state.selectedBasemapOpacity = 0.7;
            state.shouldFitMapToBasemap = false;
            state.nearbyWikiItems = [];
        },
        setImagesShownOnMap(state, images) {
            state.shownImages = images;
        },
        setImagesShownOnTimeline(state, images) {
            //console.log("setImagesShownOnTimeline");
            state.timelineImages = images;
        },
        setHistoricalMapSearchPageMap(state, map) {
            state.historicalMapSearchPageMap = map;
        },
        setHistoricalMaps(state, maps) {
            state.historicalMaps = maps;
        },
        setSelectedBasemap(state, mapInfo) {
            state.selectedBasemaps = [mapInfo];
        },
        setSelectedBasemaps(state, mapInfos) {
            //console.log("setSelectedBasemaps");
            state.selectedBasemaps = mapInfos;
        },
        setSelectedBasemapOpacity(state, opacity) { // 0 - 1
            state.selectedBasemapOpacity = opacity;
        },
        setShouldFitMapToBasemap(state, value) {
            state.shouldFitMapToBasemap = value;
        },
        setWikidocumentariesTopicTitle(state, title) {
            state.wikidocumentaries.title = title;
        },
        setWikidocumentariesTopicDescription(state, description) {
            state.wikidocumentaries.description = description;
        },
        setWikidata(state, wikidata) {
            state.wikidocumentaries.wikidata = wikidata;
        },
        setWikidataId(state, wikidataId) {
            state.wikidocumentaries.wikidataId = wikidataId;
        },
        setHeaderImageURL(state, URL) {
            state.wikidocumentaries.headerImageURL = URL;
        },
        setWikipediaExcerptHTML(state, html) {
            state.wikidocumentaries.wikipedia.excerptHTML = html;
        },
        setWikipediaRemainingHTML(state, html) {
            state.wikidocumentaries.wikipedia.remainingHTML = html;
        },
        setWikipediaURL(state, URL) {
            state.wikidocumentaries.wikipedia.wikipediaURL = URL;
        },
        setTopicGeoLocation(state, coordinates) {
            //console.log("setTopicGeoLocation", coordinates);
            state.wikidocumentaries.geo.location = "POINT(" + coordinates.lon + " " + coordinates.lat + ")"
        },
        setWikidocumentariesDataState(state, value) {
            state.wikidocumentariesDataState = value;
        },
        setWikidocumentariesImages(state, images) {
            state.wikidocumentaries.images = images;
        },
        setTopicStartYear(state, year) {
            //console.log('setTopicStartYear');
            state.wikidocumentaries.topicStartYear = year;
        },
        setHistoricalBasemaps(state, basemaps) {
            state.basemaps = basemaps;
            if (basemaps.length > 0) {
                state.selectedBasemaps = [basemaps[0]];
            }
        },
        setNearbyWikiItems(state, items) {
            state.nearbyWikiItems = items;
        }

    },
    actions: {
        updateWikidocumentaries({dispatch, commit}, params) {
            //console.log('actions.updateWikidocumentaries');
            commit('resetState');
            commit('setWikidocumentariesDataState', WIKI.STATES.LOADING_WIKI_EXTERNAL);
            commit('setWikidocumentariesTopicTitle', params.topic);
            commit('setWikidataId', params.wikidata);
            var promiseWiki = dispatch('getWikiDocumentariesData', params);
            //var promiseImages = dispatch('getTopicImages', params)
            promiseWiki.then((data) => {

                //console.log(data);

                if (data.wikidata  == null ) {
                    commit('setWikidocumentariesDataState', WIKI.STATES.FAIL_WIKI_EXTERNAL);
                }
                else {
                    params.wiki = data;

                    params.topic = data.wikidata.title;
                    commit('setWikidocumentariesTopicTitle', params.topic);

                    params.wikidata = data.wikidata.id;
                    commit('setWikidataId', params.wikidata);
		    
                    commit('setWikidocumentariesDataState', WIKI.STATES.LOADING_IMAGES_EXTERNAL);

                    dispatch('getTopicImages', params).then((result) => {

                        commit('setWikidocumentariesDataState', WIKI.STATES.READY);
                        //console.log(result);
                        //console.log(data.wikipedia.originalimage);

                        if ( data.wikipedia.originalimage != undefined && data.wikipedia.originalimage.source != null) {
                            commit('setHeaderImageURL', data.wikipedia.originalimage.source);
                        }
                        else if (result.length > 0) { // Set the first image (not pdf) in the results as header image 
                            for (var i = 0; i < result.length; i++) {
                                if (result[i].imageURL.indexOf('.pdf') == -1) {
                                    commit('setHeaderImageURL', result[i].imageURL);
                                    break;
                                }
                            }
                        }

                        //console.log(this.state.wikidocumentaries.geo);
                        // If the geo location could not be set from Wikipedia or Wikidata then
                        // try to calculate it from the image locations 
                        if (this.state.wikidocumentaries.geo.location == null) {
                            var location = calculateLocationFromImages(result);
                            if (location != null) {
                                commit('setTopicGeoLocation', location);
                            }
                            else {
                                // TODO
                            }
                        }
                    });
                }
            });
        },
        async getWikiDocumentariesData(context, params) {
            //console.log('getWikiDocumentariesData');
            return new Promise((resolve, reject) => {

                var requestConfig = {
                    baseURL: BASE_URL,
                    url: "/wiki",
                    method: "get",
                    params: {
                        topic: params.topic,
                        wikidata: params.wikidata,
                        language: params.language
                    }
                };

                axios.request(requestConfig).
                    then(function (response) {
                        //console.log(response.data);

                        if (response.data.wikidata == null ) {
                            console.log("response.data.wikipedia == null");
                            context.commit('setWikidocumentariesDataState', WIKI.STATES.FAIL_WIKI_EXTERNAL);
                        }
                        else {
                            //console.log(response.data);
                            context.commit('setWikidata', response.data.wikidata);
                            context.commit('setWikidocumentariesTopicTitle', response.data.wikidata.title);
                            context.commit('setWikidocumentariesTopicDescription', response.data.wikidata.description);
                            //console.log(response.data.wikidata.statements);
                            var startYear = calculateTopicStartYearFromWikidata(response.data.wikidata, context.state.wikidocumentaries.topicStartYear);
                            context.commit('setTopicStartYear', startYear);
                            
                            context.commit('setWikipediaExcerptHTML', response.data.wikipediaExcerptHTML);
                            context.commit('setWikipediaRemainingHTML', response.data.wikipediaRemainingHTML);
                            if (response.data.wikipedia != undefined ) {
                                context.commit('setWikipediaURL', response.data.wikipedia.content_urls.desktop.page);
                            }
                            else
                            {
                                context.commit('setWikipediaURL', "");
                            }

                            if (response.data.wikipedia != undefined && response.data.wikipedia.coordinates != undefined ) {
                                context.commit('setTopicGeoLocation', response.data.wikipedia.coordinates);
                            }
                            else if (response.data.wikidata != undefined) {
                                if (response.data.wikidata.geo.lat != null && response.data.wikidata.geo.lon != null) {
                                    context.commit('setTopicGeoLocation', response.data.wikidata.geo);
                                }
                                else {
                                    // guessTopicGeoLocationFromWikidata(params.topic, response.data.wikidata).then(data => {
                                    //     //console.log(data);
                                    //     if (data != null && data.result != null && data.result.point != null && data.result.point.coordinates != null) {
                                    //         commit('setTopicGeoLocation', {
                                    //             lon: data.result.point.coordinates[1],
                                    //             lat: data.result.point.coordinates[0]
                                    //         });
                                    //     }
                                    //     // if (location != null) {
                                    //     //     commit('setTopicGeoLocation', location);
                                    //     // }
                                    // });
                                }
                            }
                            else {
                                // // TODO
                                // commit('setTopicGeoLocation', {
                                //     lat: 0,
                                //     lon: 0
                                // });
                            }
                        }

                        resolve(response.data);
                    })
                    .catch(function (error) {
                        console.log(error);

                        reject(error);
                    });
            });
        },
        async getTopicImages({dispatch, commit}, params) {

            //console.log('getTopicImages', params);
            
            return new Promise((resolve, reject) => {

                //console.log(params);

                var requestConfig = {
                    baseURL: BASE_URL,
                    url: "/images",
                    method: "get",
                    params: {
                        topic: params.topic,
                        language: params.language
                    }
                };

                if (params.wiki.wikidata != null) {
                    var statements = params.wiki.wikidata.statements;
                    for (var i = 0; i < statements.length; i++) {
                        if (statements[i].id == "P373") {
                            requestConfig.params.commons_category = statements[i].values[0].value;
                            break;
                        }
                    }
                }

                if (params.wiki.wikipedia != undefined && params.wiki.wikipedia.coordinates != undefined) {
                    requestConfig.params.lat = params.wiki.wikipedia.coordinates.lat;
                    requestConfig.params.lon = params.wiki.wikipedia.coordinates.lon;
                    requestConfig.params.maxradius = 2000; // meters
                }
                else if (params.wiki.wikidata != undefined && params.wiki.wikidata.geo.lat != null && params.wiki.wikidata.geo.lon != null) {
                    requestConfig.params.lat = params.wiki.wikidata.geo.lat;
                    requestConfig.params.lon = params.wiki.wikidata.geo.lon;
                    requestConfig.params.maxradius = 2000;
                }

                axios.request(requestConfig).
                    then(function (response) {
                        //console.log(response.data);

                        commit('setWikidocumentariesImages', response.data);
                        commit('setImagesShownOnTimeline', response.data);
                        resolve(response.data);

                    }).catch(function (error) {
                        console.log(error);

                        reject(error);
                    });
            });
        },
        async getHistoricalBasemaps({dispatch, commit}, params) {

            var requestConfig = {
                baseURL: BASE_URL,
                url: "/basemaps",
                method: "get",
                params: params
            };

            axios.request(requestConfig).
                then(function (response) {
                    //console.log(response.data);

                    commit('setHistoricalBasemaps', response.data);

                }).catch(function (error) {
                    console.log(error);
                });
        },
        async getHistoricalMaps({dispatch, commit}, locationParams) {
            //commit('setHistoricalMaps', maps);
            commit('setHistoricalMaps',
                await dispatch('getHistoricalMapsFromCommons', {
                    locationParams: locationParams,
                    maps: []
                })
                .then((maps) => dispatch('getHistoricalMapsFromFinna', {
                    locationParams: locationParams,
                    maps: maps
                })));
        },
        async getHistoricalMapsFromCommons(context, params) {
            return new Promise((resolve, reject) => {

                //console.log(params);

                //resolve(params.maps);

                var category = "";
                switch (params.locationParams.municipality) {
                    case "Helsinki":
                        category = "Old_maps_of_Helsinki";
                        break;
                    case "Forssa":
                        category = "Old_maps_of_Forssa";
                        break;
                    case "Hämeenlinna":
                        category = "Old_maps_of_Hämeenlinna";
                        break;
                    case "Kemi":
                        category = "Old_maps_of_Kemi";
                        break;
                    case "Loimaa":
                        category = "Old_maps_of_Loimaa";
                        break;
                    case "Lohja":
                        category = "Old_maps_of_Lohja";
                        break;
                    case "Salo":
                        category = "Old_maps_of_Salo";
                        break;
                    case "Oulu":
                        category = "Old_maps_of_Oulu";
                        break;
                    case "Somero":
                        category = "Old_maps_of_Somero";
                        break;
                    case "Turku":
                        category = "Old_maps_of_Turku";
                        break;
                    default:
                        category = "Old_maps_of_cities_in_Finland";
                }

                var url = "https://commons.wikimedia.org/w/api.php?action=query" +
                    "&list=categorymembers&cmtype=file&cmtitle=Category:" + category +
                    "&format=json" +
                    "&callback=callback";

                jsonp(url, null, (error, data) => {
                    if (error) {
                        console.log(error);
                        reject(error);
                    } else {
                        //console.log(data);

                        var tasks = [];

                        for (var i = 0; i < data.query.categorymembers.length; i++) {
                            var fileName = data.query.categorymembers[i].title;

                            var task = createGetCommonsMapInfoTask(fileName);
                            tasks.push(task);
                        }

                        Promise.all(tasks).then(allInfo => {
                            //console.log(allInfo);

                            var maps = params.maps;

                            for (var i = 0; i < allInfo.length; i++) {
                                var info = Object.values(allInfo[i].query.pages)[0];
                                //console.log(info);

                                var imageURL = info.imageinfo[0].url;
                                var thumbURL = info.imageinfo[0].thumburl;
                                var parts = imageURL.split('.');
                                var imageExtension = parts[parts.length - 1].toLowerCase();
                                if (imageExtension == 'bmp' || imageExtension == 'jpg' || imageExtension == 'jpeg' || imageExtension == 'png' || imageExtension == 'gif') {
                                    // nothing to do
                                }
                                else {
                                    imageURL = "/static/pngs/map_placeholder.png";
                                }

                                var map = {
                                    id: info.pageid,
                                    title: info.title,
                                    geoLocations: [],
                                    images: [info.imageinfo[0].url],
                                    imageURL: imageURL,
                                    thumbURL: thumbURL,
                                    year: (info.imageinfo[0].extmetadata.DateTimeOriginal != undefined ? info.imageinfo[0].extmetadata.DateTimeOriginal.value : undefined),
                                    publisher: (info.imageinfo[0].extmetadata.Credit != undefined ? info.imageinfo[0].extmetadata.Credit.value.replace(/<\/?[^>]+(>|$)/g, "") : undefined),
                                    authors: (info.imageinfo[0].extmetadata.Artist != undefined ? info.imageinfo[0].extmetadata.Artist.value.replace(/<\/?[^>]+(>|$)/g, "") : undefined),
                                    institutions: (info.imageinfo[0].extmetadata.Credit != undefined ? info.imageinfo[0].extmetadata.Credit.value.replace(/<\/?[^>]+(>|$)/g, "") : undefined),
                                    license: (info.imageinfo[0].extmetadata.LicenseShortName != undefined ? info.imageinfo[0].extmetadata.LicenseShortName.value : undefined),
                                    summary: (info.imageinfo[0].extmetadata.ImageDescription != undefined ? info.imageinfo[0].extmetadata.ImageDescription.value : undefined),
                                    source: "Wikimedia Commons",
                                    infoURL: info.imageinfo[0].descriptionurl
                                }
                                maps.push(map);
                            }
                        
                            context.commit('setHistoricalMaps', maps);
                            resolve(maps);
                        });
                    }
                });
            });
        },
        async getHistoricalMapsFromFinna(context, params) {
            return new Promise((resolve, reject) => {

                //resolve(params.maps);
                //return;

                var requestConfig = {
                    baseURL: "https://api.finna.fi/",
                    url: "/v1/search",
                    method: "get",
                    params: {
                        limit: 50,
                        type: "AllFields",
                        "filter[0]": '~format:"0/Map/"',
                        "filter[1]": 'online_boolean:"1"',
                        // "filter[2]": '{!bbox sfield=location_geo pt=' +
                        // params.locationParams.centerLon + ',' + params.locationParams.centerLat + ' d=' + params.locationParams.distance + '}',
                        // "filter[2]": '~geographic_facet:"' + params.locationParams.municipality + '"',
                        "field[0]": 'id',
                        "field[1]": 'title',
                        "field[2]": 'geoLocations',
                        "field[3]": 'images',
                        "field[4]": 'year',
                        "field[5]": 'publisher',
                        "field[6]": 'authors',
                        "field[7]": 'institutions',
                        "field[8]": 'events',
                        "field[9]": 'imageRights',
                        "field[10]": 'summary',
                        "field[11]": 'onlineUrls',
                        "field[12]": 'nonPresenterAuthors',
                    }
                };
                axios.request(requestConfig).
                    then(function (response) {
                        //console.log(response);

                        var maps = params.maps;

                        if (response.data.resultCount > 0) {
                            for (var i = 0; i < response.data.records.length; i++) {
                                var record = response.data.records[i];
                                if (record.images != undefined && record.images.length > 0) {
                                    var images = [];
                                    for (var j = 0; j < record.images.length; j++) {
                                        images.push("https://api.finna.fi" + record.images[j]);
                                    }

                                    var authors = "";
                                    if (record.authors != undefined) {
                                        for (var author in record.authors) {
                                            if (record.authors.hasOwnProperty(author)) {
                                                //console.log(author);
                                                for (var key in record.authors[author]) {
                                                    //console.log(key);
                                                    if (record.authors[author].hasOwnProperty(key)) {
                                                        authors += key + ", ";
                                                    }
                                                }
                                            }
                                        }
                                        authors = authors.slice(0, -2);
                                    }

                                    var institutions = "";
                                    if (record.institutions != undefined) {
                                        for (var j = 0; j < record.institutions.length; j++) {
                                            institutions += record.institutions[j].translated + ', ';
                                        }

                                        institutions = institutions.slice(0, -2);
                                    }

                                    //console.log(images);
                                    var map = {
                                        id: record.id,
                                        title: record.title,
                                        //geoLocations: record.geoLocations,
                                        images: images,
                                        thumbURL: images[0],
                                        imageURL: images[0],
                                        year: record.year,
                                        publisher: record.publisher,
                                        authors: authors,
                                        institutions: institutions,
                                        events: record.events,
                                        imageRights: (record.imageRights != undefined ? record.imageRights.copyright : undefined),
                                        license: record.imageRights.copyright,
                                        summary: record.summary,
                                        source: "Finna",
                                        infoURL: "https://www.finna.fi/Record/" + encodeURIComponent(record.id)
                                    }

                                    maps.push(map);
                                }
                                else { // Handle Doria Fennica maps differently
                                    if (record.onlineUrls != undefined &&
                                        record.onlineUrls[0].source != undefined && 
                                        record.onlineUrls[0].source.value != undefined &&
                                        record.onlineUrls[0].source.value == "fennica" &&
                                        record.onlineUrls[0].url != undefined
                                    ) {
                                        var images = [];
                                        images.push("/static/pngs/map_placeholder.png");
                                        //images.push("/static/wikifont/svgs/uniE032 - map.svg");

                                        var authors = "";
                                        if (record.nonPresenterAuthors != undefined && record.nonPresenterAuthors[0].name != undefined) {
                                            authors = record.nonPresenterAuthors[0].name
                                        }

                                        var institutions = "";
                                        if (record.institutions != undefined) {
                                            for (var j = 0; j < record.institutions.length; j++) {
                                                institutions += record.institutions[j].translated + ', ';
                                            }

                                            institutions = institutions.slice(0, -2);
                                        }

                                        var map = {
                                            id: record.id,
                                            title: record.title,
                                            year: record.year,
                                            authors: authors,
                                            institutions: institutions,
                                            infoURL: record.onlineUrls[0].url,
                                            images: images,
                                            thumbURL: images[0],
                                            imageURL: images[0],
                                            source: "finna_fennica",
                                            license: "Ks. lähde"
                                        }
        
                                        maps.push(map);
                                    }
                                }
                            }
                        }

                        context.commit('setHistoricalMaps', maps);
                        resolve(maps);
                    })
                    .catch(function (error) {
                        console.log(error);

                        reject(error);

                    });
            });
        },
        async getNearbyPlaces(context, params) {
            var requestConfig = {
                baseURL: BASE_URL,
                url: "/wiki/items/by/latlon",
                method: "get",
                params: params
            };

            axios.request(requestConfig).
                then(function (response) {
                    //console.log(response.data);

                    context.commit('setNearbyWikiItems', response.data);

                }).catch(function (error) {
                    console.log(error);
                });
        }
    }
});

function isWikidataBasedUnchangableStartYear(wikidocumentaries) {

    var isUnchangable = false;

    // See also https://www.wikidata.org/w/index.php?title=Special:ListProperties/time
    var unchangableStartYearProperties = [
        'P569', // date of birth
        'P580', // start time
    ];

    if (wikidocumentaries.wikidata != undefined) {
        var wikidataDates = wikidocumentaries.wikidata.dates;
        for (var i = 0; i < wikidataDates.length; i++) {
            if (unchangableStartYearProperties.indexOf(wikidataDates[i].wikidata_property) != -1) {
                isUnchangable = true;
                break;
            }
        }
    }

    return isUnchangable;
}

function calculateTopicStartYearFromWikidata(wikidata, currentStartYear) {
    // See also https://www.wikidata.org/wiki/Help:Dates and
    // https://www.wikidata.org/w/index.php?title=Special:ListProperties/time&limit=50&offset=0

    var minYear = (currentStartYear != null ? currentStartYear : (new Date()).getFullYear());

    wikidata.dates.forEach(dateItem => {

        //console.log(dateItem);

        var timeString = dateItem.value.time;

        var year = parseInt((timeString.indexOf('-') != 0 ? timeString.substring(1, timeString.indexOf('-')) : timeString.substring(0, timeString.indexOf('-', 1))), 10);

        if (year < minYear) {
            minYear = year;
        }

        // if (mainsnak.property == 'P569') {
        //     dateItem.type = "date_of_birth";
        // }
        // else if (mainsnak.property == 'P570') {
        //     dateItem.type = "date_of_death";
        // }
        // else if (mainsnak.property == 'P571') {
        //     dateItem.type = "inception"; // date founded
        // }
    });

    return minYear;
}

function createGetCommonsMapInfoTask(fileName) {
    return new Promise((resolve, reject) => {
                                
        var url = "https://commons.wikimedia.org/w/api.php?" +
            "action=query" +
            "&titles=" + fileName +
            "&prop=imageinfo" +
            "&iiprop=user|url|extmetadata" +
            "&iiurlwidth=400" +
            "&iiurlheight=400" +
            "&redirects=resolve" +
            "&format=json" +
            "&callback=callback";

        jsonp(url, null, (error, data) => {
            if (error) {
                console.log(error);
                reject(error);
            } else {
                //console.log(data);
                resolve(data);
            }
        });
    });
}