var SmartLocation;
if (SmartLocation == undefined) {
SmartLocation = function (settings) {
this.init(settings);
};
}
SmartLocation.URL_PLACES = 'https://www.smartpost.ee/places.js';
SmartLocation.URL_IMG = 'https://www.smartpost.ee/widget/smartpost-logo.jpg';
SmartLocation.ID_CONTAINER = 'smartpost_cont';
SmartLocation.ID_SELECT = 'smartpost_select';
SmartLocation.ID_INPUT = 'smartpost_input';
SmartLocation.ID_CITY = 'smartpost_city';
SmartLocation.ID_ADDRESS = 'smartpost_address';
SmartLocation.ID_OPEN = 'smartpost_open';
SmartLocation.ID_DESCRIPTION = 'smartpost_description';
SmartLocation.NEXT_ID = 0;
SmartLocation.prototype.init = function (settings) {
this.curId = "" + (SmartLocation.NEXT_ID++);
this.settings = settings;
this.selectedId = this.settings.selected_id || 0;
this.initSettings();
var container = document.getElementById(this.settings.target_id);
if(container) {
container.innerHTML = this.getWidgetHTML();
this.fetchData(function(){
this.fillSelect();
this.select.onchange(1);
});
} else {
throw new Error("SmartPOST widget container `" + this.settings.target_id + "` not found");
}
};
SmartLocation.prototype.initSettings = function () {
this.ensureDefault = function (settingName, defaultValue) {
if(!this.settings) {this.settings = new Array();}
this.settings[settingName] = (this.settings[settingName] == undefined) ? defaultValue : this.settings[settingName];
};
this.ensureDefault("target_id", "smartpost_widget_target");
this.ensureDefault("placeid_name", "smartpost_terminal_id");
this.ensureDefault("placename_name", "smartpost_terminal_name");
this.ensureDefault("show_infotext", true);
this.ensureDefault("show_logo", true);
this.ensureDefault("show_city", true);
this.ensureDefault("show_address", true);
this.ensureDefault("show_opened", true);
this.ensureDefault("show_description", false);
this.ensureDefault("text_infotext", 'Tellimus viiakse kaubanduskeskuses asuvasse iseteeninduslikku pakiautomaati. ' +
'
Vaata lähemalt: '+
'Mis on pakiautomaat? Vaata ka pakiautomaatide asukohtasid kaardil.');
this.ensureDefault("text_place", "Pakiautomaadi asukoht:");
this.ensureDefault("text_address", "Pakiautomaadi aadress:");
this.ensureDefault("text_city", "Pakiautomaadi linn:");
this.ensureDefault("text_opened", "Pakiautomaadi lahtiolekuaeg:");
this.ensureDefault("text_description", "Kirjeldus:");
delete this.ensureDefault;
};
SmartLocation.prototype.fetchData = function (callback) {
if(window['places_info'] == undefined){
var head = document.getElementsByTagName("head").item(0);
var id = "smartpostjs";
var oScript = document.getElementById(id);
if (oScript) head.removeChild(oScript);
oScript = document.createElement("SCRIPT");
oScript.setAttribute("src", SmartLocation.URL_PLACES);
oScript.setAttribute("type", "text/javascript");
oScript.setAttribute("id",id);
head.appendChild(oScript);
}
this.onPlacesAvailable.call(this, callback);
}
SmartLocation.prototype.onPlacesAvailable = function(callback) {
if (window['places_info'] != undefined) {
callback.call(this);
} else {
var t = this;
setTimeout(function () {
t.onPlacesAvailable.call(t, callback);
}, 50)
}
}
SmartLocation.prototype.fillSelect = function() {
this.placesMap = new Array();
this.select = document.getElementById(SmartLocation.ID_SELECT + this.curId);
var buffer = new Array();
for(var i = 0; i < places_info.length; i++) {
var place = places_info[i];
var next_place = null;
if(i < places_info.length-1) {
next_place = places_info[i+1];
}
if(!next_place || next_place.group_id != place.group_id) {
var optgroup = document.createElement('optgroup');
optgroup.label = place.group_name;
buffer.push(place);
for(var j = 0; j < buffer.length;j++) {
var option = document.createElement('option');
option.appendChild(document.createTextNode(buffer[j].name));
option.value = buffer[j].place_id;
optgroup.appendChild(option);
if(this.selectedId == option.value){
option.selected = true;
}
}
buffer = new Array();
this.select.appendChild(optgroup);
} else {
buffer.push(place);
}
this.placesMap[place.place_id] = place;
}
this.select.onchange = this.setSelected;
this.select.smartLocation = this;
}
SmartLocation.prototype.setSelected = function(a) {
var place = this.smartLocation.placesMap[this.value];
document.getElementById(SmartLocation.ID_CITY + this.smartLocation.curId).value = place.city;
document.getElementById(SmartLocation.ID_ADDRESS + this.smartLocation.curId).value = place.address;
document.getElementById(SmartLocation.ID_OPEN + this.smartLocation.curId).value = place.opened;
document.getElementById(SmartLocation.ID_DESCRIPTION + this.smartLocation.curId).value = place.description;
document.getElementById(SmartLocation.ID_INPUT + this.smartLocation.curId).value = place.name;
var vorm = document.getElementById(this.smartLocation.settings.transport_form);
if (this.smartLocation.settings.submit_after_select && vorm && a != 1)
vorm.submit();
}
SmartLocation.prototype.getWidgetHTML = function() {
return '
'+this.settings.text_infotext+'
' : '') +''+ '' + (this.settings.show_logo ? '' : '') +''+ ''+this.settings.text_place+' | ' + '' + ' |
'+this.settings.text_city+' | ' + '' + ' |
'+this.settings.text_address+' | ' + '' + ' |
'+this.settings.text_opened+' | ' + '' + ' |
'+this.settings.text_description+' | ' + '' + ' |