Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

[Prototype] EZP-26309: Prototype the usage of REST Embedding in PlatformUI #687

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Resources/config/yui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
37 changes: 24 additions & 13 deletions Resources/public/js/models/extensions/ez-contentinfo-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: response.document.VersionList.VersionItem.forEach() is a better, native way of doing it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the nitpicking, but what about the core feature/implementation ? (In case, you missed it, that's only a Prototype)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dpobel I'm going to check it ASAP. Very interesting approach.

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);
}
},

/**
Expand All @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions Resources/public/js/models/ez-restmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Object.keys() works natively everywhere

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;
}
Expand Down Expand Up @@ -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']
Expand Down
52 changes: 33 additions & 19 deletions Resources/public/js/models/ez-usermodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand All @@ -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",
Expand Down
31 changes: 30 additions & 1 deletion Resources/public/js/models/ez-versioninfomodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
/**
Expand Down Expand Up @@ -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;
}
},
}
});
});
41 changes: 4 additions & 37 deletions Resources/public/js/views/services/plugins/ez-userdraftsplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ YUI.add('ez-versionsplugin', function (Y) {
var service = this.get('host'),
capi = service.get('capi'),
options = {
preloadCreator: true,
api: capi,
};

Expand Down
6 changes: 3 additions & 3 deletions Resources/public/templates/tabs/versions.hbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{{languageCodes}}
</td>
<td>
{{resources.Creator}}
{{creator.name}}
</td>
<td>
{{creationDate}}
Expand Down Expand Up @@ -99,7 +99,7 @@
{{languageCodes}}
</td>
<td>
{{resources.Creator}}
{{creator.name}}
</td>
<td>
{{creationDate}}
Expand Down Expand Up @@ -150,7 +150,7 @@
{{languageCodes}}
</td>
<td>
{{resources.Creator}}
{{creator.name}}
</td>
<td>
{{creationDate}}
Expand Down