From 42f3f3446be71c214d2caab8bd26ffc2d6e2ca21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 11 Apr 2018 23:50:45 +0200 Subject: [PATCH] EZP-28781: Content shouldn't be visible in ezobjectrelationlist after sending it to trash (#955) * EZP-28781: Content shouldn't be visible in ezobjectrelationlist after sending it to trash * fixup! EZP-28781: Content shouldn't be visible in ezobjectrelationlist after sending it to trash * Native bind() changed to Y.bind() --- .../views/fields/ez-relationlist-editview.js | 28 ++++++- .../js/views/fields/ez-relationlist-view.js | 14 +++- .../templates/fields/edit/relationlist.hbt | 74 ++++++++++--------- .../templates/fields/view/relationlist.hbt | 47 ++++++------ .../assets/ez-relation-editview-tests.js | 19 ++++- .../fields/assets/ez-relation-view-tests.js | 7 +- .../assets/ez-relationlist-editview-tests.js | 47 ++++++++++-- .../assets/ez-relationlist-view-tests.js | 22 ++++-- 8 files changed, 179 insertions(+), 79 deletions(-) diff --git a/Resources/public/js/views/fields/ez-relationlist-editview.js b/Resources/public/js/views/fields/ez-relationlist-editview.js index 145063914..eae045143 100644 --- a/Resources/public/js/views/fields/ez-relationlist-editview.js +++ b/Resources/public/js/views/fields/ez-relationlist-editview.js @@ -131,18 +131,38 @@ YUI.add('ez-relationlist-editview', function (Y) { var relatedContents = this.get('relatedContents'), relatedContentsJSON = []; - Y.Array.each(relatedContents, function (value) { - relatedContentsJSON.push(value.toJSON()); - }); + if (relatedContents !== null) { + relatedContentsJSON = relatedContents.reduce(Y.bind(function (total, value) { + var relatedContentJSON = value.toJSON(); + if (this._isNewRelation(value) || relatedContentJSON.resources.MainLocation) { + total.push(relatedContentJSON); + } + + return total; + }, this), []); + } return { - relatedContents: relatedContentsJSON, + relatedContents: relatedContentsJSON, loadingError: this.get('loadingError'), + isLoaded: relatedContents !== null, isEmpty: this._isFieldEmpty(), isRequired: this.get('fieldDefinition').isRequired, }; }, + /** + * Check if relation to the content is new (non-existed before edit) + * + * @method _isNewRelation + * @protected + * @param {eZ.ContentInfo|ez.eZ.Content} object + * @return {boolean} + */ + _isNewRelation: function (object) { + return object.name === 'contentInfoModel'; + }, + /** * Tap event handler for the remove relation buttons. * It remove the content related to the button from the relation list. diff --git a/Resources/public/js/views/fields/ez-relationlist-view.js b/Resources/public/js/views/fields/ez-relationlist-view.js index 9d55a85ee..c3199f13f 100644 --- a/Resources/public/js/views/fields/ez-relationlist-view.js +++ b/Resources/public/js/views/fields/ez-relationlist-view.js @@ -65,12 +65,20 @@ YUI.add('ez-relationlist-view', function (Y) { var relatedContents = this.get('relatedContents'), relatedContentsJSON = []; - Y.Array.each(relatedContents, function (value) { - relatedContentsJSON.push(value.toJSON()); - }); + if (relatedContents !== null) { + relatedContentsJSON = relatedContents.reduce(Y.bind(function (total, value) { + var relatedContentJSON = value.toJSON(); + if (relatedContentJSON.resources.MainLocation) { + total.push(relatedContentJSON); + } + + return total; + }, this), []); + } return { relatedContents: relatedContentsJSON, + isLoaded: relatedContents !== null, loadingError: this.get('loadingError'), }; }, diff --git a/Resources/public/templates/fields/edit/relationlist.hbt b/Resources/public/templates/fields/edit/relationlist.hbt index c8e240851..30c93ca78 100644 --- a/Resources/public/templates/fields/edit/relationlist.hbt +++ b/Resources/public/templates/fields/edit/relationlist.hbt @@ -19,44 +19,48 @@ {{#if isEmpty}}

