This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
/
selector.js
189 lines (166 loc) · 5.66 KB
/
selector.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
/*
* Copyright (c) 2012, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jslint anon:true, sloppy:true, nomen:true*/
/*global YUI*/
/**
* @module ResourceStoreAddon
*/
/**
* @class RSAddonSelector
* @extension ResourceStore.server
*/
YUI.add('addon-rs-selector', function(Y, NAME) {
function RSAddonSelector() {
RSAddonSelector.superclass.constructor.apply(this, arguments);
}
RSAddonSelector.NS = 'selector';
RSAddonSelector.DEPS = ['config'];
Y.extend(RSAddonSelector, Y.Plugin.Base, {
/**
* This methods is part of Y.Plugin.Base. See documentation for that for details.
* @method initializer
* @param {object} config Configuration object as per Y.Plugin.Base
* @return {nothing}
*/
initializer: function(config) {
this.appRoot = config.appRoot;
this.mojitoRoot = config.mojitoRoot;
this._appConfigYCB = config.host.config.getAppConfigYCB();
this._poslCache = {}; // context: POSL
},
/**
* Returns a list of all priority-ordered selector lists (POSLs).
* @method getAllPOSLs
* @return {array} list of priority-ordered selector lists
*/
getAllPOSLs: function() {
var c,
ctx,
ctxs,
posl,
posls = {};
ctxs = this._listUsedContexts();
for (c = 0; c < ctxs.length; c += 1) {
ctx = ctxs[c];
posl = this.getPOSLFromContext(ctx);
posls[JSON.stringify(posl)] = posl;
}
ctxs = null; // free a bunch of memory
return Y.Object.values(posls);
},
/**
* Returns the priority-ordered selector list (POSL) for the context.
* @method getPOSLFromContext
* @param {object} ctx runtime context
* @return {array} priority-ordered selector list
*/
getPOSLFromContext: function(ctx) {
var store = this.get('host'),
cacheKey,
posl,
p,
part,
parts;
cacheKey = JSON.stringify(ctx);
posl = this._poslCache[cacheKey];
if (!posl) {
posl = ['*'];
// TODO: use rs.config for this too
parts = this._appConfigYCB.readNoMerge(ctx, {});
for (p = 0; p < parts.length; p += 1) {
part = parts[p];
if (part.selector && store.selectors[part.selector]) {
posl.unshift(part.selector);
}
}
this._poslCache[cacheKey] = posl;
}
return Y.mojito.util.copy(posl);
},
/**
* Returns the a list of dimensions that are actually used in the
* application.json file.
* @private
* @method _listUsedDimensions
* @return {array} list of dimensions and values
* (values have no structure)
*/
_listUsedDimensions: function() {
var ctxs = [],
ctxValues = {}; // dimName: value: true
this._appConfigYCB.walkSettings(function(settings, config) {
Y.Object.each(settings, function(val, name) {
if (!ctxValues[name]) {
ctxValues[name] = {};
}
ctxValues[name][val] = true;
});
return true;
});
Y.Object.each(ctxValues, function(vals, name) {
ctxs[name] = Object.keys(vals);
});
return ctxs;
},
/**
* Generates a list of contexts to which application.json is sensitive.
* @private
* @method _listUsedContexts
* @return {array of objects} all contexts in application.json
*/
_listUsedContexts: function() {
var dims = this._listUsedDimensions(),
nctxs,
c,
ctxs = [],
dn,
dname,
dnames = Object.keys(dims),
dv,
dval,
dvals,
e,
each,
mod;
nctxs = 1;
for (dn = 0; dn < dnames.length; dn += 1) {
dname = dnames[dn];
dvals = dims[dname];
if (dname !== 'runtime') {
// we never have indeterminant runtime
dvals.push('*');
}
nctxs *= dvals.length;
}
for (c = 0; c < nctxs; c += 1) {
ctxs[c] = {};
}
mod = 1;
for (dn = 0; dn < dnames.length; dn += 1) {
dname = dnames[dn];
dvals = dims[dname];
mod *= dvals.length;
each = nctxs / mod;
e = each;
dv = 0;
for (c = 0; c < nctxs; e -= 1, c += 1) {
if (0 === e) {
e = each;
dv += 1;
dv = dv % dvals.length;
}
dval = dvals[dv];
if ('*' !== dval) {
ctxs[c][dname] = dval;
}
}
}
return ctxs;
}
});
Y.namespace('mojito.addons.rs');
Y.mojito.addons.rs.selector = RSAddonSelector;
}, '0.0.1', { requires: ['plugin', 'oop', 'mojito-util']});