diff --git a/Resources/public/js/views/fields/ez-relation-editview.js b/Resources/public/js/views/fields/ez-relation-editview.js index c1955f07b..d3130006a 100644 --- a/Resources/public/js/views/fields/ez-relation-editview.js +++ b/Resources/public/js/views/fields/ez-relation-editview.js @@ -91,16 +91,47 @@ YUI.add('ez-relation-editview', function (Y) { * @return Object */ _variables: function () { - var dest = this.get('destinationContent'); + var destinationContent = this.get('destinationContent'), + destinationContentJSON = null, + isLoaded = destinationContent !== null; + + if (isLoaded && (this._isNewRelation(destinationContent) || !this._isTrashed(destinationContent))) { + destinationContentJSON = destinationContent.toJSON(); + } return { - destinationContent: dest ? dest.toJSON() : null, + destinationContent: destinationContentJSON, loadingError: this.get('loadingError'), isEmpty: this._isFieldEmpty(), + isLoaded: isLoaded, isRequired: this.get('fieldDefinition').isRequired, }; }, + /** + * Check if related content is trashed. + * + * @method _isTrashed + * @protected + * @param {eZ.ContentInfo|ez.eZ.Content} model + * @return {boolean} + */ + _isTrashed: function(model) { + return !model.get('resources.MainLocation'); + }, + + /** + * Check if relation to the content is new (non-existed before edit) + * + * @method _isNewRelation + * @protected + * @param {eZ.ContentInfo|ez.eZ.Content} model + * @return {boolean} + */ + _isNewRelation: function (model) { + return model.name === 'contentInfoModel'; + }, + /** * Tap event handler for the remove button. It resets the relation. * diff --git a/Resources/public/js/views/fields/ez-relation-view.js b/Resources/public/js/views/fields/ez-relation-view.js index 8f45a898b..f490349ea 100644 --- a/Resources/public/js/views/fields/ez-relation-view.js +++ b/Resources/public/js/views/fields/ez-relation-view.js @@ -59,10 +59,18 @@ YUI.add('ez-relation-view', function (Y) { * @return Object */ _variables: function () { - var dest = this.get('destinationContent'); + var destinationContent = this.get('destinationContent'), + destinationContentJSON = null, + isLoaded = destinationContent !== null; + + if (isLoaded && destinationContent.get('resources.MainLocation') !== undefined) { + destinationContentJSON = destinationContent.toJSON(); + } return { - destinationContent: dest ? dest.toJSON() : null, + destinationContent: destinationContentJSON, + isLoaded: isLoaded, + isEmpty: this._isFieldEmpty(), loadingError: this.get('loadingError'), }; }, diff --git a/Resources/public/templates/fields/edit/relation.hbt b/Resources/public/templates/fields/edit/relation.hbt index be466ce65..9364a98ae 100644 --- a/Resources/public/templates/fields/edit/relation.hbt +++ b/Resources/public/templates/fields/edit/relation.hbt @@ -15,50 +15,58 @@
-
- {{#if isEmpty}} -

- {{translate "relation.empty" "fieldedit"}} -

- {{else}} - {{#if destinationContent }} -
-
-
-

{{ destinationContent.name }}

-
    -
  • - {{translate "relation.published" "fieldedit"}} {{ formatTime destinationContent.publishedDate day="numeric" month="long" year="numeric" hour="numeric" minute="2-digit" }} -
  • -
  • - {{translate "relation.modified" "fieldedit"}} {{ formatTime destinationContent.lastModificationDate day="numeric" month="long" year="numeric" hour="numeric" minute="2-digit" }} -
  • -
-
-
+
+
+ {{#if isEmpty}} +

+ {{translate "relation.empty" "fieldedit"}} +

{{else}} - {{#if loadingError}} -

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

+ {{#if isLoaded}} + {{#if destinationContent }} +
+
+
+

{{ destinationContent.name }}

+
    +
  • + {{translate "relation.published" "fieldedit"}} {{ formatTime destinationContent.publishedDate day="numeric" month="long" year="numeric" hour="numeric" minute="2-digit" }} +
  • +
  • + {{translate "relation.modified" "fieldedit"}} {{ formatTime destinationContent.lastModificationDate day="numeric" month="long" year="numeric" hour="numeric" minute="2-digit" }} +
  • +
+
+
+ {{else}} + {{#if loadingError}} +

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

+ {{else}} +

+ {{translate "relation.empty" "fieldedit"}} +

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

{{translate "relation.loading" "fieldedit"}}

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

- - -

-
+

+ + +

+
+
diff --git a/Resources/public/templates/fields/view/relation.hbt b/Resources/public/templates/fields/view/relation.hbt index 1a412964a..b9b30e04f 100644 --- a/Resources/public/templates/fields/view/relation.hbt +++ b/Resources/public/templates/fields/view/relation.hbt @@ -2,22 +2,30 @@

{{ translate_property fieldDefinition.names }}

-
- {{#if isEmpty}} +
+
+ {{#if isEmpty }} {{translate 'fieldview.field.empty' 'fieldview'}} {{else}} - {{#if destinationContent }} - {{ destinationContent.name }} - {{else}} - {{#if loadingError}} -

- {{translate 'relation.error.loading' 'fieldview'}} - -

+ {{#if isLoaded }} + {{#if destinationContent }} + + {{ destinationContent.name }} + {{else}} -

{{translate 'relation.loading' 'fieldview'}}

+ {{#if loadingError}} +

+ {{translate 'relation.error.loading' 'fieldview'}} + +

+ {{else}} + {{translate 'fieldview.field.empty' 'fieldview'}} + {{/if}} {{/if}} + {{else}} +

{{translate 'relation.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 06f242e75..0c4dfd560 100644 --- a/Tests/js/views/fields/assets/ez-relation-editview-tests.js +++ b/Tests/js/views/fields/assets/ez-relation-editview-tests.js @@ -32,11 +32,20 @@ YUI.add('ez-relation-editview-tests', function (Y) { method: 'toJSON', returns: this.destinationContentToJSON }); + Y.Mock.expect(this.destinationContent, { method: 'get', - args: ['contentId'], - returns: 45 + args: [Y.Mock.Value.String], + run: function (key) { + var values = { + 'resources.MainLocation': true, + 'contentId': 45 + }; + + return values[key]; + } }); + this.fieldDefinitionIdentifier= "niceField"; this.fieldDefinition = { fieldType: "ezobjectrelation", @@ -55,6 +64,7 @@ YUI.add('ez-relation-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(); @@ -97,7 +107,7 @@ YUI.add('ez-relation-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" @@ -134,6 +144,10 @@ YUI.add('ez-relation-editview-tests', function (Y) { variables.isNotTranslatable, "The isNotTranslatable should 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.Assert.areSame(expectRequired, variables.isRequired); return ''; @@ -324,15 +338,25 @@ YUI.add('ez-relation-editview-tests', function (Y) { name: 'me', publishedDate: 'yesterday', lastModificationDate: 'tomorrow', - resources: {} + resources: { + MainLocation: true + } } }); Y.Mock.expect(contentInfoMock, { method: 'get', - args: ['contentId'], - returns: 51 + args: [Y.Mock.Value.String], + run: function (key) { + var values = { + 'resources.MainLocation': true, + 'contentId': 51 + }; + + return values[key]; + } }); + this.view.on('contentDiscover', function (e) { that.resume(function () { 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 c4239972d..61f1cb664 100644 --- a/Tests/js/views/fields/assets/ez-relation-view-tests.js +++ b/Tests/js/views/fields/assets/ez-relation-view-tests.js @@ -19,12 +19,21 @@ YUI.add('ez-relation-view-tests', function (Y) { MainLocation: true } }; + + Y.Mock.expect(this.destinationContent, { + method: 'get', + args: ['resources.MainLocation'], + returns: { + MainLocation: true + } + }); + Y.Mock.expect(this.destinationContent, { method: 'toJSON', returns: this.destinationContentToJSON }); - this.templateVariablesCount = 6; + this.templateVariablesCount = 7; this.fieldDefinitionIdentifier= "niceField"; this.fieldDefinition = { fieldType: "ezobjectrelation", @@ -127,7 +136,7 @@ YUI.add('ez-relation-view-tests', function (Y) { name: "eZ Relation View tests (without related content)", setUp: function () { - this.templateVariablesCount = 6; + this.templateVariablesCount = 7; this.fieldDefinition = {fieldType: "ezobjectrelation", identifier: 'some_identifier'}; this.field = {fieldValue: {destinationContentId: null}}; this.isEmpty = true;