forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl.js
More file actions
138 lines (126 loc) · 4.28 KB
/
url.js
File metadata and controls
138 lines (126 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const getLanguage = require('./language').getLanguage;
const japanese_client = require('../common_functions/country_base').japanese_client;
const Url = function (url) {
this.history_supported = typeof window !== 'undefined' && window.history && window.history.pushState;
if (typeof url !== 'undefined') {
this.location = $('<a>', { href: decodeURIComponent(url) })[0];
} else if (typeof window !== 'undefined') {
this.location = window.location;
}
};
Url.prototype = {
reset: function() {
if (typeof window !== 'undefined') {
this.location = window.location;
}
this._param_hash = undefined;
$(this).trigger('change', [this]);
},
update: function(url) {
const state_info = { container: 'content', url: url, useClass: 'pjaxload' };
if (this.history_supported) {
history.pushState(state_info, '', url);
this.reset();
}
},
param: function(name) {
const param_hash = this.params_hash();
return param_hash[name];
},
path_matches: function(url) {
const this_pathname = this.location.pathname,
url_pathname = url.location.pathname;
return (this_pathname === url_pathname || '/' + this_pathname === url_pathname);
},
params_hash_to_string: function(params) {
return Object.keys(params)
.map(function(key) { return key + '=' + params[key]; })
.join('&');
},
is_in: function(url) {
if (this.path_matches(url)) {
const this_params = this.params();
let param_count = this_params.length,
match_count = 0;
while (param_count--) {
if (url.param(this_params[param_count][0]) === this_params[param_count][1]) {
match_count++;
}
}
if (match_count === this_params.length) {
return true;
}
}
return false;
},
params_hash: function() {
if (!this._param_hash || (this._param_hash && Object.keys(this._param_hash).length === 0)) {
this._param_hash = {};
const params = this.params();
let param = params.length;
while (param--) {
if (params[param][0]) {
this._param_hash[params[param][0]] = params[param][1];
}
}
}
return this._param_hash;
},
params: function() {
const params = [],
parsed = this.location.search.substr(1).split('&');
let p_l = parsed.length;
while (p_l--) {
const param = parsed[p_l].split('=');
params.push(param);
}
return params;
},
};
const url_for = function(path, params) {
if (!path) {
path = '';
} else if (path.length > 0 && path[0] === '/') {
path = path.substr(1);
}
const lang = getLanguage().toLowerCase();
let url = '';
if (typeof window !== 'undefined') {
url = window.location.href;
}
return url.substring(0, url.indexOf('/' + lang + '/') + lang.length + 2) + (path || 'home') + '.html' + (params ? '?' + params : '');
};
const url_for_static = function(path) {
if (!path) {
path = '';
} else if (path.length > 0 && path[0] === '/') {
path = path.substr(1);
}
let staticHost;
if (typeof window !== 'undefined') {
staticHost = window.staticHost;
}
if (!staticHost || staticHost.length === 0) {
staticHost = $('script[src*="binary.min.js"],script[src*="binary.js"]').attr('src');
if (staticHost && staticHost.length > 0) {
staticHost = staticHost.substr(0, staticHost.indexOf('/js/') + 1);
} else {
staticHost = 'https://www.binary.com/';
}
if (typeof window !== 'undefined') {
window.staticHost = staticHost;
}
}
return staticHost + path;
};
const default_redirect_url = function() {
return url_for(japanese_client() ? 'multi_barriers_trading' : 'trading');
};
const url = new Url();
module.exports = {
Url : Url,
url_for : url_for,
url_for_static : url_for_static,
default_redirect_url: default_redirect_url,
url : url,
};