Skip to content

Commit a54d3c0

Browse files
committed
add tools.js assets
1 parent 4d64b1f commit a54d3c0

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

src/assets/ToolsAsset.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* @author Dmytro Karpovych
4+
* @copyright 2018 NRE
5+
*/
6+
7+
8+
namespace nullref\core\assets;
9+
10+
use yii\web\AssetBundle;
11+
12+
/**
13+
* Class ToolsAsset
14+
* @package nullref\core\assets
15+
*/
16+
class ToolsAsset extends AssetBundle
17+
{
18+
public $sourcePath = '@nullref/core/assets';
19+
public $js = [
20+
'js/tools.js',
21+
];
22+
}

src/assets/js/tools.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* @section useful methods and polyfills
3+
*/
4+
(function () {
5+
if (!String.prototype.startsWith) {
6+
Object.defineProperty(String.prototype, 'startsWith', {
7+
enumerable: false,
8+
configurable: false,
9+
writable: false,
10+
value: function (searchString, position) {
11+
position = position || 0;
12+
return this.lastIndexOf(searchString, position) === position;
13+
}
14+
});
15+
}
16+
if (!String.prototype.replaceAll) {
17+
Object.defineProperty(String.prototype, 'replaceAll', {
18+
enumerable: false,
19+
configurable: false,
20+
writable: false,
21+
value: function (search, replace) {
22+
return this.split(search).join(replace);
23+
}
24+
});
25+
}
26+
String.prototype.addUrlParam = function (key, value) {
27+
return tools.addUrlParam(this, key, value);
28+
};
29+
String.prototype.format = function (map) {
30+
var newValue = this, key, value, i, index, arg;
31+
for (i = 0; i < arguments.length; i++) {
32+
arg = arguments[i];
33+
if (tools.isObject(arg)) {
34+
for (index in arg) {
35+
if (arg.hasOwnProperty(index)) {
36+
value = arg[index];
37+
key = '{' + index + '}';
38+
newValue = newValue.replace(new RegExp(key, 'g'), value);
39+
}
40+
}
41+
} else {
42+
key = '{' + i + '\\}';
43+
newValue = newValue.replace(new RegExp(key, 'g'), arg);
44+
}
45+
}
46+
return newValue;
47+
};
48+
})();
49+
/**
50+
* @section tools object
51+
*/
52+
var tools = {
53+
isString: function (obj) {
54+
return typeof obj === 'string';
55+
},
56+
isObject: function (obj) {
57+
return typeof obj === 'object';
58+
},
59+
isNumber: function (obj) {
60+
return typeof obj === 'number';
61+
},
62+
isUndefined: function (value) {
63+
return typeof value === 'undefined';
64+
},
65+
isDefined: function (value) {
66+
return typeof value !== 'undefined';
67+
},
68+
isMobile: function () {
69+
if (window.mobileBrowserClientCache === undefined) {
70+
window.mobileBrowserClientCache = false;
71+
(function(a) {
72+
if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) {
73+
window.mobileBrowserClientCache = true;
74+
}
75+
})(navigator.userAgent || navigator.vendor || window.opera);
76+
}
77+
return window.mobileBrowserClientCache;
78+
},
79+
addUrlParam: function (base, key, value) {
80+
if (base && key && value) {
81+
var sep = (base.indexOf('?') > -1) ? '&' : '?';
82+
return base + sep + key + '=' + value;
83+
}
84+
return base;
85+
},
86+
getFunction: function (fn) {
87+
return this.isFunction(fn) ? fn : function(){};
88+
},
89+
isImageLoaded: function (img) {
90+
if (!img.complete) {
91+
return false;
92+
}
93+
94+
if (typeof img.naturalWidth !== "undefined" && img.naturalWidth === 0) {
95+
return false;
96+
}
97+
return true;
98+
},
99+
cookie: {
100+
set: function (name, value, options) {
101+
options = options || {};
102+
103+
var expires = options.expires;
104+
105+
if (typeof expires == "number" && expires) {
106+
var d = new Date();
107+
d.setTime(d.getTime() + expires * 1000);
108+
expires = options.expires = d;
109+
}
110+
if (expires && expires.toUTCString) {
111+
options.expires = expires.toUTCString();
112+
}
113+
114+
value = encodeURIComponent(value);
115+
116+
var updatedCookie = name + "=" + value;
117+
118+
for (var propName in options) {
119+
updatedCookie += "; " + propName;
120+
var propValue = options[propName];
121+
if (propValue !== true) {
122+
updatedCookie += "=" + propValue;
123+
}
124+
}
125+
126+
document.cookie = updatedCookie;
127+
},
128+
get: function (name) {
129+
var matches = document.cookie.match(new RegExp(
130+
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
131+
));
132+
return matches ? decodeURIComponent(matches[1]) : undefined;
133+
},
134+
delete: function (name) {
135+
tools.cookie.set(name, "", {
136+
expires: -1
137+
});
138+
}
139+
}
140+
};

0 commit comments

Comments
 (0)