This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputToMacAddressDirective.js
More file actions
50 lines (41 loc) · 1.47 KB
/
InputToMacAddressDirective.js
File metadata and controls
50 lines (41 loc) · 1.47 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
app.directive('macAddress', function () {
return {
restrict: "A",
require: "ngModel",
link: function (scope, ele, attr, ctrl) {
var symbol = attr.symbol ? attr.symbol : '-';
if (!ctrl) {
return;
}
var macAddressParse = function (value) {
return value.toUpperCase();
};
var macAddressFormat = function (value) {
if (!value) {
return undefined;
}
var numbers = value.replace(/[-\.:]/g, "");
if (numbers.length % 3 === 0) {
var result = numbers.replace(/([0-9A-Za-z]{2})/g, "$1" + symbol);
if (result.length === 18 && result[result.length - 1] === symbol) {
result = result.slice(0, -1);
}
return result;
}
};
ctrl.$parsers.push(macAddressParse);
ctrl.$formatters.push(macAddressFormat);
window.elem = ele;
ele.on("keyup keydown change", function (e) {
if (e.keyCode !== 8) {
var value = macAddressFormat(ele.val());
if (value !== undefined) {
ctrl.$setViewValue(value);
ctrl.$render();
}
}
scope.$apply();
});
}
};
});