-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-hyper-cj.js
339 lines (291 loc) · 8.28 KB
/
ng-hyper-cj.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
;(function(){
/**
* Require the module at `name`.
*
* @param {String} name
* @return {Object} exports
* @api public
*/
function require(name) {
var module = require.modules[name];
if (!module) throw new Error('failed to require "' + name + '"');
if (!('exports' in module) && typeof module.definition === 'function') {
module.client = module.component = true;
module.definition.call(this, module.exports = {}, module);
delete module.definition;
}
return module.exports;
}
/**
* Meta info, accessible in the global scope unless you use AMD option.
*/
require.loader = 'component';
/**
* Internal helper object, contains a sorting function for semantiv versioning
*/
require.helper = {};
require.helper.semVerSort = function(a, b) {
var aArray = a.version.split('.');
var bArray = b.version.split('.');
for (var i=0; i<aArray.length; ++i) {
var aInt = parseInt(aArray[i], 10);
var bInt = parseInt(bArray[i], 10);
if (aInt === bInt) {
var aLex = aArray[i].substr((""+aInt).length);
var bLex = bArray[i].substr((""+bInt).length);
if (aLex === '' && bLex !== '') return 1;
if (aLex !== '' && bLex === '') return -1;
if (aLex !== '' && bLex !== '') return aLex > bLex ? 1 : -1;
continue;
} else if (aInt > bInt) {
return 1;
} else {
return -1;
}
}
return 0;
}
/**
* Find and require a module which name starts with the provided name.
* If multiple modules exists, the highest semver is used.
* This function can only be used for remote dependencies.
* @param {String} name - module name: `user~repo`
* @param {Boolean} returnPath - returns the canonical require path if true,
* otherwise it returns the epxorted module
*/
require.latest = function (name, returnPath) {
function showError(name) {
throw new Error('failed to find latest module of "' + name + '"');
}
// only remotes with semvers, ignore local files conataining a '/'
var versionRegexp = /(.*)~(.*)@v?(\d+\.\d+\.\d+[^\/]*)$/;
var remoteRegexp = /(.*)~(.*)/;
if (!remoteRegexp.test(name)) showError(name);
var moduleNames = Object.keys(require.modules);
var semVerCandidates = [];
var otherCandidates = []; // for instance: name of the git branch
for (var i=0; i<moduleNames.length; i++) {
var moduleName = moduleNames[i];
if (new RegExp(name + '@').test(moduleName)) {
var version = moduleName.substr(name.length+1);
var semVerMatch = versionRegexp.exec(moduleName);
if (semVerMatch != null) {
semVerCandidates.push({version: version, name: moduleName});
} else {
otherCandidates.push({version: version, name: moduleName});
}
}
}
if (semVerCandidates.concat(otherCandidates).length === 0) {
showError(name);
}
if (semVerCandidates.length > 0) {
var module = semVerCandidates.sort(require.helper.semVerSort).pop().name;
if (returnPath === true) {
return module;
}
return require(module);
}
// if the build contains more than one branch of the same module
// you should not use this funciton
var module = otherCandidates.pop().name;
if (returnPath === true) {
return module;
}
return require(module);
}
/**
* Registered modules.
*/
require.modules = {};
/**
* Register module at `name` with callback `definition`.
*
* @param {String} name
* @param {Function} definition
* @api private
*/
require.register = function (name, definition) {
require.modules[name] = {
definition: definition
};
};
/**
* Define a module's exports immediately with `exports`.
*
* @param {String} name
* @param {Generic} exports
* @api private
*/
require.define = function (name, exports) {
require.modules[name] = {
exports: exports
};
};
require.register("[email protected]", function (exports, module) {
/**
* Transform a Collection+JSON resource into a hyper+json representation
*
* @param {Object} resource
* @return {Object}
*/
module.exports = function(resource) {
var root = get('collection', resource);
if (!root) throw new Error('Missing root "collection" property');
var hyperObj = {};
mergeItem(hyperObj, root, null, true);
var template = get('data', get('template', root));
if (template) hyperObj.create = {
enctype: 'application/collection+json',
action: hyperObj.href,
method: 'POST',
input: transformInputs(template)
};
hyperObj.collection = get('items', root, []).map(function(item) {
return mergeItem({}, item, template);
});
get('queries', root, []).forEach(function(query) {
var rel = get('rel', query);
if (!rel) return;
var form = hyperObj[rel] = {
method: 'GET',
input: {}
};
Object.keys(query).forEach(function(key) {
if (key === 'rel') return;
if (key === 'href') return form.action = query[key];
if (key === 'data') return form.input = transformInputs(query[key]);
form[key] = query[key];
});
});
return hyperObj;
};
/**
* Merge an item's properties into a document
*
* @param {Object} doc
* @param {Object} item
* @param {Array} template
* @param {Boolean} isRoot
* @return {Object}
*/
function mergeItem(doc, item, template, isRoot) {
var href = doc.href = get('href', item);
get('links', item, []).forEach(function(link) {
var rel = get('rel', link);
if (!rel) return;
var render = get('render', link);
var out = doc[rel] = {};
Object.keys(link).forEach(function(key) {
if (key === 'rel' || key === 'render') return;
if (key === 'href' && render === 'image') return out.src = link[key];
out[key] = link[key];
});
});
get('data', item, []).forEach(function(datum) {
var name = get('name', datum);
if (!name) return;
doc[name] = Object.keys(datum).reduce(function(acc, key) {
if (key === 'name') return acc;
if (key === 'value') acc.data = datum[key];
else acc[key] = datum[key];
return acc;
}, {});
});
if (template) doc.update = {
enctype: 'application/collection+json',
method: 'PUT',
action: href,
input: transformInputs(template, doc)
}
if (!isRoot) doc.delete = {
method: 'DELETE',
action: href
};
return doc;
}
/**
* Transform CJ inputs into hyper rep, filling in any values
*
* @param {Array} inputs
* @param {Object} values
* @return {Object}
*/
function transformInputs(inputs, values) {
var hyperInputs = {};
inputs.forEach(mergeInput.bind(null, hyperInputs, values));
return hyperInputs;
}
/**
* Merge input into inputs
*
* @param {Object} inputs
* @param {Object} values
* @param {Object} input
*/
function mergeInput(inputs, values, input) {
var name = get('name', input);
if (!name) return;
var out = inputs[name] = {};
Object.keys(input).forEach(function(key) {
if (key === 'name') return;
if (!values || key !== 'value') return out[key] = input[key];
var value = values[name];
if (!value || typeof value !== 'object') return out.value = value;
out.value = value.href || value.src || value.data;
});
}
/**
* Gracefully get a value
*
* @param {String} key
* @param {Any} obj
* @param {Any} fallback
* @return {Any}
*/
function get(key, obj, fallback) {
if (!obj) return undefined;
if (obj.hasOwnProperty(key)) return obj[key];
return fallback;
}
});
require.register("ng-hyper-cj", function (exports, module) {
/**
* Module dependencies
*/
var angular = window.angular;
var cj2hyper = require('[email protected]');
/**
* Initialize the package
*/
var pkg = module.exports = angular.module('ng-hyper-cj', ['ng-hyper']);
pkg.factory('cj2hyperInterceptor', [
function() {
return {
response: function(res) {
var type = res.headers()['content-type'] || '';
if (!~type.indexOf('collection+json')) return res;
try {
res.data = cj2hyper(res.data);
} catch (err) {
console.error(err);
}
return res;
}
};
}]);
pkg.config([
'$httpProvider',
function($httpProvider) {
$httpProvider.interceptors.push('cj2hyperInterceptor');
}
]);
});
if (typeof exports == "object") {
module.exports = require("ng-hyper-cj");
} else if (typeof define == "function" && define.amd) {
define("ng-hyper-cj", [], function(){ return require("ng-hyper-cj"); });
} else {
(this || window)["ng-hyper-cj"] = require("ng-hyper-cj");
}
})()