Files
@ 4fce6b4feaa7
Branch filter:
Location: NPO-Accounting/experimental-accounting-api/accounting/static/js/accounting.js
4fce6b4feaa7
3.9 KiB
application/javascript
[tests] Added bin/runtests
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 | 'use strict';
// Part of the accounting-api project:
// https://gitorious.org/conservancy/accounting-api
// License: AGPLv3-or-later
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
};
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function Transaction(args) {
var self = this;
this.__type__ = 'Transaction';
this.id = null;
this.payee = null;
this.date = null;
this.postings = null;
this.metadata = {};
this.generateId = function () {
self.id = guid();
}
for (var i in args) {
this[i] = args[i];
}
}
function Posting(args) {
this.__type__ = 'Posting';
this.account = null;
this.amount = null;
for (var i in args) {
this[i] = args[i];
}
}
function Amount(args) {
this.__type__ = 'Amount';
this.symbol = null;
this.amount = null;
for (var i in args) {
this[i] = args[i];
}
}
var models = {
'Amount': Amount,
'Posting': Posting,
'Transaction': Transaction
};
function accountingObjectHook(object) {
if ('__type__' in object) {
console.log('Found typed object: ', object.__type__, object);
var model = new models[object.__type__];
for (var i in object) {
model[i] = object[i];
}
return model;
}
return object;
}
function transformResponseFactory(object_hook) {
var self = this;
self.object_hook = object_hook;
function transformResponse(json, headers) {
var transformed;
console.log('json: ', json);
if (typeof json == 'undefined') {
return;
} else if ((typeof json == 'object') &&
Object.prototype.toString.call(json) == '[object Object]') {
console.log('json is Object, json: ', json);
for (var i in json) {
json[i] = transformResponse(json[i]);
}
transformed = self.object_hook(json)
} else if ((typeof json == 'object') &&
Object.prototype.toString.call(json) == '[object Array]') {
console.log('json is Array, json: ', json);
for (var i in json) {
json[i] = transformResponse(json[i]);
}
transformed = json;
} else {
transformed = json;
}
return transformed;
};
return transformResponse;
}
function transformRequestFactory(object_hook) {
function transformRequest(data, headerSetter) {
console.log('transformRequest', data, headerSetter)
headerSetter()['Content-Type'] = 'application/json';
return data;
}
return transformRequest;
}
angular.module('accountingApi', ['ngResource'])
.factory('Transaction', function () {
return Transaction;
})
.factory('Posting', function () {
return Posting;
})
.factory('Amount', function () {
return Amount;
})
.factory('AccountingApi', function($resource, $http) {
$http.defaults.useXDomain = true;
$http.defaults.transformResponse.push(new transformResponseFactory(
accountingObjectHook));
var AccountingApi = $resource(
'/transaction/:id',
{},
{
get: {
method: 'GET',
transformResponse: Array.prototype.concat(
$http.defaults.transformResponse,
[function (data){
if (typeof data == 'object' &&
'transaction' in data) {
return data.transaction;
}
return data;
}]
)
},
save: {
method: 'POST',
transformRequest: [new transformRequestFactory()].concat(
$http.defaults.transformRequest)
}
}
);
return AccountingApi;
});
|