From 367b2577c89f997b1702ef8d7443affee67178ee Mon Sep 17 00:00:00 2001 From: Justin Merz Date: Wed, 10 Mar 2021 09:53:34 -0800 Subject: [PATCH] adding FcAppConfigModel, #76 --- .../public/elements/pages/home/app-home.js | 3 + .../ucd-lib-client/client/public/lib/index.js | 5 +- .../public/lib/models/FcAppConfigModel.js | 71 +++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 services/ucd-lib-client/client/public/lib/models/FcAppConfigModel.js diff --git a/services/ucd-lib-client/client/public/elements/pages/home/app-home.js b/services/ucd-lib-client/client/public/elements/pages/home/app-home.js index 3457fed..97c3f6e 100644 --- a/services/ucd-lib-client/client/public/elements/pages/home/app-home.js +++ b/services/ucd-lib-client/client/public/elements/pages/home/app-home.js @@ -44,6 +44,9 @@ class AppHome extends Mixin(PolymerElement) constructor() { super(); this.active = true; + + this._injectModel('FcAppConfigModel'); + console.log(this.FcAppConfigModel.getFeaturedImages()); } /** diff --git a/services/ucd-lib-client/client/public/lib/index.js b/services/ucd-lib-client/client/public/lib/index.js index 704b6d4..e5f254b 100644 --- a/services/ucd-lib-client/client/public/lib/index.js +++ b/services/ucd-lib-client/client/public/lib/index.js @@ -6,5 +6,6 @@ module.exports = { MediaModel : require('./models/MediaModel'), CitationModel : require('./models/CitationsModel'), SeoModel : require('./models/SeoModel'), - FiltersModel : require('./models/FiltersModel') -} \ No newline at end of file + FiltersModel : require('./models/FiltersModel'), + FcAppConfigModel : require('./models/FcAppConfigModel') +}; \ No newline at end of file diff --git a/services/ucd-lib-client/client/public/lib/models/FcAppConfigModel.js b/services/ucd-lib-client/client/public/lib/models/FcAppConfigModel.js new file mode 100644 index 0000000..009651e --- /dev/null +++ b/services/ucd-lib-client/client/public/lib/models/FcAppConfigModel.js @@ -0,0 +1,71 @@ +const {BaseModel} = require('@ucd-lib/cork-app-utils'); + +/** + * @class FcAppConfigModel + * @description a wrapper class around APP_CONFIG.fcAppConfig which contains + * the /application/ucd-lib-client graph from fcrepo. Adds nice accessor + * methods + */ +class FcAppConfigModel extends BaseModel { + + constructor() { + super(); + + this.TYPES = { + APPLICATION_CONTAINER : 'http://digital.ucdavis.edu/schema#ApplicationContainer' + } + + this.byId = {}; + (APP_CONFIG.fcAppConfig || []).forEach(item => this.byId[item['@id']] = item); + + this.register('FcAppConfigModel'); + } + + /** + * @method getFeaturedCollections + * @description return any defined featured collections + * + * @returns {Array} + */ + getFeaturedCollections() { + let appContainer = this.getApplicationContainer(); + return asArray(appContainer.featuredCollection) + .map(item => { + return this.byId[item['@id']]; + }); + } + + /** + * @method getFeaturedImages + * @description return any defined featured images + * + * @returns {Array} + */ + getFeaturedImages() { + let appContainer = this.getApplicationContainer(); + return asArray(appContainer.featuredImage) + .map(item => { + return this.byId[item['@id']]; + }); + } + + /** + * @method getApplicationContainer + * @description return the main application container from the app graph + * + * @returns {Object} + */ + getApplicationContainer() { + return (APP_CONFIG.fcAppConfig || []) + .find(item => item['@type'].includes(this.TYPES.APPLICATION_CONTAINER)); + } + +} + +function asArray(val) { + if( val === undefined ) return []; + if( Array.isArray(val) ) return val; + return [val]; +} + +module.exports = new FcAppConfigModel(); \ No newline at end of file