src/directives/glImage.js
angular.module('mapboxgl-directive').directive('glImage', ['mapboxglImageUtils', function (mapboxglImageUtils) {
function mapboxGlImageDirectiveLink (scope, element, attrs, controller) {
if (!controller) {
throw new Error('Invalid angular-mapboxgl-directive controller');
}
var mapboxglScope = controller.getMapboxGlScope();
var imagenWatched = function (map, controller, image) {
if (angular.isDefined(image)) {
if (Object.prototype.toString.call(image) === Object.prototype.toString.call({})) {
mapboxglImageUtils.createImageByObject(map, image);
controller.addImageObject(image);
} else if (Object.prototype.toString.call(image) === Object.prototype.toString.call([])) {
image.map(function (eachImage) {
mapboxglImageUtils.createImageByObject(map, eachImage);
controller.addImageObject(eachImage);
});
} else {
throw new Error('Invalid image parameter');
}
}
};
scope.$on('mapboxglMap:styleChanged', function () {
});
controller.getMap().then(function (map) {
mapboxglScope.$watchCollection('glImage', function (image) {
if (map.style.loaded()) {
imagenWatched(map, controller, image);
} else {
map.once('style.load', function () {
imagenWatched(map, controller, image);
});
}
});
});
}
var directive = {
restrict: 'A',
scope: false,
replace: false,
require: '?^mapboxgl',
link: mapboxGlImageDirectiveLink
};
return directive;
}]);