vuejs2 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.md +21 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/vuejs2/engine.rb +4 -0
- data/lib/vuejs2/version.rb +3 -0
- data/lib/vuejs2.rb +6 -0
- data/vendor/assets/javascripts/vue2-resource.js +1524 -0
- data/vendor/assets/javascripts/vue2-router.js +1837 -0
- data/vendor/assets/javascripts/vue2.js +7401 -0
- data/vuejs2.gemspec +35 -0
- metadata +86 -0
@@ -0,0 +1,1524 @@
|
|
1
|
+
/*!
|
2
|
+
* vue-resource v1.0.3
|
3
|
+
* https://github.com/vuejs/vue-resource
|
4
|
+
* Released under the MIT License.
|
5
|
+
*/
|
6
|
+
|
7
|
+
(function (global, factory) {
|
8
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
9
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
10
|
+
(global.VueResource = factory());
|
11
|
+
}(this, (function () { 'use strict';
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Promises/A+ polyfill v1.1.4 (https://github.com/bramstein/promis)
|
15
|
+
*/
|
16
|
+
|
17
|
+
var RESOLVED = 0;
|
18
|
+
var REJECTED = 1;
|
19
|
+
var PENDING = 2;
|
20
|
+
|
21
|
+
function Promise$1(executor) {
|
22
|
+
|
23
|
+
this.state = PENDING;
|
24
|
+
this.value = undefined;
|
25
|
+
this.deferred = [];
|
26
|
+
|
27
|
+
var promise = this;
|
28
|
+
|
29
|
+
try {
|
30
|
+
executor(function (x) {
|
31
|
+
promise.resolve(x);
|
32
|
+
}, function (r) {
|
33
|
+
promise.reject(r);
|
34
|
+
});
|
35
|
+
} catch (e) {
|
36
|
+
promise.reject(e);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
Promise$1.reject = function (r) {
|
41
|
+
return new Promise$1(function (resolve, reject) {
|
42
|
+
reject(r);
|
43
|
+
});
|
44
|
+
};
|
45
|
+
|
46
|
+
Promise$1.resolve = function (x) {
|
47
|
+
return new Promise$1(function (resolve, reject) {
|
48
|
+
resolve(x);
|
49
|
+
});
|
50
|
+
};
|
51
|
+
|
52
|
+
Promise$1.all = function all(iterable) {
|
53
|
+
return new Promise$1(function (resolve, reject) {
|
54
|
+
var count = 0,
|
55
|
+
result = [];
|
56
|
+
|
57
|
+
if (iterable.length === 0) {
|
58
|
+
resolve(result);
|
59
|
+
}
|
60
|
+
|
61
|
+
function resolver(i) {
|
62
|
+
return function (x) {
|
63
|
+
result[i] = x;
|
64
|
+
count += 1;
|
65
|
+
|
66
|
+
if (count === iterable.length) {
|
67
|
+
resolve(result);
|
68
|
+
}
|
69
|
+
};
|
70
|
+
}
|
71
|
+
|
72
|
+
for (var i = 0; i < iterable.length; i += 1) {
|
73
|
+
Promise$1.resolve(iterable[i]).then(resolver(i), reject);
|
74
|
+
}
|
75
|
+
});
|
76
|
+
};
|
77
|
+
|
78
|
+
Promise$1.race = function race(iterable) {
|
79
|
+
return new Promise$1(function (resolve, reject) {
|
80
|
+
for (var i = 0; i < iterable.length; i += 1) {
|
81
|
+
Promise$1.resolve(iterable[i]).then(resolve, reject);
|
82
|
+
}
|
83
|
+
});
|
84
|
+
};
|
85
|
+
|
86
|
+
var p$1 = Promise$1.prototype;
|
87
|
+
|
88
|
+
p$1.resolve = function resolve(x) {
|
89
|
+
var promise = this;
|
90
|
+
|
91
|
+
if (promise.state === PENDING) {
|
92
|
+
if (x === promise) {
|
93
|
+
throw new TypeError('Promise settled with itself.');
|
94
|
+
}
|
95
|
+
|
96
|
+
var called = false;
|
97
|
+
|
98
|
+
try {
|
99
|
+
var then = x && x['then'];
|
100
|
+
|
101
|
+
if (x !== null && typeof x === 'object' && typeof then === 'function') {
|
102
|
+
then.call(x, function (x) {
|
103
|
+
if (!called) {
|
104
|
+
promise.resolve(x);
|
105
|
+
}
|
106
|
+
called = true;
|
107
|
+
}, function (r) {
|
108
|
+
if (!called) {
|
109
|
+
promise.reject(r);
|
110
|
+
}
|
111
|
+
called = true;
|
112
|
+
});
|
113
|
+
return;
|
114
|
+
}
|
115
|
+
} catch (e) {
|
116
|
+
if (!called) {
|
117
|
+
promise.reject(e);
|
118
|
+
}
|
119
|
+
return;
|
120
|
+
}
|
121
|
+
|
122
|
+
promise.state = RESOLVED;
|
123
|
+
promise.value = x;
|
124
|
+
promise.notify();
|
125
|
+
}
|
126
|
+
};
|
127
|
+
|
128
|
+
p$1.reject = function reject(reason) {
|
129
|
+
var promise = this;
|
130
|
+
|
131
|
+
if (promise.state === PENDING) {
|
132
|
+
if (reason === promise) {
|
133
|
+
throw new TypeError('Promise settled with itself.');
|
134
|
+
}
|
135
|
+
|
136
|
+
promise.state = REJECTED;
|
137
|
+
promise.value = reason;
|
138
|
+
promise.notify();
|
139
|
+
}
|
140
|
+
};
|
141
|
+
|
142
|
+
p$1.notify = function notify() {
|
143
|
+
var promise = this;
|
144
|
+
|
145
|
+
nextTick(function () {
|
146
|
+
if (promise.state !== PENDING) {
|
147
|
+
while (promise.deferred.length) {
|
148
|
+
var deferred = promise.deferred.shift(),
|
149
|
+
onResolved = deferred[0],
|
150
|
+
onRejected = deferred[1],
|
151
|
+
resolve = deferred[2],
|
152
|
+
reject = deferred[3];
|
153
|
+
|
154
|
+
try {
|
155
|
+
if (promise.state === RESOLVED) {
|
156
|
+
if (typeof onResolved === 'function') {
|
157
|
+
resolve(onResolved.call(undefined, promise.value));
|
158
|
+
} else {
|
159
|
+
resolve(promise.value);
|
160
|
+
}
|
161
|
+
} else if (promise.state === REJECTED) {
|
162
|
+
if (typeof onRejected === 'function') {
|
163
|
+
resolve(onRejected.call(undefined, promise.value));
|
164
|
+
} else {
|
165
|
+
reject(promise.value);
|
166
|
+
}
|
167
|
+
}
|
168
|
+
} catch (e) {
|
169
|
+
reject(e);
|
170
|
+
}
|
171
|
+
}
|
172
|
+
}
|
173
|
+
});
|
174
|
+
};
|
175
|
+
|
176
|
+
p$1.then = function then(onResolved, onRejected) {
|
177
|
+
var promise = this;
|
178
|
+
|
179
|
+
return new Promise$1(function (resolve, reject) {
|
180
|
+
promise.deferred.push([onResolved, onRejected, resolve, reject]);
|
181
|
+
promise.notify();
|
182
|
+
});
|
183
|
+
};
|
184
|
+
|
185
|
+
p$1.catch = function (onRejected) {
|
186
|
+
return this.then(undefined, onRejected);
|
187
|
+
};
|
188
|
+
|
189
|
+
/**
|
190
|
+
* Promise adapter.
|
191
|
+
*/
|
192
|
+
|
193
|
+
if (typeof Promise === 'undefined') {
|
194
|
+
window.Promise = Promise$1;
|
195
|
+
}
|
196
|
+
|
197
|
+
function PromiseObj(executor, context) {
|
198
|
+
|
199
|
+
if (executor instanceof Promise) {
|
200
|
+
this.promise = executor;
|
201
|
+
} else {
|
202
|
+
this.promise = new Promise(executor.bind(context));
|
203
|
+
}
|
204
|
+
|
205
|
+
this.context = context;
|
206
|
+
}
|
207
|
+
|
208
|
+
PromiseObj.all = function (iterable, context) {
|
209
|
+
return new PromiseObj(Promise.all(iterable), context);
|
210
|
+
};
|
211
|
+
|
212
|
+
PromiseObj.resolve = function (value, context) {
|
213
|
+
return new PromiseObj(Promise.resolve(value), context);
|
214
|
+
};
|
215
|
+
|
216
|
+
PromiseObj.reject = function (reason, context) {
|
217
|
+
return new PromiseObj(Promise.reject(reason), context);
|
218
|
+
};
|
219
|
+
|
220
|
+
PromiseObj.race = function (iterable, context) {
|
221
|
+
return new PromiseObj(Promise.race(iterable), context);
|
222
|
+
};
|
223
|
+
|
224
|
+
var p = PromiseObj.prototype;
|
225
|
+
|
226
|
+
p.bind = function (context) {
|
227
|
+
this.context = context;
|
228
|
+
return this;
|
229
|
+
};
|
230
|
+
|
231
|
+
p.then = function (fulfilled, rejected) {
|
232
|
+
|
233
|
+
if (fulfilled && fulfilled.bind && this.context) {
|
234
|
+
fulfilled = fulfilled.bind(this.context);
|
235
|
+
}
|
236
|
+
|
237
|
+
if (rejected && rejected.bind && this.context) {
|
238
|
+
rejected = rejected.bind(this.context);
|
239
|
+
}
|
240
|
+
|
241
|
+
return new PromiseObj(this.promise.then(fulfilled, rejected), this.context);
|
242
|
+
};
|
243
|
+
|
244
|
+
p.catch = function (rejected) {
|
245
|
+
|
246
|
+
if (rejected && rejected.bind && this.context) {
|
247
|
+
rejected = rejected.bind(this.context);
|
248
|
+
}
|
249
|
+
|
250
|
+
return new PromiseObj(this.promise.catch(rejected), this.context);
|
251
|
+
};
|
252
|
+
|
253
|
+
p.finally = function (callback) {
|
254
|
+
|
255
|
+
return this.then(function (value) {
|
256
|
+
callback.call(this);
|
257
|
+
return value;
|
258
|
+
}, function (reason) {
|
259
|
+
callback.call(this);
|
260
|
+
return Promise.reject(reason);
|
261
|
+
});
|
262
|
+
};
|
263
|
+
|
264
|
+
/**
|
265
|
+
* Utility functions.
|
266
|
+
*/
|
267
|
+
|
268
|
+
var debug = false;var util = {};var slice = [].slice;
|
269
|
+
|
270
|
+
|
271
|
+
function Util (Vue) {
|
272
|
+
util = Vue.util;
|
273
|
+
debug = Vue.config.debug || !Vue.config.silent;
|
274
|
+
}
|
275
|
+
|
276
|
+
function warn(msg) {
|
277
|
+
if (typeof console !== 'undefined' && debug) {
|
278
|
+
console.warn('[VueResource warn]: ' + msg);
|
279
|
+
}
|
280
|
+
}
|
281
|
+
|
282
|
+
function error(msg) {
|
283
|
+
if (typeof console !== 'undefined') {
|
284
|
+
console.error(msg);
|
285
|
+
}
|
286
|
+
}
|
287
|
+
|
288
|
+
function nextTick(cb, ctx) {
|
289
|
+
return util.nextTick(cb, ctx);
|
290
|
+
}
|
291
|
+
|
292
|
+
function trim(str) {
|
293
|
+
return str.replace(/^\s*|\s*$/g, '');
|
294
|
+
}
|
295
|
+
|
296
|
+
function toLower(str) {
|
297
|
+
return str ? str.toLowerCase() : '';
|
298
|
+
}
|
299
|
+
|
300
|
+
function toUpper(str) {
|
301
|
+
return str ? str.toUpperCase() : '';
|
302
|
+
}
|
303
|
+
|
304
|
+
var isArray = Array.isArray;
|
305
|
+
|
306
|
+
function isString(val) {
|
307
|
+
return typeof val === 'string';
|
308
|
+
}
|
309
|
+
|
310
|
+
function isBoolean(val) {
|
311
|
+
return val === true || val === false;
|
312
|
+
}
|
313
|
+
|
314
|
+
function isFunction(val) {
|
315
|
+
return typeof val === 'function';
|
316
|
+
}
|
317
|
+
|
318
|
+
function isObject(obj) {
|
319
|
+
return obj !== null && typeof obj === 'object';
|
320
|
+
}
|
321
|
+
|
322
|
+
function isPlainObject(obj) {
|
323
|
+
return isObject(obj) && Object.getPrototypeOf(obj) == Object.prototype;
|
324
|
+
}
|
325
|
+
|
326
|
+
function isBlob(obj) {
|
327
|
+
return typeof Blob !== 'undefined' && obj instanceof Blob;
|
328
|
+
}
|
329
|
+
|
330
|
+
function isFormData(obj) {
|
331
|
+
return typeof FormData !== 'undefined' && obj instanceof FormData;
|
332
|
+
}
|
333
|
+
|
334
|
+
function when(value, fulfilled, rejected) {
|
335
|
+
|
336
|
+
var promise = PromiseObj.resolve(value);
|
337
|
+
|
338
|
+
if (arguments.length < 2) {
|
339
|
+
return promise;
|
340
|
+
}
|
341
|
+
|
342
|
+
return promise.then(fulfilled, rejected);
|
343
|
+
}
|
344
|
+
|
345
|
+
function options(fn, obj, opts) {
|
346
|
+
|
347
|
+
opts = opts || {};
|
348
|
+
|
349
|
+
if (isFunction(opts)) {
|
350
|
+
opts = opts.call(obj);
|
351
|
+
}
|
352
|
+
|
353
|
+
return merge(fn.bind({ $vm: obj, $options: opts }), fn, { $options: opts });
|
354
|
+
}
|
355
|
+
|
356
|
+
function each(obj, iterator) {
|
357
|
+
|
358
|
+
var i, key;
|
359
|
+
|
360
|
+
if (obj && typeof obj.length == 'number') {
|
361
|
+
for (i = 0; i < obj.length; i++) {
|
362
|
+
iterator.call(obj[i], obj[i], i);
|
363
|
+
}
|
364
|
+
} else if (isObject(obj)) {
|
365
|
+
for (key in obj) {
|
366
|
+
if (obj.hasOwnProperty(key)) {
|
367
|
+
iterator.call(obj[key], obj[key], key);
|
368
|
+
}
|
369
|
+
}
|
370
|
+
}
|
371
|
+
|
372
|
+
return obj;
|
373
|
+
}
|
374
|
+
|
375
|
+
var assign = Object.assign || _assign;
|
376
|
+
|
377
|
+
function merge(target) {
|
378
|
+
|
379
|
+
var args = slice.call(arguments, 1);
|
380
|
+
|
381
|
+
args.forEach(function (source) {
|
382
|
+
_merge(target, source, true);
|
383
|
+
});
|
384
|
+
|
385
|
+
return target;
|
386
|
+
}
|
387
|
+
|
388
|
+
function defaults(target) {
|
389
|
+
|
390
|
+
var args = slice.call(arguments, 1);
|
391
|
+
|
392
|
+
args.forEach(function (source) {
|
393
|
+
|
394
|
+
for (var key in source) {
|
395
|
+
if (target[key] === undefined) {
|
396
|
+
target[key] = source[key];
|
397
|
+
}
|
398
|
+
}
|
399
|
+
});
|
400
|
+
|
401
|
+
return target;
|
402
|
+
}
|
403
|
+
|
404
|
+
function _assign(target) {
|
405
|
+
|
406
|
+
var args = slice.call(arguments, 1);
|
407
|
+
|
408
|
+
args.forEach(function (source) {
|
409
|
+
_merge(target, source);
|
410
|
+
});
|
411
|
+
|
412
|
+
return target;
|
413
|
+
}
|
414
|
+
|
415
|
+
function _merge(target, source, deep) {
|
416
|
+
for (var key in source) {
|
417
|
+
if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
|
418
|
+
if (isPlainObject(source[key]) && !isPlainObject(target[key])) {
|
419
|
+
target[key] = {};
|
420
|
+
}
|
421
|
+
if (isArray(source[key]) && !isArray(target[key])) {
|
422
|
+
target[key] = [];
|
423
|
+
}
|
424
|
+
_merge(target[key], source[key], deep);
|
425
|
+
} else if (source[key] !== undefined) {
|
426
|
+
target[key] = source[key];
|
427
|
+
}
|
428
|
+
}
|
429
|
+
}
|
430
|
+
|
431
|
+
/**
|
432
|
+
* Root Prefix Transform.
|
433
|
+
*/
|
434
|
+
|
435
|
+
function root (options, next) {
|
436
|
+
|
437
|
+
var url = next(options);
|
438
|
+
|
439
|
+
if (isString(options.root) && !url.match(/^(https?:)?\//)) {
|
440
|
+
url = options.root + '/' + url;
|
441
|
+
}
|
442
|
+
|
443
|
+
return url;
|
444
|
+
}
|
445
|
+
|
446
|
+
/**
|
447
|
+
* Query Parameter Transform.
|
448
|
+
*/
|
449
|
+
|
450
|
+
function query (options, next) {
|
451
|
+
|
452
|
+
var urlParams = Object.keys(Url.options.params),
|
453
|
+
query = {},
|
454
|
+
url = next(options);
|
455
|
+
|
456
|
+
each(options.params, function (value, key) {
|
457
|
+
if (urlParams.indexOf(key) === -1) {
|
458
|
+
query[key] = value;
|
459
|
+
}
|
460
|
+
});
|
461
|
+
|
462
|
+
query = Url.params(query);
|
463
|
+
|
464
|
+
if (query) {
|
465
|
+
url += (url.indexOf('?') == -1 ? '?' : '&') + query;
|
466
|
+
}
|
467
|
+
|
468
|
+
return url;
|
469
|
+
}
|
470
|
+
|
471
|
+
/**
|
472
|
+
* URL Template v2.0.6 (https://github.com/bramstein/url-template)
|
473
|
+
*/
|
474
|
+
|
475
|
+
function expand(url, params, variables) {
|
476
|
+
|
477
|
+
var tmpl = parse(url),
|
478
|
+
expanded = tmpl.expand(params);
|
479
|
+
|
480
|
+
if (variables) {
|
481
|
+
variables.push.apply(variables, tmpl.vars);
|
482
|
+
}
|
483
|
+
|
484
|
+
return expanded;
|
485
|
+
}
|
486
|
+
|
487
|
+
function parse(template) {
|
488
|
+
|
489
|
+
var operators = ['+', '#', '.', '/', ';', '?', '&'],
|
490
|
+
variables = [];
|
491
|
+
|
492
|
+
return {
|
493
|
+
vars: variables,
|
494
|
+
expand: function (context) {
|
495
|
+
return template.replace(/\{([^\{\}]+)\}|([^\{\}]+)/g, function (_, expression, literal) {
|
496
|
+
if (expression) {
|
497
|
+
|
498
|
+
var operator = null,
|
499
|
+
values = [];
|
500
|
+
|
501
|
+
if (operators.indexOf(expression.charAt(0)) !== -1) {
|
502
|
+
operator = expression.charAt(0);
|
503
|
+
expression = expression.substr(1);
|
504
|
+
}
|
505
|
+
|
506
|
+
expression.split(/,/g).forEach(function (variable) {
|
507
|
+
var tmp = /([^:\*]*)(?::(\d+)|(\*))?/.exec(variable);
|
508
|
+
values.push.apply(values, getValues(context, operator, tmp[1], tmp[2] || tmp[3]));
|
509
|
+
variables.push(tmp[1]);
|
510
|
+
});
|
511
|
+
|
512
|
+
if (operator && operator !== '+') {
|
513
|
+
|
514
|
+
var separator = ',';
|
515
|
+
|
516
|
+
if (operator === '?') {
|
517
|
+
separator = '&';
|
518
|
+
} else if (operator !== '#') {
|
519
|
+
separator = operator;
|
520
|
+
}
|
521
|
+
|
522
|
+
return (values.length !== 0 ? operator : '') + values.join(separator);
|
523
|
+
} else {
|
524
|
+
return values.join(',');
|
525
|
+
}
|
526
|
+
} else {
|
527
|
+
return encodeReserved(literal);
|
528
|
+
}
|
529
|
+
});
|
530
|
+
}
|
531
|
+
};
|
532
|
+
}
|
533
|
+
|
534
|
+
function getValues(context, operator, key, modifier) {
|
535
|
+
|
536
|
+
var value = context[key],
|
537
|
+
result = [];
|
538
|
+
|
539
|
+
if (isDefined(value) && value !== '') {
|
540
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
541
|
+
value = value.toString();
|
542
|
+
|
543
|
+
if (modifier && modifier !== '*') {
|
544
|
+
value = value.substring(0, parseInt(modifier, 10));
|
545
|
+
}
|
546
|
+
|
547
|
+
result.push(encodeValue(operator, value, isKeyOperator(operator) ? key : null));
|
548
|
+
} else {
|
549
|
+
if (modifier === '*') {
|
550
|
+
if (Array.isArray(value)) {
|
551
|
+
value.filter(isDefined).forEach(function (value) {
|
552
|
+
result.push(encodeValue(operator, value, isKeyOperator(operator) ? key : null));
|
553
|
+
});
|
554
|
+
} else {
|
555
|
+
Object.keys(value).forEach(function (k) {
|
556
|
+
if (isDefined(value[k])) {
|
557
|
+
result.push(encodeValue(operator, value[k], k));
|
558
|
+
}
|
559
|
+
});
|
560
|
+
}
|
561
|
+
} else {
|
562
|
+
var tmp = [];
|
563
|
+
|
564
|
+
if (Array.isArray(value)) {
|
565
|
+
value.filter(isDefined).forEach(function (value) {
|
566
|
+
tmp.push(encodeValue(operator, value));
|
567
|
+
});
|
568
|
+
} else {
|
569
|
+
Object.keys(value).forEach(function (k) {
|
570
|
+
if (isDefined(value[k])) {
|
571
|
+
tmp.push(encodeURIComponent(k));
|
572
|
+
tmp.push(encodeValue(operator, value[k].toString()));
|
573
|
+
}
|
574
|
+
});
|
575
|
+
}
|
576
|
+
|
577
|
+
if (isKeyOperator(operator)) {
|
578
|
+
result.push(encodeURIComponent(key) + '=' + tmp.join(','));
|
579
|
+
} else if (tmp.length !== 0) {
|
580
|
+
result.push(tmp.join(','));
|
581
|
+
}
|
582
|
+
}
|
583
|
+
}
|
584
|
+
} else {
|
585
|
+
if (operator === ';') {
|
586
|
+
result.push(encodeURIComponent(key));
|
587
|
+
} else if (value === '' && (operator === '&' || operator === '?')) {
|
588
|
+
result.push(encodeURIComponent(key) + '=');
|
589
|
+
} else if (value === '') {
|
590
|
+
result.push('');
|
591
|
+
}
|
592
|
+
}
|
593
|
+
|
594
|
+
return result;
|
595
|
+
}
|
596
|
+
|
597
|
+
function isDefined(value) {
|
598
|
+
return value !== undefined && value !== null;
|
599
|
+
}
|
600
|
+
|
601
|
+
function isKeyOperator(operator) {
|
602
|
+
return operator === ';' || operator === '&' || operator === '?';
|
603
|
+
}
|
604
|
+
|
605
|
+
function encodeValue(operator, value, key) {
|
606
|
+
|
607
|
+
value = operator === '+' || operator === '#' ? encodeReserved(value) : encodeURIComponent(value);
|
608
|
+
|
609
|
+
if (key) {
|
610
|
+
return encodeURIComponent(key) + '=' + value;
|
611
|
+
} else {
|
612
|
+
return value;
|
613
|
+
}
|
614
|
+
}
|
615
|
+
|
616
|
+
function encodeReserved(str) {
|
617
|
+
return str.split(/(%[0-9A-Fa-f]{2})/g).map(function (part) {
|
618
|
+
if (!/%[0-9A-Fa-f]/.test(part)) {
|
619
|
+
part = encodeURI(part);
|
620
|
+
}
|
621
|
+
return part;
|
622
|
+
}).join('');
|
623
|
+
}
|
624
|
+
|
625
|
+
/**
|
626
|
+
* URL Template (RFC 6570) Transform.
|
627
|
+
*/
|
628
|
+
|
629
|
+
function template (options) {
|
630
|
+
|
631
|
+
var variables = [],
|
632
|
+
url = expand(options.url, options.params, variables);
|
633
|
+
|
634
|
+
variables.forEach(function (key) {
|
635
|
+
delete options.params[key];
|
636
|
+
});
|
637
|
+
|
638
|
+
return url;
|
639
|
+
}
|
640
|
+
|
641
|
+
/**
|
642
|
+
* Service for URL templating.
|
643
|
+
*/
|
644
|
+
|
645
|
+
var ie = document.documentMode;
|
646
|
+
var el = document.createElement('a');
|
647
|
+
|
648
|
+
function Url(url, params) {
|
649
|
+
|
650
|
+
var self = this || {},
|
651
|
+
options = url,
|
652
|
+
transform;
|
653
|
+
|
654
|
+
if (isString(url)) {
|
655
|
+
options = { url: url, params: params };
|
656
|
+
}
|
657
|
+
|
658
|
+
options = merge({}, Url.options, self.$options, options);
|
659
|
+
|
660
|
+
Url.transforms.forEach(function (handler) {
|
661
|
+
transform = factory(handler, transform, self.$vm);
|
662
|
+
});
|
663
|
+
|
664
|
+
return transform(options);
|
665
|
+
}
|
666
|
+
|
667
|
+
/**
|
668
|
+
* Url options.
|
669
|
+
*/
|
670
|
+
|
671
|
+
Url.options = {
|
672
|
+
url: '',
|
673
|
+
root: null,
|
674
|
+
params: {}
|
675
|
+
};
|
676
|
+
|
677
|
+
/**
|
678
|
+
* Url transforms.
|
679
|
+
*/
|
680
|
+
|
681
|
+
Url.transforms = [template, query, root];
|
682
|
+
|
683
|
+
/**
|
684
|
+
* Encodes a Url parameter string.
|
685
|
+
*
|
686
|
+
* @param {Object} obj
|
687
|
+
*/
|
688
|
+
|
689
|
+
Url.params = function (obj) {
|
690
|
+
|
691
|
+
var params = [],
|
692
|
+
escape = encodeURIComponent;
|
693
|
+
|
694
|
+
params.add = function (key, value) {
|
695
|
+
|
696
|
+
if (isFunction(value)) {
|
697
|
+
value = value();
|
698
|
+
}
|
699
|
+
|
700
|
+
if (value === null) {
|
701
|
+
value = '';
|
702
|
+
}
|
703
|
+
|
704
|
+
this.push(escape(key) + '=' + escape(value));
|
705
|
+
};
|
706
|
+
|
707
|
+
serialize(params, obj);
|
708
|
+
|
709
|
+
return params.join('&').replace(/%20/g, '+');
|
710
|
+
};
|
711
|
+
|
712
|
+
/**
|
713
|
+
* Parse a URL and return its components.
|
714
|
+
*
|
715
|
+
* @param {String} url
|
716
|
+
*/
|
717
|
+
|
718
|
+
Url.parse = function (url) {
|
719
|
+
|
720
|
+
if (ie) {
|
721
|
+
el.href = url;
|
722
|
+
url = el.href;
|
723
|
+
}
|
724
|
+
|
725
|
+
el.href = url;
|
726
|
+
|
727
|
+
return {
|
728
|
+
href: el.href,
|
729
|
+
protocol: el.protocol ? el.protocol.replace(/:$/, '') : '',
|
730
|
+
port: el.port,
|
731
|
+
host: el.host,
|
732
|
+
hostname: el.hostname,
|
733
|
+
pathname: el.pathname.charAt(0) === '/' ? el.pathname : '/' + el.pathname,
|
734
|
+
search: el.search ? el.search.replace(/^\?/, '') : '',
|
735
|
+
hash: el.hash ? el.hash.replace(/^#/, '') : ''
|
736
|
+
};
|
737
|
+
};
|
738
|
+
|
739
|
+
function factory(handler, next, vm) {
|
740
|
+
return function (options) {
|
741
|
+
return handler.call(vm, options, next);
|
742
|
+
};
|
743
|
+
}
|
744
|
+
|
745
|
+
function serialize(params, obj, scope) {
|
746
|
+
|
747
|
+
var array = isArray(obj),
|
748
|
+
plain = isPlainObject(obj),
|
749
|
+
hash;
|
750
|
+
|
751
|
+
each(obj, function (value, key) {
|
752
|
+
|
753
|
+
hash = isObject(value) || isArray(value);
|
754
|
+
|
755
|
+
if (scope) {
|
756
|
+
key = scope + '[' + (plain || hash ? key : '') + ']';
|
757
|
+
}
|
758
|
+
|
759
|
+
if (!scope && array) {
|
760
|
+
params.add(value.name, value.value);
|
761
|
+
} else if (hash) {
|
762
|
+
serialize(params, value, key);
|
763
|
+
} else {
|
764
|
+
params.add(key, value);
|
765
|
+
}
|
766
|
+
});
|
767
|
+
}
|
768
|
+
|
769
|
+
/**
|
770
|
+
* XDomain client (Internet Explorer).
|
771
|
+
*/
|
772
|
+
|
773
|
+
function xdrClient (request) {
|
774
|
+
return new PromiseObj(function (resolve) {
|
775
|
+
|
776
|
+
var xdr = new XDomainRequest(),
|
777
|
+
handler = function (_ref) {
|
778
|
+
var type = _ref.type;
|
779
|
+
|
780
|
+
|
781
|
+
var status = 0;
|
782
|
+
|
783
|
+
if (type === 'load') {
|
784
|
+
status = 200;
|
785
|
+
} else if (type === 'error') {
|
786
|
+
status = 500;
|
787
|
+
}
|
788
|
+
|
789
|
+
resolve(request.respondWith(xdr.responseText, { status: status }));
|
790
|
+
};
|
791
|
+
|
792
|
+
request.abort = function () {
|
793
|
+
return xdr.abort();
|
794
|
+
};
|
795
|
+
|
796
|
+
xdr.open(request.method, request.getUrl());
|
797
|
+
xdr.timeout = 0;
|
798
|
+
xdr.onload = handler;
|
799
|
+
xdr.onerror = handler;
|
800
|
+
xdr.ontimeout = handler;
|
801
|
+
xdr.onprogress = function () {};
|
802
|
+
xdr.send(request.getBody());
|
803
|
+
});
|
804
|
+
}
|
805
|
+
|
806
|
+
/**
|
807
|
+
* CORS Interceptor.
|
808
|
+
*/
|
809
|
+
|
810
|
+
var ORIGIN_URL = Url.parse(location.href);
|
811
|
+
var SUPPORTS_CORS = 'withCredentials' in new XMLHttpRequest();
|
812
|
+
|
813
|
+
function cors (request, next) {
|
814
|
+
|
815
|
+
if (!isBoolean(request.crossOrigin) && crossOrigin(request)) {
|
816
|
+
request.crossOrigin = true;
|
817
|
+
}
|
818
|
+
|
819
|
+
if (request.crossOrigin) {
|
820
|
+
|
821
|
+
if (!SUPPORTS_CORS) {
|
822
|
+
request.client = xdrClient;
|
823
|
+
}
|
824
|
+
|
825
|
+
delete request.emulateHTTP;
|
826
|
+
}
|
827
|
+
|
828
|
+
next();
|
829
|
+
}
|
830
|
+
|
831
|
+
function crossOrigin(request) {
|
832
|
+
|
833
|
+
var requestUrl = Url.parse(Url(request));
|
834
|
+
|
835
|
+
return requestUrl.protocol !== ORIGIN_URL.protocol || requestUrl.host !== ORIGIN_URL.host;
|
836
|
+
}
|
837
|
+
|
838
|
+
/**
|
839
|
+
* Body Interceptor.
|
840
|
+
*/
|
841
|
+
|
842
|
+
function body (request, next) {
|
843
|
+
|
844
|
+
if (isFormData(request.body)) {
|
845
|
+
|
846
|
+
request.headers.delete('Content-Type');
|
847
|
+
} else if (isObject(request.body) || isArray(request.body)) {
|
848
|
+
|
849
|
+
if (request.emulateJSON) {
|
850
|
+
request.body = Url.params(request.body);
|
851
|
+
request.headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
852
|
+
} else {
|
853
|
+
request.body = JSON.stringify(request.body);
|
854
|
+
}
|
855
|
+
}
|
856
|
+
|
857
|
+
next(function (response) {
|
858
|
+
|
859
|
+
Object.defineProperty(response, 'data', {
|
860
|
+
get: function () {
|
861
|
+
return this.body;
|
862
|
+
},
|
863
|
+
set: function (body) {
|
864
|
+
this.body = body;
|
865
|
+
}
|
866
|
+
});
|
867
|
+
|
868
|
+
return response.bodyText ? when(response.text(), function (text) {
|
869
|
+
|
870
|
+
var type = response.headers.get('Content-Type');
|
871
|
+
|
872
|
+
if (isString(type) && type.indexOf('application/json') === 0) {
|
873
|
+
|
874
|
+
try {
|
875
|
+
response.body = JSON.parse(text);
|
876
|
+
} catch (e) {
|
877
|
+
response.body = null;
|
878
|
+
}
|
879
|
+
} else {
|
880
|
+
response.body = text;
|
881
|
+
}
|
882
|
+
|
883
|
+
return response;
|
884
|
+
}) : response;
|
885
|
+
});
|
886
|
+
}
|
887
|
+
|
888
|
+
/**
|
889
|
+
* JSONP client.
|
890
|
+
*/
|
891
|
+
|
892
|
+
function jsonpClient (request) {
|
893
|
+
return new PromiseObj(function (resolve) {
|
894
|
+
|
895
|
+
var name = request.jsonp || 'callback',
|
896
|
+
callback = '_jsonp' + Math.random().toString(36).substr(2),
|
897
|
+
body = null,
|
898
|
+
handler,
|
899
|
+
script;
|
900
|
+
|
901
|
+
handler = function (_ref) {
|
902
|
+
var type = _ref.type;
|
903
|
+
|
904
|
+
|
905
|
+
var status = 0;
|
906
|
+
|
907
|
+
if (type === 'load' && body !== null) {
|
908
|
+
status = 200;
|
909
|
+
} else if (type === 'error') {
|
910
|
+
status = 500;
|
911
|
+
}
|
912
|
+
|
913
|
+
resolve(request.respondWith(body, { status: status }));
|
914
|
+
|
915
|
+
delete window[callback];
|
916
|
+
document.body.removeChild(script);
|
917
|
+
};
|
918
|
+
|
919
|
+
request.params[name] = callback;
|
920
|
+
|
921
|
+
window[callback] = function (result) {
|
922
|
+
body = JSON.stringify(result);
|
923
|
+
};
|
924
|
+
|
925
|
+
script = document.createElement('script');
|
926
|
+
script.src = request.getUrl();
|
927
|
+
script.type = 'text/javascript';
|
928
|
+
script.async = true;
|
929
|
+
script.onload = handler;
|
930
|
+
script.onerror = handler;
|
931
|
+
|
932
|
+
document.body.appendChild(script);
|
933
|
+
});
|
934
|
+
}
|
935
|
+
|
936
|
+
/**
|
937
|
+
* JSONP Interceptor.
|
938
|
+
*/
|
939
|
+
|
940
|
+
function jsonp (request, next) {
|
941
|
+
|
942
|
+
if (request.method == 'JSONP') {
|
943
|
+
request.client = jsonpClient;
|
944
|
+
}
|
945
|
+
|
946
|
+
next(function (response) {
|
947
|
+
|
948
|
+
if (request.method == 'JSONP') {
|
949
|
+
|
950
|
+
return when(response.json(), function (json) {
|
951
|
+
|
952
|
+
response.body = json;
|
953
|
+
|
954
|
+
return response;
|
955
|
+
});
|
956
|
+
}
|
957
|
+
});
|
958
|
+
}
|
959
|
+
|
960
|
+
/**
|
961
|
+
* Before Interceptor.
|
962
|
+
*/
|
963
|
+
|
964
|
+
function before (request, next) {
|
965
|
+
|
966
|
+
if (isFunction(request.before)) {
|
967
|
+
request.before.call(this, request);
|
968
|
+
}
|
969
|
+
|
970
|
+
next();
|
971
|
+
}
|
972
|
+
|
973
|
+
/**
|
974
|
+
* HTTP method override Interceptor.
|
975
|
+
*/
|
976
|
+
|
977
|
+
function method (request, next) {
|
978
|
+
|
979
|
+
if (request.emulateHTTP && /^(PUT|PATCH|DELETE)$/i.test(request.method)) {
|
980
|
+
request.headers.set('X-HTTP-Method-Override', request.method);
|
981
|
+
request.method = 'POST';
|
982
|
+
}
|
983
|
+
|
984
|
+
next();
|
985
|
+
}
|
986
|
+
|
987
|
+
/**
|
988
|
+
* Header Interceptor.
|
989
|
+
*/
|
990
|
+
|
991
|
+
function header (request, next) {
|
992
|
+
|
993
|
+
var headers = assign({}, Http.headers.common, !request.crossOrigin ? Http.headers.custom : {}, Http.headers[toLower(request.method)]);
|
994
|
+
|
995
|
+
each(headers, function (value, name) {
|
996
|
+
if (!request.headers.has(name)) {
|
997
|
+
request.headers.set(name, value);
|
998
|
+
}
|
999
|
+
});
|
1000
|
+
|
1001
|
+
next();
|
1002
|
+
}
|
1003
|
+
|
1004
|
+
/**
|
1005
|
+
* Timeout Interceptor.
|
1006
|
+
*/
|
1007
|
+
|
1008
|
+
function timeout (request, next) {
|
1009
|
+
|
1010
|
+
var timeout;
|
1011
|
+
|
1012
|
+
if (request.timeout) {
|
1013
|
+
timeout = setTimeout(function () {
|
1014
|
+
request.abort();
|
1015
|
+
}, request.timeout);
|
1016
|
+
}
|
1017
|
+
|
1018
|
+
next(function (response) {
|
1019
|
+
|
1020
|
+
clearTimeout(timeout);
|
1021
|
+
});
|
1022
|
+
}
|
1023
|
+
|
1024
|
+
/**
|
1025
|
+
* XMLHttp client.
|
1026
|
+
*/
|
1027
|
+
|
1028
|
+
function xhrClient (request) {
|
1029
|
+
return new PromiseObj(function (resolve) {
|
1030
|
+
|
1031
|
+
var xhr = new XMLHttpRequest(),
|
1032
|
+
handler = function (event) {
|
1033
|
+
|
1034
|
+
var response = request.respondWith('response' in xhr ? xhr.response : xhr.responseText, {
|
1035
|
+
status: xhr.status === 1223 ? 204 : xhr.status, // IE9 status bug
|
1036
|
+
statusText: xhr.status === 1223 ? 'No Content' : trim(xhr.statusText)
|
1037
|
+
});
|
1038
|
+
|
1039
|
+
each(trim(xhr.getAllResponseHeaders()).split('\n'), function (row) {
|
1040
|
+
response.headers.append(row.slice(0, row.indexOf(':')), row.slice(row.indexOf(':') + 1));
|
1041
|
+
});
|
1042
|
+
|
1043
|
+
resolve(response);
|
1044
|
+
};
|
1045
|
+
|
1046
|
+
request.abort = function () {
|
1047
|
+
return xhr.abort();
|
1048
|
+
};
|
1049
|
+
|
1050
|
+
if (request.progress) {
|
1051
|
+
if (request.method === 'GET') {
|
1052
|
+
xhr.addEventListener('progress', request.progress);
|
1053
|
+
} else if (/^(POST|PUT)$/i.test(request.method)) {
|
1054
|
+
xhr.upload.addEventListener('progress', request.progress);
|
1055
|
+
}
|
1056
|
+
}
|
1057
|
+
|
1058
|
+
xhr.open(request.method, request.getUrl(), true);
|
1059
|
+
|
1060
|
+
if ('responseType' in xhr) {
|
1061
|
+
xhr.responseType = 'blob';
|
1062
|
+
}
|
1063
|
+
|
1064
|
+
if (request.credentials === true) {
|
1065
|
+
xhr.withCredentials = true;
|
1066
|
+
}
|
1067
|
+
|
1068
|
+
request.headers.forEach(function (value, name) {
|
1069
|
+
xhr.setRequestHeader(name, value);
|
1070
|
+
});
|
1071
|
+
|
1072
|
+
xhr.timeout = 0;
|
1073
|
+
xhr.onload = handler;
|
1074
|
+
xhr.onerror = handler;
|
1075
|
+
xhr.send(request.getBody());
|
1076
|
+
});
|
1077
|
+
}
|
1078
|
+
|
1079
|
+
/**
|
1080
|
+
* Base client.
|
1081
|
+
*/
|
1082
|
+
|
1083
|
+
function Client (context) {
|
1084
|
+
|
1085
|
+
var reqHandlers = [sendRequest],
|
1086
|
+
resHandlers = [],
|
1087
|
+
handler;
|
1088
|
+
|
1089
|
+
if (!isObject(context)) {
|
1090
|
+
context = null;
|
1091
|
+
}
|
1092
|
+
|
1093
|
+
function Client(request) {
|
1094
|
+
return new PromiseObj(function (resolve) {
|
1095
|
+
|
1096
|
+
function exec() {
|
1097
|
+
|
1098
|
+
handler = reqHandlers.pop();
|
1099
|
+
|
1100
|
+
if (isFunction(handler)) {
|
1101
|
+
handler.call(context, request, next);
|
1102
|
+
} else {
|
1103
|
+
warn('Invalid interceptor of type ' + typeof handler + ', must be a function');
|
1104
|
+
next();
|
1105
|
+
}
|
1106
|
+
}
|
1107
|
+
|
1108
|
+
function next(response) {
|
1109
|
+
|
1110
|
+
if (isFunction(response)) {
|
1111
|
+
|
1112
|
+
resHandlers.unshift(response);
|
1113
|
+
} else if (isObject(response)) {
|
1114
|
+
|
1115
|
+
resHandlers.forEach(function (handler) {
|
1116
|
+
response = when(response, function (response) {
|
1117
|
+
return handler.call(context, response) || response;
|
1118
|
+
});
|
1119
|
+
});
|
1120
|
+
|
1121
|
+
when(response, resolve);
|
1122
|
+
|
1123
|
+
return;
|
1124
|
+
}
|
1125
|
+
|
1126
|
+
exec();
|
1127
|
+
}
|
1128
|
+
|
1129
|
+
exec();
|
1130
|
+
}, context);
|
1131
|
+
}
|
1132
|
+
|
1133
|
+
Client.use = function (handler) {
|
1134
|
+
reqHandlers.push(handler);
|
1135
|
+
};
|
1136
|
+
|
1137
|
+
return Client;
|
1138
|
+
}
|
1139
|
+
|
1140
|
+
function sendRequest(request, resolve) {
|
1141
|
+
|
1142
|
+
var client = request.client || xhrClient;
|
1143
|
+
|
1144
|
+
resolve(client(request));
|
1145
|
+
}
|
1146
|
+
|
1147
|
+
var classCallCheck = function (instance, Constructor) {
|
1148
|
+
if (!(instance instanceof Constructor)) {
|
1149
|
+
throw new TypeError("Cannot call a class as a function");
|
1150
|
+
}
|
1151
|
+
};
|
1152
|
+
|
1153
|
+
/**
|
1154
|
+
* HTTP Headers.
|
1155
|
+
*/
|
1156
|
+
|
1157
|
+
var Headers = function () {
|
1158
|
+
function Headers(headers) {
|
1159
|
+
var _this = this;
|
1160
|
+
|
1161
|
+
classCallCheck(this, Headers);
|
1162
|
+
|
1163
|
+
|
1164
|
+
this.map = {};
|
1165
|
+
|
1166
|
+
each(headers, function (value, name) {
|
1167
|
+
return _this.append(name, value);
|
1168
|
+
});
|
1169
|
+
}
|
1170
|
+
|
1171
|
+
Headers.prototype.has = function has(name) {
|
1172
|
+
return getName(this.map, name) !== null;
|
1173
|
+
};
|
1174
|
+
|
1175
|
+
Headers.prototype.get = function get(name) {
|
1176
|
+
|
1177
|
+
var list = this.map[getName(this.map, name)];
|
1178
|
+
|
1179
|
+
return list ? list[0] : null;
|
1180
|
+
};
|
1181
|
+
|
1182
|
+
Headers.prototype.getAll = function getAll(name) {
|
1183
|
+
return this.map[getName(this.map, name)] || [];
|
1184
|
+
};
|
1185
|
+
|
1186
|
+
Headers.prototype.set = function set(name, value) {
|
1187
|
+
this.map[normalizeName(getName(this.map, name) || name)] = [trim(value)];
|
1188
|
+
};
|
1189
|
+
|
1190
|
+
Headers.prototype.append = function append(name, value) {
|
1191
|
+
|
1192
|
+
var list = this.getAll(name);
|
1193
|
+
|
1194
|
+
if (list.length) {
|
1195
|
+
list.push(trim(value));
|
1196
|
+
} else {
|
1197
|
+
this.set(name, value);
|
1198
|
+
}
|
1199
|
+
};
|
1200
|
+
|
1201
|
+
Headers.prototype.delete = function _delete(name) {
|
1202
|
+
delete this.map[getName(this.map, name)];
|
1203
|
+
};
|
1204
|
+
|
1205
|
+
Headers.prototype.forEach = function forEach(callback, thisArg) {
|
1206
|
+
var _this2 = this;
|
1207
|
+
|
1208
|
+
each(this.map, function (list, name) {
|
1209
|
+
each(list, function (value) {
|
1210
|
+
return callback.call(thisArg, value, name, _this2);
|
1211
|
+
});
|
1212
|
+
});
|
1213
|
+
};
|
1214
|
+
|
1215
|
+
return Headers;
|
1216
|
+
}();
|
1217
|
+
|
1218
|
+
function getName(map, name) {
|
1219
|
+
return Object.keys(map).reduce(function (prev, curr) {
|
1220
|
+
return toLower(name) === toLower(curr) ? curr : prev;
|
1221
|
+
}, null);
|
1222
|
+
}
|
1223
|
+
|
1224
|
+
function normalizeName(name) {
|
1225
|
+
|
1226
|
+
if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) {
|
1227
|
+
throw new TypeError('Invalid character in header field name');
|
1228
|
+
}
|
1229
|
+
|
1230
|
+
return trim(name);
|
1231
|
+
}
|
1232
|
+
|
1233
|
+
/**
|
1234
|
+
* HTTP Response.
|
1235
|
+
*/
|
1236
|
+
|
1237
|
+
var Response = function () {
|
1238
|
+
function Response(body, _ref) {
|
1239
|
+
var url = _ref.url;
|
1240
|
+
var headers = _ref.headers;
|
1241
|
+
var status = _ref.status;
|
1242
|
+
var statusText = _ref.statusText;
|
1243
|
+
classCallCheck(this, Response);
|
1244
|
+
|
1245
|
+
|
1246
|
+
this.url = url;
|
1247
|
+
this.ok = status >= 200 && status < 300;
|
1248
|
+
this.status = status || 0;
|
1249
|
+
this.statusText = statusText || '';
|
1250
|
+
this.headers = new Headers(headers);
|
1251
|
+
this.body = body;
|
1252
|
+
|
1253
|
+
if (isString(body)) {
|
1254
|
+
|
1255
|
+
this.bodyText = body;
|
1256
|
+
} else if (isBlob(body)) {
|
1257
|
+
|
1258
|
+
this.bodyBlob = body;
|
1259
|
+
|
1260
|
+
if (isBlobText(body)) {
|
1261
|
+
this.bodyText = blobText(body);
|
1262
|
+
}
|
1263
|
+
}
|
1264
|
+
}
|
1265
|
+
|
1266
|
+
Response.prototype.blob = function blob() {
|
1267
|
+
return when(this.bodyBlob);
|
1268
|
+
};
|
1269
|
+
|
1270
|
+
Response.prototype.text = function text() {
|
1271
|
+
return when(this.bodyText);
|
1272
|
+
};
|
1273
|
+
|
1274
|
+
Response.prototype.json = function json() {
|
1275
|
+
return when(this.text(), function (text) {
|
1276
|
+
return JSON.parse(text);
|
1277
|
+
});
|
1278
|
+
};
|
1279
|
+
|
1280
|
+
return Response;
|
1281
|
+
}();
|
1282
|
+
|
1283
|
+
function blobText(body) {
|
1284
|
+
return new PromiseObj(function (resolve) {
|
1285
|
+
|
1286
|
+
var reader = new FileReader();
|
1287
|
+
|
1288
|
+
reader.readAsText(body);
|
1289
|
+
reader.onload = function () {
|
1290
|
+
resolve(reader.result);
|
1291
|
+
};
|
1292
|
+
});
|
1293
|
+
}
|
1294
|
+
|
1295
|
+
function isBlobText(body) {
|
1296
|
+
return body.type.indexOf('text') === 0 || body.type.indexOf('json') !== -1;
|
1297
|
+
}
|
1298
|
+
|
1299
|
+
/**
|
1300
|
+
* HTTP Request.
|
1301
|
+
*/
|
1302
|
+
|
1303
|
+
var Request = function () {
|
1304
|
+
function Request(options) {
|
1305
|
+
classCallCheck(this, Request);
|
1306
|
+
|
1307
|
+
|
1308
|
+
this.body = null;
|
1309
|
+
this.params = {};
|
1310
|
+
|
1311
|
+
assign(this, options, {
|
1312
|
+
method: toUpper(options.method || 'GET')
|
1313
|
+
});
|
1314
|
+
|
1315
|
+
if (!(this.headers instanceof Headers)) {
|
1316
|
+
this.headers = new Headers(this.headers);
|
1317
|
+
}
|
1318
|
+
}
|
1319
|
+
|
1320
|
+
Request.prototype.getUrl = function getUrl() {
|
1321
|
+
return Url(this);
|
1322
|
+
};
|
1323
|
+
|
1324
|
+
Request.prototype.getBody = function getBody() {
|
1325
|
+
return this.body;
|
1326
|
+
};
|
1327
|
+
|
1328
|
+
Request.prototype.respondWith = function respondWith(body, options) {
|
1329
|
+
return new Response(body, assign(options || {}, { url: this.getUrl() }));
|
1330
|
+
};
|
1331
|
+
|
1332
|
+
return Request;
|
1333
|
+
}();
|
1334
|
+
|
1335
|
+
/**
|
1336
|
+
* Service for sending network requests.
|
1337
|
+
*/
|
1338
|
+
|
1339
|
+
var CUSTOM_HEADERS = { 'X-Requested-With': 'XMLHttpRequest' };
|
1340
|
+
var COMMON_HEADERS = { 'Accept': 'application/json, text/plain, */*' };
|
1341
|
+
var JSON_CONTENT_TYPE = { 'Content-Type': 'application/json;charset=utf-8' };
|
1342
|
+
|
1343
|
+
function Http(options) {
|
1344
|
+
|
1345
|
+
var self = this || {},
|
1346
|
+
client = Client(self.$vm);
|
1347
|
+
|
1348
|
+
defaults(options || {}, self.$options, Http.options);
|
1349
|
+
|
1350
|
+
Http.interceptors.forEach(function (handler) {
|
1351
|
+
client.use(handler);
|
1352
|
+
});
|
1353
|
+
|
1354
|
+
return client(new Request(options)).then(function (response) {
|
1355
|
+
|
1356
|
+
return response.ok ? response : PromiseObj.reject(response);
|
1357
|
+
}, function (response) {
|
1358
|
+
|
1359
|
+
if (response instanceof Error) {
|
1360
|
+
error(response);
|
1361
|
+
}
|
1362
|
+
|
1363
|
+
return PromiseObj.reject(response);
|
1364
|
+
});
|
1365
|
+
}
|
1366
|
+
|
1367
|
+
Http.options = {};
|
1368
|
+
|
1369
|
+
Http.headers = {
|
1370
|
+
put: JSON_CONTENT_TYPE,
|
1371
|
+
post: JSON_CONTENT_TYPE,
|
1372
|
+
patch: JSON_CONTENT_TYPE,
|
1373
|
+
delete: JSON_CONTENT_TYPE,
|
1374
|
+
custom: CUSTOM_HEADERS,
|
1375
|
+
common: COMMON_HEADERS
|
1376
|
+
};
|
1377
|
+
|
1378
|
+
Http.interceptors = [before, timeout, method, body, jsonp, header, cors];
|
1379
|
+
|
1380
|
+
['get', 'delete', 'head', 'jsonp'].forEach(function (method) {
|
1381
|
+
|
1382
|
+
Http[method] = function (url, options) {
|
1383
|
+
return this(assign(options || {}, { url: url, method: method }));
|
1384
|
+
};
|
1385
|
+
});
|
1386
|
+
|
1387
|
+
['post', 'put', 'patch'].forEach(function (method) {
|
1388
|
+
|
1389
|
+
Http[method] = function (url, body, options) {
|
1390
|
+
return this(assign(options || {}, { url: url, method: method, body: body }));
|
1391
|
+
};
|
1392
|
+
});
|
1393
|
+
|
1394
|
+
/**
|
1395
|
+
* Service for interacting with RESTful services.
|
1396
|
+
*/
|
1397
|
+
|
1398
|
+
function Resource(url, params, actions, options) {
|
1399
|
+
|
1400
|
+
var self = this || {},
|
1401
|
+
resource = {};
|
1402
|
+
|
1403
|
+
actions = assign({}, Resource.actions, actions);
|
1404
|
+
|
1405
|
+
each(actions, function (action, name) {
|
1406
|
+
|
1407
|
+
action = merge({ url: url, params: assign({}, params) }, options, action);
|
1408
|
+
|
1409
|
+
resource[name] = function () {
|
1410
|
+
return (self.$http || Http)(opts(action, arguments));
|
1411
|
+
};
|
1412
|
+
});
|
1413
|
+
|
1414
|
+
return resource;
|
1415
|
+
}
|
1416
|
+
|
1417
|
+
function opts(action, args) {
|
1418
|
+
|
1419
|
+
var options = assign({}, action),
|
1420
|
+
params = {},
|
1421
|
+
body;
|
1422
|
+
|
1423
|
+
switch (args.length) {
|
1424
|
+
|
1425
|
+
case 2:
|
1426
|
+
|
1427
|
+
params = args[0];
|
1428
|
+
body = args[1];
|
1429
|
+
|
1430
|
+
break;
|
1431
|
+
|
1432
|
+
case 1:
|
1433
|
+
|
1434
|
+
if (/^(POST|PUT|PATCH)$/i.test(options.method)) {
|
1435
|
+
body = args[0];
|
1436
|
+
} else {
|
1437
|
+
params = args[0];
|
1438
|
+
}
|
1439
|
+
|
1440
|
+
break;
|
1441
|
+
|
1442
|
+
case 0:
|
1443
|
+
|
1444
|
+
break;
|
1445
|
+
|
1446
|
+
default:
|
1447
|
+
|
1448
|
+
throw 'Expected up to 4 arguments [params, body], got ' + args.length + ' arguments';
|
1449
|
+
}
|
1450
|
+
|
1451
|
+
options.body = body;
|
1452
|
+
options.params = assign({}, options.params, params);
|
1453
|
+
|
1454
|
+
return options;
|
1455
|
+
}
|
1456
|
+
|
1457
|
+
Resource.actions = {
|
1458
|
+
|
1459
|
+
get: { method: 'GET' },
|
1460
|
+
save: { method: 'POST' },
|
1461
|
+
query: { method: 'GET' },
|
1462
|
+
update: { method: 'PUT' },
|
1463
|
+
remove: { method: 'DELETE' },
|
1464
|
+
delete: { method: 'DELETE' }
|
1465
|
+
|
1466
|
+
};
|
1467
|
+
|
1468
|
+
/**
|
1469
|
+
* Install plugin.
|
1470
|
+
*/
|
1471
|
+
|
1472
|
+
function plugin(Vue) {
|
1473
|
+
|
1474
|
+
if (plugin.installed) {
|
1475
|
+
return;
|
1476
|
+
}
|
1477
|
+
|
1478
|
+
Util(Vue);
|
1479
|
+
|
1480
|
+
Vue.url = Url;
|
1481
|
+
Vue.http = Http;
|
1482
|
+
Vue.resource = Resource;
|
1483
|
+
Vue.Promise = PromiseObj;
|
1484
|
+
|
1485
|
+
Object.defineProperties(Vue.prototype, {
|
1486
|
+
|
1487
|
+
$url: {
|
1488
|
+
get: function () {
|
1489
|
+
return options(Vue.url, this, this.$options.url);
|
1490
|
+
}
|
1491
|
+
},
|
1492
|
+
|
1493
|
+
$http: {
|
1494
|
+
get: function () {
|
1495
|
+
return options(Vue.http, this, this.$options.http);
|
1496
|
+
}
|
1497
|
+
},
|
1498
|
+
|
1499
|
+
$resource: {
|
1500
|
+
get: function () {
|
1501
|
+
return Vue.resource.bind(this);
|
1502
|
+
}
|
1503
|
+
},
|
1504
|
+
|
1505
|
+
$promise: {
|
1506
|
+
get: function () {
|
1507
|
+
var _this = this;
|
1508
|
+
|
1509
|
+
return function (executor) {
|
1510
|
+
return new Vue.Promise(executor, _this);
|
1511
|
+
};
|
1512
|
+
}
|
1513
|
+
}
|
1514
|
+
|
1515
|
+
});
|
1516
|
+
}
|
1517
|
+
|
1518
|
+
if (typeof window !== 'undefined' && window.Vue) {
|
1519
|
+
window.Vue.use(plugin);
|
1520
|
+
}
|
1521
|
+
|
1522
|
+
return plugin;
|
1523
|
+
|
1524
|
+
})));
|