diff --git a/Resources/config/yui.yml b/Resources/config/yui.yml index 55f4d2489..8185ae9ab 100644 --- a/Resources/config/yui.yml +++ b/Resources/config/yui.yml @@ -1074,7 +1074,7 @@ system: requires: ['ez-versioninfomodel'] path: "%ez_platformui.public_dir%/js/models/ez-versionmodel.js" ez-versioninfomodel: - requires: ['ez-restmodel'] + requires: ['ez-restmodel', 'ez-contentinfomodel', 'ez-usermodel'] path: '%ez_platformui.public_dir%/js/models/ez-versioninfomodel.js' ez-locationmodel: requires: ['ez-restmodel', 'ez-contentinfomodel', array-extras] diff --git a/Resources/public/js/models/extensions/ez-contentinfo-base.js b/Resources/public/js/models/extensions/ez-contentinfo-base.js index 79dacd70a..fb91d8f93 100644 --- a/Resources/public/js/models/extensions/ez-contentinfo-base.js +++ b/Resources/public/js/models/extensions/ez-contentinfo-base.js @@ -25,26 +25,36 @@ YUI.add('ez-contentinfo-base', function (Y) { * @method loadVersions * @param {Object} options * @param {Object} options.api (required) the JS REST client instance + * @param {Boolean} [options.preloadCreator] * @param {Function} callback */ loadVersions: function (options, callback) { var versions = [], - contentService = options.api.getContentService(); + contentService = options.api.getContentService(), + cb = function (error, response) { + if (error) { + callback(error, response); + return; + } - contentService.loadVersions(this.get('id'), function (error, response) { - if (error) { - callback(error, response); - return; - } + Y.Array.each(response.document.VersionList.VersionItem, function (versionItemHash) { + var versionInfo = new Y.eZ.VersionInfo(); + versionInfo.loadFromHash(versionItemHash); + versions.push(versionInfo); + }); - Y.Array.each(response.document.VersionList.VersionItem, function (versionItemHash) { - var versionInfo = new Y.eZ.VersionInfo(); - versionInfo.loadFromHash(versionItemHash); - versions.push(versionInfo); - }); + callback(error, versions); + }; - callback(error, versions); - }); + if ( options.preloadCreator ) { + contentService.loadVersions( + this.get('id'), + {'x-ez-embed-value': 'VersionList.VersionItem.VersionInfo.Creator'}, + cb + ); + } else { + contentService.loadVersions(this.get('id'), cb); + } }, /** @@ -53,6 +63,7 @@ YUI.add('ez-contentinfo-base', function (Y) { * @method loadVersionsSortedByStatus * @param {Object} options * @param {Object} options.api (required) the JS REST client instance + * @param {Boolean} [options.preloadCreator] * @param {Function} callback */ loadVersionsSortedByStatus: function (options, callback) { diff --git a/Resources/public/js/models/ez-restmodel.js b/Resources/public/js/models/ez-restmodel.js index 3e6fa01b2..5649ba798 100644 --- a/Resources/public/js/models/ez-restmodel.js +++ b/Resources/public/js/models/ez-restmodel.js @@ -134,6 +134,17 @@ YUI.add('ez-restmodel', function (Y) { } }); Y.Array.each(this.constructor.LINKS_MAP, function (item) { + var attrName, linkName; + + if ( L.isObject(item) ) { + linkName = Y.Object.keys(item)[0]; + attrName = item[linkName]; + item = linkName; + // TODO: this should not only be trigger by the resources + // being represented as an object but also if the resources + // is embedded. + attrs[attrName] = struct[linkName]; + } if ( struct[item] ) { links[item] = struct[item]._href; } @@ -264,6 +275,7 @@ YUI.add('ez-restmodel', function (Y) { /** * Array of linked resources to parse and make available * in the resources attribute + * TODO document REST Embedding usage * Example: * * ['Owner', 'MainLocation'] diff --git a/Resources/public/js/models/ez-usermodel.js b/Resources/public/js/models/ez-usermodel.js index 6a6bb8306..cf29ae700 100644 --- a/Resources/public/js/models/ez-usermodel.js +++ b/Resources/public/js/models/ez-usermodel.js @@ -63,14 +63,18 @@ YUI.add('ez-usermodel', function (Y) { * @param {Object} the response document */ _parseStruct: function (struct, responseDoc) { - var attrs, imageField, - imageIdentifiers = responseDoc._contentType.getFieldDefinitionIdentifiers('ezimage'); + var attrs, imageField, imageIdentifiers = []; attrs = this.constructor.superclass._parseStruct.call(this, struct); - imageField = struct.Version.Fields.field.filter(function (field) { - return field.fieldDefinitionIdentifier == imageIdentifiers[0]; - }); - attrs.avatar = imageField.length ? imageField[0].fieldValue : null; + if ( responseDoc._contentType ) { + imageIdentifiers = responseDoc._contentType.getFieldDefinitionIdentifiers('ezimage'); + } + if ( imageIdentifiers.length ) { + imageField = struct.Version.Fields.field.filter(function (field) { + return field.fieldDefinitionIdentifier == imageIdentifiers[0]; + }); + attrs.avatar = imageField.length ? imageField[0].fieldValue : null; + } return attrs; }, @@ -80,27 +84,37 @@ YUI.add('ez-usermodel', function (Y) { * @method loadDrafts * @param options {Object} * @param options.api {Object} (required) the JS REST client instance + * @param [options.preloadContentInfo] boolean * @param callback {Function} function to call after processing response */ loadDrafts: function (options, callback) { - options.api.getContentService().loadUserDrafts(this.get('id'), function (error, response) { - var versions = []; + var cb = function (error, response) { + var versions = []; - if (error) { - callback(error, response); + if (error) { + callback(error, response); - return; - } + return; + } - response.document.VersionList.VersionItem.forEach(function (versionItemHash) { - var versionInfo = new Y.eZ.VersionInfo(); + response.document.VersionList.VersionItem.forEach(function (versionItemHash) { + var versionInfo = new Y.eZ.VersionInfo(); - versionInfo.loadFromHash(versionItemHash); - versions.push(versionInfo); - }); + versionInfo.loadFromHash(versionItemHash); + versions.push(versionInfo); + }); - callback(error, versions); - }); + callback(error, versions); + }; + if ( options.preloadContentInfo ) { + options.api.getContentService().loadUserDrafts( + this.get('id'), + {"x-ez-embed-value": "VersionList.VersionItem.VersionInfo.Content"}, + cb + ); + } else { + options.api.getContentService().loadUserDrafts(this.get('id'), cb); + } }, }, { REST_STRUCT_ROOT: "User", diff --git a/Resources/public/js/models/ez-versioninfomodel.js b/Resources/public/js/models/ez-versioninfomodel.js index aff41b702..1838bf0f9 100644 --- a/Resources/public/js/models/ez-versioninfomodel.js +++ b/Resources/public/js/models/ez-versioninfomodel.js @@ -131,7 +131,10 @@ YUI.add('ez-versioninfomodel', function (Y) { "creationDate", "modificationDate", "languageCodes", "initialLanguageCode", "names", ], - LINKS_MAP: ['Content', 'Creator'], + LINKS_MAP: [ + {'Content': 'contentInfo'}, + {'Creator': 'creator'}, + ], ATTRS: { /** @@ -224,6 +227,32 @@ YUI.add('ez-versioninfomodel', function (Y) { setter: '_setterLocalizedValue', value: {} }, + + contentInfo: { + getter: function (value) { + var contentInfo = new Y.eZ.ContentInfo(), + parseVal = {}; + + if ( value ) { + parseVal[Y.eZ.ContentInfo.REST_STRUCT_ROOT] = value; + contentInfo.setAttrs(contentInfo.parse({document: parseVal})); + } + return contentInfo; + } + }, + + creator: { + getter: function (value) { + var user = new Y.eZ.User(), + parseVal = {}; + + if ( value ) { + parseVal[Y.eZ.User.REST_STRUCT_ROOT] = value; + user.setAttrs(user.parse({document: parseVal})); + } + return user; + } + }, } }); }); diff --git a/Resources/public/js/views/services/plugins/ez-userdraftsplugin.js b/Resources/public/js/views/services/plugins/ez-userdraftsplugin.js index 734a2c3f3..ca0a60c4c 100644 --- a/Resources/public/js/views/services/plugins/ez-userdraftsplugin.js +++ b/Resources/public/js/views/services/plugins/ez-userdraftsplugin.js @@ -39,7 +39,7 @@ YUI.add('ez-userdraftsplugin', function (Y) { */ _loadUserDrafts: function (event) { var service = this.get('host'), - options = {api: service.get('capi')}, + options = {api: service.get('capi'), preloadContentInfo: true}, target = event.target; service.get('app').get('user').loadDrafts(options, Y.bind(function (error, versions) { @@ -87,9 +87,11 @@ YUI.add('ez-userdraftsplugin', function (Y) { * @return {Y.Promise} */ _collectDraftsData: function (target, attributeName, versions) { + versions.forEach(function (struct) { + struct.contentInfo = struct.version.get('contentInfo'); + }); Y.Promise .resolve(versions) - .then(Y.bind(this._loadDraftContentInfo, this)) .then(Y.bind(this._loadDraftContentType, this)) .then(function (versions) { var res = { @@ -104,41 +106,6 @@ YUI.add('ez-userdraftsplugin', function (Y) { }); }, - /** - * Prepares the loading of the ContentInfo corresponding to each - * Versions. - * - * @method _loadDraftContentInfo - * @protected - * @param versions {Array} list of version structures - * @return {Y.Promise} - */ - _loadDraftContentInfo: function (versions) { - var promises, - capi = this.get('host').get('capi'); - - promises = versions.map(function (versionStruct) { - return new Y.Promise(function (resolve, reject) { - var contentInfo = new Y.eZ.ContentInfo({ - id: versionStruct.version.get('resources').Content - }); - - contentInfo.load({api: capi}, function (error) { - if (error) { - reject(error); - - return; - } - - versionStruct.contentInfo = contentInfo; - resolve(versionStruct); - }); - }); - }); - - return Y.Promise.all(promises); - }, - /** * Prepares the loading of the Content Type corresponding to each * Versions. diff --git a/Resources/public/js/views/services/plugins/ez-versionsplugin.js b/Resources/public/js/views/services/plugins/ez-versionsplugin.js index 6c554c3cb..58b961508 100644 --- a/Resources/public/js/views/services/plugins/ez-versionsplugin.js +++ b/Resources/public/js/views/services/plugins/ez-versionsplugin.js @@ -57,6 +57,7 @@ YUI.add('ez-versionsplugin', function (Y) { var service = this.get('host'), capi = service.get('capi'), options = { + preloadCreator: true, api: capi, }; diff --git a/Resources/public/templates/tabs/versions.hbt b/Resources/public/templates/tabs/versions.hbt index 4b578bb0d..3da116541 100644 --- a/Resources/public/templates/tabs/versions.hbt +++ b/Resources/public/templates/tabs/versions.hbt @@ -39,7 +39,7 @@ {{languageCodes}} - {{resources.Creator}} + {{creator.name}} {{creationDate}} @@ -99,7 +99,7 @@ {{languageCodes}} - {{resources.Creator}} + {{creator.name}} {{creationDate}} @@ -150,7 +150,7 @@ {{languageCodes}} - {{resources.Creator}} + {{creator.name}} {{creationDate}}