-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsym-insertupdatedeletevalue.js
167 lines (131 loc) · 4.33 KB
/
sym-insertupdatedeletevalue.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(function (PV) {
"use strict";
function symbolVis() { };
PV.deriveVisualizationFromBase(symbolVis);
var definition = {
typeName: "insertupdatedeletevalue",
displayName: 'Insert Update Delete Value',
visObjectType: symbolVis,
datasourceBehavior: PV.Extensibility.Enums.DatasourceBehaviors.Multiple,
iconUrl: '/Scripts/app/editor/symbols/ext/Icons/insertupdatedeletevalue.png',
getDefaultConfig: function(){
return {
DataShape: 'Timeseries',
Height: 150,
Width: 150,
TextColor: 'white',
btnText: 'BtnText',
}
},
configOptions: function(){
return [
{
title: "Format Symbol",
mode: "format"
}
];
},
inject: ['$http', '$q'],
}
var origin = window.location.origin;
var baseUrl = origin + "/piwebapi/"
symbolVis.prototype.init = function(scope, elem, $http, $q) {
this.onDataUpdate = dataUpdate;
scope.inputInEdition = false;
function dataUpdate(data){
var userLang;
userLang = navigator.language || navigator.userLanguage;
if(!data) return;
if(scope.inputInEdition) return;
var dataValues = data.Data[0];
var i;
if(userLang == "pt" || userLang == "pt-BR"){
for (i=0; i<dataValues.Values.length; i++){
dataValues.Values[i].Value = dataValues.Values[i].Value.replace('.','').replace(',','.');
}
}
scope.Values = dataValues.Values;
if(dataValues.Label){
scope.Units = dataValues.Units;
scope.Label = dataValues.Label;
}
};
scope.insertUpdateDeleteValue = function(index,type){
scope.inputInEdition = true;
var userLang;
userLang = navigator.language || navigator.userLanguage;
var namestream = scope.symbol.DataSources[0];
var datastream = scope.Values[index];
var body = {};
var isAttribute = /af:/.test(namestream);
var fullPath = isAttribute ? namestream.replace(/af\:(.*)/,'$1') : namestream.replace(/pi\:(\\\\.*)\?{1}.*(\\.*)\?{1}.*/,'$1$2');
var path = isAttribute ? fullPath.split("?")[0] + "|" + (fullPath.split("?")[1]).split("|")[1] : path = fullPath.split("?")[0];
var label = isAttribute ? path.match(/\w*\|.*$/)[0] : path.match(/\w+$/)[0];
var friendlyName = isAttribute ? label.match(/\|(.*$)/)[1] : label;
var getDataStreamURL = encodeURI(baseUrl + "attributes?path=" + path);
var dateTimeString;
var data;
if(userLang == "pt" || userLang == "pt-BR"){
var dateTimeSplit = datastream.Time.split(' ');
var dateSplit = dateTimeSplit[0].split('/');
var timeSplit = dateTimeSplit[1].split(':');
dateTimeString = new Date(dateSplit[2],dateSplit[1]-1,dateSplit[0],timeSplit[0],timeSplit[1],timeSplit[2])
data = {
"Timestamp": dateTimeString.toISOString(),
"Value": datastream.Value.replace(',','.')
};
}
else {
data = {
"Timestamp": datastream.Time,
"Value": datastream.Value
};
}
var method = "POST";
body = {
"1": {
"Method": "GET",
"Resource": getDataStreamURL
},
"2": {
"Method": method,
"Resource": (type == 'Delete'?"{0}?updateOption=remove":"{0}"),
"Content": JSON.stringify(data),
"Headers": {
'Content-Type': 'application/json'
},
"ParentIds": ["1"],
"Parameters": ["$.1.Content.Links.Value"],
}
}
return $http.post(baseUrl + 'batch', JSON.stringify(body), {withCredentials: true})
.then(function successCallback(response) {
scope.inputInEdition = false;
}, function errorCallback(response) {
scope.inputInEdition = false;
});
};
scope.newValue = function(){
scope.inputInEdition = true;
var valuesLength = scope.Values.length;
var values;
if(valuesLength>0){
values = scope.Values[valuesLength-1];
}
else{
values = {};
}
var localeDate = new Date();
if(values.New == undefined ){
scope.Values.push({'Value':"",
'Time':localeDate.toLocaleString(),
'New':true
})
}
}
scope.inputClick = function(index){
scope.inputInEdition = true;
};
};
PV.symbolCatalog.register(definition);
})(window.PIVisualization);