helper.js
6.87 KB
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
/*
* ADOBE CONFIDENTIAL
*
* Copyright 2015 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* globals com, java, org, resource, use */
use(function() {
'use strict';
// Define some constants to be reused throughout the project
var constants = {
jcr: com.day.cq.commons.jcr.JcrConstants,
sling: org.apache.sling.jcr.resource.api.JcrResourceConstants,
cq: com.day.cq.wcm.api.NameConstants,
screens: com.adobe.cq.screens.binding.ScreensConstants,
device: com.adobe.cq.screens.binding.DeviceConstants
};
/**
* Compute the attributes for the element from the specified properties and exclusions.
*
* @param {ValueMap} properties The properties to use to populate the attributes
* @param {Map} [attributes] The attribtues map to extend
* @param {String[]} [excludes] The properties to exclude from the list, in addition to the default ones.
*
* @return {Map} The computed attributes
*/
var computeDataAttributes = function(properties, attributes, excludes) {
attributes = attributes || {};
var propertiesIterator = properties.keySet().iterator();
var key;
while (propertiesIterator.hasNext()) {
key = propertiesIterator.next();
if (!key.startsWith('jcr:') && !key.startsWith('sling:') && !key.startsWith('cq:') // ignore default properties
&& excludes.indexOf(String(key)) === -1) { // ignore excluded properties
attributes['data-' + key] = properties.get(key).toString();
}
}
return attributes;
};
/**
* Check if a resource extends any of the specified resource types.
*
* @param {Resource} resource The resource to test.
* @param {String|String[]} types The resource type(s) to check against
*
* @return {String|Boolean} Returns the extended resource type, or `false`
*/
var isResourceType = function(resource, types) {
if (resource == null) { // eslint-disable-line
return false;
}
var i;
types = ([]).concat(types);
for (i = 0; i < types.length; i++) {
if (resource.isResourceType(types[i])) {
return types[i];
}
}
return false;
};
/**
* Check if a resource has children.
*
* @param {Resource} resource The resource to test.
*
* @return {Boolean} Returns `true` if the resource has any children, and `false` otherwise
*/
var hasChildren = function(resource) {
var childrenIterator = resource.resourceResolver.listChildren(resource);
var childResource;
while (childrenIterator.hasNext()) {
childResource = childrenIterator.next();
if (childResource.name.indexOf('rep:') > -1
|| childResource.getName().equals('jcr:content')) {
continue;
}
return true;
}
return false;
};
/**
* Check if a resource has children.
*
* @param {Resource} resource The resource to test.
* @param {String[]} types The resource types to check against.
*
* @return {Boolean} Returns `true` if the resource has any children, and `false` otherwise
*/
var hasChildrenWithResourceType = function(resource, types) {
types = ([]).concat(types);
var childrenIterator = resource.resourceResolver.listChildren(resource);
var childResource;
while (childrenIterator.hasNext()) {
childResource = childrenIterator.next();
if (childResource.name !== constants.cq.NN_CONTENT) {
var contentResource = childResource.getChild(constants.cq.NN_CONTENT);
if (contentResource && isResourceType(contentResource, types)) {
return true;
}
}
}
return false;
};
/**
* Apply a function on each child resource of the specified parent.
*
* @param {Resource} resource The parent resource
* @param {Function} mapFnc The function to apply
* @param {String[]} excludes Resource types to ignore
*
* @return {Object[]} An array of results returned by the map function
*/
var mapChildren = function(resource, mapFnc, excludes) {
if (resource == null) { // eslint-disable-line
return [];
}
var mapResults = [];
var childrenIterator = resource.resourceResolver.listChildren(resource);
var childResource, childContentResource, childProperties;
while (childrenIterator.hasNext()) {
childResource = childrenIterator.next();
if (excludes && isResourceType(childResource, excludes)) {
continue;
}
childContentResource = childResource.getChild(constants.cq.NN_CONTENT);
childProperties = childContentResource;
if (childProperties == null) { // eslint-disable-line
childProperties = childResource;
}
childProperties = childProperties.adaptTo(org.apache.sling.api.resource.ValueMap);
mapResults.push(mapFnc(childResource, childProperties));
}
return mapResults;
};
var windowPreference = function() {
var auth = resource.resourceResolver.adaptTo(org.apache.jackrabbit.api.security.user.Authorizable);
var upm = resource.resourceResolver.adaptTo(com.adobe.granite.security.user.UserPropertiesManager);
if (upm) {
var preferences = upm.getUserProperties(auth.getID(),
com.adobe.granite.security.user.UserPropertiesService.PREFERENCES_PATH);
if (preferences) {
return preferences.getProperty('winMode', 'multi', java.lang.String);
}
}
return 'multi';
};
return {
constants: constants,
computeDataAttributes: computeDataAttributes,
isResourceType: isResourceType,
hasChildren: hasChildren,
hasChildrenWithResourceType: hasChildrenWithResourceType,
mapChildren: mapChildren,
windowPreference: {
attrs: {
target: String(windowPreference()) === 'multi' ? '_blank' : ''
},
string: String(windowPreference()) === 'multi' ? ',"target":"_blank"' : ''
}
};
});