Last active
February 1, 2019 12:26
-
-
Save GeorgDangl/4e98e5a02f83e1f6871b8648a34ee4cf to your computer and use it in GitHub Desktop.
AVACloud JavaScript Examples - https://www.dangl-it.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the Dangl.Identity OpenID token endpoint | |
const danglIdentityTokenEndpoint = 'https://identity.dangl-it.com/connect/token'; | |
let globalAccessToken; | |
// This function retrieves the JWT Token and stores it in an Html input field and in a global variable | |
async function getAccessToken(clientId, clientSecret, tokenFormFieldId) { | |
// This is an OpenID Client Credentials grant request | |
const clientCredentialsRequest = new Promise(function (resolve, reject) { | |
const requestHeaders = new Headers(); | |
requestHeaders.append('Authorization', 'Basic ' + btoa(clientId + ':' + clientSecret)); | |
requestHeaders.append('Content-Type', 'application/x-www-form-urlencoded'); | |
fetch(danglIdentityTokenEndpoint, { | |
method: 'post', | |
headers: requestHeaders, | |
body: 'grant_type=client_credentials&scope=avacloud' | |
}) | |
.then(function (response) { | |
response.json().then(j => resolve(j)); | |
}); | |
}); | |
try { | |
const clientCredentialsResult = await clientCredentialsRequest; | |
accessToken = clientCredentialsResult['access_token']; | |
$(tokenFormFieldId).val(accessToken); | |
globalAccessToken = accessToken; | |
// Setting the access token for the API clients | |
const defaultClient = DanglAVACloudClient.ApiClient.instance; | |
var danglIdentityAuth = defaultClient.authentications['Dangl.Identity']; | |
danglIdentityAuth.accessToken = globalAccessToken; | |
} catch (e) { | |
console.log(e); | |
alert('Failed to obtain an access token. Have you read the documentation and set up your OAuth2 client?'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This function sends the GAEB file to AVACloud and returns the project model | |
function getAvaProject() { | |
const apiClient = new DanglAVACloudClient.GaebConversionApi(); | |
var fileName = $('#gaebFile')[0].files[0]; | |
var opts = { | |
gaebFile: fileName, | |
myOp: true | |
}; | |
const avaConversion = new Promise(function (resolve, reject) { | |
var callback = function (error, data, response) { | |
if (error) { | |
alert('Failed to convert the GAEB input file'); | |
console.error(error); | |
reject(error); | |
} else { | |
resolve(data); | |
} | |
}; | |
apiClient.gaebConversionConvertToAva(opts, callback); | |
}); | |
return avaConversion; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getNewGaebFile() { | |
// This is the AVA Project that we are transforming to a GAEB file | |
// It is very simple and contains only a single position | |
// with the short text 'Concrete Wall' | |
const avaProject = { | |
serviceSpecifications: [ | |
{ | |
elements: [ | |
{ | |
elementTypeDiscriminator: 'PositionDto', | |
shortText: 'Concrete Wall', | |
unitTag: 'm²', | |
quantityComponents: [ | |
{ | |
formula: '10' | |
} | |
], | |
priceComponents: [ | |
{ | |
values: [ | |
{ | |
formula: '80' | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
] | |
}; | |
// In the options, we specify the conversion target to GAEB XML V3.2 | |
const opts = { | |
destinationGaebType: 'GaebXml_V3_2' | |
}; | |
const apiClient = new DanglAVACloudClient.AvaConversionApi(); | |
const gaebConversion = new Promise(function (resolve, reject) { | |
var callback = function (error, data, response) { | |
if (error) { | |
alert('Failed to convert to a GAEB file'); | |
console.error(error); | |
reject(error); | |
} else { | |
resolve(data); | |
} | |
}; | |
apiClient.avaConversionConvertToGaeb(avaProject, opts, callback); | |
}); | |
return gaebConversion; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This function displays a list of all positions in the result area | |
async function listAllPositions() { | |
const avaProject = await getAvaProject(); | |
const servSpec = avaProject | |
.serviceSpecifications[0]; | |
const positionsList = []; | |
getPositionsInElementList(servSpec.elements, positionsList); | |
let positionListHtml = '<ol>'; | |
for (var i = 0; i < positionsList.length; i++) { | |
positionListHtml+='<li> Item Number: ' | |
+ positionsList[i].itemNumber.stringRepresentation | |
+ ', Short Text: ' | |
+ positionsList[i].shortText | |
+ '</li>' | |
} | |
positionListHtml+='</ol>'; | |
setHtmlResult(positionListHtml); | |
} | |
// This function puts all elements of type PositionDto | |
// on the 'positionList' array. It calls itself recursively | |
function getPositionsInElementList(elements, positionsList) { | |
if (elements == null) { | |
return; | |
} | |
for (var i = 0; i < elements.length; i++) { | |
const element = elements[i]; | |
if (element.elementTypeDiscriminator === 'PositionDto') { | |
positionsList.push(element); | |
} else if (element.elementTypeDiscriminator === 'ServiceSpecificationGroupDto') { | |
getPositionsInElementList(element.elements, positionsList); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "avacloud-demo-javascript", | |
"version": "0.0.0", | |
"private": true, | |
"scripts": { | |
"serve": "http-server", | |
"postinstall": "xcopy /S /I /Q /Y /F node_modules\\@dangl\\avacloud-client-javascript\\bundle.js dist\\avacloud.bundle.js*" | |
}, | |
"dependencies": { | |
"@dangl/avacloud-client-javascript": "1.3.0" | |
}, | |
"devDependencies": { | |
"http-server": "0.11.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment