Skip to content

Instantly share code, notes, and snippets.

@goldenflower
Created July 14, 2016 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldenflower/7a2c41d5c6f00f2a5b12980d0842bd55 to your computer and use it in GitHub Desktop.
Save goldenflower/7a2c41d5c6f00f2a5b12980d0842bd55 to your computer and use it in GitHub Desktop.
/* Task */
L.Micello.EntityPropertyManagement = L.Layer.extend({
options: {
},
initialize: function (taskControl) {
L.Util.setOptions(this, taskControl.options);
this.taskControl = taskControl;
this.mapControl = taskControl.getMapControl();
this.mapData = this.mapControl.getMapData();
this.metaData = this.mapControl.getMetaData();
this.comEditor = taskControl.getComEditor();
this.propFrm = null;
this.currEntity = null;
return this;
},
getInfo: function () {
return {
name: "Upload file",
icon: ""
}
},
_clickHandler: function (e) {
//alert('hello from out side ...');
this.displayPropertyForm(e);
//return this;
},
onAdd: function (map) {
var task = this;
this.presets = this.comEditor.getPresets();
this.pl = this._propList();
this.datahandler = this.comEditor.getDataHandler();
this.mapControl.on ( 'indoorClick', this._clickHandler, this);
return this;
},
onRemove: function () {
var task = this;
this.mapControl.off ( 'indoorClick', this._clickHandler, this);
return this;
},
enities_callback: function (evt, type) {
var reader = new FileReader();
if(type == 'drag'){ //drag and drop
evt.stopPropagation();
evt.preventDefault();
var file = evt.dataTransfer.files[0];
console.log(type);
}else{ //upload button
var file = evt.target.files[0];
console.log(type);
}
var filename = file.name;
if(!file.type.match('csv.*')) {
alert(file.name + " is not a valid CSV file.");
}
if(file){
reader.onload = function (evt) {
document.getElementById('progress_bar').innerHTML = 'loading...';
var output = {};
var csvdata = [];
var rows = evt.target.result.split(/[\r\n]+/g);
output.file = file.name;
var myheader = rows[0].replace(/\"/g, '');
output.header = myheader.split(',');
rows.shift();
for (var i=0; i<rows.length; i++) {
csvdata.push(rows[i].replace(/\"/g, ''));
}
output.data = csvdata;
output.success = 'true';
output.msg = 'null';
theData = JSON.stringify(output);
document.getElementById("list").innerHTML = theData;
document.getElementById('progress_bar').innerHTML = 'Completed.';
}
reader.readAsText(file);
}else {
alert("Failed to load file");
}
reader.onerror = function (evt) {
if(evt.target.error.name == "NotReadableError") {
alert("Canno't read file !");
}
}
},
displayPropertyForm: function (e) {
// start calles from invoker end
var div = L.DomUtil.create('div', '', document.body);
var test = L.micello.uploadmanager (div,{
"id": "entities-upload-example",
"callback": this.enities_callback,
"backGroundColor": '',
"width": '',
"height": '',
"border": '',
"greyTextColor":'',
"blueTextColor" : ''
});
var content = test.loadForm();
// end calles from invoker end
if( content ) {
this.win = L.micello.window(this.mapControl, {
type: 'big',
content: content,
menu: this._menu()
}).addTo(this.mapControl.getMap());
}
console.log('>>>>>>>>>> class working ');
},
_menu: function () {
var instance = this;
var menu = [];
var menuItem;
menuItem = {
label: 'Close',
callback: function () {
instance.win.removeFrom(instance.mapControl.getMap());
}
}
menu.push(menuItem);
return menu;
},
_langDeterminate: function (key) {
if(!this.pl[key]) { return null; }
if( !this.pl[key].has_lang ) {
return null;
}
return this.mapControl.getMapLang();
},
_getData: function (entity) {
var propList = this.mapData.getEntityPropertyList(entity);
var found, str, valObj;
var props = {};
var keyName;
for( var x = 0; x < propList.length; x++ ) {
keyName = propList[x];
found = this.mapData.getEntityPropertyLangStrict(entity, keyName, this._langDeterminate(keyName));
if( found.length > 0 ) {
if( !props[keyName] ) {
props[keyName] = [];
}
for( var i = 0; i < found.length; i++ ) {
valObj = found[i];
str = valObj.v.replace(/\s+/g," ");
valObj.v = str;
props[keyName].push(valObj);
}
}
}
return props;
},
_propertyDisplay: function (e) {
this.mapData = this.mapControl.getMapData();
console.log(e.indoor);
var entityList = this.mapData.getGeomEntities(e.indoor);
if( !entityList ) { return false; }
/* Load the first entity by default, but if there's more than one provide an interface
* to change entities
*/
if( entityList.length > 1 ) {
return this._entityChoice(entityList);
} else {
return this._propertyForm(entityList[0]);
}
},
_entityChoice: function (entityList) {
var entity, entityProps, entityChoice, entityMgr = this;
var display = L.DomUtil.create('div', 'micello-ce-task-epm-multi');
var title = L.DomUtil.create('div', 'micello-ce-task-epm-multi-title', display);
title.innerHTML = "Multiple Entities";
var desc = L.DomUtil.create('div', 'micello-ce-task-epm-multi-desc', display);
desc.innerHTML = "There are multiple entities occupying this space. Please choose which one to edit.";
for( var x = 0; x < entityList.length; x++ ) {
entityProps = this._getData(entityList[x]);
entity = L.DomUtil.create('div', 'micello-ce-task-epm-multi-choice', display);
entity.innerHTML = entityProps["name"][0].v;
entityChoice = entityList[x];
(function (eC) {
return (function () {
var entityChoiceLoc = eC;
L.DomEvent.addListener(entity, 'click touchend', function (e) {
L.DomEvent.stop(e);
entityMgr.win.updateContent(entityMgr._propertyForm(entityChoiceLoc));
});
})();
})(entityChoice);
}
return display;
},
_propertyForm: function (entity) {
var epm = this;
this.currEntity = entity;
this.currEntityName = this.mapData.getEntityProperty(this.currEntity, 'name', 0);
this.ogValues = this._getData(entity);
this.pf = L.DomUtil.create('div', 'micello-ce-task-epm leaflet-zoom-hide');
var title = L.DomUtil.create('div', 'micello-ce-task-epm-title leaflet-zoom-hide', this.pf);
title.innerHTML = this.currEntityName+' Properties';
var lang = L.DomUtil.create('div', 'micello-ce-task-epm-lang leaflet-zoom-hide', this.pf);
var langCode = this.mapControl.getMapLang();;
var defaultLangCode = this.mapControl.getMapDefaultLang();;
var langDesc = this.metaData.translate("lang"+":"+langCode, langCode);
var defaultLangDesc = this.metaData.translate("lang"+":"+defaultLangCode, defaultLangCode);
lang.innerHTML = "Language: "+langDesc;
validate = function (formElements) {
return epm._validation(formElements);
}
submit = function (formElements) {
return epm._submit(formElements);
}
this.editExclude = [
"name",
"$icon",
"$style",
"$image_url",
"$label_text",
"$body_color",
"$outline_color",
"$outline_width",
"$text_color"
];
/* Handle non default lang fields right here */
if( this.mapControl.getMapLang() != this.mapControl.getMapDefaultLang() ) {
L.DomUtil.create('div', 'micello-ce-task-epm-lang-notice leaflet-zoom-hide', this.pf).innerHTML = "Only properties that can be translated to "+ langDesc+" are available. Use the default language ("+defaultLangDesc+") to edit properties that are not available for translation."
for( var key in this.pl ) {
if( !this.pl[key].has_lang ) {
this.editExclude.push(key);
}
}
/* now catch any illegal properties and force them to the default lang */
for( var key in this.ogValues ) {
if( !this.pl[key] || !this.pl[key].has_lang ) {
this.editExclude.push(key);
}
}
}
var options = {
editExclude: this.editExclude,
validation: [validate],
submit: [submit],
recordId: entity.Id
}
this.propFrm = L.micello.ui.properties(this.taskControl, this.pl, this.ogValues, options);
this.pf.appendChild(this.propFrm.getMarkup());
return this.pf;
},
_validation: function (formElements) {
},
_submit: function (formElements) {
var fe, key, existKey = {}, ordr, val, lang, valCompare, recordId, id;
/* Find the recordId if it exists */
for( var x = 0; x < formElements.length; x++ ) {
fe = formElements[x];
key = fe.getOption('k');
if( key == 'recordId' ) {
recordId = fe.getValue(0);
}
}
for( var x = 0; x < formElements.length; x++ ) {
fe = formElements[x];
key = fe.getOption('k');
existKey[key] = [];
lang = this._langDeterminate(key);
if( key == 'recordId' ) { continue; }
for( var i = 0; i < fe.elements.length; i++ ) {
ordr = Number(fe.elements[i].getAttribute("ordr"));
val = fe.getValue(ordr);
id = Number(fe.getAttrib(ordr, 'id'));
existKey[key].push(Number(id));
valCompare = '';
if( this.ogValues[key] && this.ogValues[key][i] ) {
valCompare = this.ogValues[key][i].v;
}
if( val != valCompare ) {
if( id ) {
this.datahandler.save('UpdateEntityProperty', {
key_name: key,
key_value: val,
id: id,
title: '('+this.currEntityName+') Updated '+key+' to '+val
});
} else {
this.datahandler.save('AddEntityProperty', {
key_name: key,
key_value: val,
lang: lang,
entity_id: Number(recordId),
title: '('+this.currEntityName+') Added '+val+' ('+key+')'
});
if(!this.ogValues[key]) { this.ogValues[key] = []; }
this.ogValues[key] = this.mapData.getEntityPropertyLangStrict(this.currEntity,key,lang);
existKey[key].push(this.ogValues[key][0].id);
fe.setAttrib(ordr, 'id', this.ogValues[key][0].id);
}
}
}
}
/* Now flip through the og values to see whats missing / needs to be deleted */
for( key in this.ogValues ) {
/* else check to see if an ordr field has been deleted */
for( var i = 0; i < this.ogValues[key].length; i++ ) {
if( (!existKey[key] || existKey[key].indexOf(this.ogValues[key][i].id) < 0) && this.editExclude.indexOf(key) < 0 ) {
this.datahandler.save('DeleteEntityProperty', {
key_name: key,
ordr: this.ogValues[key][i].ordr,
id: Number(this.ogValues[key][i].id),
title: '('+this.currEntityName+') Deleted '+key
});
}
}
}
this.propFrm.displaySubmitMessage("The properties have been saved.");
},
_propList: function () {
var propTree = this.presets.getPropertyTree("entity");
var propTreeOrganized = {}, prop;
for( var x = 0; x < propTree.length; x++ ){
prop = propTree[x];
prop.addList = true;
propTreeOrganized[this.presets.getKeyKey(prop)] = prop;
}
/* Special cases */
var obj;
propTreeOrganized['main admin region'].addList = false;
for( var country in propTreeOrganized['country'].v ) {
obj = propTreeOrganized['country'].v[country];
if( !obj.ks ) {
propTreeOrganized['country'].v[country].ks = [];
propTreeOrganized['country'].v[country].ks.push(propTreeOrganized['main admin region']);
}
}
return propTreeOrganized;
}
});
L.micello.uploder = function (taskManager, options) {
return new L.Micello.EntityPropertyManagement(taskManager, options);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment