forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
445 lines (414 loc) · 13.2 KB
/
utility.js
File metadata and controls
445 lines (414 loc) · 13.2 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//////////////////////////////////////////////////////////////////
// Purpose: Write loading image to a container for ajax request
// Parameters:
// 1) container - a jQuery object
//////////////////////////////////////////////////////////////////
function showLoadingImage(container)
{
var image_link = page.settings.get('image_link');
container.empty().append('<div id="std_loading_img"><p>'+text.localize('loading...')+'</p><img src="'+image_link['hourglass']+'" /></div>');
}
/////////////////////////////////////////////////////////////////
// Purpose : Generate form's parameters in the format that is
// required by XMLHttpRequest.send
// Return : Parameters string e.g. var1=val1&var2=var2
// Parameters: Targeted form object
/////////////////////////////////////////////////////////////////
function getFormParams(form_obj)
{
var params_arr = [];
if (! form_obj) return '';
var elem = form_obj.elements;
var j=0;
for (var i = 0; i < elem.length; i++)
{
if(elem[i].name)
{
if(elem[i].nodeName == 'INPUT' && elem[i].type.match(/radio|checkbox/) && !elem[i].checked)
{
continue; // skip if it is not checked
}
params_arr[j] = elem[i].name+'='+encodeURIComponent(elem[i].value);
j++;
}
}
var params_str = params_arr.join('&');
return params_str;
}
/**
* Adds thousand separators for numbers.
*
* @param Number num: any number (int or float)
* @param string separator [optional] string to use for the separator (default is , as the name suggests)
* @return string
*/
function virgule(given_num)
{
if (isNaN(given_num)) {
return given_num;
}
var maybe_minus = '';
var num = given_num;
if (given_num < 0) {
num = num * -1;
maybe_minus = '-';
}
if (num < 1000) {
return maybe_minus + num;
}
var separator = ',';
if (arguments.length > 1) {
separator = arguments[1];
}
var int_part = num;
var float_part = '';
var float_match = /(\d{3,})\.(\d+)/.exec(num);
if (float_match) {
int_part = float_match[1];
float_part = '.' + float_match[2];
}
var match = /(\d+)(\d\d\d)$/.exec(int_part);
return maybe_minus + virgule(match[1], separator) + separator + match[2] + float_part;
}
function getImageLink() {
var image_link = page.settings.get('image_link');
return '<img src="' + image_link['hourglass'] + '" class="bet_bottom_loading_image" />';
}
/**
* updates a container node for when a price value inside is updated.
* Like when the bet price is changed, updates the bet buy price container
* with arrows to display the change.
*
* @param item Object: the node object
* @param old_val: what it used to be
* @param new_val: what it is now
*/
function price_moved (item, old_val, new_val) {
if (new_val < old_val) {
item.removeClass("price_moved_up");
item.addClass("price_moved_down");
} else if (new_val > old_val) {
item.removeClass("price_moved_down");
item.addClass("price_moved_up");
} else {
item.removeClass("price_moved_down");
item.removeClass("price_moved_up");
}
}
/**
* Returns the highest z-index in the page.
* Accepts a jquery style selector to only check those elements,
* uses all container tags by default
* If no element found, returns null.
*
* @param selector: a jquery style selector for target elements
* @return int|null
*/
function get_highest_zindex(selector) {
if (!selector) {
selector = 'div,p,area,nav,section,header,canvas,aside,span';
}
var all = [];
var _store_zindex = function () {
if ($(this).is(':visible')) {
var z = $(this).css("z-index");
if ( !isNaN(z) ) {
all.push(z);
}
}
};
$(selector).each(_store_zindex);
return all.length ? Math.max.apply(Math, all) : null;
}
var user_country;
var get_user_country = function(callback) {
if(user_country) {
callback.call(user_country);
} else {
$.ajax({ crossDomain: true, url: page.url.url_for('country'), async: true, dataType: "json" }).done(function(response) {
user_country = response;
callback.call(response);
});
}
};
/**
* Returns a stylized price for a value as units and cents.
* this could be used anywhere we need to show a float value
* like in bet_sell.js to display the current sell price.
*/
function stylized_price(val) {
var units = '0';
var cents = '00';
if (val) {
val = Math.round(val * 100) / 100;
var val_str = val.toString();
var parts = val_str.split('.');
units = virgule(parts[0]);
cents = parts[1] || '00';
if (cents.length < 2) {
cents += '0';
}
}
return {
units: units,
cents: '.' + cents
};
}
/**
* Add login param which contains the login cookie.
* Required as our most of our ajax requests are now Cross domain and it will no longer send the login cookie.
* Replaces the old header X-AJAX-COOKIE as this way it works for both IE9 and other newer browsers.
* Not adding the header also avoid extra options request saving a whole 700ms on pricing.
*/
var ajax_loggedin = function(params) {
var login_cookie = $.cookie('login');
if(login_cookie) {
var extra_params = 'login=' + encodeURIComponent(login_cookie);
var staff_cookie = $.cookie('staff');
if(staff_cookie) {
extra_params += '&staff=' + encodeURIComponent(staff_cookie);
}
if(params.data) {
params.data += '&' + extra_params;
} else {
params.data = extra_params;
}
}
//A magical limit to param length imposed by IE.
if(params.data && params.data.length > 2000) {
params.type = "POST";
}
return params;
};
/**
* Gets a DOM or jQuery element and reads its data attributes
* and return an object of data stored in element attributes.
* This is used where we store some data as element attributes.
* Excludes commont HTML attributes from the element.
*
* @param element: DOM|jQuery element
* @return object
*/
function element_data_attrs(element) {
if (element && element instanceof jQuery) {
element = element.get().pop();
}
if (!element || !element.attributes) {
console.log(element);
throw new Error("Can not get data attributes from none element parameter");
}
var data = {};
var attrs = element.attributes;
if (attrs.length) {
var attr_blacklist = ['id', 'class', 'name', 'style', 'href', 'src', 'title', 'onclick'];
for (var i = 0; i < attrs.length; i++) {
var attr = attrs[i];
if (attr_blacklist.indexOf(attr.name.toLowerCase()) > -1) continue;
data[attr.name] = attr.value;
}
}
return data;
}
/**
* Gets a DOM or jQuery element and reads its data attributes
* and returns a URL encoded string (like a form data)
* This is used where we store some data as element attributes.
*
* @param element: DOM or jQuery element
* @return string
*/
function element_data_attrs_as_form_params(element) {
var data = element_data_attrs(element);
var params = [];
var key;
for (key in data) if (data.hasOwnProperty(key)) {
var val = data[key];
if (val === undefined) continue;
params.push( key + '=' + encodeURIComponent(val) );
}
return params.join('&');
}
/**
* Converts a snake_cased string to a camelCased string.
* The first character case not changed unless requested.
*
* @param snake: snake_cased string
* @param lower_case_first_char: boolean to force the first char to be lower cased
* @param chars: string of chars to be considered a separator (default is _ and -)
*/
function snake_case_to_camel_case(snake, lower_case_first_char, chars) {
chars = chars || '_-';
var _upper2ndchar = function (m) { return m[1].toUpperCase(); };
var regex = new RegExp('[' + chars + ']([a-zA-Z])', 'g');
var camel = snake.replace(regex, _upper2ndchar);
camel.replace('_', '');
if (lower_case_first_char) {
camel = camel[0].toLowerCase() + camel.substr(1);
}
return camel;
}
/**
* attaches a datepicker to the specified element
* This is a thin wrapper for datepicker, helps to keep a unique site-wide
* default configurations for the datepicker.
*
* @param element any jquery selector or DOM/jQuery object to attach the datepicker to
* @param config custom configurations for the datepicker
*/
function attach_date_picker(element, conf) {
var k,
target = $(element);
if (!target || !target.length) return false;
var today = new Date();
var next_year = new Date();
next_year.setDate(today.getDate() + 365);
var options = {
dateFormat: 'yy-mm-dd',
maxDate: next_year,
};
for (k in conf) if (conf.hasOwnProperty(k)) {
options[k] = conf[k];
}
return target.datepicker(options);
}
/**
* attaches a timepicker to the specified element.
* This is a thin wrapper for timepicker, helps to keep a unique site-wide
* default configurations for the timepicker.
*
* @param element any jquery selector or DOM/jQuery object to attach the timepicker to
* @param config custom configurations for the timepicker
*/
function attach_time_picker(element, conf) {
var attr, k, target = $(element);
if (!target || !target.length) return false;
var opts = {
timeSeparator: ':',
showLeadingZero: true,
howMinutesLeadingZero: true,
hourText: text.localize("Hour"),
minuteText: text.localize("Minute"),
minTime: {},
maxTime: {},
};
var data_attrs = element_data_attrs(target);
var regex = /^time\:(.+)/;
for (attr in data_attrs) if (data_attrs.hasOwnProperty(attr)) {
var matched = attr.match(regex);
if (matched) {
var data = data_attrs[attr];
var opt_name = matched[1].trim();
if (data == 'true') {
data = true;
} else if (data == 'false') {
data = false;
}
opt_name = snake_case_to_camel_case(opt_name, true).toLowerCase();
switch (opt_name) {
case 'mintimehour':
opts.minTime.hour = data;
break;
case 'mintimeminute':
opts.minTime.minute = data;
break;
case 'maxtimehour':
opts.maxTime.hour = data;
break;
case 'maxtimeminute':
opts.maxTime.minute = data;
break;
}
}
}
for (k in conf) if (conf.hasOwnProperty(k)) {
opts[k] = conf[k];
}
return target.timepicker(opts);
}
/**
* attaches an inpage popup to the specified element.
*
* @param element any jquery selector or DOM/jQuery object to attach the inpage popups to
*/
function attach_inpage_popup(element) {
var targets = $(element);
var popups = [];
var regx = /^popup-(.+)/;
targets.each(function () {
var attr,
matched,
attrs = element_data_attrs(this),
conf = {};
for (attr in attrs) if (attrs.hasOwnProperty(attr)) {
matched = attr.match(regx);
if (matched) {
conf[matched[1]] = attrs[attr];
}
}
var popup = new InPagePopup(conf);
popup.attach(this);
popups.push(popup);
});
return popups;
}
/**
* Calculate container width for chart as of now but can
* be used to get current container width
*/
function get_container_width() {
var width = 960;
if ($('.chart_holder').length > 0) {
width = $('.chart_holder').width();
} else {
width = $('.grd-container').width();
}
return width;
}
/**
* in a jquery UI tabs object, finds out whitch tab is marked to be the
* active tab by default.
*
* The default active tab is selected based on CSS classes of tab list items.
*
* @param element any jquery selector or DOM/jquery object that contains a jquery UI tab UL
* @return int the index of active list item or 0 if none of the items were
* marked as active.
*/
function find_active_jqtab(el) {
var jqel = $(el);
var ul = jqel.children('ul');
if (!ul) throw new Error("Invalid parameter. element is not a jquery UI tab container");
ul = ul.filter(":first");
var items = ul.children('li');
for (var i = 0; i < items.length; i++) {
if ($(items[i]).hasClass('active')) {
return i;
}
}
return 0;
}
/**
* attaches tabs to the specified element selector
*
* @param element any jquery selector or DOM/jQuery object
*/
function attach_tabs(element) {
var targets = $(element);
targets.each(function () {
var jqel = $(this);
var conf = {};
var active = 0;
try {
active = find_active_jqtab(jqel);
} catch (e) {
console.log(e);
console.log(jqel);
}
if (active) {
conf['active'] = active;
$('li.active', jqel).removeClass('active');
}
jqel.tabs(conf);
});
return targets;
}