{{translate "relationlist.empty" "fieldedit"}}

{{else}} - {{#if relatedContents }} - - - - - - - - - - - {{#each relatedContents}} - - - - - + {{#if isLoaded }} + {{#if relatedContents }} +
{{translate "relationlist.table.name" "fieldedit"}}{{translate "relationlist.table.published" "fieldedit"}}{{translate "relationlist.table.modified" "fieldedit"}}
{{ name }}{{ publishedDate }}{{ lastModificationDate }} - -
+ + + + + + - {{/each}} - -
{{translate "relationlist.table.name" "fieldedit"}}{{translate "relationlist.table.published" "fieldedit"}}{{translate "relationlist.table.modified" "fieldedit"}}
- {{else}} - {{#if loadingError}} -

- {{translate "relationlist.error" "fieldedit"}} - -

+ + + {{#each relatedContents}} + + {{ name }} + {{ publishedDate }} + {{ lastModificationDate }} + + + + + {{/each}} + + {{else}} -

- {{translate "relationlist.loading" "fieldedit"}} -

+ {{#if loadingError}} +

+ {{translate "relationlist.error" "fieldedit"}} + +

+ {{else}} +

{{translate "relationlist.empty" "fieldedit"}}

+ {{/if}} {{/if}} + {{else}} +

+ {{translate "relationlist.loading" "fieldedit"}} +

{{/if}} {{/if}}

diff --git a/Resources/public/templates/fields/view/relationlist.hbt b/Resources/public/templates/fields/view/relationlist.hbt index a992eacea..466c08c4e 100644 --- a/Resources/public/templates/fields/view/relationlist.hbt +++ b/Resources/public/templates/fields/view/relationlist.hbt @@ -3,32 +3,35 @@

{{ translate_property fieldDefinition.names }}

- {{#if isEmpty}} + {{#if isEmpty }} {{translate 'fieldview.field.empty' 'fieldview'}} {{else}} - {{#if relatedContents }} -
    - {{#each relatedContents}} -
  • - {{ name }} -
  • - {{/each}} -
- {{#if loadingError}} -
- {{translate 'relationlist.error.loading' 'fieldview'}} - -
- {{/if}} - {{else}} - {{#if loadingError}} -
- {{translate 'relationlist.error.loading' 'fieldview'}} - -
+ {{#if isLoaded }} + {{#if relatedContents }} +
    + {{#each relatedContents}} +
  • + {{ name }} +
  • + {{/each}} +
+ {{#if loadingError}} +
+ {{translate 'relationlist.error.loading' 'fieldview'}} + +
+ {{/if}} {{else}} -
{{translate 'relationlist.loading' 'fieldview'}}
+ {{translate 'fieldview.field.empty' 'fieldview'}} + {{#if loadingError}} +
+ {{translate 'relationlist.error.loading' 'fieldview'}} + +
+ {{/if}} {{/if}} + {{ else }} +
{{translate 'relationlist.loading' 'fieldview'}}
{{/if}} {{/if}}
diff --git a/Tests/js/views/fields/assets/ez-relation-editview-tests.js b/Tests/js/views/fields/assets/ez-relation-editview-tests.js index 5bc5df714..289b1d648 100644 --- a/Tests/js/views/fields/assets/ez-relation-editview-tests.js +++ b/Tests/js/views/fields/assets/ez-relation-editview-tests.js @@ -18,7 +18,12 @@ YUI.add('ez-relation-editview-tests', function (Y) { setUp: function () { this.destinationContent = new Y.Mock(); - this.destinationContentToJSON = {anythingJSONed: 'somethingJSONed'}; + this.destinationContentToJSON = { + anythingJSONed: 'somethingJSONed', + resources: { + MainLocation: true + } + }; Y.Mock.expect(this.destinationContent, { method: 'toJSON', returns: this.destinationContentToJSON @@ -304,9 +309,19 @@ YUI.add('ez-relation-editview-tests', function (Y) { contentInfoMock = new Y.Mock(), fakeEventFacade = {selection: {contentInfo: contentInfoMock}}; + Y.Mock.expect(contentInfoMock, { + property: 'name', + value: 'contentInfoModel' + }); + Y.Mock.expect(contentInfoMock, { method: 'toJSON', - returns: {name: 'me', publishedDate: 'yesterday', lastModificationDate: 'tomorrow'} + returns: { + name: 'me', + publishedDate: 'yesterday', + lastModificationDate: 'tomorrow', + resources: {} + } }); Y.Mock.expect(contentInfoMock, { diff --git a/Tests/js/views/fields/assets/ez-relation-view-tests.js b/Tests/js/views/fields/assets/ez-relation-view-tests.js index 473cc19e0..c4239972d 100644 --- a/Tests/js/views/fields/assets/ez-relation-view-tests.js +++ b/Tests/js/views/fields/assets/ez-relation-view-tests.js @@ -13,7 +13,12 @@ YUI.add('ez-relation-view-tests', function (Y) { setUp: function () { this.destinationContent = new Y.Mock(); - this.destinationContentToJSON = {anythingJSONed: 'somethingJSONed'}; + this.destinationContentToJSON = { + anythingJSONed: 'somethingJSONed', + resources: { + MainLocation: true + } + }; Y.Mock.expect(this.destinationContent, { method: 'toJSON', returns: this.destinationContentToJSON diff --git a/Tests/js/views/fields/assets/ez-relationlist-editview-tests.js b/Tests/js/views/fields/assets/ez-relationlist-editview-tests.js index a7275e341..db55c63e9 100644 --- a/Tests/js/views/fields/assets/ez-relationlist-editview-tests.js +++ b/Tests/js/views/fields/assets/ez-relationlist-editview-tests.js @@ -44,6 +44,7 @@ YUI.add('ez-relationlist-editview-tests', function (Y) { this.jsonContentType = {}; this.jsonVersion = {}; this.loadingError = false; + this.isLoaded = true; this.content = new Y.Mock(); this.version = new Y.Mock(); this.contentType = new Y.Mock(); @@ -86,7 +87,7 @@ YUI.add('ez-relationlist-editview-tests', function (Y) { this.view.template = function (variables) { Y.Assert.isObject(variables, "The template should receive some variables"); - Y.Assert.areEqual(10, Y.Object.keys(variables).length, "The template should receive 10 variables"); + Y.Assert.areEqual(11, Y.Object.keys(variables).length, "The template should receive 11 variables"); Y.Assert.areSame( that.jsonContent, variables.content, "The content should be available in the field edit view template" @@ -113,7 +114,11 @@ YUI.add('ez-relationlist-editview-tests', function (Y) { ); Y.Assert.areSame( that.view.get('loadingError'), variables.loadingError, - "The field should be available in the field edit view template" + "The loadingError be available in the field edit view template" + ); + Y.Assert.areSame( + that.isLoaded, variables.isLoaded, + "The isLoaded should be available in the field edit view template" ); Y.Array.each(that.view.get('relatedContents'), function (destContent) { destContentToJSONArray.push(destContent.toJSON()); @@ -350,7 +355,12 @@ YUI.add('ez-relationlist-editview-tests', function (Y) { contentIdsArray = Y.Array.dedupe(that.view.get('destinationContentsIds')); Y.Mock.expect(contentInfoMock1, { method: 'toJSON', - returns: {name: 'me', publishedDate: 'yesterday', lastModificationDate: 'tomorrow'} + returns: { + name: 'me', + publishedDate: 'yesterday', + lastModificationDate: 'tomorrow', + resources: {} + } }); Y.Mock.expect(contentInfoMock1, { @@ -359,9 +369,24 @@ YUI.add('ez-relationlist-editview-tests', function (Y) { returns: 42 }); + Y.Mock.expect(contentInfoMock1, { + property: 'name', + value: 'contentInfoModel' + }); + Y.Mock.expect(contentInfoMock2, { method: 'toJSON', - returns: {name: 'me', publishedDate: 'yesterday', lastModificationDate: 'tomorrow'} + returns: { + name: 'me', + publishedDate: 'yesterday', + lastModificationDate: 'tomorrow', + resources: {} + } + }); + + Y.Mock.expect(contentInfoMock2, { + property: 'name', + value: 'contentInfoModel' }); Y.Mock.expect(contentInfoMock2, { @@ -463,7 +488,12 @@ YUI.add('ez-relationlist-editview-tests', function (Y) { setUp: function () { this.destinationContent1 = new Y.Mock(); - this.destinationContent1ToJSON = {anythingJSONed: 'somethingJSONed'}; + this.destinationContent1ToJSON = { + anythingJSONed: 'somethingJSONed', + resources: { + MainLocation: true + } + }; Y.Mock.expect(this.destinationContent1, { method: 'toJSON', @@ -483,7 +513,12 @@ YUI.add('ez-relationlist-editview-tests', function (Y) { } }); this.destinationContent2 = new Y.Mock(); - this.destinationContent2ToJSON = {anythingJSONed2: 'somethingJSONed2'}; + this.destinationContent2ToJSON = { + anythingJSONed2: 'somethingJSONed2', + resources: { + MainLocation: true + } + }; Y.Mock.expect(this.destinationContent2, { method: 'toJSON', diff --git a/Tests/js/views/fields/assets/ez-relationlist-view-tests.js b/Tests/js/views/fields/assets/ez-relationlist-view-tests.js index 7e9bf58a9..73e82ac77 100644 --- a/Tests/js/views/fields/assets/ez-relationlist-view-tests.js +++ b/Tests/js/views/fields/assets/ez-relationlist-view-tests.js @@ -22,10 +22,20 @@ YUI.add('ez-relationlist-view-tests', function (Y) { }); this.relatedContents = [this.destinationContent1, this.destinationContent2]; - this.destinationContent1ToJSON = {anythingJSONed: 'somethingJSONed'}; - this.destinationContent2ToJSON = {anythingJSONed: 'somethingJSONed'}; + this.destinationContent1ToJSON = { + anythingJSONed: 'somethingJSONed', + resources: { + MainLocation: true + } + }; + this.destinationContent2ToJSON = { + anythingJSONed: 'somethingJSONed', + resources: { + MainLocation: true + } + }; - this.templateVariablesCount = 6; + this.templateVariablesCount = 7; this.fieldDefinitionIdentifier= "niceField"; this.fieldDefinition = { fieldType: "ezobjectrelationlist", @@ -158,7 +168,7 @@ YUI.add('ez-relationlist-view-tests', function (Y) { name: "eZ Relation list View tests (without related content)", setUp: function () { - this.templateVariablesCount = 6; + this.templateVariablesCount = 7; this.fieldDefinition = {fieldType: "ezobjectrelationlist", identifier: 'some_identifier'}; this.field = {fieldValue: {destinationContentsId: null}}; this.isEmpty = true; @@ -185,7 +195,7 @@ YUI.add('ez-relationlist-view-tests', function (Y) { name: "eZ Relation list View tests (without related content)", setUp: function () { - this.templateVariablesCount = 6; + this.templateVariablesCount = 7; this.fieldDefinition = {fieldType: "ezobjectrelationlist", identifier: 'some_identifier'}; this.field = {fieldValue: {destinationContentsId: []}}; this.isEmpty = true; @@ -212,7 +222,7 @@ YUI.add('ez-relationlist-view-tests', function (Y) { name: "eZ Relation list View tests (without related content)", setUp: function () { - this.templateVariablesCount = 6; + this.templateVariablesCount = 7; this.fieldDefinition = {fieldType: "ezobjectrelationlist", identifier: 'some_identifier'}; this.field = {fieldValue: null}; this.isEmpty = true;