This repository has been archived by the owner on May 3, 2022. It is now read-only.
/
ez-contentcreateplugin.js
173 lines (156 loc) · 5.85 KB
/
ez-contentcreateplugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (C) eZ Systems AS. All rights reserved.
* For full copyright and license information view LICENSE file distributed with this source code.
*/
YUI.add('ez-contentcreateplugin', function (Y) {
'use strict';
/**
* Provides the content create plugin for the location view view service.
*
* @module ez-contentcreateplugin
*/
Y.namespace('eZ.Plugin');
/**
* Content create plugin.
*
* @namespace eZ.Plugin
* @class ContentCreate
* @constructor
* @extends eZ.Plugin.ViewServiceBase
*/
Y.eZ.Plugin.ContentCreate = Y.Base.create('contentCreate', Y.eZ.Plugin.ViewServiceBase, [], {
initializer: function () {
this.onHostEvent('*:createContent', this._handleCreateContentAction);
this.afterHostEvent('*:createContentAction', this._getContentTypes);
},
/**
* Resets the state of the plugin's attributes
*
* @method parallelLoad
* @param {Function} next
*/
parallelLoad: function (next) {
this.set('contentType', undefined);
this.set('parentLocation', undefined);
next();
},
/**
* Sets the contentType, languageCode and parentLocation on the next
* view service if the users wants to create a new content
*
* @method setNextViewServiceParameters
* @param {eZ.ViewService} service
*/
setNextViewServiceParameters: function (service) {
if ( this.get('contentType') && this.get('parentLocation') ) {
service.setAttrs({
contentType: this.get('contentType'),
languageCode: this.get('languageCode'),
parentLocation: this.get('parentLocation'),
parentContent: this.get('parentContent'),
});
}
},
/**
* Fetches the content type groups and the content types.
*
* @protected
* @method _getContentTypesList
* @param {Object} event event facade
*/
_getContentTypes: function (event) {
var capi = this.get('host').get('capi'),
view = event.target,
typeService = this.get('host').get('capi').getContentTypeService();
if ( !view.get('expanded') ) {
return;
}
typeService.loadContentTypeGroups(function (error, response) {
var groups = [], hasError = false,
parallel = new Y.Parallel();
if ( error ) {
return view.set('loadingError', true);
}
Y.Array.each(response.document.ContentTypeGroupList.ContentTypeGroup, function (groupHash) {
var group = new Y.eZ.ContentTypeGroup();
group.set('id', groupHash._href);
group.loadFromHash(groupHash);
groups.push(group);
group.loadContentTypes({api: capi}, parallel.add(function (error) {
if ( error ) {
hasError = true;
}
}));
});
parallel.done(function () {
if ( hasError ) {
view.set('loadingError', true);
} else {
view.set('contentTypeGroups', groups);
}
});
});
},
/**
* Retrieves and stores the content creation parameters from
* the createContent event and redirect the user to the createContent
* route.
*
* @protected
* @method _handleCreateContentAction
* @param {Object} event event facade
*/
_handleCreateContentAction: function (event) {
var service = this.get('host'),
languageCode = event.languageCode ? event.languageCode : this.get('languageCode');
document.querySelector(`#content_create_content_type_${event.contentType.get('identifier')}`).checked = true;
document.querySelector(`#content_create_language option[value="${languageCode}"]`).selected = true;
document.querySelector('#content_create_parent_location').value = service.get('location').get('locationId');
document.querySelector('#content_create_create').click();
},
}, {
NS: 'contentCreate',
ATTRS: {
/**
* The content type to use to create the new content
*
* @attribute contentType
* @default undefined
* @type Y.eZ.ContentType
*/
contentType: {},
/**
* The language code to use to create the new content
*
* @attribute languageCode
* @default value taken from app.contentCreationDefaultLanguageCode
* @type String
*/
languageCode: {
valueFn: function () {
var app = this.get('host').get('app');
return app.get('contentCreationDefaultLanguageCode');
}
},
/**
* The parent location of the content that will be created
*
* @attribute parentLocation
* @type Y.eZ.Location
* @default undefined
*/
parentLocation: {},
/**
* The parent content of the content that will be created
*
* @attribute parentContent
* @type Y.eZ.Content
* @default undefined
*/
parentContent: {},
}
});
Y.eZ.PluginRegistry.registerPlugin(
Y.eZ.Plugin.ContentCreate, ['locationViewViewService']
);
});