-
Notifications
You must be signed in to change notification settings - Fork 11
/
cof-contenttypeselectorview.js
218 lines (190 loc) · 7.35 KB
/
cof-contenttypeselectorview.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright (C) eZ Systems AS. All rights reserved.
* For full copyright and license information view LICENSE file distributed with this source code.
*/
YUI.add('cof-contenttypeselectorview', function (Y) {
'use strict';
/**
* Provides the Content Creation Widget Content Type Selector View class
*
* @module cof-contenttypeselectorview
*/
Y.namespace('cof');
var CLASS_HIDDEN = 'cof-is-hidden',
CLASS_INVISIBLE = 'cof-invisible',
SELECTOR_ITEM_SELECTED = '.ez-selection-filter-item-selected',
SELECTOR_CONTENT_TYPE_LIST = '.ez-contenttypeselector-list',
SELECTOR_TOOLTIP = '.cof-content-creation__tooltip',
ATTR_ID = 'data-id',
ATTR_DESCRIPTION = 'data-description',
PX = 'px',
SCROLL_TIMEOUT = 100;
/**
* The Content Creation Widget Content Type Selector view
*
* @namespace cof
* @class ContentTypeSelectorView
* @constructor
* @extends eZ.ContentTypeSelectorView
*/
Y.cof.ContentTypeSelectorView = Y.Base.create('ContentTypeSelectorView', Y.eZ.ContentTypeSelectorView, [], {
initializer: function () {
this._scrollContentTypeHandler = null;
this._correctTooltipPositionTimeout = null;
this.get('container').addClass(this._generateViewClassName(Y.eZ.ContentTypeSelectorView.NAME));
this.after('filterViewChange', this._attachOnSelectEventListener, this);
},
/**
* Attach event listener on select.
*
* @protected
* @method _attachOnSelectEventListener
*/
_attachOnSelectEventListener: function () {
this.get('filterView').on('select', this._addDescriptionToSelectedItem, this);
},
/**
* Adds the description to the selected item.
*
* @protected
* @method _addDescriptionToSelectedItem
* @param event {Object} event facade
*/
_addDescriptionToSelectedItem: function (event) {
var contentTypeItem = event.elementNode,
contentTypeItemId = contentTypeItem.getAttribute(ATTR_ID),
contentTypeGroups = this.get('contentTypeGroups');
contentTypeGroups.forEach(function (contentTypeGroup) {
contentTypeGroup.get('contentTypes').forEach(function (contentType) {
if (contentTypeItemId === contentType.get('id')) {
var description = contentType.get('descriptions')[contentType.get('mainLanguageCode')];
if (description) {
contentTypeItem.setAttribute(ATTR_DESCRIPTION, description);
}
}
});
});
/**
* Informs an item is selected.
* Listened in the cof.ContentCreationView
*
* @event itemSelected
* @param text {String} text to show on remove button
*/
this.fire('itemSelected', {text: event.text});
this._correctTooltipPosition();
},
/**
* Shows the tooltip.
*
* @method showTooltip
*/
showTooltip: function () {
this._toggleTooltip(true);
this._setCorrectTooltipPositionOnScroll();
},
/**
* Hides the tooltip.
*
* @method hideTooltip
*/
hideTooltip: function () {
this._toggleTooltip(false);
if (this._scrollContentTypeHandler) {
this._scrollContentTypeHandler.detach();
}
},
/**
* Toggle visibility of the tooltip.
*
* @protected
* @method _toggleTooltip
* @param show {Boolean} should show the tooltip?
*/
_toggleTooltip: function (show) {
var methodName = show ? 'removeClass' : 'addClass',
container = this.get('container'),
selectedItem = container.one(SELECTOR_ITEM_SELECTED),
tooltip = container.one(SELECTOR_TOOLTIP);
if (selectedItem) {
tooltip.setHTML(selectedItem.getAttribute(ATTR_DESCRIPTION));
tooltip[methodName](CLASS_HIDDEN);
}
},
/**
* Sets detected content type scroll position event handler to manage the tooltip position.
*
* @protected
* @method _setCorrectTooltipPositionOnScroll
*/
_setCorrectTooltipPositionOnScroll: function () {
this._scrollContentTypeHandler = this.get('container').one(SELECTOR_CONTENT_TYPE_LIST).on('scroll', Y.bind(this._setCorrectTooltipPositionTimeout, this));
},
/**
* Sets the timeout on correct timeout position.
* @protected
* @method _setCorrectTooltipPositionTimeout
*/
_setCorrectTooltipPositionTimeout: function () {
window.clearTimeout(this._correctTooltipPositionTimeout);
this._correctTooltipPositionTimeout = window.setTimeout(Y.bind(this._correctTooltipPosition, this), SCROLL_TIMEOUT);
},
/**
* Corrects the tooltip position.
*
* @protected
* @method _correctTooltipPosition
*/
_correctTooltipPosition: function () {
var container = this.get('container'),
selectedItem = container.one(SELECTOR_ITEM_SELECTED),
selectedItemRect,
contentTypeListRect = container.one(SELECTOR_CONTENT_TYPE_LIST).getDOMNode().getBoundingClientRect(),
tooltipSpace = 40,
tooltip = container.one(SELECTOR_TOOLTIP);
if (!selectedItem) {
return;
}
selectedItemRect = selectedItem.getDOMNode().getBoundingClientRect();
if (selectedItemRect.top < contentTypeListRect.top || selectedItemRect.top > contentTypeListRect.top + contentTypeListRect.height) {
tooltip.addClass(CLASS_INVISIBLE);
} else {
tooltip.removeClass(CLASS_INVISIBLE);
tooltip.setStyle('top', selectedItemRect.top + PX);
tooltip.setStyle('left', selectedItemRect.left + selectedItemRect.width + tooltipSpace + PX);
}
},
/**
* Sets the selected content type.
* Overriden to stop redirecting user to the content creation.
*
* @protected
* @method _createContentEvent
*/
_createContentEvent: function (typeId) {
var type;
type = Y.Array.find(this._getContentTypes(), function (t) {
return t.get('id') === typeId;
});
this.set('selectedContentType', type);
/**
* Fired to prepare content model for content type.
* Listened in the eZS.Plugin.SelectCreateContent
*
* @event prepareContentModel
* @param contentType {eZ.ContentType} the content type model
*/
this.fire('prepareContentModel', {contentType: type});
},
}, {
ATTRS: {
/**
* The selected content type
*
* @attribute selectedContentType
* @type Object
*/
selectedContentType: {}
}
});
});