kpiplus
Version:
KPI+
333 lines (295 loc) • 8.73 kB
JavaScript
var stpUntils= angular.module('stp-untils',['ngCookies']);
var STP={math:Math}
STP.sum = function(arr,field,where){
if(!arr || !field) return 0;
var kq =0,_arr;
if(!where){
_arr = arr;
}else{
_arr = _.where(arr,where);
}
_arr.forEach(function(a){
var v =Number(a[field]);
if(v && _.isNumber(v)) kq = kq + v;
})
return kq;
}
STP.round = function(number, precision) {
if(!precision) precision =0;
var factor = Math.pow(10, precision);
var tempNumber = number * factor;
var roundedTempNumber = Math.round(tempNumber);
return roundedTempNumber / factor;
};
STP.toDate = function(string){
return new Date(string);
}
STP.tructed = function(string,len,direction){
if(!string) return "";
if(string.length>len){
if(direction==='right'){
string = '...' + string.slice(-(len-3));
}else{
string = string.substring(0,len-3) + '...';
}
}
return string;
}
STP.add = function(arr,item,notResetLine,properties,sumFields){
if(!arr) arr =[];
if(!item){
return arr;
}
if(properties){
var _item = _.find(arr,function(r){
return _.isMatch(r,properties);
})
if(_item){
if(sumFields){
sumFields.forEach(function(s){
_item[s] = (_item[s]?_item[s]:0) + (item[s]?item[s]:0)
})
}
}else{
arr.push(item);
}
}else{
arr.push(item);
}
if(!notResetLine){
for(var line=0;line<arr.length;line++){
arr[line].line = line;
}
}
return arr;
}
STP.reject = function(arr,properties,notResetLine){
if(!properties){
return arr;
}
for(var k in properties){
if(properties[k] == undefined){
delete properties[k];
}
}
for(var i=0;i<arr.length;i++){
if(_.isMatch(arr[i],properties)){
arr.splice(i,1);
}
}
if(!notResetLine){
for(var line=0;line<arr.length;line++){
arr[line].line = line;
}
}
return arr;
}
STP.encodeBase64 = function(string){
return window.btoa(unescape(encodeURIComponent(string)));
}
STP.where = function(arr,properties,notResetLine){
if(!properties){
return arr;
}
var kq = _.where(arr,properties);
if(!notResetLine){
for(var line=0;line<kq.length;line++){
kq.line = line;
}
}
return kq;
}
STP.queryStringToJSON = function(queryString) {
if(queryString.indexOf('?') > -1){
queryString = queryString.split('?')[1];
}else{
return {};
}
var pairs = queryString.split('&');
var result = {};
pairs.forEach(function(pair) {
pair = pair.split('=');
result[pair[0]] = decodeURIComponent(pair[1] || '');
});
return result;
}
STP.browser = function() {
var name = "Unknown";
if(navigator.userAgent.indexOf("MSIE")!=-1){
name = "MSIE";
}
else if(navigator.userAgent.indexOf("Firefox")!=-1){
name = "firefox";
}
else if(navigator.userAgent.indexOf("Opera")!=-1){
name = "opera";
}
else if(navigator.userAgent.indexOf("Chrome") != -1){
name = "chrome";
}
else if(navigator.userAgent.indexOf("Safari")!=-1){
name = "safari";
}
return name;
}
stpUntils.factory('untils',['$timeout', function($timeout) {
return STP;
}])
stpUntils.factory('$localStorage', ['$window','$cookies',function ($window,$cookies) {
return {
remove:function(key){
if($window.localStorage){
$window.localStorage[key] = "";
}else{
$cookies[key] ="";
}
},
set:function(key,value){
if($window.localStorage){
$window.localStorage[key] = value;
}else{
$cookies[key]=value;
}
},
get:function(key){
if($window.localStorage){
return $window.localStorage[key];
}else{
return $cookies[key];
}
},
setObject:function(key,obj){
if($window.localStorage){
$window.localStorage[key] = JSON.stringify(obj);
}
},
getObject:function(key,defaultvalue){
if(!defaultvalue) defaultvalue =[];
var obj;
if($window.localStorage){
obj = $window.localStorage[key];
}
if(!obj) obj = defaultvalue;
if(_.isObject(obj) || _.isArray(obj)){
return obj;
}else{
try{
return JSON.parse(obj);
}catch(e){
return defaultvalue;
console.log("error parse data",obj);
}
}
},
}
}])
stpUntils.factory('Base64', function() {
var keyStr = 'ABCDEFGHIJKLMNOP' +
'QRSTUVWXYZabcdef' +
'ghijklmnopqrstuv' +
'wxyz0123456789+/' +
'=';
return {
encode: function (input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
},
decode: function (input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
var base64test = /[^A-Za-z0-9\+\/\=]/g;
if (base64test.exec(input)) {
alert("There were invalid base64 characters in the input text.\n" +
"Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
"Expect errors in decoding.");
}
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
};
});
//temp
stpUntils.directive("icSrc",function(){
return{
restrict:'A',
scope:{
icSrc:'@'
},
link:function(scope,elem,attrs,controller){
scope.$watch('icSrc',function(newValue,oldValue){
elem.attr("src",newValue);
});
}
}
})
stpUntils.filter('propsFilter', function() {
return function(items, props) {
var out = [];
if (angular.isArray(items)) {
var keys = Object.keys(props);
items.forEach(function(item) {
var itemMatches = false;
for (var i = 0; i < keys.length; i++) {
var prop = keys[i];
var text = props[prop].toString().toLowerCase();
if (item[prop].toString().toLowerCase().indexOf(text) !== -1) {
itemMatches = true;
break;
}
}
if (itemMatches) {
out.push(item);
}
});
} else {
// Let the output be the input untouched
out = items;
}
return out;
};
});