This repository was archived by the owner on Dec 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 683
Expand file tree
/
Copy pathUtilsSrv.js
More file actions
134 lines (120 loc) · 5.3 KB
/
UtilsSrv.js
File metadata and controls
134 lines (120 loc) · 5.3 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
(function() {
'use strict';
angular.module('theHiveServices')
.factory('UtilsSrv', function($location) {
var sensitiveTypes = ['url', 'ip', 'mail', 'domain', 'filename'];
var service = {
guid: function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
},
objectify: function(arr, property) {
return _.map(arr, function(str) {
var obj = {};
obj[property] = str;
return obj;
});
},
fangValue: function(value) {
return value
.replace(/\[\.\]/g, ".")
.replace(/hxxp/gi, "http")
.replace(/\./g, "[.]")
.replace(/http/gi, "hxxp");
},
fang: function(observable) {
if (sensitiveTypes.indexOf(observable.dataType) === -1) {
return observable.data;
}
return service.fangValue(observable.data);
},
unfang: function(observable) {
return observable.data
.replace(/\[\.\]/g, ".")
.replace(/hxxp/gi, "http");
},
shallowClearAndCopy: function(src, dst) {
dst = dst || {};
angular.forEach(dst, function(value, key) {
delete dst[key];
});
for (var key in src) {
if (src.hasOwnProperty(key) && !(key.charAt(0) === '$' && key.charAt(1) === '$')) {
dst[key] = src[key];
}
}
return dst;
},
updatableLink: function(scope, element, attrs) {
scope.MAX_INPUT_SIZE = 5;
scope.updatable = {
'updating': false
};
scope.oldValue = scope.value;
if (!angular.isDefined(scope.active)) {
scope.active = false;
}
scope.edit = function() {
scope.updatable.updating = true;
};
scope.update = function(newValue) {
if (angular.isDefined(newValue)) {
scope.value = newValue;
}
if (angular.isDefined(attrs.onUpdate)) {
var updateResult = scope.onUpdate({
'newValue': scope.value
});
if (angular.isDefined(updateResult) && angular.isDefined(updateResult.$promise)) {
updateResult = updateResult.$promise;
}
if (angular.isObject(updateResult) && angular.isFunction(updateResult.then)) {
updateResult.then(function() {
scope.oldValue = scope.value;
scope.active = false;
scope.format = 'static';
}, function() {
scope.value = scope.oldValue;
});
} else {
scope.oldValue = scope.value;
scope.active = false;
scope.format = 'static';
}
}
scope.updatable.updating = false;
};
scope.cancel = function() {
scope.value = scope.oldValue;
scope.updatable.updating = false;
};
scope.setFieldSize = function(options) {
if(options.length <= scope.MAX_INPUT_SIZE)
return options.length;
else
return scope.MAX_INPUT_SIZE;
};
},
extractQueryParam: function(paramName, queryString) {
if (!queryString || !paramName) {
return;
}
var param = $location.search()[paramName];
if (param) {
return param;
} else {
var parsedQuery = _.find(queryString.split('&'), function(str) {
return str.startsWith(paramName + '=');
});
return parsedQuery ? parsedQuery.substr(paramName.length + 1) : undefined;
}
}
};
return service;
});
})();