-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
97 lines (90 loc) · 3.29 KB
/
common.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
'use strict';
/**
* Created by almgun on 30.08.2014.
*/
angular.module('ga.domino-utils').factory('helpers', function ($q, objectFactory) {
return {
responseHandler: function (prom, extra) {
var deferred = $q.defer();
var rr = objectFactory.serverResponseFactory();
prom.then(function (res) {
if (res.data && res.data.status) {
if (res.data.status === "OK" || res.data.status === "NOK") {
rr.setObj(res.data);
deferred.resolve(rr);
}
else {
rr.setObj( {message: "UNKNOWN STATUS IN RESPONSE",status:"NOK"} );
deferred.reject(rr);
}
}
else {
rr.setObj({"message": "UNKNOWN RESPONSE (HTML login page ?)" + extra || ''});
deferred.reject(rr);
}
}, function () {
rr.setObj({"message": "REQUEST REJECTED"});
deferred.reject(rr);
});
return deferred.promise;
}
}
}).factory('objectFactory', function () {
var _severResponsePrototype = {
reset: function () {
this.status = "NOK";
this.message = "";
this.data = {};
}, setObj: function (response) {
this.status = response.status || "NOK";
this.message = response.message || "";
this.data = response.data || {};
}
};
return{
serverResponseFactory: function () {
return Object.create(_severResponsePrototype, {
status: { writable: true, enumerable: true, value: "NOK" },
message: { writable: true, enumerable: true, value: "" },
data: { writable: true, enumerable: true, value: {} }
});
},
userRecord: function (userName, roles, aclLevel) {
return {userName: (userName || {}), roles: (roles || []), aclLevel: (aclLevel || 0) };
}
}
}).factory('transformRequestAsFormPost', function () {
//post JSON data as form data
// http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm
function transformRequest(data, getHeaders) {
var headers = getHeaders();
headers[ "Content-Type" ] = "application/x-www-form-urlencoded; charset=utf-8";
return( serializeData(data) );
}
return( transformRequest );
function serializeData(data) {
if (!angular.isObject(data)) {
return( ( data == null ) ? "" : data.toString() );
}
var buffer = [];
// Serialize each key in the object.
for (var name in data) {
if (!data.hasOwnProperty(name)) {
continue;
}
var value = data[ name ];
buffer.push(
encodeURIComponent(name) +
"=" +
encodeURIComponent(( value == null ) ? "" : value)
);
}
// Serialize the buffer and clean it up for transportation.
var source = buffer
.join("&")
.replace(/%20/g, "+")
;
console.log('transformed: ', source);
return( source );
}
});