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