-
Notifications
You must be signed in to change notification settings - Fork 45
/
spree.js
78 lines (65 loc) · 2.02 KB
/
spree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//= require jsuri
function Spree () {}
Spree.ready = function (callback) {
return jQuery(document).on('page:load turbolinks:load', function () {
return callback(jQuery)
})
}
Spree.mountedAt = function () {
return window.SpreePaths.mounted_at
}
Spree.adminPath = function () {
return window.SpreePaths.admin
}
Spree.pathFor = function (path) {
var locationOrigin = (window.location.protocol + '//' + window.location.hostname) + (window.location.port ? ':' + window.location.port : '')
return this.url('' + locationOrigin + (this.mountedAt()) + path, this.url_params).toString()
}
Spree.localizedPathFor = function(path) {
if (typeof (SPREE_LOCALE) !== 'undefined' && typeof (SPREE_CURRENCY) !== 'undefined') {
var fullUrl = new URL(Spree.pathFor(path))
var params = fullUrl.searchParams
var pathName = fullUrl.pathname
params.set('currency', SPREE_CURRENCY)
if (pathName.match(/api\/v/)) {
params.set('locale', SPREE_LOCALE)
} else {
pathName = (this.mountedAt()) + SPREE_LOCALE + '/' + path
}
return fullUrl.origin + pathName + '?' + params.toString()
}
return Spree.pathFor(path)
}
Spree.adminPathFor = function (path) {
return this.pathFor('' + (this.adminPath()) + path)
}
Spree.url = function (uri, query) {
if (uri.path === void 0) {
// eslint-disable-next-line no-undef
uri = new Uri(uri)
}
if (query) {
$.each(query, function (key, value) {
return uri.addQueryParam(key, value)
})
}
return uri
}
Spree.ajax = function (urlOrSettings, settings) {
var url
if (typeof urlOrSettings === 'string') {
return $.ajax(Spree.url(urlOrSettings).toString(), settings)
} else {
url = urlOrSettings['url']
delete urlOrSettings['url']
return $.ajax(Spree.url(url).toString(), urlOrSettings)
}
}
Spree.routes = {
states_search: Spree.pathFor('api/v1/states'),
apply_coupon_code: function (orderId) {
return Spree.pathFor('api/v1/orders/' + orderId + '/apply_coupon_code')
},
cart: Spree.pathFor('cart')
}
Spree.url_params = {}