cullmann.io/themes/CodeIT/assets/lib/mapbox-gl/mapbox-gl-language.js

306 lines
8.7 KiB
JavaScript
Raw Normal View History

2021-02-18 21:44:01 +01:00
/**
* Create a new [Mapbox GL JS plugin](https://www.mapbox.com/blog/build-mapbox-gl-js-plugins/) that
* modifies the layers of the map style to use the 'text-field' that matches the browser language.
* @constructor
* @param {object} options - Options to configure the plugin.
* @param {string[]} [options.supportedLanguages] - List of supported languages
* @param {Function} [options.languageTransform] - Custom style transformation to apply
* @param {RegExp} [options.languageField=/^\{name/] - RegExp to match if a text-field is a language field
* @param {Function} [options.getLanguageField] - Given a language choose the field in the vector tiles
* @param {string} [options.languageSource] - Name of the source that contains the different languages.
* @param {string} [options.defaultLanguage] - Name of the default language to initialize style after loading.
* @param {string[]} [options.excludedLayerIds] - Name of the layers that should be excluded from translation.
*/
function MapboxLanguage(options) {
options = Object.assign({}, options);
if (!(this instanceof MapboxLanguage)) {
2021-02-18 22:21:36 +01:00
throw new Error("MapboxLanguage needs to be called with the new keyword");
2021-02-18 21:44:01 +01:00
}
this.setLanguage = this.setLanguage.bind(this);
this._initialStyleUpdate = this._initialStyleUpdate.bind(this);
this._defaultLanguage = options.defaultLanguage;
this._isLanguageField = options.languageField || /^\{name/;
2021-02-18 22:21:36 +01:00
this._getLanguageField =
options.getLanguageField ||
function nameField(language) {
return language === "mul" ? "{name}" : "{name_" + language + "}";
};
2021-02-18 21:44:01 +01:00
this._languageSource = options.languageSource || null;
2021-02-18 22:21:36 +01:00
this._languageTransform =
options.languageTransform ||
function (style, language) {
if (language === "ar") {
return noSpacing(style);
} else {
return standardSpacing(style);
}
};
2021-02-18 21:44:01 +01:00
this._excludedLayerIds = options.excludedLayerIds || [];
2021-02-18 22:21:36 +01:00
this.supportedLanguages = options.supportedLanguages || [
"ar",
"en",
"es",
"fr",
"de",
"ja",
"ko",
"mul",
"pt",
"ru",
"zh",
];
2021-02-18 21:44:01 +01:00
}
function standardSpacing(style) {
var changedLayers = style.layers.map(function (layer) {
2021-02-18 22:21:36 +01:00
if (!(layer.layout || {})["text-field"]) return layer;
2021-02-18 21:44:01 +01:00
var spacing = 0;
2021-02-18 22:21:36 +01:00
if (layer["source-layer"] === "state_label") {
2021-02-18 21:44:01 +01:00
spacing = 0.15;
}
2021-02-18 22:21:36 +01:00
if (layer["source-layer"] === "marine_label") {
2021-02-18 21:44:01 +01:00
if (/-lg/.test(layer.id)) {
spacing = 0.25;
}
if (/-md/.test(layer.id)) {
spacing = 0.15;
}
if (/-sm/.test(layer.id)) {
spacing = 0.1;
}
}
2021-02-18 22:21:36 +01:00
if (layer["source-layer"] === "place_label") {
2021-02-18 21:44:01 +01:00
if (/-suburb/.test(layer.id)) {
spacing = 0.15;
}
if (/-neighbour/.test(layer.id)) {
spacing = 0.1;
}
if (/-islet/.test(layer.id)) {
spacing = 0.01;
}
}
2021-02-18 22:21:36 +01:00
if (layer["source-layer"] === "airport_label") {
2021-02-18 21:44:01 +01:00
spacing = 0.01;
}
2021-02-18 22:21:36 +01:00
if (layer["source-layer"] === "rail_station_label") {
2021-02-18 21:44:01 +01:00
spacing = 0.01;
}
2021-02-18 22:21:36 +01:00
if (layer["source-layer"] === "poi_label") {
2021-02-18 21:44:01 +01:00
if (/-scalerank/.test(layer.id)) {
spacing = 0.01;
}
}
2021-02-18 22:21:36 +01:00
if (layer["source-layer"] === "road_label") {
2021-02-18 21:44:01 +01:00
if (/-label-/.test(layer.id)) {
spacing = 0.01;
}
if (/-shields/.test(layer.id)) {
spacing = 0.05;
}
}
return Object.assign({}, layer, {
layout: Object.assign({}, layer.layout, {
2021-02-18 22:21:36 +01:00
"text-letter-spacing": spacing,
}),
2021-02-18 21:44:01 +01:00
});
});
return Object.assign({}, style, {
2021-02-18 22:21:36 +01:00
layers: changedLayers,
2021-02-18 21:44:01 +01:00
});
}
function noSpacing(style) {
var changedLayers = style.layers.map(function (layer) {
2021-02-18 22:21:36 +01:00
if (!(layer.layout || {})["text-field"]) return layer;
2021-02-18 21:44:01 +01:00
var spacing = 0;
return Object.assign({}, layer, {
layout: Object.assign({}, layer.layout, {
2021-02-18 22:21:36 +01:00
"text-letter-spacing": spacing,
}),
2021-02-18 21:44:01 +01:00
});
});
return Object.assign({}, style, {
2021-02-18 22:21:36 +01:00
layers: changedLayers,
2021-02-18 21:44:01 +01:00
});
}
function isNameStringField(isLangField, property) {
2021-02-18 22:21:36 +01:00
return typeof property === "string" && isLangField.test(property);
2021-02-18 21:44:01 +01:00
}
function isNameFunctionField(isLangField, property) {
2021-02-18 22:21:36 +01:00
return (
property.stops &&
property.stops.filter(function (stop) {
return isLangField.test(stop[1]);
}).length > 0
);
2021-02-18 21:44:01 +01:00
}
function adaptPropertyLanguage(isLangField, property, languageFieldName) {
if (isNameStringField(isLangField, property)) return languageFieldName;
if (isNameFunctionField(isLangField, property)) {
var newStops = property.stops.map(function (stop) {
if (isLangField.test(stop[1])) {
return [stop[0], languageFieldName];
}
return stop;
});
return Object.assign({}, property, {
2021-02-18 22:21:36 +01:00
stops: newStops,
2021-02-18 21:44:01 +01:00
});
}
return property;
}
2021-02-18 22:21:36 +01:00
function changeLayerTextProperty(
isLangField,
layer,
languageFieldName,
excludedLayerIds
) {
if (
layer.layout &&
layer.layout["text-field"] &&
excludedLayerIds.indexOf(layer.id) === -1
) {
2021-02-18 21:44:01 +01:00
return Object.assign({}, layer, {
layout: Object.assign({}, layer.layout, {
2021-02-18 22:21:36 +01:00
"text-field": adaptPropertyLanguage(
isLangField,
layer.layout["text-field"],
languageFieldName
),
}),
2021-02-18 21:44:01 +01:00
});
}
return layer;
}
function findStreetsSource(style) {
var sources = Object.keys(style.sources).filter(function (sourceName) {
var source = style.sources[sourceName];
return /mapbox-streets-v\d/.test(source.url);
});
return sources[0];
}
/**
* Explicitly change the language for a style.
* @param {object} style - Mapbox GL style to modify
* @param {string} language - The language iso code
* @returns {object} the modified style
*/
MapboxLanguage.prototype.setLanguage = function (style, language) {
2021-02-18 22:21:36 +01:00
if (this.supportedLanguages.indexOf(language) < 0)
throw new Error("Language " + language + " is not supported");
2021-02-18 21:44:01 +01:00
var streetsSource = this._languageSource || findStreetsSource(style);
if (!streetsSource) return style;
var field = this._getLanguageField(language);
var isLangField = this._isLanguageField;
var excludedLayerIds = this._excludedLayerIds;
var changedLayers = style.layers.map(function (layer) {
2021-02-18 22:21:36 +01:00
if (layer.source === streetsSource)
return changeLayerTextProperty(
isLangField,
layer,
field,
excludedLayerIds
);
2021-02-18 21:44:01 +01:00
return layer;
});
var languageStyle = Object.assign({}, style, {
2021-02-18 22:21:36 +01:00
layers: changedLayers,
2021-02-18 21:44:01 +01:00
});
return this._languageTransform(languageStyle, language);
};
MapboxLanguage.prototype._initialStyleUpdate = function () {
var style = this._map.getStyle();
2021-02-18 22:21:36 +01:00
var language =
this._defaultLanguage || browserLanguage(this.supportedLanguages);
2021-02-18 21:44:01 +01:00
// We only update the style once
2021-02-18 22:21:36 +01:00
this._map.off("styledata", this._initialStyleUpdate);
2021-02-18 21:44:01 +01:00
this._map.setStyle(this.setLanguage(style, language));
};
function browserLanguage(supportedLanguages) {
2021-02-18 22:21:36 +01:00
var language = navigator.languages
? navigator.languages[0]
: navigator.language || navigator.userLanguage;
var parts = language.split("-");
2021-02-18 21:44:01 +01:00
var languageCode = language;
if (parts.length > 1) {
languageCode = parts[0];
}
if (supportedLanguages.indexOf(languageCode) > -1) {
return languageCode;
}
return null;
}
MapboxLanguage.prototype.onAdd = function (map) {
this._map = map;
2021-02-18 22:21:36 +01:00
this._map.on("styledata", this._initialStyleUpdate);
this._container = document.createElement("div");
2021-02-18 21:44:01 +01:00
return this._container;
};
MapboxLanguage.prototype.onRemove = function () {
2021-02-18 22:21:36 +01:00
this._map.off("styledata", this._initialStyleUpdate);
2021-02-18 21:44:01 +01:00
this._map = undefined;
};
function ie11Polyfill() {
2021-02-18 22:21:36 +01:00
if (typeof Object.assign != "function") {
2021-02-18 21:44:01 +01:00
// Must be writable: true, enumerable: false, configurable: true
2021-02-18 22:21:36 +01:00
Object.defineProperty(Object, "assign", {
2021-02-18 21:44:01 +01:00
// eslint-disable-next-line no-unused-vars
2021-02-18 22:21:36 +01:00
value: function assign(target, varArgs) {
// .length of function is 2
2021-02-18 21:44:01 +01:00
// eslint-disable-next-line strict
2021-02-18 22:21:36 +01:00
"use strict";
if (target === null) {
// TypeError if undefined or null
throw new TypeError("Cannot convert undefined or null to object");
2021-02-18 21:44:01 +01:00
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
2021-02-18 22:21:36 +01:00
if (nextSource !== null) {
// Skip over if undefined or null
2021-02-18 21:44:01 +01:00
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
},
writable: true,
2021-02-18 22:21:36 +01:00
configurable: true,
2021-02-18 21:44:01 +01:00
});
}
}
2021-02-18 22:21:36 +01:00
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
2021-02-18 21:44:01 +01:00
module.exports = MapboxLanguage;
} else {
ie11Polyfill();
window.MapboxLanguage = MapboxLanguage;
}