Skip to content

Commit

Permalink
Mixin default popup template options with user provided options.
Browse files Browse the repository at this point in the history
  • Loading branch information
green3g committed Oct 10, 2016
1 parent b16f887 commit 96948e2
Showing 1 changed file with 46 additions and 47 deletions.
93 changes: 46 additions & 47 deletions viewer/js/gis/dijit/Identify.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,52 +354,41 @@ define([
},

getInfoTemplate: function (layer, layerId, result) {
var popup = null,
content = null;
var popup, config;
if (result) {
layerId = result.layerId;
} else if (layerId === null) {
layerId = layer.layerId;
}

// get infoTemplate from the layer's infoTemplates array
if (layer.infoTemplates) {
if (layer.infoTemplates.hasOwnProperty(layerId)) {
return layer.infoTemplates[layerId].infoTemplate;
} else {
return null;
}
}

// see if we have a Popup config defined for this layer
if (this.identifies.hasOwnProperty(layer.id)) {
if (this.identifies[layer.id].hasOwnProperty(layerId)) {
popup = this.identifies[layer.id][layerId];
if (popup) {
if (typeof (popup.declaredClass) !== 'string') { // has it been created already?
if (popup.content) {
content = popup.content;
}
popup = new PopupTemplate(popup);
if (content) {
popup.setContent(content);
}
this.identifies[layer.id][layerId] = popup;
}
var ids = this.identifies;
if (ids.hasOwnProperty(layer.id)) {
if (ids[layer.id].hasOwnProperty(layerId)) {
popup = ids[layer.id][layerId];
if (popup instanceof PopupTemplate) {
return popup;
}
}
} else {
ids[layer.id] = {};
}

// if no Popup config found, create one with all attributes or layer fields
if (!popup) {
popup = this.createDefaultInfoTemplate(layer, layerId, result);
// by mixin in the users config with the default props we can
// generate a config object that provides the basics automatically
// while letting the user override only the parts they want...like mediaInfos
config = lang.mixin(this.createDefaultInfoTemplate(layer, layerId, result), ids[layer.id][layerId] || {});

popup = ids[layer.id][layerId] = new PopupTemplate(config);
if (config.content) {
popup.setContent(config.content);
}

return popup;
return ids[layer.id][layerId];
},

createDefaultInfoTemplate: function (layer, layerId, result) {
var popup = null, fieldInfos = [];
var popup = null,
fieldInfos = [];

var layerName = this.getLayerName(layer);
if (result) {
Expand All @@ -414,14 +403,14 @@ define([
if (attributes.hasOwnProperty(prop)) {
this.addDefaultFieldInfo(fieldInfos, {
fieldName: prop,
label: prop.replace(/_/g, ' '),
label: this.makeSentenceCase(prop),
visible: true
});
}
}
}

// from the outFields of the layer
// from the outFields of the layer
} else if (layer._outFields && (layer._outFields.length) && (layer._outFields[0] !== '*')) {

var fields = layer.fields;
Expand All @@ -433,39 +422,49 @@ define([
this.addDefaultFieldInfo(fieldInfos, {
fieldName: foundField[0].name,
label: foundField[0].alias,
visible: true,
formatter: typeof this.defaultFormatters[foundField[0].type] !== 'undefined' ? this.defaultFormatters[foundField[0].type] : undefined
visible: true
});
}
}));

// from the fields layer
// from the fields layer
} else if (layer.fields) {

array.forEach(layer.fields, lang.hitch(this, function (field) {
this.addDefaultFieldInfo(fieldInfos, {
fieldName: field.name,
label: field.alias,
visible: true,
formatter: typeof this.defaultFormatters[field.type] !== 'undefined' ? this.defaultFormatters[field.type] : undefined
label: field.alias === field.name ? this.makeSentenceCase(field.name) : field.alias,
visible: true
});
}));
}

if (fieldInfos.length > 0) {
var featLayer = this.getFeatureLayerForDynamicSublayer(layer, layerId);
popup = new PopupTemplate({
popup = {
title: layerName,
fieldInfos: fieldInfos,
showAttachments: (layer.hasAttachments || featLayer.hasAttachments)
});
if (!this.identifies[layer.id]) {
this.identifies[layer.id] = {};
}
this.identifies[layer.id][layerId] = popup;
showAttachments: (layer.hasAttachments)
};
}

return popup;
},
/**
* converts a string to a nice sentence case format
* @url http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript
* @param {string} str The string to convert
* @return {string} The converted string
*/
makeSentenceCase: function (str) {
if (!str.length) {
return '';
}
str = str.toLowerCase().replace(/_/g, ' ').split(' ');
for (var i = 0; i < str.length; i++) {
str[i] = str[i].charAt(0).toUpperCase() + (str[i].substr(1).length ? str[i].substr(1) : '');
}
return (str.length ? str.join(' ') : str);
},

addDefaultFieldInfo: function (fieldInfos, field) {
var nameLC = field.fieldName.toLowerCase();
Expand Down

0 comments on commit 96948e2

Please sign in to comment.