Skip to content

Commit

Permalink
Add content item resolver filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarnef committed Nov 24, 2023
1 parent 7bad6b9 commit b1d06c0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(function () {
"use strict";

function contentItemResolverFilterService(contentResource, eventsService) {

var contentKeysRequest = [];
var contentItemCache = [];

var service = {

getByKey: function (key) {
// Is it cached, then get that:
const cachedcontentItem = contentItemCache.find(cache => key === cache.key);
if (cachedcontentItem) {
return cachedcontentItem;
}

// check its not already being loaded, and then start loading:
if (contentKeysRequest.indexOf(key) === -1) {
contentKeysRequest.push(key);
contentResource.getById(key).then(contentItem => {
if (contentItem) {
contentItemCache.push(contentItem);
}
});
}

return null;
}
};

eventsService.on("content.saved", function (name, args) {
const index = contentItemCache.findIndex(cache => cache.key === args.content.key);
if (index !== -1) {
contentItemCache[index] = args.content;
}
});

return service;

}

angular.module("umbraco.filters").factory("contentItemResolverFilterService", contentItemResolverFilterService);


// Filter loads content Item Model from a content Key.
// Usage: {{ mycontentProperty[0].contentKey | contentItemResolver }}
angular.module("umbraco.filters").filter("contentItemResolver", function (contentItemResolverFilterService) {

contentItemResolverFilter.$stateful = true;
function contentItemResolverFilter(input) {

// Check we have a value at all
if (typeof input === 'string' && input.length > 0) {
return contentItemResolverFilterService.getByKey(input);
}

return null;
}

return contentItemResolverFilter;

});

})();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function () {
(function () {
"use strict";

function mediaItemResolverFilterService(mediaResource, eventsService) {
Expand All @@ -11,15 +11,15 @@
getByKey: function (key) {
// Is it cached, then get that:
const cachedMediaItem = mediaItemCache.find(cache => key === cache.key);
if(cachedMediaItem) {
if (cachedMediaItem) {
return cachedMediaItem;
}

// check its not already being loaded, and then start loading:
if(mediaKeysRequest.indexOf(key) === -1) {
if (mediaKeysRequest.indexOf(key) === -1) {
mediaKeysRequest.push(key);
mediaResource.getById(key).then(function (mediaItem) {
if(mediaItem) {
mediaResource.getById(key).then(mediaItem => {
if (mediaItem) {
mediaItemCache.push(mediaItem);
}
});
Expand All @@ -31,7 +31,7 @@

eventsService.on("editors.media.saved", function (name, args) {
const index = mediaItemCache.findIndex(cache => cache.key === args.media.key);
if(index !== -1) {
if (index !== -1) {
mediaItemCache[index] = args.media;
}
});
Expand Down Expand Up @@ -62,4 +62,4 @@

});

})();
})();

0 comments on commit b1d06c0

Please sign in to comment.