2013-10-17 16:56:56 -05:00
|
|
|
define(['jquery','underscore','moment','chromath'],
|
2013-10-05 18:41:20 -05:00
|
|
|
function($, _, moment) {
|
2013-07-20 17:59:09 -05:00
|
|
|
'use strict';
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
var kbn = {};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.get_object_fields = function(obj) {
|
|
|
|
var field_array = [];
|
2013-07-20 17:59:09 -05:00
|
|
|
obj = kbn.flatten_json(obj._source);
|
|
|
|
for (var field in obj) {
|
2013-07-20 16:56:59 -05:00
|
|
|
field_array.push(field);
|
2013-01-25 22:10:28 -06:00
|
|
|
}
|
2013-07-20 16:56:59 -05:00
|
|
|
return field_array.sort();
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.get_all_fields = function(data) {
|
2013-10-15 13:26:41 -05:00
|
|
|
var _d = data;
|
2013-07-20 16:56:59 -05:00
|
|
|
var fields = [];
|
2013-10-15 13:26:41 -05:00
|
|
|
_.each(_d,function(hit) {
|
|
|
|
fields = _.uniq(fields.concat(_.keys(kbn.flatten_json(hit._source))));
|
2013-07-20 17:59:09 -05:00
|
|
|
});
|
2013-07-20 16:56:59 -05:00
|
|
|
// Remove stupid angular key
|
2013-07-20 17:59:09 -05:00
|
|
|
fields = _.without(fields,'$$hashKey');
|
2013-07-20 16:56:59 -05:00
|
|
|
return fields;
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.has_field = function(obj,field) {
|
2013-07-20 17:59:09 -05:00
|
|
|
var obj_fields = kbn.get_object_fields(obj);
|
2013-07-20 16:56:59 -05:00
|
|
|
if (_.inArray(obj_fields,field) < 0) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.get_related_fields = function(docs,field) {
|
2013-07-20 17:59:09 -05:00
|
|
|
var field_array = [];
|
2013-07-20 16:56:59 -05:00
|
|
|
_.each(docs, function(doc) {
|
2013-07-20 17:59:09 -05:00
|
|
|
var keys = _.keys(doc);
|
|
|
|
if(_.contains(keys,field)) {
|
|
|
|
field_array = field_array.concat(keys);
|
|
|
|
}
|
|
|
|
});
|
2013-07-20 16:56:59 -05:00
|
|
|
var counts = _.countBy(_.without(field_array,field),function(field){return field;});
|
2013-10-11 06:14:29 -05:00
|
|
|
return _.map(counts, function(num, key){return {name:key,count:num};});
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.recurse_field_dots = function(object,field) {
|
|
|
|
var value = null;
|
2013-07-20 17:59:09 -05:00
|
|
|
var nested;
|
|
|
|
if (typeof object[field] !== 'undefined') {
|
2013-07-20 16:56:59 -05:00
|
|
|
value = object[field];
|
2013-07-20 17:59:09 -05:00
|
|
|
}
|
|
|
|
else if (nested = field.match(/(.*?)\.(.*)/)) {
|
|
|
|
if(typeof object[nested[1]] !== 'undefined') {
|
|
|
|
value = (typeof object[nested[1]][nested[2]] !== 'undefined') ?
|
|
|
|
object[nested[1]][nested[2]] : kbn.recurse_field_dots(
|
2013-07-20 16:56:59 -05:00
|
|
|
object[nested[1]],nested[2]);
|
2013-07-20 17:59:09 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-20 16:56:59 -05:00
|
|
|
|
|
|
|
return value;
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-09-16 17:09:25 -05:00
|
|
|
kbn.top_field_values = function(docs,field,count,grouped) {
|
2013-08-27 16:53:23 -05:00
|
|
|
var all_values = _.pluck(docs,field),
|
2013-09-16 17:09:25 -05:00
|
|
|
groups = {},
|
|
|
|
counts,
|
|
|
|
hasArrays;
|
2013-08-27 16:53:23 -05:00
|
|
|
// manually grouping into pairs allows us to keep the original value,
|
|
|
|
_.each(all_values, function (value) {
|
2013-09-16 17:09:25 -05:00
|
|
|
var k;
|
|
|
|
if(_.isArray(value)) {
|
|
|
|
hasArrays = true;
|
|
|
|
}
|
|
|
|
if(_.isArray(value) && !grouped) {
|
|
|
|
k = value;
|
2013-08-27 16:53:23 -05:00
|
|
|
} else {
|
2013-09-16 17:09:25 -05:00
|
|
|
k = _.isUndefined(value) ? '' : [value.toString()];
|
2013-08-27 16:53:23 -05:00
|
|
|
}
|
2013-09-16 17:09:25 -05:00
|
|
|
_.each(k, function(key) {
|
|
|
|
if (_.has(groups, key)) {
|
|
|
|
groups[key][1] ++;
|
|
|
|
} else {
|
|
|
|
groups[key] = [(grouped ? value : key), 1];
|
|
|
|
}
|
|
|
|
});
|
2013-07-20 16:56:59 -05:00
|
|
|
});
|
2013-08-27 16:53:23 -05:00
|
|
|
|
2013-09-16 17:09:25 -05:00
|
|
|
counts = _.values(groups).sort(function(a, b) {
|
2013-07-20 17:59:09 -05:00
|
|
|
return a[1] - b[1];
|
|
|
|
}).reverse().slice(0,count);
|
2013-09-16 17:09:25 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
counts: counts,
|
|
|
|
hasArrays : hasArrays
|
|
|
|
};
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
/**
|
|
|
|
* Calculate a graph interval
|
|
|
|
*
|
|
|
|
* from:: Date object containing the start time
|
|
|
|
* to:: Date object containing the finish time
|
|
|
|
* size:: Calculate to approximately this many bars
|
|
|
|
* user_interval:: User specified histogram interval
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
kbn.calculate_interval = function(from,to,size,user_interval) {
|
2013-07-20 17:59:09 -05:00
|
|
|
if(_.isObject(from)) {
|
2013-07-20 16:56:59 -05:00
|
|
|
from = from.valueOf();
|
2013-07-20 17:59:09 -05:00
|
|
|
}
|
|
|
|
if(_.isObject(to)) {
|
2013-07-20 16:56:59 -05:00
|
|
|
to = to.valueOf();
|
2013-07-20 17:59:09 -05:00
|
|
|
}
|
|
|
|
return user_interval === 0 ? kbn.round_interval((to - from)/size) : user_interval;
|
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.round_interval = function(interval) {
|
|
|
|
switch (true) {
|
2013-07-20 17:59:09 -05:00
|
|
|
// 0.5s
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 500):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 100; // 0.1s
|
|
|
|
// 5s
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 5000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 1000; // 1s
|
|
|
|
// 7.5s
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 7500):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 5000; // 5s
|
|
|
|
// 15s
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 15000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 10000; // 10s
|
|
|
|
// 45s
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 45000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 30000; // 30s
|
|
|
|
// 3m
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 180000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 60000; // 1m
|
|
|
|
// 9m
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 450000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 300000; // 5m
|
|
|
|
// 20m
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 1200000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 600000; // 10m
|
|
|
|
// 45m
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 2700000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 1800000; // 30m
|
|
|
|
// 2h
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 7200000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 3600000; // 1h
|
|
|
|
// 6h
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 21600000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 10800000; // 3h
|
|
|
|
// 24h
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 86400000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 43200000; // 12h
|
|
|
|
// 48h
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 172800000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 86400000; // 24h
|
|
|
|
// 1w
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 604800000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 86400000; // 24h
|
|
|
|
// 3w
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval <= 1814400000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 604800000; // 1w
|
|
|
|
// 2y
|
2013-08-26 19:56:40 -05:00
|
|
|
case (interval < 3628800000):
|
2013-07-20 17:59:09 -05:00
|
|
|
return 2592000000; // 30d
|
2013-08-26 19:56:40 -05:00
|
|
|
default:
|
2013-07-20 17:59:09 -05:00
|
|
|
return 31536000000; // 1y
|
2013-07-20 16:56:59 -05:00
|
|
|
}
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.secondsToHms = function(seconds){
|
2013-07-20 17:59:09 -05:00
|
|
|
var numyears = Math.floor(seconds / 31536000);
|
|
|
|
if(numyears){
|
|
|
|
return numyears + 'y';
|
|
|
|
}
|
|
|
|
var numdays = Math.floor((seconds % 31536000) / 86400);
|
|
|
|
if(numdays){
|
|
|
|
return numdays + 'd';
|
|
|
|
}
|
|
|
|
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
|
|
|
|
if(numhours){
|
|
|
|
return numhours + 'h';
|
|
|
|
}
|
|
|
|
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
|
|
|
|
if(numminutes){
|
|
|
|
return numminutes + 'm';
|
|
|
|
}
|
|
|
|
var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
|
|
|
|
if(numseconds){
|
|
|
|
return numseconds + 's';
|
|
|
|
}
|
|
|
|
return 'less then a second'; //'just now' //or other string you like;
|
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.to_percent = function(number,outof) {
|
2013-08-20 09:24:04 -05:00
|
|
|
return Math.floor((number/outof)*10000)/100 + "%";
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.addslashes = function(str) {
|
|
|
|
str = str.replace(/\\/g, '\\\\');
|
|
|
|
str = str.replace(/\'/g, '\\\'');
|
|
|
|
str = str.replace(/\"/g, '\\"');
|
|
|
|
str = str.replace(/\0/g, '\\0');
|
|
|
|
return str;
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-09-17 13:04:23 -05:00
|
|
|
kbn.interval_regex = /(\d+(?:\.\d+)?)([Mwdhmsy])/;
|
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
// histogram & trends
|
2013-09-25 16:19:19 -05:00
|
|
|
kbn.intervals_in_seconds = {
|
2013-09-17 13:04:23 -05:00
|
|
|
y: 31536000,
|
|
|
|
M: 2592000,
|
|
|
|
w: 604800,
|
|
|
|
d: 86400,
|
|
|
|
h: 3600,
|
|
|
|
m: 60,
|
|
|
|
s: 1
|
|
|
|
};
|
|
|
|
|
2013-09-25 16:19:19 -05:00
|
|
|
kbn.describe_interval = function (string) {
|
2013-09-17 13:04:23 -05:00
|
|
|
var matches = string.match(kbn.interval_regex);
|
2013-09-25 16:19:19 -05:00
|
|
|
if (!matches || !_.has(kbn.intervals_in_seconds, matches[2])) {
|
2013-09-17 13:04:23 -05:00
|
|
|
throw new Error('Invalid interval string, expexcting a number followed by one of "Mwdhmsy"');
|
|
|
|
} else {
|
2013-09-25 16:19:19 -05:00
|
|
|
return {
|
|
|
|
sec: kbn.intervals_in_seconds[matches[2]],
|
|
|
|
type: matches[2],
|
|
|
|
count: parseInt(matches[1], 10)
|
|
|
|
};
|
2013-08-26 19:56:40 -05:00
|
|
|
}
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-09-25 16:19:19 -05:00
|
|
|
kbn.interval_to_ms = function(string) {
|
|
|
|
var info = kbn.describe_interval(string);
|
|
|
|
return info.sec * 1000 * info.count;
|
|
|
|
};
|
|
|
|
|
2013-09-17 13:04:23 -05:00
|
|
|
kbn.interval_to_seconds = function (string) {
|
2013-09-25 16:19:19 -05:00
|
|
|
var info = kbn.describe_interval(string);
|
|
|
|
return info.sec * info.count;
|
2013-09-17 13:04:23 -05:00
|
|
|
};
|
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
// This should go away, moment.js can do this
|
|
|
|
kbn.time_ago = function(string) {
|
2013-09-17 13:04:23 -05:00
|
|
|
return new Date(new Date().getTime() - (kbn.interval_to_ms(string)));
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-10-05 18:41:20 -05:00
|
|
|
/* This is a simplified version of elasticsearch's date parser */
|
|
|
|
kbn.parseDate = function(text) {
|
|
|
|
if(_.isDate(text)) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
var time,
|
|
|
|
mathString = "",
|
|
|
|
index,
|
|
|
|
parseString;
|
|
|
|
if (text.substring(0,3) === "now") {
|
|
|
|
time = new Date();
|
|
|
|
mathString = text.substring("now".length);
|
|
|
|
} else {
|
|
|
|
index = text.indexOf("||");
|
|
|
|
parseString;
|
|
|
|
if (index === -1) {
|
|
|
|
parseString = text;
|
|
|
|
mathString = ""; // nothing else
|
|
|
|
} else {
|
|
|
|
parseString = text.substring(0, index);
|
|
|
|
mathString = text.substring(index + 2);
|
|
|
|
}
|
|
|
|
// We're going to just require ISO8601 timestamps, k?
|
|
|
|
time = new Date(parseString);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mathString.length) {
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
//return [time,parseString,mathString];
|
|
|
|
return kbn.parseDateMath(mathString, time);
|
|
|
|
};
|
|
|
|
|
|
|
|
kbn.parseDateMath = function(mathString, time, roundUp) {
|
|
|
|
var dateTime = moment(time);
|
|
|
|
for (var i = 0; i < mathString.length; ) {
|
|
|
|
var c = mathString.charAt(i++),
|
|
|
|
type,
|
|
|
|
num,
|
|
|
|
unit;
|
|
|
|
if (c === '/') {
|
|
|
|
type = 0;
|
|
|
|
} else if (c === '+') {
|
|
|
|
type = 1;
|
|
|
|
} else if (c === '-') {
|
|
|
|
type = 2;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNaN(mathString.charAt(i))) {
|
|
|
|
num = 1;
|
|
|
|
} else {
|
|
|
|
var numFrom = i;
|
|
|
|
while (!isNaN(mathString.charAt(i))) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
num = parseInt(mathString.substring(numFrom, i),10);
|
|
|
|
}
|
|
|
|
if (type === 0) {
|
|
|
|
// rounding is only allowed on whole numbers
|
|
|
|
if (num !== 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unit = mathString.charAt(i++);
|
|
|
|
switch (unit) {
|
|
|
|
case 'M':
|
|
|
|
if (type === 0) {
|
|
|
|
roundUp ? dateTime.endOf('month') : dateTime.startOf('month');
|
|
|
|
} else if (type === 1) {
|
|
|
|
dateTime.add('months',num);
|
|
|
|
} else if (type === 2) {
|
|
|
|
dateTime.subtract('months',num);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
if (type === 0) {
|
|
|
|
roundUp ? dateTime.endOf('week') : dateTime.startOf('week');
|
|
|
|
} else if (type === 1) {
|
|
|
|
dateTime.add('weeks',num);
|
|
|
|
} else if (type === 2) {
|
|
|
|
dateTime.subtract('weeks',num);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (type === 0) {
|
|
|
|
roundUp ? dateTime.endOf('day') : dateTime.startOf('day');
|
|
|
|
} else if (type === 1) {
|
|
|
|
dateTime.add('days',num);
|
|
|
|
} else if (type === 2) {
|
|
|
|
dateTime.subtract('days',num);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
if (type === 0) {
|
|
|
|
roundUp ? dateTime.endOf('hour') : dateTime.startOf('hour');
|
|
|
|
} else if (type === 1) {
|
|
|
|
dateTime.add('hours',num);
|
|
|
|
} else if (type === 2) {
|
|
|
|
dateTime.subtract('hours',num);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (type === 0) {
|
|
|
|
roundUp ? dateTime.endOf('minute') : dateTime.startOf('minute');
|
|
|
|
} else if (type === 1) {
|
|
|
|
dateTime.add('minutes',num);
|
|
|
|
} else if (type === 2) {
|
|
|
|
dateTime.subtract('minutes',num);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (type === 0) {
|
|
|
|
roundUp ? dateTime.endOf('second') : dateTime.startOf('second');
|
|
|
|
} else if (type === 1) {
|
|
|
|
dateTime.add('seconds',num);
|
|
|
|
} else if (type === 2) {
|
|
|
|
dateTime.subtract('seconds',num);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dateTime.toDate();
|
|
|
|
};
|
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
// LOL. hahahahaha. DIE.
|
|
|
|
kbn.flatten_json = function(object,root,array) {
|
2013-07-20 17:59:09 -05:00
|
|
|
if (typeof array === 'undefined') {
|
|
|
|
array = {};
|
|
|
|
}
|
|
|
|
if (typeof root === 'undefined') {
|
|
|
|
root = '';
|
|
|
|
}
|
2013-07-20 16:56:59 -05:00
|
|
|
for(var index in object) {
|
2013-07-20 17:59:09 -05:00
|
|
|
var obj = object[index];
|
|
|
|
var rootname = root.length === 0 ? index : root + '.' + index;
|
|
|
|
if(typeof obj === 'object' ) {
|
2013-07-20 16:56:59 -05:00
|
|
|
if(_.isArray(obj)) {
|
|
|
|
if(obj.length > 0 && typeof obj[0] === 'object') {
|
|
|
|
var strval = '';
|
|
|
|
for (var objidx = 0, objlen = obj.length; objidx < objlen; objidx++) {
|
|
|
|
if (objidx > 0) {
|
|
|
|
strval = strval + ', ';
|
|
|
|
}
|
2013-08-26 19:56:40 -05:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
strval = strval + JSON.stringify(obj[objidx]);
|
2013-05-22 13:07:20 -05:00
|
|
|
}
|
2013-07-20 16:56:59 -05:00
|
|
|
array[rootname] = strval;
|
|
|
|
} else if(obj.length === 1 && _.isNumber(obj[0])) {
|
|
|
|
array[rootname] = parseFloat(obj[0]);
|
|
|
|
} else {
|
|
|
|
array[rootname] = typeof obj === 'undefined' ? null : obj;
|
2013-05-22 13:07:20 -05:00
|
|
|
}
|
2013-04-01 18:52:18 -05:00
|
|
|
} else {
|
2013-07-20 17:59:09 -05:00
|
|
|
kbn.flatten_json(obj,rootname,array);
|
2013-04-01 18:52:18 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-07-20 16:56:59 -05:00
|
|
|
array[rootname] = typeof obj === 'undefined' ? null : obj;
|
2013-04-01 18:52:18 -05:00
|
|
|
}
|
2013-01-25 22:10:28 -06:00
|
|
|
}
|
2013-07-20 16:56:59 -05:00
|
|
|
return kbn.sortObj(array);
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.xmlEnt = function(value) {
|
|
|
|
if(_.isString(value)) {
|
|
|
|
var stg1 = value.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/\r\n/g, '<br/>')
|
|
|
|
.replace(/\r/g, '<br/>')
|
|
|
|
.replace(/\n/g, '<br/>')
|
|
|
|
.replace(/\t/g, ' ')
|
|
|
|
.replace(/ /g, ' ')
|
|
|
|
.replace(/<del>/g, '<del>')
|
|
|
|
.replace(/<\/del>/g, '</del>');
|
|
|
|
return stg1;
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
kbn.sortObj = function(arr) {
|
|
|
|
// Setup Arrays
|
2013-07-20 17:59:09 -05:00
|
|
|
var sortedKeys = [];
|
2013-07-20 16:56:59 -05:00
|
|
|
var sortedObj = {};
|
2013-07-20 17:59:09 -05:00
|
|
|
var i;
|
2013-07-20 16:56:59 -05:00
|
|
|
// Separate keys and sort them
|
2013-07-20 17:59:09 -05:00
|
|
|
for (i in arr) {
|
2013-07-20 16:56:59 -05:00
|
|
|
sortedKeys.push(i);
|
2013-01-25 22:10:28 -06:00
|
|
|
}
|
2013-07-20 16:56:59 -05:00
|
|
|
sortedKeys.sort();
|
2013-01-25 22:10:28 -06:00
|
|
|
|
2013-07-20 16:56:59 -05:00
|
|
|
// Reconstruct sorted obj based on keys
|
2013-07-20 17:59:09 -05:00
|
|
|
for (i in sortedKeys) {
|
2013-07-20 16:56:59 -05:00
|
|
|
sortedObj[sortedKeys[i]] = arr[sortedKeys[i]];
|
|
|
|
}
|
|
|
|
return sortedObj;
|
2013-07-20 17:59:09 -05:00
|
|
|
};
|
2013-08-28 13:13:59 -05:00
|
|
|
|
|
|
|
kbn.query_color_dot = function (color, diameter) {
|
2013-08-28 14:14:09 -05:00
|
|
|
return '<div class="icon-circle" style="' + [
|
2013-08-28 13:13:59 -05:00
|
|
|
'display:inline-block',
|
2013-08-28 14:14:09 -05:00
|
|
|
'color:' + color,
|
|
|
|
'font-size:' + diameter + 'px',
|
2013-08-28 13:13:59 -05:00
|
|
|
].join(';') + '"></div>';
|
|
|
|
};
|
|
|
|
|
2013-10-17 16:56:56 -05:00
|
|
|
kbn.colorSteps = function(col,steps) {
|
|
|
|
var _d = 1.6/steps, // distance between steps
|
|
|
|
_p = []; // adjustment percentage
|
|
|
|
|
|
|
|
// Create a range of numbers between -0.8 and 0.8
|
|
|
|
for(var i = 1; i<steps+1; i+=1) {
|
|
|
|
_p.push(i%2 ? ((i-1)*_d*-1)/2 : i*_d/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the color range
|
|
|
|
return _.map(_p.sort(function(a,b){return a-b;}),function(v) {
|
|
|
|
return v<0 ? Chromath.darken(col,v*-1).toString() : Chromath.lighten(col,v).toString();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
return kbn;
|
|
|
|
});
|