Skip to content

Instantly share code, notes, and snippets.

@asantaga
Last active March 30, 2017 06:59
Show Gist options
  • Save asantaga/76f93ed2a6422423d39b795a47b7d32d to your computer and use it in GitHub Desktop.
Save asantaga/76f93ed2a6422423d39b795a47b7d32d to your computer and use it in GitHub Desktop.
define(['https://maps.googleapis.com/maps/api/js?key=AIzaSyATjZW-pRiH1P337GbDXZEELVDxjwlbgGs'], function () {
'use strict';
/**
* View model of your custom knockout web component.
*
* See http://knockoutjs.com/documentation/component-overview.html for more information about knockout web components.
*
* @param {Object} params - is an object whose key/value pairs are the parameters passed
* from the component binding or custom element
* @param {Object} componentInfo.elemnent - the element the component is being injected into.
* When createViewModel is called, the template has already been injected into this element,
* but isn't yet bound.
* @param {Object} componentInfo.templateNodes - is an array containing any DOM nodes that
* have been supplied to the component.
*/
var CustomComponentViewModel = function (params, componentInfo) {
AbcsLib.checkThis(this);
this.params = params;
this.pageViewModel = params.root;
//the current context view model - typically it's the pageViewModel,
//but may be some other view model if your component instance is placed
//into a another component which define its own view models
//(e.g. list component row view model)
this.currentViewModel = params.data;
//DOM element the component is being injected into
this.element = componentInfo.element;
this.loadDataAndMap();
};
CustomComponentViewModel.prototype.loadDataAndMap = function () {
require([
'operation/js/api/Conditions',
'operation/js/api/Operator'
], function(Conditions, Operator) {
var userUsername = Abcs.System().getLoggedInUser().username;
console.log(userUsername);
var mapdiv = document.getElementById('map');
var map = new google.maps.Map(mapdiv,
{ zoom: 6 });
var accounts = Abcs.Entities().findById('com.oracle.abcs.fa.deR8S.accounts');
var openopty = accounts.getProperty('OrganizationDEO_Open_opty_c');
var usernameField= accounts.getProperty('o OrganizationDEO_AccountOwnerEmail_c');
var condition = Conditions.SIMPLE(openopty, Operator.MORE_OR_EQUAL, '1');
var operation = Abcs.Operations().read({
entity: accounts,
condition: condition
});
var geocodeThisWithname = function(companyName,address)
{
return function(results, status) {
if (status === 'OK') {
console.log(' has long/lat: '+results[0].geometry.location);
if (!addressFocused) {
map.setCenter(results[0].geometry.location);
addressFocused = true;
}
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
let infowindow = new google.maps.InfoWindow({content: "<b>"+companyName+"</b>"});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
// Centre Map on last valid entry
map.setCenter(results[0].geometry.location);
} else {
console.log('Geocode was not successful for location: '+address+'; reason: ' + status);
}
}
};
var addressFocused = false;
operation.perform().then(function(operationResult) {
if (operationResult.isSuccess()) {
var res = operationResult.getData();
//console.log(JSON.stringify(res));
for (var index=0; index < res.length && index < 5; index++) {
var address = res[index].PrimaryAddress__FormattedAddress;
var accountName= res[index].PartyUniqueName;
var geocoder = new google.maps.Geocoder();
console.log('The address = '+address);
geocoder.geocode({'address': address}, geocodeThisWithname(accountName,address)
);
}
}
}).catch(function(operationResult) {
console.log("cannot fetch accounts "+operationResult);
});
});
};
return CustomComponentViewModel;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment